/**
 * wangweipeng  2021/07/12 11:25    SWAG-C4P8CM
 * OPDIfNewestHaveOpportunityBatch   测试类
 * 每天更新 opd计划表的 if_Newest_HaveOpportunity__c 字段值
 */
@isTest
private class OPDIfNewestHaveOpportunityBatchTest {
    static testMethod void testMethod1() {
        ControllerUtil.EscapeNFM001Trigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
        StaticParameter.EscapeOppandStaTrigger = true;
        StaticParameter.EscapeNFM110Trigger = true;
        StaticParameter.EscapeRaTrigger = true;
        Oly_TriggerHandler.bypass('ContactTriggerHandler');
        Oly_TriggerHandler.bypass(Product2Handler.Class.getName());
        Oly_TriggerHandler.bypass(AssetHandler.Class.getName());
        Oly_TriggerHandler.bypass(OPDPlanHandler.Class.getName());
        Test.startTest();
        SS_Batch_Column_Mapping__c oppMapping = new SS_Batch_Column_Mapping__c(Name = 'targetKeyProduct', SS_TableName__c='targetKeyProduct__c');

        oppMapping.From_Column_118__c = 'Sales_target_CV290__c';
        oppMapping.SS_Column_118__c = 'CV-290';
        oppMapping.From_Column_119__c = 'Sales_target_290Miro__c';
        oppMapping.SS_Column_119__c = 'GI-290镜子,GI-290镜子放大,GI-290镜子CF,BF-290镜子';
        oppMapping.From_Column_120__c = 'Sales_target_TB__c';
        oppMapping.SS_Column_120__c = 'TB';
        oppMapping.From_Column_129__c = 'Opp_cnt_290MiroBF__c';
        oppMapping.SS_Column_129__c = 'BF-290镜子';
        oppMapping.From_Column_130__c = 'Opp_cnt_290MiroGI__c';
        oppMapping.SS_Column_130__c = 'GI-290镜子';
        oppMapping.From_Column_131__c = 'Opp_cnt_290MiroGI_CF__c';
        oppMapping.SS_Column_131__c = 'GI-290镜子CF';
        oppMapping.From_Column_146__c = 'Asset_Cnt_290MiroBF__c';
        oppMapping.SS_Column_146__c = 'BF-290镜子';
        oppMapping.From_Column_147__c = 'Asset_Cnt_290MiroGI__c';
        oppMapping.SS_Column_147__c = 'GI-290镜子';
        oppMapping.From_Column_148__c = 'Asset_Cnt_290MiroGI_CF__c';
        oppMapping.SS_Column_148__c = 'GI-290镜子CF';
        oppMapping.From_Column_163__c = 'OPD_amount_290MiroBF__c';
        oppMapping.SS_Column_163__c = 'BF-290镜子';
        oppMapping.From_Column_164__c = 'OPD_amount_290MiroGI__c';
        oppMapping.SS_Column_164__c = 'GI-290镜子';
        oppMapping.From_Column_165__c = 'OPD_amount_290MiroGI_CF__c';
        oppMapping.SS_Column_165__c = 'GI-290镜子CF';
        insert new SS_Batch_Column_Mapping__c[] {oppMapping};
        

       //医院 记录类型
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'];
        if (rectHp.size() == 0) {
            return;
        }
        //科室 记录类型
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'];
        if (rectDpt.size() == 0) {
            return;
        }

        //简档
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];

        //用户

