@isTest
public with sharing class LexOPDSupplementaryControllerTest {

    static void setupTestData() {
        ControllerUtil.EscapeNFM001Trigger = true;
        FixtureUtil.SkipTrigger = true;
        StaticParameter.EscapeNFM110Trigger = true;
        Oly_TriggerHandler.bypass('ContactTrigger');
        Oly_TriggerHandler.bypass('ContactTrigger');
        Oly_TriggerHandler.bypass('ContactTriggerHandler');
        Oly_TriggerHandler.bypass('AssetTrigger');
        Oly_TriggerHandler.bypass('MaintenanceContractTrigger');
        Oly_TriggerHandler.bypass('MaintenanceContractAfterUpdate');
        Oly_TriggerHandler.bypass('MaintenanceContractBeforeDelete');
        Oly_TriggerHandler.bypass('MaintenanceContractHpDeptUpd');
        Oly_TriggerHandler.bypass('NFM106Controller');
        Oly_TriggerHandler.bypass('NFM106Trigger');
        Oly_TriggerHandler.bypass('AssetRecordTypeUpd');
        Oly_TriggerHandler.bypass('NFM101Controller');
        Oly_TriggerHandler.bypass('NFM001Controller');
        Oly_TriggerHandler.bypass('SyncProduct2');
        Oly_TriggerHandler.bypass('RentalApplyBeforeUpdate');
        Oly_TriggerHandler.bypass('ContactTriggerHandler');
        Oly_TriggerHandler.bypass('OPDPlanUp');
        Oly_TriggerHandler.bypass('OPDPlanHandler');
        StaticParameter.ContentDocumentLink = false; 
        StaticParameter.OPDPlanUp = true;

        RecordType rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity' ];
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName IN ('Department_GI', 'Department_BF') order by DeveloperName desc];
        // 病院を作る
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital.Name = 'test hospital';
        insert hospital;
        // 戦略科室を得る
        Account[] strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_OTH'];
        // 診療科を作る
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
        dep.Name = 'test dep';
        dep.AgentCode_Ext__c = '9999998';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        dep.Department_Class_Label__c = '123';
        insert dep;
        Contact contact2 = new Contact();
        contact2.AccountId = dep.Id;
        // contact2.FirstName = '責任者test1经销商';
        contact2.LastName = '責任者test1经销商';
        insert contact2;

        Opportunity opp = new Opportunity(
            Name='Opp',
            StageName='引合',
            CloseDate=Date.today().addDays(10),
            Close_Forecasted_Date__c=Date.today(),
            CurrencyIsoCode = 'CNY'
        );
        insert opp;

        // 省
        Address_Level__c al = new Address_Level__c();
        al.Name = '東京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;
        // 市
        Address_Level2__c al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-99';
        al2.Level1_Sys_No__c = '999999';
        al2.Level1_Name__c = '東京';
        al2.Name = '渋谷区';
        al2.Level2_Code__c = 'CN-9999';
        al2.Level2_Sys_No__c = '9999999';
        al2.Address_Level__c = al.id;
        insert al2;

        //产品
        Product2 pro1 = new Product2(Name='name01',IsActive=true,Family='GI',
                Fixture_Model_No__c='n01',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'n01',
                ProductCode_Ext__c='pc01',Manual_Entry__c=false);

        // 备品配套1(主体 两个附属品(个体管理 数量管理))
        Fixture_Set__c fsObjA1 = new Fixture_Set__c();
        fsObjA1.Name = 'n01';
        fsObjA1.Fixture_Set_Body_Model_No__c = 'n01';
        fsObjA1.Loaner_code__c = 'n01';
        fsObjA1.Loaner_name__c = 'n01';
        fsObjA1.Product_Type__c = '新产品';
        fsObjA1.Loaner_categoryI__c = '硬性内镜'; 
        fsObjA1.Loaner_categoryII__c = '摄像头'; 
        insert fsObjA1;
        Product2 pro2 = new Product2(Name='name02',IsActive=true,Family='GI',
                Fixture_Model_No__c='n02',Serial_Lot_No__c='Lot tracing',
                ProductCode_Ext__c='pc02',Manual_Entry__c=false);
        Product2 pro3 = new Product2(Name='name03',IsActive=true,Family='GI',
                Fixture_Model_No__c='n03',Serial_Lot_No__c='Lot tracing',
                ProductCode_Ext__c='pc03',Manual_Entry__c=false);
        insert new Product2[] {pro1, pro2, pro3};

        String timenow = Datetime.now().format('yyyyMMddHHmmss');
        User user1 = new User(Test_staff__c = true, LastName = 'TestMao1',
                  Alias = 'hp1', Email = 'Test1@sunbridge.com',
                  Username = 'Test1' + timenow + '@sunbridge.com', IsActive = true, 
                  EmailEncodingKey = 'ISO-2022-JP',TimeZoneSidKey = 'Asia/Tokyo', 
                  LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja',
                  ProfileId = System.Label.ProfileId_SystemAdmin,
                  Dept__c = '医疗华北营业本部', Job_Category__c = '销售服务', 
                  Province__c = '北京');

        OPDPlan__c opd = new OPDPlan__c();
        opd.Status__c = '草案中';
        opd.OPDPlan_ImplementDate__c = Date.today().addDays(5);
        opd.NoOpp_Reason__c = 'HCP对应';
        opd.OPDType__c = '科室';
        opd.PlanProdDetail__c = 'n01*1;n02*1';
        opd.Account_Laboratory__c = dep.Id;
        insert opd;

        // 备品配套明细
        Fixture_Set_Detail__c fsdObjA1 = new Fixture_Set_Detail__c();
        fsdObjA1.Name = '备品配套明细名1';
        fsdObjA1.Name_CHN_Created__c = '中文名称1';
        fsdObjA1.Product2__c = pro1.Id;
        fsdObjA1.Fixture_Set__c = fsObjA1.Id;
        fsdObjA1.Is_Body__c = true;
        fsdObjA1.Is_Optional__c = false;
        fsdObjA1.UniqueKey__c = fsObjA1.Id + ':' + pro1.Id;
        fsdObjA1.SortInt__c = 1;
        // 备品配套明细
        Fixture_Set_Detail__c fsdObjA2 = new Fixture_Set_Detail__c();
        fsdObjA2.Name = '备品配套明细名2';
        fsdObjA2.Name_CHN_Created__c = '中文名称2';
        fsdObjA2.Product2__c = pro2.Id;
        fsdObjA2.Fixture_Set__c = fsObjA1.Id;
        fsdObjA2.Is_Body__c = false;
        fsdObjA2.Is_Optional__c = true;
        fsdObjA2.UniqueKey__c = fsObjA1.Id + ':' + pro2.Id;
        fsdObjA2.SortInt__c = 2;
        // 备品配套明细
        Fixture_Set_Detail__c fsdObjA3 = new Fixture_Set_Detail__c();
        fsdObjA3.Name = '备品配套明细名3';
        fsdObjA3.Name_CHN_Created__c = '中文名称3';
        fsdObjA3.Product2__c = pro3.Id;
        fsdObjA3.Fixture_Set__c = fsObjA1.Id;
        fsdObjA3.Is_Body__c = false;
        fsdObjA3.Is_Optional__c = true;
        fsdObjA3.UniqueKey__c = fsObjA1.Id + ':' + pro3.Id;
        fsdObjA3.SortInt__c = 3;
        insert new Fixture_Set_Detail__c[] {fsdObjA1,fsdObjA2,fsdObjA3};
        //Id userName = [SELECT Id, Name FROM User WHERE Name = '陈海山'].Id;
        //备品借出申请
        Rental_Apply__c raObj = new Rental_Apply__c();
        raObj.Name = 'testra';
        raObj.Product_category__c = 'GI';
        raObj.Demo_purpose1__c = '产品试用';
        raObj.demo_purpose2__c = FixtureUtil.raDemo_purpose2MAP.get('shiyongwuxunjia');
        raObj.direct_send__c = '医疗机构';
        // raObj.Medical_Institution_Address__c = '医疗机构';
        // raObj.Medical_Institution_Address_sup__c = '医疗机构';
        raObj.Loaner_received_staff__c = '王五';
        raObj.Loaner_received_staff_phone__c = '110';
        raObj.direct_shippment_address__c = '北京市';
        raObj.Hospital__c = hospital.Id;
        raObj.Strategic_dept__c = strategicDep[0].Id;
        raObj.Account__c = dep.Id;
        raObj.Request_shipping_day__c = Date.toDay().addDays(2);  // 希望到货日
        raObj.Hope_Lonaer_date_Num__c = 1;  // 希望借用天数
        raObj.Phone_number__c = '1234567890';
        raObj.Loaner_medical_Staff__c = contact2.Id;
        raObj.Status__c = '申请中（OPD未通过）';
        raObj.applyUser__c = user1.Id;
        raObj.Person_In_Charge__c = user1.Id;
        // raObj.Department_Class__c = strategicDep[0].Id;
        insert raObj;
        // Application_for_Conference_Adjudication__c aca = new Application_for_Conference_Adjudication__c();
        // aca.MeetingApprovedNo__c            = 'ME154P03-0012';
        // aca.Name                            = 'ME154P03-0012'; // 用决裁编号替代名称
        // aca.CampaignTheme__c                = 'obpm发起赞助决裁';
        // aca.Num__c                          = 'test1';
        // aca.StartDate__c                    = Date.today().addDays(10);
        // aca.EndDate__c                      = Date.today().addDays(13);
        // aca.Implementation_Purpose1__c      = '其他';
        // aca.OfficeCategory__c               = '1.消化科';
        // aca.MeetingType__c                  = '主办会';
        // aca.StateMaster__c                  = '北京市';
        // aca.CityMaster__c                   = '北京市';
        // aca.WorkshopPlace__c                = 'CTEC';
        // aca.HostName__c                     = '医学会';
        // aca.CooperatorCompany__c            = '中华医学会';
        // aca.ConveningParticipantsNum__c     = 0;
        // aca.IsPaidToSponsor__c              = '否';
        // aca.SponsorshipCategory__c          = '资金';
        // aca.ExpectedOlympusAttendance__c    = 0;
        // aca.ActivityTypeName__c             = '自主主办会';
        // aca.ExpectedHcpAttendance__c        = 0;
        // aca.ProcessState__c                 = '草稿';
        // aca.Type_of_adjudication_meeting__c = '线下';
        // aca.Activities_of_a_share__c        = '否';
        // aca.IsSupplemental__c = true;

        // insert aca;

        // Campaign cam = new Campaign();
        // Id RtId = Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('Society').getRecordTypeId();
        // // Id RtId2 = Schema.SObjectType.CampaignMember.getRecordTypeInfosByDeveloperName().get('WorkShop').getRecordTypeId();
        // cam.RecordTypeId                     = RtId;
        // // cam.CampaignMemberRecordTypeId       = RtId2;

        // cam.Name                             = aca.MeetingApprovedNo__c;
        // cam.Name2__c                         = aca.MeetingApprovedNo__c;
        // cam.MeetingApprovedNo__c             = aca.MeetingApprovedNo__c;
        // cam.Meeting_Approved_No__c           = aca.Id;
        // cam.StartDate                        = aca.StartDate__c;
        // cam.EndDate                          = aca.EndDate__c;
        // cam.Implementation_Purpose__c                   = aca.Implementation_Purpose1__c;
        // cam.OfficeCategory__c                = aca.OfficeCategory__c;
        // cam.Meeting_Type__c                  = aca.MeetingType__c;
        // cam.Activity_Type_Name__c            = aca.ActivityTypeName__c;
        
        // cam.WorkshopPlace__c                 = aca.WorkshopPlace__c;
        // cam.HostName__c                      = aca.HostName__c;
        // cam.cooperatorCompany__c             = aca.CooperatorCompany__c;
        // cam.Convening_Participants_Num__c    = aca.ConveningParticipantsNum__c;
        // cam.IF_Approved__c                   = true;

        // // OBPM对接 新增字段
        // cam.Expected_Olympus_Attendance__c   = aca.ExpectedOlympusAttendance__c;
        // cam.Expected_Hcp_Attendance__c       = aca.ExpectedHcpAttendance__c;
        // cam.SponsorshipCategory__c           = aca.SponsorshipCategory__c;
        // cam.IsPaidToSponsor__c               = aca.IsPaidToSponsor__c;
        // cam.Activity_Type_Name__c            = aca.ActivityTypeName__c;
        // cam.Is_LendProduct__c                = '是';
        // cam.PlanBackData__c                  = NFMUtil.parseDateTimeStr2Date('2022-04-30');
        // cam.LoadNum__c                       = 'n01*1;';
        // insert cam;

        OPDPlan__c oPDPlan0 = new OPDPlan__c();
        oPDPlan0.Status__c = '计划中';
        oPDPlan0.OPDPlan_ImplementDate__c = Date.today();
        oPDPlan0.NoOpp_Reason__c = 'HCP对应';
        oPDPlan0.OPDType__c = '事件';
        insert oPDPlan0;

        OPDPlan__c oPDPlan0Clone = new OPDPlan__c();
        oPDPlan0Clone = oPDPlan0.clone();
        oPDPlan0Clone.Id = null;
        oPDPlan0Clone.RentalReson__c = '追加配套';
        oPDPlan0Clone.PlanProdDetail__c = 'n01*1;n02*1';
        insert oPDPlan0Clone;
        OPDPlan__c oPDPlan0Clone2 = new OPDPlan__c();
        oPDPlan0Clone2 = oPDPlan0.clone();
        oPDPlan0Clone2.Id = null;
        oPDPlan0Clone2.RentalReson__c = '追加配套';
        oPDPlan0Clone2.OriginalOpdPlanApplication__c = oPDPlan0.Id;
        oPDPlan0Clone2.PlanProdDetail__c = 'n01*1;n02*1';
        insert oPDPlan0Clone2;


        OPDPlan__c oPDPlan4 = new OPDPlan__c();
        oPDPlan4.Status__c = '草案中';
        oPDPlan4.Account_Laboratory__c =dep.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.PlanProdDetailSys__c ='n01*1';
        oPDPlan4.PlanProdDetail__c = 'n01*1';
        insert oPDPlan4;

        //原OPD关联备品申请
        // Rental_Apply__c raObj1 = new Rental_Apply__c();
        // raObj1.Name = 'testra';
        // raObj1.Product_category__c = 'GI';
        // raObj1.Demo_purpose1__c = '其他';
        // raObj1.demo_purpose2__c = '其他';
        // raObj1.Account__c = dep.Id;
        // raObj1.Person_In_Charge__c = Userinfo.getUserId();
        // raObj1.applyUser__c = Userinfo.getUserId();
        // raObj1.Demo_purpose_text__c = '123';
        // raObj1.Request_shipping_day__c = Date.toDay();
        // raObj1.Request_return_day__c = Date.toDay();
        // raObj1.direct_send__c = '医疗机构';
        // raObj1.direct_shippment_address__c = '北京市';
        // raObj1.Loaner_received_staff__c = '王五';
        // raObj1.Loaner_received_staff_phone__c = '110';
        // raObj1.pickup_time__c = System.now();
        // raObj1.OPDPlan__c = oPDPlan4.Id;
        // raObj1.Cancel_Reason__c = '主动取消';
        // insert raObj1;

        //System.assertEquals(oPDPlan4.Account_Laboratory__c , raObj1.Account__c);
        //取消
        CancelPostponePlan__c tempCancelPostponePlan = new  CancelPostponePlan__c();
        tempCancelPostponePlan.Status__c = '取消成功';
        tempCancelPostponePlan.CancelOPDPlan__c = oPDPlan4.Id;
        tempCancelPostponePlan.RecordTypeId = Schema.SObjectType.CancelPostponePlan__c.getRecordTypeInfosByDeveloperName().get('CancelType').getRecordTypeId();
        tempCancelPostponePlan.cancelReasonCombobox__c  = '无备品';
        insert tempCancelPostponePlan;

        OPDPlan__c oPDPlan5 = new OPDPlan__c();
        oPDPlan5.Status__c = '审批中';
        oPDPlan5.OPDType__c = '科室';
        oPDPlan5.Account_Laboratory__c =dep.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 = 'n01*1';
        oPDPlan5.PlanProdDetail__c = 'n01*1';
        oPDPlan5.OriginalOpdPlan__c = oPDPlan4.Id;
        insert oPDPlan5;

    }

    @isTest
    static void testMethod1(){
        System.Test.startTest();
        setupTestData();
        System.Test.stopTest();
        OPDPlan__c opd = [SELECT Id FROM OPDPlan__c LIMIT 1];

        LexOPDSupplementaryController.init('123');
        LexOPDSupplementaryController.init(opd.Id);
        LexOPDSupplementaryController.getTheOPDPlan('真的');
        LexOPDSupplementaryController.getTheOPDPlan(opd.Id);
    }

    @isTest
    static void testMethod2(){
        System.Test.startTest();
        setupTestData();
        System.Test.stopTest();
        Account acc = [SELECT Id FROM Account limit 1];
        LexOPDSupplementaryController.initNewOPDButton(acc.Id);
        LexOPDSupplementaryController.initNewOPDButton('12323');
        
    }
    @isTest
    static void testMethod2_1(){
        System.Test.startTest();
        setupTestData();
        System.Test.stopTest();
        OPDPlan__c opd = [SELECT Id FROM OPDPlan__c LIMIT 1];
        LexOPDSupplementaryController.initNewOPDButton2(opd.Id);
        LexOPDSupplementaryController.initCancleSumbit('123');
        LexOPDSupplementaryController.initCancleSumbit(opd.Id);
        
    }


    @isTest
    static void testMethod3(){
        ControllerUtil.EscapeNFM001Trigger = true;
        FixtureUtil.SkipTrigger = true;
        Oly_TriggerHandler.bypass('ContactTrigger');
        Oly_TriggerHandler.bypass('ContactTriggerHandler');
        Oly_TriggerHandler.bypass('AssetTrigger');
        Oly_TriggerHandler.bypass('MaintenanceContractTrigger');
        Oly_TriggerHandler.bypass('MaintenanceContractAfterUpdate');
        Oly_TriggerHandler.bypass('MaintenanceContractBeforeDelete');
        Oly_TriggerHandler.bypass('MaintenanceContractHpDeptUpd');
        Oly_TriggerHandler.bypass('NFM106Controller');
        Oly_TriggerHandler.bypass('NFM106Trigger');
        Oly_TriggerHandler.bypass('AssetRecordTypeUpd');
        Oly_TriggerHandler.bypass('NFM101Controller');
        Oly_TriggerHandler.bypass('NFM001Controller');
        Oly_TriggerHandler.bypass('SyncProduct2');
        Oly_TriggerHandler.bypass('RentalApplyBeforeUpdate');
        Oly_TriggerHandler.bypass('ContactTriggerHandler');
        Oly_TriggerHandler.bypass('OPDPlanUp');
        Oly_TriggerHandler.bypass('RentalApplyTriggerHandler');
        StaticParameter.ContentDocumentLink = false; 
        StaticParameter.OPDPlanUp = true;
        //setupTestData();

        Product2 pro1 = new Product2(Name='name01',IsActive=true,Family='GI',
        Fixture_Model_No__c='n01',Serial_Lot_No__c='S/N tracing',
        Fixture_Model_No_T__c = 'n01',
        Product_Type__c = '新产品',
        ProductCode_Ext__c='pc01',Manual_Entry__c=false);
        INSERT pro1;

        User thisUser =  [select Id from User where Id = :UserInfo.getuserId()];
        User u ;
        Account a;
        System.runAs(thisUser){
            u = new User(Test_staff__c = true, LastName = 'TestMao', 
                Alias = 'hp', CommunityNickname = 'TestMao', Email = 'Test@sunbridge.com',
                Username ='standarduser22@testorg.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', 
                TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = System.Label.ProfileId_SystemAdmin,
                Dept__c = '能量事业本部', Province__c = '北京');
        }

        system.runAs(u){
            OPDPlan__c opd2 = new OPDPlan__c();
            opd2.Status__c = '草案中';
            opd2.OPDPlan_ImplementDate__c = Date.today().addDays(10);
            opd2.NoOpp_Reason__c = 'HCP对应';
            opd2.PlanProdDetail__c = 'n01*1;';
            opd2.RentalReson__c = '新产品评价';
            opd2.OPDType__c = '事件';
            insert opd2;
            LexOPDSupplementaryController.newRentalApply(opd2.Id); 
        }
    }

    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;

    @isTest
    static void testMethod4(){
        Test.startTest();
        FixtureUtil.SkipTrigger = true;
        StaticParameter.EscapeNFM110Trigger = true;
        Oly_TriggerHandler.bypass('ContactTriggerHandler');
        StaticParameter.EscapeVMCTrigger = true;
        StaticParameter.MaintenanceContractTrigger = false;
        ControllerUtil.EscapeNFM001Trigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
        StaticParameter.EscapeOppandStaTrigger = true;
        Oly_TriggerHandler.bypass('OPDPlanHandler');
        StaticParameter.ContentDocumentLink = false; 
        StaticParameter.OPDPlanUp = true;

        Address_Level__c al = new Address_Level__c();
        al.Name = '東京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;
        // 市
        Address_Level2__c al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-99';
        al2.Level1_Sys_No__c = '999999';
        al2.Level1_Name__c = '東京';
        al2.Name = '渋谷区';
        al2.Level2_Code__c = 'CN-9999';
        al2.Level2_Sys_No__c = '9999999';
        al2.Address_Level__c = al.id;
        insert al2;

        // 病院を作る
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital.Name = 'test hospital';
        hospital.Is_Active__c = '有効';
        hospital.Attribute_Type__c = '卫生部';
        hospital.Speciality_Type__c = '综合医院';
        hospital.Grade__c = '一级';
        hospital.OCM_Category__c = 'SLTV';
        hospital.Is_Medical__c = '医疗机构';
        hospital.State_Master__c = al.id;
        hospital.City_Master__c = al2.id;
        hospital.Town__c = '东京';
        insert hospital;

        RecordType rectCam =
            [SELECT Id FROM RecordType
             WHERE IsActive = true AND SobjectType = 'Campaign'
                              AND DeveloperName = 'ServiceEngineerTraining'];

         Product2 prd1 = new Product2();
            prd1.Name     = 'Prd1';
            prd1.ProductCode_Ext__c     = 'Prd1';
            prd1.ProductCode            = 'Prd1';
            prd1.Asset_Model_No__c = '1001';
            prd1.Important_product__c = true;
            prd1.MDM_Model_No__c = 'CF-H170I';
            prd1.Important_Rroduct_1GI__c = true;
            prd1.RentalSubject__c = true;
            insert prd1;  


        Account[] strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_OTH'];


        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
        dep.Name = 'test dep';
        dep.AgentCode_Ext__c = '9999998';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;

        Contact contact2 = new Contact();
        contact2.AccountId = dep.Id;
        contact2.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        // contact2.FirstName = '責任者test1经销商';
        contact2.LastName = '責任者test1经销商';
        contact2.Agency_User__c = true;
        insert contact2;
        PricebookEntry entry = new PricebookEntry( Pricebook2Id = pricebookId, Product2Id = prd1.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = 'CNY';
        insert entry;

        Opportunity opp = new Opportunity(
            Name='Opp',
            StageName='引合',
            Department_Class__c = strategicDep[0].Id,
            CloseDate=Date.today().addDays(10),
            Close_Forecasted_Date__c=Date.today(),
            CurrencyIsoCode = 'CNY'
        );
        insert opp;
        
        Quote q = new Quote(
            Name = 'quote',
            OpportunityId = opp.Id,
            Pricebook2Id = pricebookId
        );
        insert q;

        opp.Estimation_Id__c = q.Id;
        // opp.Estimation_Id__c = String.valueOf(Date d)
        update opp;

        QuoteLineItem quoteItem = new QuoteLineItem();
        quoteItem.Name__c = 'テスト商品';
        quoteItem.QuoteId = q.id;
        quoteItem.Quantity = 1;
        quoteItem.Cost_Subtotal__c = 0;
        quoteItem.Cost__c = 200;
        quoteItem.Item_Order__c  = 3;
        quoteItem.UnitPrice = 10;
        quoteItem.Product2Id = prd1.id;
        quoteItem.PricebookEntryId = entry.Id;
        insert quoteItem;

        Asset assetA1 = new Asset(Asset_Owner__c = 'Olympus');
        assetA1.RecordTypeId = System.Label.Asset_RecordType;
        assetA1.SerialNumber = 'ass01';
        assetA1.Name = 'ass01';
        assetA1.AccountId = dep.Id;
        assetA1.Department_Class__c = strategicDep[0].Id;
        assetA1.Hospital__c = hospital.Id;
        assetA1.Product2Id = prd1.Id;
        assetA1.Quantity = 1;
        assetA1.Main_OneToOne__c = true;
        assetA1.Status = '有库存';
        assetA1.Manage_type__c = '个体管理';
        assetA1.Loaner_accsessary__c = false;
        assetA1.Out_of_wh__c = 0;
        assetA1.Delete_Flag__c = False;
        assetA1.Consumable_Guaranteen_end__c = null;
        assetA1.Salesdepartment__c = '1.华北营业本部';
        assetA1.Internal_asset_location__c = '北京 备品中心';
        assetA1.Product_category__c = 'GI';
        assetA1.Equipment_Type__c = '产品试用';
        assetA1.SalesProvince__c = '北京';
        assetA1.CompanyOfEquipment__c = '北京';
        assetA1.Internal_Asset_number__c = '0001';
        assetA1.WH_location__c = '货架号1';
        // assetA1.Fixture_Model_No_F__c = 'CF-H170I';
        insert assetA1;

        Fixture_Set__c fsObjA1 = new Fixture_Set__c();
        fsObjA1.Name = 'set1';
        fsObjA1.Fixture_Set_Body_Model_No__c = 'modelNo1';
        fsObjA1.Loaner_name__c = 'name1';
        insert fsObjA1;

        // 备品配套明细
        Fixture_Set_Detail__c fsdObjA1 = new Fixture_Set_Detail__c();
        fsdObjA1.Name = '备品配套明细名1';
        fsdObjA1.Name_CHN_Created__c = '中文名称1';
        fsdObjA1.Product2__c = prd1.Id;
        fsdObjA1.Fixture_Set__c = fsObjA1.Id;
        fsdObjA1.Is_Body__c = true;
        fsdObjA1.Is_Optional__c = false;
        fsdObjA1.UniqueKey__c = fsObjA1.Id + ':' + prd1.Id;
        fsdObjA1.SortInt__c = 1;
        // fsdObjA1.Fixture_Model_No_F__c = 'CF-H170I';
        insert fsdObjA1;
        //OPD计划
        OPDPlan__c oPDPlan = new OPDPlan__c();
        oPDPlan.Status__c = '计划中';
        oPDPlan.RentalReson__c = 'OPD';
        oPDPlan.OPDPlan_ImplementDate__c =  Date.today().addMonths(1);
        oPDPlan.NoOpp_Reason__c = '';     //无询价理由
        oPDPlan.OPDLendSortDraft__c = 1;       //备品借出优先度
        oPDPlan.OPDType__c = '科室';
        oPDPlan.Related_Opportunity1_ID__c = opp.Id;
        oPDPlan.Account_Laboratory__c = dep.Id;
        // oPDPlan.Department_Class__c  = dep.Id;
        insert oPDPlan;


        Plan_Rental_Equipment__c planRE = new Plan_Rental_Equipment__c();
        planRE.OPD_Plan__c = oPDPlan.Id;
        planRE.Rental_Equipment__c = 'CF-H170I';
        planRE.Name = 'CF-H170I';
        planRE.Rental_Quantity__c = '10';
        planRE.ProductCode__c = 'N4480080';
        planRE.MDM_Model_No__c = 'CF-H170I';
        insert planRE;
        Plan_Rental_Equipment__c planRE1 = new Plan_Rental_Equipment__c();
        planRE1.OPD_Plan__c = oPDPlan.Id;
        planRE1.Rental_Equipment__c = '1001';
        planRE1.Name = '1001';
        planRE1.Rental_Quantity__c = '10';
        planRE1.ProductCode__c = '1001';
        planRE1.MDM_Model_No__c = '1001';
        insert planRE1;

        Rental_Apply__c raObj = new Rental_Apply__c();
        raObj.Name = 'testra';
        raObj.Product_category__c = 'GI';
        raObj.Demo_purpose1__c = '产品试用';
        raObj.demo_purpose2__c = FixtureUtil.raDemo_purpose2MAP.get('shiyongwuxunjia');
        raObj.direct_send__c = '医疗机构';
        // raObj.Medical_Institution_Address__c = '医疗机构';
        // raObj.Medical_Institution_Address_sup__c = '医疗机构';
        raObj.Loaner_received_staff__c = '王五';
        raObj.Loaner_received_staff_phone__c = '110';
        raObj.direct_shippment_address__c = '北京市';
        raObj.Hospital__c = hospital.Id;
        raObj.Strategic_dept__c = strategicDep[0].Id;
        raObj.Account__c = dep.Id;
        raObj.Request_shipping_day__c = Date.toDay();  // 希望到货日
        raObj.Hope_Lonaer_date_Num__c = 1;  // 希望借用天数
        raObj.applyUser__c = Userinfo.getUserId();
        raObj.Person_In_Charge__c = Userinfo.getUserId();
        raObj.Phone_number__c = '1234567890';
        raObj.Loaner_medical_Staff__c = contact2.Id;
        raObj.Add_Approval_Status__c = '';
        insert raObj;
     
        // 借出备品配套一览
        Rental_Apply__c raObjSelect = [SELECT Id,RA_Status__c,RA_Status_Text__c FROM Rental_Apply__c WHERE Id =: raObj.Id];
        // System.debug('申请状态------' + raObjSelect.RA_Status__c);
        Rental_Apply_Equipment_Set__c raesObj = new Rental_Apply_Equipment_Set__c();
        raesObj.Rental_Apply__c = raObj.Id;
        raesObj.Fixture_Set__c = fsObjA1.Id;
        raesObj.IndexFromUniqueKey__c = 1;
        raesObj.UniqueKey__c = '1:'+ fsObjA1.Id + ':1';
        raesObj.Rental_End_Date__c = Date.today().addDays(15);
        raesObj.Rental_Start_Date__c = Date.today().addDays(5);
        raesObj.Loaner_code_text__c = '1001';
        insert raesObj;


        Rental_Apply_Equipment_Set_Detail__c raesdObj1 = new Rental_Apply_Equipment_Set_Detail__c();
        raesdObj1.Rental_Apply__c = raObj.Id;
        raesdObj1.Fixture_Set_Detail__c = fsdObjA1.Id;
        raesdObj1.Rental_Num__c = null;
        raesdObj1.Queue_Number__c = null;
        raesdObj1.Is_Body__c = true;
        raesdObj1.Rental_Apply_Equipment_Set__c = raesObj.Id;
        raesdObj1.Asset__c = assetA1.Id;
        raesdObj1.IndexFromUniqueKey__c = 1;
        raesdObj1.UniqueKey__c = '1:'+ raesObj.Id + ':' + fsdObjA1.Id + ':1';
        raesdObj1.Salesdepartment_before__c = '1.华北营业本部';
        raesdObj1.Internal_asset_location_before__c = '北京 备品中心';
        raesdObj1.Product_category_text__c = 'GI';
        raesdObj1.Equipment_Type_text__c = '产品试用';
        raesdObj1.Fixture_Model_No_text__c = 'CF-H170I';
        raesdObj1.ExternalKey__c = '123';
        insert raesdObj1; 

        raesObj.First_RAESD__c = raesdObj1.Id;
        update raesObj;
        LexOPDSupplementaryController.init2(oPDPlan.Id);
        oPDPlan.RentalReson__c = '模型出借';
        update oPDPlan;
        LexOPDSupplementaryController.init2(oPDPlan.Id);
        // LexOPDSupplementaryController.UserInfo_Owner();
        // LexOPDSupplementaryController.UpdateSign(oPDPlan.Id);
        Test.stopTest();
    }   



    @isTest
    static void testMethod5(){
        FixtureUtil.SkipTrigger = true;
        Oly_TriggerHandler.bypass('ContactTrigger');
        Oly_TriggerHandler.bypass('ContactTriggerHandler');
        Oly_TriggerHandler.bypass('AssetTrigger');
        Oly_TriggerHandler.bypass('MaintenanceContractTrigger');
        Oly_TriggerHandler.bypass('MaintenanceContractAfterUpdate');
        Oly_TriggerHandler.bypass('MaintenanceContractBeforeDelete');
        Oly_TriggerHandler.bypass('MaintenanceContractHpDeptUpd');
        Oly_TriggerHandler.bypass('NFM106Controller');
        Oly_TriggerHandler.bypass('NFM106Trigger');
        Oly_TriggerHandler.bypass('AssetRecordTypeUpd');
        Oly_TriggerHandler.bypass('NFM101Controller');
        Oly_TriggerHandler.bypass('NFM001Controller');
        Oly_TriggerHandler.bypass('SyncProduct2');
        Oly_TriggerHandler.bypass('RentalApplyBeforeUpdate');
        Oly_TriggerHandler.bypass('ContactTriggerHandler');
        Oly_TriggerHandler.bypass('RentalApplyTriggerHandler');


        ControllerUtil.EscapeNFM001Trigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
        StaticParameter.EscapeOppandStaTrigger = true;
        CampaignCopyToOPDPlanHandler.HandlerFlag = true;

        Address_Level__c al = new Address_Level__c();
        al.Name = '東京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;
        // 市
        Address_Level2__c al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-99';
        al2.Level1_Sys_No__c = '999999';
        al2.Level1_Name__c = '東京';
        al2.Name = '渋谷区';
        al2.Level2_Code__c = 'CN-9999';
        al2.Level2_Sys_No__c = '9999999';
        al2.Address_Level__c = al.id;
        insert al2;

        // 病院を作る
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital.Name = 'test hospital';
        hospital.Is_Active__c = '有効';
        hospital.Attribute_Type__c = '卫生部';
        hospital.Speciality_Type__c = '综合医院';
        hospital.Grade__c = '一级';
        hospital.OCM_Category__c = 'SLTV';
        hospital.Is_Medical__c = '医疗机构';
        hospital.State_Master__c = al.id;
        hospital.City_Master__c = al2.id;
        hospital.Town__c = '东京';
        insert hospital;
        Account[] strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_OTH'];
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
        dep.Name = 'test dep';
        dep.AgentCode_Ext__c = '9999998';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;
        Opportunity opp = new Opportunity(
            Name='Opp',
            StageName='引合',
            Department_Class__c = strategicDep[0].Id,
            CloseDate=Date.today().addDays(10),
            Close_Forecasted_Date__c=Date.today(),
            CurrencyIsoCode = 'CNY'
        );
        insert opp;
        Opportunity opp1 = [SELECT Id FROM Opportunity LIMIT 1];

        Product2 pro1 = new Product2(Name='n01',IsActive=true,Family='GI',
                Fixture_Model_No__c='n01',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'n01',
                ProductCode_Ext__c='pc01',Manual_Entry__c=false);
        insert pro1;

        // 备品配套1(主体 两个附属品(个体管理 数量管理))
        Fixture_Set__c fsObjA1 = new Fixture_Set__c();
        fsObjA1.Name = 'set1';
        fsObjA1.Fixture_Set_Body_Model_No__c = 'n01';
        fsObjA1.Loaner_code__c = 'n01';
        fsObjA1.Loaner_name__c = 'name1';
        insert fsObjA1;

        OPDPlan__c opd3 = new OPDPlan__c();
        opd3.Status__c = '草案中';
        opd3.OPDPlan_ImplementDate__c = Date.today().addDays(10);
        opd3.PlanProdDetail__c = 'n01*1;';
        opd3.OPDType__c = '询价';
        opd3.Related_Opportunity1_ID__c =opp1.id;
        opd3.Account_Laboratory__c = dep.Id;
        insert opd3;
        

        List<String> fixtureModels = new List<String>();
        if(opd3.PlanProdDetail__c != null){
            List<String> fixSetStrings = opd3.PlanProdDetail__c.split(';');
            if(fixSetStrings.size() > 0){
                for(String fixSet : fixSetStrings){
                    String tempString = fixSet.trim();
                    if(tempString.indexOf('*')!= -1){
                        fixtureModels.add(tempString.substring(0, tempString.indexOf('*')));
                    }
                }
            }
        }
        List<Product2> firstProduct = [SELECT Id, Family FROM Product2 WHERE Fixture_Model_No_T__c = :fixtureModels];
        System.assertEquals(fixtureModels[0], 'n01');
        System.assertEquals(firstProduct.size(), 1);

        LexOPDSupplementaryController.newRentalApply(opd3.Id); 
    }

    @IsTest
    static void testMethod6(){
        System.Test.startTest();
        setupTestData();
        System.Test.stopTest();
        OPDPlan__c opd = [SELECT Id FROM OPDPlan__c LIMIT 1];
        LexOPDSupplementaryController.initReportSubmit('123');
        LexOPDSupplementaryController.initReportSubmit(opd.Id);
        LexOPDSupplementaryController.cloneOPD(opd.Id);
        LexOPDSupplementaryController.cloneOPD('123');
        LexOPDSupplementaryController.getRecordTypeByNameAndSobject('办事处','Account');
        LexOPDSupplementaryController.getRecordTypeByNameAndSobject('123','12323');
        LexOPDSupplementaryController.UserInfo_Owner();
        
    }

    @IsTest
    static void testMethod7(){
        System.Test.startTest();
        setupTestData();
        System.Test.stopTest();
        OPDPlan__c opd = [SELECT Id FROM OPDPlan__c LIMIT 1];
        LexOPDSupplementaryController.NewAndUpdateOPDStatus('12323232',True);
        LexOPDSupplementaryController.init3('12323232');
        LexOPDSupplementaryController.init3(opd.Id);
       
    }

    @IsTest
    static void testMethod8(){
        System.Test.startTest();
        setupTestData();
        System.Test.stopTest();
        OPDPlan__c opd = [SELECT Id FROM OPDPlan__c LIMIT 1];
        LexOPDSupplementaryController.reapplyFindOPD('12323232');
        LexOPDSupplementaryController.reapplyFindOPD(opd.Id);
        LexOPDPostPoneController.findRecordId();
        
    }

    @IsTest
    static void testMethod999(){
        LexOPDSupplementaryController.sendTemperatureEmail('opd1.Name', '测试失败邮件', 'opd1.Id');
    }

    @IsTest
    static void testMethod9(){
        // setupTestData();
        // Campaign cam = [SELECT Id FROM Campaign limit 1];
        // OPDPlan__c opd1 = new OPDPlan__c();
        // opd1.Status__c = '草案中';
        // opd1.OPDPlan_ImplementDate__c = Date.today().addDays(10);
        // opd1.NoOpp_Reason__c = 'HCP对应';
        // opd1.OPDType__c = '学会';
        // opd1.PlanProdDetail__c = 'n02*1';
        // opd1.RentalReson__c = '学会展会';
        // opd1.Campaign__c = cam.Id;
        // insert opd1;
        // LexOPDSupplementaryController.newRentalApply(opd1.Id); 
        // LexOPDSupplementaryController.sendTemperatureEmail(opd1.Name, '测试失败邮件', opd1.Id);
    }

    //补充申请OPD
    @IsTest
    static void testMethod10(){
        System.Test.startTest();
        Oly_TriggerHandler.bypass('RentalApplyBeforeUpdate');
        Oly_TriggerHandler.bypass('RentalApplyTriggerHandler');
        setupTestData();
        System.Test.stopTest();
        User u = [SELECT Id FROM User Limit 1];
        List<OPDPlan__c> opdList = [SELECT Id, RentalReson__c, OriginalOpdPlanApplication__c, OriginalOpdPlan__c FROM OPDPlan__c WHERE OriginalOpdPlanApplication__c !=null];

        System.assertEquals(opdList[0].RentalReson__c, '追加配套' );
        System.assertEquals(String.isBlank(opdList[0].OriginalOpdPlanApplication__c),false);
        System.assertEquals(String.isBlank(opdList[0].OriginalOpdPlan__c),true);
        LexOPDSupplementaryController.newRentalApply(opdList[0].Id); 
        LexOPDSupplementaryController los = new LexOPDSupplementaryController();
        
    }

    //无询价OPD
    // @IsTest
    // static void testMethod11(){
    //     ControllerUtil.EscapeNFM001Trigger = true;
    //     FixtureUtil.SkipTrigger = true;
    //     Oly_TriggerHandler.bypass('ContactTrigger');
    //     Oly_TriggerHandler.bypass('ContactTriggerHandler');
    //     Oly_TriggerHandler.bypass('AssetTrigger');
    //     Oly_TriggerHandler.bypass('MaintenanceContractTrigger');
    //     Oly_TriggerHandler.bypass('MaintenanceContractAfterUpdate');
    //     Oly_TriggerHandler.bypass('MaintenanceContractBeforeDelete');
    //     Oly_TriggerHandler.bypass('MaintenanceContractHpDeptUpd');
    //     Oly_TriggerHandler.bypass('NFM106Controller');
    //     Oly_TriggerHandler.bypass('NFM106Trigger');
    //     Oly_TriggerHandler.bypass('AssetRecordTypeUpd');
    //     Oly_TriggerHandler.bypass('NFM101Controller');
    //     Oly_TriggerHandler.bypass('NFM001Controller');
    //     Oly_TriggerHandler.bypass('SyncProduct2');
    //     Oly_TriggerHandler.bypass('RentalApplyBeforeUpdate');
    //     Oly_TriggerHandler.bypass('ContactTriggerHandler');
    //     Oly_TriggerHandler.bypass('OPDPlanUp');
    //     Oly_TriggerHandler.bypass('RentalApplyTriggerHandler');
    //     Oly_TriggerHandler.bypass('RentalApplyEquipmentSetDetailHandler');

    //     StaticParameter.ContentDocumentLink = false; 
    //     StaticParameter.OPDPlanUp = true;
        
    //     setupTestData();

    //     List<OPDPlan__c> opdList = [SELECT Id FROM OPDPlan__c WHERE NoOpp_Reason__c = 'HCP对应' AND OPDType__c != '学会'];
    //             LexOPDSupplementaryController.callUpdateAssetToCurrentMC(opdList[0].Id);
    //     LexOPDSupplementaryController.newRentalApply(opdList[0].Id);
    // }

    @IsTest
    static void testMethod12(){
        // setupTestData();
        // List<OPDPlan__c> opdList = [SELECT Id, RentalReson__c, OriginalOpdPlanApplication__c, OriginalOpdPlan__c FROM OPDPlan__c WHERE OriginalOpdPlan__c !=null];
        // System.assertEquals(opdList.size(), 1);
        // System.assertEquals(String.isBlank(opdList[0].OriginalOpdPlanApplication__c),true);
        // System.assertEquals(String.isBlank(opdList[0].OriginalOpdPlan__c),false);
        // LexOPDSupplementaryController.newRentalApply(opdList[0].Id); 
    }

}