        User hpOwner = new User(Test_staff__c = true, LastName = 'hpowner', Alias = 'hp', Work_Location__c = '北京', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        User hpOwner2 = new User(Test_staff__c = true, LastName = 'hp2owner', Alias = 'hp2', Work_Location__c = '重庆', CommunityNickname = 'hpOwner2', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner2@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner2;


        //医院
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'hp', OwnerId = hpOwner.Id);
        hp.FSE_GI_Main_Leader__c = hpOwner.Id;
        hp.FSE_SP_Main_Leader__c = hpOwner2.Id;
        insert hp;

        //战略科室
        Account dc = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_GI'];
        //科室
        Account dpt = new Account(RecordTypeId = rectDpt[0].Id);
        dpt.Name         = '*';
        dpt.Department_Name__c  = 'TestDepart';
        dpt.ParentId            = dc.Id;
        dpt.Department_Class__c = dc.Id;
        dpt.Hospital__c         = hp.Id;
        insert dpt;
        //科室1
        Account dpt1 = new Account(RecordTypeId = rectDpt[0].Id);
        dpt1.Name         = '123';
        dpt1.Department_Name__c  = 'TestDepart1';
        dpt1.ParentId            = dc.Id;
        dpt1.Department_Class__c = dc.Id;
        dpt1.Hospital__c         = hp.Id;
        insert dpt1;

         //根据opd计划实施日期 算出本财年
        Date dateToday = Date.today();
        Integer year = dateToday.year();
        Integer month = dateToday.month();
        if (month < 4) {
            year -= 1;
        }
        String selectedTab = String.valueOf(year - 1867 - 1 + 'P'); 
        String selectedTab1 = String.valueOf(year - 1867 + 'P'); //今财年

        String ocmYear = year + '年度'; // 20230516 ljh add

        //客户-目标客户 默认让opd计划和客户-目标客户的财年相同
        Account_Number_of_target__c anot = new Account_Number_of_target__c();
        anot.Account__c = dc.Id;
        anot.Account_HP__c = hp.Id;
        anot.OCM_Period__c = selectedTab;
        anot.Target_amount_170_190__c =36597;
        anot.OPD_amount_290MiroBF__c =1;
        anot.OCM_Year__c = ocmYear;
        anot.OPD_amount_290MiroGI_CF__c =1;
        insert anot;

        Account_Number_of_target__c anot1 = new Account_Number_of_target__c();
        anot1.Account__c = dc.Id;
        anot1.Account_HP__c = hp.Id;
        anot1.OCM_Period__c = selectedTab1;
        anot1.Target_amount_170_190__c =36597;
        anot1.OPD_CV290__c =1;
        anot1.OPD_290Miro__c =1;
        anot1.OPD_TB__c =1;
        anot1.Rental_Cnt_CV290__c =1;
        anot1.Rental_Cnt_TB__c =1;
        anot1.Target_amount_CV290__c =1;
        anot1.Target_amount_TB__c  =1;//Sales_target_TB__c
        anot1.Opp_cnt_290MiroGI__c =1;
        anot1.Opp_cnt_290MiroGI_CF__c =1;
        anot1.Asset_Cnt_290MiroBF__c =1;
        anot1.OPD_amount_290MiroBF__c =1;
        anot1.OPD_amount_290MiroGI_CF__c =1;
        // 20230516 ljh start 
        // anot1.OCM_Year__c = '2021年度';
        anot1.OCM_Year__c = ocmYear;
        // 20230516 ljh end
        

        insert anot1;

        OPDPlan__c oPDPlan0 = new OPDPlan__c();
        oPDPlan0.Status__c = '计划中';
        oPDPlan0.OPDType__c = '学会';
        oPDPlan0.if_OPDTarget__c=1;
        oPDPlan0.if_HaveSalestarget__c =1;
        oPDPlan0.Cnt_OPD_ThisYear__c=1;
        oPDPlan0.Cnt_OPD_LastYear__c=1;
        oPDPlan0.if_HaveOpportunity__c=1;
        oPDPlan0.if_HaveEquipment__c =1;
        oPDPlan0.Cnt_Rentals__c =1;
        oPDPlan0.PlanProdDetailSys__c ='CV-290*4; GI-290镜子*1; GI-290镜子CF*1;';
        oPDPlan0.PlanProdDetailSysT__c ='CV-290*4; GI-290镜子*1; GI-290镜子CF*1;';
        oPDPlan0.Account_Laboratory__c =dpt.Id;
        oPDPlan0.OPDPlan_ImplementDate__c = Date.today();
        oPDPlan0.OPDPlan_ImplementDate_temp__c = Date.today();
        oPDPlan0.NoOpp_Reason__c = 'HCP对应';
       // oPDPlan0.OPD_Customers_Target_Last__c=anot.id;
       // oPDPlan0.OPD_Customers_Target__c =anot1.id;
        insert oPDPlan0;
        //opd计划1
        OPDPlan__c oPDPlan1 = new OPDPlan__c();
        oPDPlan1.ThisMonthPlan__c = 1;
        oPDPlan1.Status__c = '草案中';
        oPDPlan1.Account_Laboratory__c =dpt.Id;
        oPDPlan1.OPD_Customers_Target_Last__c=null;
        oPDPlan1.OPDPlan_ImplementDate__c = Date.today().addDays(3);
        oPDPlan1.NoOpp_Reason__c = 'HCP对应';
        oPDPlan1.OPDType__c = '科室';
        insert oPDPlan1;
        oPDPlan0.Account_Laboratory__c =dpt.Id;
        oPDPlan0.OPDPlan_ImplementDate__c = oPDPlan1.OPDPlan_ImplementDate__c.addDays(40);
        update oPDPlan0;

        OPDPlan__c oPDPlan3 = new OPDPlan__c();
        oPDPlan3.ThisMonthPlan__c = 1;
        oPDPlan3.Status__c = '计划中';
        oPDPlan3.Account_Laboratory__c =dpt.Id;
        oPDPlan3.OPD_Customers_Target_Last__c=null;
        oPDPlan3.OPDPlan_ImplementDate__c = Date.today().addDays(3);
        oPDPlan3.NoOpp_Reason__c = 'HCP对应';
        oPDPlan3.OPDType__c = '科室';
        insert oPDPlan3;

        
        OPDPlan__c oPDPlan2 = new OPDPlan__c();
        oPDPlan2.Status__c = '计划中';
        oPDPlan2.Account_Laboratory__c =dpt.Id;
        //oPDPlan2.OPDPlan_ImplementDate__c = Date.today();
        oPDPlan2.NoOpp_Reason__c = 'HCP对应';
        oPDPlan2.OPDType__c = '科室';
        oPDPlan2.if_OPDTarget__c=1;
        oPDPlan2.if_HaveSalestarget__c =1;
        oPDPlan2.Cnt_OPD_ThisYear__c=1;
        oPDPlan2.Cnt_OPD_LastYear__c=1;
        oPDPlan2.if_HaveOpportunity__c=1;
        oPDPlan2.if_HaveEquipment__c =1;
        oPDPlan2.Cnt_Rentals__c =1;
        oPDPlan0.OPDType__c = '学会';
        oPDPlan2.OPD_Customers_Target_Last__c=anot.id;
        oPDPlan2.OPD_Customers_Target__c =anot1.id;
        oPDPlan2.PlanProdDetailSys__c ='CV-290*4; GI-290镜子*1; GI-290镜子CF*1;';
        oPDPlan2.PlanProdDetailSysT__c ='CV-290*4; GI-290镜子*1; GI-290镜子CF*1;';
        // oPDPlan2.OriginalOpdPlan__c = oPDPlan3.Id;
        insert oPDPlan2;
        oPDPlan2.Status__c = '审批中'; 
        
        update oPDPlan2;

        //2021-06-26   mzy  add 

        // OPDPlan__c oPDPlan4 = new OPDPlan__c();
        // oPDPlan4.Status__c = '草案中';
        // oPDPlan4.Account_Laboratory__c =dpt.Id;
        // oPDPlan4.OPDPlan_ImplementDate__c = Date.today();
        // oPDPlan4.NoOpp_Reason__c = 'HCP对应';
        // oPDPlan4.if_OPDTarget__c=1;
        // oPDPlan4.if_HaveSalestarget__c =1;
        // oPDPlan4.Cnt_OPD_ThisYear__c=1;
        // oPDPlan4.Cnt_OPD_LastYear__c=1;
        // oPDPlan4.if_HaveOpportunity__c=1;
        // oPDPlan4.if_HaveEquipment__c =1;
        // oPDPlan4.Cnt_Rentals__c =1;
        // oPDPlan4.OPDType__c = '科室';
        // oPDPlan4.OPD_Customers_Target_Last__c=anot.id;
        // oPDPlan4.OPD_Customers_Target__c =anot1.id;
        // oPDPlan4.PlanProdDetailSys__c ='CV-290*4; GI-290镜子*1; GI-290镜子CF*1;';
        // oPDPlan4.PlanProdDetailSysT__c ='CV-290*4; GI-290镜子*1; GI-290镜子CF*1;';
        // insert oPDPlan4;

        // //取消
        // CancelPostponePlan__c tempCancelPostponePlan = new  CancelPostponePlan__c();
        // tempCancelPostponePlan.Status__c = '取消成功';
        // tempCancelPostponePlan.CancelOPDPlan__c = oPDPlan4.Id;
        // // 20240302 ljh start
        // // tempCancelPostponePlan.RecordTypeId = '01210000000gQyL';
        // tempCancelPostponePlan.RecordTypeId = Schema.SObjectType.CancelPostponePlan__c.getRecordTypeInfosByDeveloperName().get('CancelType').getRecordTypeId();
        // // 20240302 ljh end
        // tempCancelPostponePlan.cancelReasonCombobox__c  = '无备品';
        // insert tempCancelPostponePlan;


        // oPDPlan4 = [select id,DelayCancel_Reason__c,Status__c from OPDPlan__c where id = :oPDPlan4.Id];
        //System.assertEquals('',oPDPlan4.Status__c);
        //System.assertEquals('',oPDPlan4.DelayCancel_Reason__c);

        OPDPlan__c oPDPlan5 = new OPDPlan__c();
        oPDPlan5.Status__c = '审批中';
        oPDPlan5.OPDType__c = '科室';
        oPDPlan5.Account_Laboratory__c =dpt.Id;
        oPDPlan5.OPDPlan_ImplementDate__c = Date.today();
        oPDPlan5.NoOpp_Reason__c = 'HCP对应';
        oPDPlan5.if_OPDTarget__c=1;
        oPDPlan5.if_HaveSalestarget__c =1;
        oPDPlan5.Cnt_OPD_ThisYear__c=1;
        oPDPlan5.Cnt_OPD_LastYear__c=1;
        oPDPlan5.if_HaveOpportunity__c=1;
        oPDPlan5.if_HaveEquipment__c =1;
        oPDPlan5.Cnt_Rentals__c =1;
        oPDPlan5.OPD_Customers_Target_Last__c=anot.id;
        oPDPlan5.OPD_Customers_Target__c =anot1.id;
        oPDPlan5.PlanProdDetailSys__c ='CV-290*4; GI-290镜子*1; GI-290镜子CF*1;';
        oPDPlan5.PlanProdDetailSysT__c ='CV-290*4; GI-290镜子*1; GI-290镜子CF*1;';
        // oPDPlan5.OriginalOpdPlan__c = oPDPlan4.Id;
        insert oPDPlan5;

        OPDPlan__c oPDPlan6 = new OPDPlan__c();
        oPDPlan6.Status__c = '计划中';
        oPDPlan6.OPDType__c = '学会';
        oPDPlan6.Account_Laboratory__c =dpt.Id;
        oPDPlan6.OPDPlan_ImplementDate__c = Date.today().addDays(3);
        oPDPlan6.NoOpp_Reason__c = 'HCP对应';
        oPDPlan6.if_OPDTarget__c=1;
        oPDPlan6.if_HaveSalestarget__c =1;
        oPDPlan6.Cnt_OPD_ThisYear__c=1;
        oPDPlan6.Cnt_OPD_LastYear__c=1;
        oPDPlan6.if_HaveOpportunity__c=1;
        oPDPlan6.if_HaveEquipment__c =1;
        oPDPlan6.Cnt_Rentals__c =1;
        oPDPlan6.OPD_Customers_Target_Last__c=anot.id;
        oPDPlan6.OPD_Customers_Target__c =anot1.id;
        oPDPlan6.PlanProdDetailSys__c ='CV-290*4; GI-290镜子*1; GI-290镜子CF*1;';
        oPDPlan6.PlanProdDetailSysT__c ='CV-290*4; GI-290镜子*1; GI-290镜子CF*1;';
        oPDPlan6.OriginalOpdPlanApplication__c = oPDPlan3.Id;
        oPDPlan6.RentalReson__c = '追加配套';
        insert oPDPlan6;

        List<Id> listId = new List<ID>();
        listId.add(oPDPlan0.id);
        Id execBTId = Database.executeBatch(new OPDIfNewestHaveOpportunityBatch(listId), 200);

        Database.executeBatch(new OPDIfNewestHaveOpportunityBatch(), 200);
        Test.stopTest();
    }
}