@isTest
private class RentalFixtureSetAssignAgencyControTest {
    private static User u;
    @testSetup
    static void setupTestData() {
         System.runAs(new User(Id = Userinfo.getUserId())) {
            StaticParameter.EscapeNFM001AgencyContractTrigger = true;
            StaticParameter.EscapeNFM001Trigger = true;
            Oly_TriggerHandler.bypass(ContactTriggerHandler.class.getName());
            Oly_TriggerHandler.bypass(AgencyHospitalHandler.class.getName());
            Oly_TriggerHandler.bypass(RentalApplyTriggerHandler.class.getName());

            // システム管理者
            u = new User(Test_staff__c = true);
            u.LastName = '_サンブリッジ';
            u.FirstName = 'う';
            u.Alias = 'う';
            u.Email = 'olympusTest03@sunbridge.com';
            u.Username = 'olympusTest03@sunbridge.com';
            u.CommunityNickname = 'う';
            u.IsActive = true;
            u.EmailEncodingKey = 'ISO-2022-JP';
            u.TimeZoneSidKey = 'Asia/Tokyo';
            u.LocaleSidKey = 'ja_JP';
            u.LanguageLocaleKey = 'ja';
            u.ProfileId = System.Label.ProfileId_SystemAdmin;
            u.Province__c = '北京';
            u.Dept__c = '医疗华北营业本部';
            u.Use_Start_Date__c = Date.today().addMonths(-6);
            insert u;
        }
         // 省
        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;

        // 产品
        Product2 pro1 = new Product2(Name='name01',IsActive=true,Family='GI',
                Fixture_Model_No__c='modlen01',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'n01', Asset_Model_No__c = 'Pro1',
                ProductCode_Ext__c='pc01',Manual_Entry__c=false);
        Product2 pro2 = new Product2(Name='name02',IsActive=true,Family='GI',
                Fixture_Model_No__c='n02',Serial_Lot_No__c='Lot tracing',
                Fixture_Model_No_T__c = 'n02', Asset_Model_No__c = 'Pro2',
                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',
                Fixture_Model_No_T__c = 'n03', Asset_Model_No__c = 'Pro3',
                ProductCode_Ext__c='pc03',Manual_Entry__c=false);
        insert new Product2[] {pro1, pro2, pro3};

        //20210524 ljh add 1829 start
        // 产品
        Product2 pro10 = new Product2(Name='name010',IsActive=true,Family='GI',
                Fixture_Model_No__c='n010',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'n010', Asset_Model_No__c = 'Pro10',
                ProductCode_Ext__c='pc010',Manual_Entry__c=false);
        Product2 pro20 = new Product2(Name='name020',IsActive=true,Family='GI',
                Fixture_Model_No__c='n020',Serial_Lot_No__c='Lot tracing',
                Fixture_Model_No_T__c = 'n020', Asset_Model_No__c = 'Pro20',
                ProductCode_Ext__c='pc020',Manual_Entry__c=false);
        Product2 pro30 = new Product2(Name='name030',IsActive=true,Family='GI',
                Fixture_Model_No__c='n030',Serial_Lot_No__c='Lot tracing',
                Fixture_Model_No_T__c = 'n030', Asset_Model_No__c = 'Pro30',
                ProductCode_Ext__c='pc030',Manual_Entry__c=false);
        insert new Product2[] {pro10, pro20, pro30};
        //20210524 ljh add 1829 end


        // 保有设备
        Asset asset1 = new Asset(Asset_Owner__c = 'Olympus');
        asset1.RecordTypeId = System.Label.Asset_RecordType;
        asset1.SerialNumber = 'asset1';
        asset1.Name = 'asset1';
        asset1.AccountId = dep.Id;
        asset1.Department_Class__c = strategicDep[0].Id;
        asset1.Hospital__c = hospital.Id;
        asset1.Product2Id = pro1.Id;
        asset1.Quantity = 1;
        asset1.Status = '不明';
        asset1.Manage_type__c = '个体管理';
        asset1.Loaner_accsessary__c = false;
        asset1.Out_of_wh__c = 0;
        asset1.Salesdepartment__c = '1.华北营业本部';
        asset1.Internal_asset_location__c = '北京 备品中心';
        asset1.Product_category__c = 'GI';
        asset1.Equipment_Type__c = '产品试用';
        asset1.SalesProvince__c = '北京';
        asset1.CompanyOfEquipment__c = '北京';
        asset1.Internal_Asset_number__c = '0001';
        asset1.Main_OneToOne__c = true;


        Asset asset2 = new Asset(Asset_Owner__c = 'Olympus');
        asset2.RecordTypeId = System.Label.Asset_RecordType;
        asset2.SerialNumber = 'asset2';
        asset2.Name = 'asset2';
        asset2.AccountId = dep.Id;
        asset2.Department_Class__c = strategicDep[0].Id;
        asset2.Hospital__c = hospital.Id;
        asset2.Product2Id = pro2.Id;
        asset2.Quantity = 1;
        asset2.Status = '不明';
        asset2.Manage_type__c = '个体管理';
        asset2.Loaner_accsessary__c = true;
        asset2.Out_of_wh__c = 0;
        asset2.Salesdepartment__c = '1.华北营业本部';
        asset2.Internal_asset_location__c = '北京 备品中心';
        asset2.Product_category__c = 'GI';
        asset2.Equipment_Type__c = '产品试用';
        asset2.SalesProvince__c = '北京';
        asset2.CompanyOfEquipment__c = '北京';
        asset2.Internal_Asset_number__c = '0002';

        Asset asset3 = new Asset(Asset_Owner__c = 'Olympus');
        asset3.RecordTypeId = System.Label.Asset_RecordType;
        asset3.SerialNumber = 'asset3';
        asset3.Name = 'asset3';
        asset3.AccountId = dep.Id;
        asset3.Department_Class__c = strategicDep[0].Id;
        asset3.Hospital__c = hospital.Id;
        asset3.Product2Id = pro3.Id;
        asset3.Quantity = 50;
        asset3.Status = '不明';
        asset3.Manage_type__c = '数量管理';
        asset3.Loaner_accsessary__c = true;
        asset3.Out_of_wh__c = 0;
        asset3.Salesdepartment__c = '1.华北营业本部';
        asset3.Internal_asset_location__c = '北京 备品中心';
        asset3.Product_category__c = 'GI';
        asset3.Equipment_Type__c = '产品试用';
        asset3.SalesProvince__c = '北京';
        asset3.CompanyOfEquipment__c = '北京';
        asset3.Internal_Asset_number__c = '0003';
        insert new Asset[] {asset1, asset2, asset3};

        // 备品一对一Link表
        Fixture_OneToOne_Link__c foLink1 = new Fixture_OneToOne_Link__c();
        Fixture_OneToOne_Link__c foLink2 = new Fixture_OneToOne_Link__c();
        foLink1.Main_Asset__c = asset1.Id;
        foLink1.Accessory_Asset__c = asset2.Id;
        foLink1.Quantity__c = 1;

        foLink2.Main_Asset__c = asset1.Id;
        foLink2.Accessory_Asset__c = asset3.Id;
        foLink2.Quantity__c = 1;
        insert new Fixture_OneToOne_Link__c[] {foLink1, foLink2};

         // 备品配套
        Fixture_Set__c fsObj1 = new Fixture_Set__c();
        //fsObj1.Name = 'set1';
        fsObj1.Name = 'n01';
        fsObj1.Fixture_Set_Body_Model_No__c = 'modlen01';
        fsObj1.Loaner_name__c = 'name1';
        insert fsObj1;

        // 备品配套明细
        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 = fsObj1.Id;
        fsdObjA1.Is_Body__c = true;
        fsdObjA1.Is_Optional__c = false;
        fsdObjA1.UniqueKey__c = fsObj1.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 = fsObj1.Id;
        fsdObjA2.Is_Body__c = false;
        fsdObjA2.Is_Optional__c = true;
        fsdObjA2.UniqueKey__c = fsObj1.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 = fsObj1.Id;
        fsdObjA3.Is_Body__c = false;
        fsdObjA3.Is_Optional__c = true;
        fsdObjA3.UniqueKey__c = fsObj1.Id + ':' + pro3.Id;
        fsdObjA3.SortInt__c = 3;
        insert new Fixture_Set_Detail__c[] {fsdObjA1,fsdObjA2,fsdObjA3};
        //20210524 ljh add 1829 start
        // 备品配套
        Fixture_Set__c fsObj10 = new Fixture_Set__c();
        fsObj10.Name = 'n010';
        fsObj10.Fixture_Set_Body_Model_No__c = 'ModleNo10';
        fsObj10.Loaner_name__c = 'name10';
        insert fsObj10;

        // 备品配套明细
        Fixture_Set_Detail__c fsdObjA10 = new Fixture_Set_Detail__c();
        fsdObjA10.Name = '备品配套明细名10';
        fsdObjA10.Name_CHN_Created__c = '中文名称10';
        fsdObjA10.Product2__c = pro10.Id;
        fsdObjA10.Fixture_Set__c = fsObj10.Id;
        fsdObjA10.Is_Body__c = true;
        fsdObjA10.Is_Optional__c = false;
        fsdObjA10.UniqueKey__c = fsObj10.Id + ':' + pro10.Id;
        fsdObjA10.SortInt__c = 1;
        fsdObjA10.Quantity__c = 1;

        Fixture_Set_Detail__c fsdObjA20 = new Fixture_Set_Detail__c();
        fsdObjA20.Name = '备品配套明细名20';
        fsdObjA20.Name_CHN_Created__c = '中文名称20';
        fsdObjA20.Product2__c = pro20.Id;
        fsdObjA20.Fixture_Set__c = fsObj10.Id;
        fsdObjA20.Is_Body__c = false;
        fsdObjA20.Is_Optional__c = true;
        fsdObjA20.UniqueKey__c = fsObj10.Id + ':' + pro20.Id;
        fsdObjA20.SortInt__c = 2;
        fsdObjA20.Quantity__c = 1;

        Fixture_Set_Detail__c fsdObjA30 = new Fixture_Set_Detail__c();
        fsdObjA30.Name = '备品配套明细名30';
        fsdObjA30.Name_CHN_Created__c = '中文名称30';
        fsdObjA30.Product2__c = pro30.Id;
        fsdObjA30.Fixture_Set__c = fsObj10.Id;
        fsdObjA30.Is_Body__c = false;
        fsdObjA30.Is_Optional__c = true;
        fsdObjA30.UniqueKey__c = fsObj10.Id + ':' + pro30.Id;
        fsdObjA30.SortInt__c = 3;
        fsdObjA30.Quantity__c = 1;
        insert new Fixture_Set_Detail__c[] {fsdObjA10,fsdObjA20,fsdObjA30};
        //20210524 ljh add 1829 end

        //备品借出申请
        System.runAs(u) {
            Rental_Apply__c raObj = new Rental_Apply__c();
            raObj.Name = 'testra';
            raObj.Product_category__c = 'GI';
            raObj.Demo_purpose1__c = '其他';
            raObj.demo_purpose2__c = '其他';

            raObj.direct_send__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.Request_return_day__c = Date.toDay();
            raObj.Status__c = '草案中';
            raObj.SalesdeptSelect__c = '医疗华北营业本部';
            raObj.ToAgency__c = '北京';
            raObj.Hope_Lonaer_date_Num__c = 1;
            Map<String,Schema.RecordTypeInfo>  DEVELOPERNAMEMAP  = Schema.SObjectType.Rental_Apply__c.getRecordTypeInfosByDeveloperName();
            raObj.RecordTypeId = DEVELOPERNAMEMAP.get('AgencyRequest').getRecordTypeId();
            insert raObj;
            // 借出备品配套一览
            Rental_Apply_Equipment_Set__c raesObj = new Rental_Apply_Equipment_Set__c();
            raesObj.Rental_Apply__c = raObj.Id;
            raesObj.Fixture_Set__c = fsObj1.Id;
            raesObj.Cancel_Select__c = false;
            raesObj.Rental_Start_Date__c = Date.toDay()-7;
            raesObj.Rental_End_Date__c = Date.toDay();
            raesObj.IndexFromUniqueKey__c = 1;
            raesObj.UniqueKey__c = '1:'+ fsObj1.Id + ':1';
            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 = 1;
            raesdObj1.Queue_Number__c = null;
            raesdObj1.Is_Body__c = true;
            raesdObj1.Rental_Apply_Equipment_Set__c = raesObj.Id;
            raesdObj1.IndexFromUniqueKey__c = 1;
            raesdObj1.UniqueKey__c = '1:'+ raesObj.Id + ':' + fsdObjA1.Id + ':1';
            raesdObj1.FSD_OneToOneAccessory_Cnt__c = 0;
            raesdObj1.FSD_Is_Optional__c = false;
            raesdObj1.FSD_Is_OneToOne__c = false;
            raesdObj1.ApplyPersonAppended__c = false;
            raesdObj1.FSD_Fixture_Model_No__c = 'n01';
            raesdObj1.Fixture_Model_No_text__c = 'n01';
            raesdObj1.Salesdepartment_before__c = '1.华北营业本部';
            raesdObj1.Internal_asset_location_before__c = '北京 备品中心';
            raesdObj1.Product_category_text__c = 'GI';
            raesdObj1.Equipment_Type_text__c = '产品试用';
            raesdObj1.Cancel_Select__c=false;
          

            Rental_Apply_Equipment_Set_Detail__c raesdObj2 = new Rental_Apply_Equipment_Set_Detail__c();
            raesdObj2.Rental_Apply__c = raObj.Id;
            raesdObj2.Fixture_Set_Detail__c = fsdObjA2.Id;
            raesdObj2.Rental_Num__c = 1;
            raesdObj2.Queue_Number__c = null;
            raesdObj2.Is_Body__c = false;
            raesdObj2.Rental_Apply_Equipment_Set__c = raesObj.Id;
            raesdObj2.IndexFromUniqueKey__c = 2;
            raesdObj2.UniqueKey__c = '1:'+ raesObj.Id + ':' + fsdObjA2.Id + ':2';
            raesdObj2.FSD_OneToOneAccessory_Cnt__c = 0;
            raesdObj2.FSD_Is_Optional__c = false;
            raesdObj2.FSD_Is_OneToOne__c = false;
            raesdObj2.ApplyPersonAppended__c = false;
            raesdObj2.FSD_Fixture_Model_No__c = 'n02';
            raesdObj2.Fixture_Model_No_text__c = 'n02';
            raesdObj2.Salesdepartment_before__c = '1.华北营业本部';
            raesdObj2.Internal_asset_location_before__c = '北京 备品中心';
            raesdObj2.Product_category_text__c = 'GI';
            raesdObj2.Equipment_Type_text__c = '产品试用';
            raesdObj2.Cancel_Select__c=false;
          

            Rental_Apply_Equipment_Set_Detail__c raesdObj3 = new Rental_Apply_Equipment_Set_Detail__c();
            raesdObj3.Rental_Apply__c = raObj.Id;        //raesdObj3.Asset__c = asset3.Id;
            raesdObj3.Fixture_Set_Detail__c = fsdObjA3.Id;
            raesdObj3.Rental_Num__c = 1;
            raesdObj3.Queue_Number__c = null;
            raesdObj3.Is_Body__c = false;
            raesdObj3.Rental_Apply_Equipment_Set__c = raesObj.Id;
            raesdObj3.IndexFromUniqueKey__c = 3;
            raesdObj3.UniqueKey__c = '1:'+ raesObj.Id + ':' + fsdObjA3.Id + ':3';
            raesdObj3.FSD_OneToOneAccessory_Cnt__c = 2;
            raesdObj3.FSD_Is_Optional__c = false;
            raesdObj3.FSD_Is_OneToOne__c = false;
            raesdObj3.ApplyPersonAppended__c = false;
            raesdObj3.FSD_Fixture_Model_No__c = 'n03';
            raesdObj3.Fixture_Model_No_text__c = 'n03';
            raesdObj3.Salesdepartment_before__c = '1.华北营业本部';
            raesdObj3.Internal_asset_location_before__c = '北京 备品中心';
            raesdObj3.Product_category_text__c = 'GI';
            raesdObj3.Equipment_Type_text__c = '产品试用';
            raesdObj3.Cancel_Select__c=false;

            Rental_Apply_Equipment_Set_Detail__c raesdObj4 = new Rental_Apply_Equipment_Set_Detail__c();
            raesdObj4.Rental_Apply__c = raObj.Id;        //raesdObj3.Asset__c = asset3.Id;
            raesdObj4.Fixture_Set_Detail__c = fsdObjA3.Id;
            raesdObj4.Rental_Num__c = 1;
            raesdObj4.Queue_Number__c = null;
            raesdObj4.Is_Body__c = false;
            raesdObj4.Rental_Apply_Equipment_Set__c = raesObj.Id;
            raesdObj4.IndexFromUniqueKey__c = 4;
            raesdObj4.UniqueKey__c = '1:'+ raesObj.Id + ':' + fsdObjA3.Id + ':4';
            raesdObj4.FSD_OneToOneAccessory_Cnt__c = 2;
            raesdObj4.FSD_Is_Optional__c = false;
            raesdObj4.FSD_Is_OneToOne__c = false;
            raesdObj4.ApplyPersonAppended__c = false;
            raesdObj4.FSD_Fixture_Model_No__c = 'n04';
            raesdObj4.Fixture_Model_No_text__c = 'n04';
            raesdObj4.Salesdepartment_before__c = '1.华北营业本部';
            raesdObj4.Internal_asset_location_before__c = '北京 备品中心';
            raesdObj4.Product_category_text__c = 'GI';
            raesdObj4.Equipment_Type_text__c = '产品试用';
            raesdObj4.Cancel_Select__c=true;
            raesdObj4.Cancel_Reason__c= '主动取消';
            insert new Rental_Apply_Equipment_Set_Detail__c[] {raesdObj1,raesdObj2,raesdObj3,raesdObj4};

            // 申请单变为已批准
            raObj.Status__c = '已批准';
            update raObj;
        }

        FixtureDeliverySlip__c fdsObj = new FixtureDeliverySlip__c();
        fdsObj.Name = '00001';
        fdsObj.DeliveryCompany__c = '利讯';
        fdsObj.Distributor_method__c = '陆运';
        fdsObj.DeliveryType__c = '发货';
        fdsObj.Shippment_loaner_time__c = System.now();
        insert fdsObj;

        Shipment_address__c address = new Shipment_address__c();
        address.Name = '北京市';
        address.Post_Code__c = '100876';
        address.Address__c = '北京';
        insert address;

    }

    @isTest
    public static void test_applyDate_error1() {
        List<Rental_Apply__c> esList = [SELECT Id FROM Rental_Apply__c];
        PageReference ref = new PageReference('/apex/RentalFixtureSetAssignAgency?pt_recid=' + esList[0].Id);
        Test.setCurrentPage(ref);
        RentalFixtureSetAssignAgencyController controller = new RentalFixtureSetAssignAgencyController();
        Test.startTest();
        controller.init();
        controller.esdList[0].checked = true;
        controller.parentObj.Asset_loaner_start_day__c = date.newInstance(1989, 4, 21);
        controller.applyDate();
        controller.searchSlip();
        List<Apexpages.message> msgs = ApexPages.getMessages();
        System.assertEquals('[备品预计出货日]必须入力从今天开始起的日期 ', msgs[0].getDetail());
        Test.stopTest();
        System.debug('init Error');
    }

     //@isTest
    public static void test_assign2() {

        List<Rental_Apply__c> esList = [SELECT Id FROM Rental_Apply__c];

        PageReference ref = new PageReference('/apex/RentalFixtureSetAssignAgency?pt_recid=' + esList[0].Id);
        Test.setCurrentPage(ref);

        RentalFixtureSetAssignAgencyController controller = new RentalFixtureSetAssignAgencyController();
        Test.startTest();

        controller.init();
        controller.esdList[0].checked = true;
        controller.parentObj.Asset_loaner_start_day__c = Date.today();
        controller.applyDate();
        controller.stepName = '分配';
        controller.qrcode = '511100011';
        controller.scanAndMatch();
        controller.assign();
        Test.stopTest();

        System.assertEquals(true, controller.done_flg);
        System.debug('init Success');

    }

    @isTest
    public static void test_cancelSet() {

        List<Rental_Apply__c> esList = [SELECT Id FROM Rental_Apply__c];


        PageReference ref = new PageReference('/apex/RentalFixtureSetAssignAgency?pt_recid=' + esList[0].Id);
        Test.setCurrentPage(ref);

        RentalFixtureSetAssignAgencyController controller = new RentalFixtureSetAssignAgencyController();
        Test.startTest();

        controller.init();
        // 20210916 ljh update start
        // controller.cancelReason = '主动取消';
        controller.parentObj.Cancel_Reason__c = '主动取消';
        controller.parentObj.Loaner_cancel_reason__c = '重复申请';
        // controller.cancelReasonRemark = 'aaaa';
        // 20210916 ljh update start
        controller.cancelSet();
        Test.stopTest();

        System.assertEquals(false, controller.done_flg);
        System.debug('init Success');

    }

    @isTest
    public static void test_cancelSet3() {

        List<Rental_Apply__c> esList = [SELECT Id FROM Rental_Apply__c];

        PageReference ref = new PageReference('/apex/RentalFixtureSetAssignAgency?pt_recid=' + esList[0].Id);
        Test.setCurrentPage(ref);

        RentalFixtureSetAssignAgencyController controller = new RentalFixtureSetAssignAgencyController();
        Test.startTest();

        controller.init();
        controller.esdList[0].checked = false;
        // 20210916 ljh update start
        // controller.cancelReason = '主动取消';
        // controller.cancelReasonRemark = 'aaaa';
        controller.parentObj.Cancel_Reason__c = '主动取消';
        controller.parentObj.Loaner_cancel_reason__c = '重复申请';
        // 20210916 ljh update end
        controller.cancelSet();
        Test.stopTest();

        //System.assertEquals(true, controller.done_flg);
        //System.debug('init Success');

    }

    @isTest
    public static void test_cancelSet2() {

        List<Rental_Apply__c> esList = [SELECT Id FROM Rental_Apply__c];

        PageReference ref = new PageReference('/apex/RentalFixtureSetAssignAgency?pt_recid=' + esList[0].Id);
        Test.setCurrentPage(ref);

        RentalFixtureSetAssignAgencyController controller = new RentalFixtureSetAssignAgencyController();
        Test.startTest();

        controller.init();
        controller.esdList[0].checked = true;
        // 20210916 ljh update start
        // controller.cancelReason = '主动取消';
        // controller.cancelReasonRemark = 'aaaa';
        controller.parentObj.Cancel_Reason__c = '主动取消';
        controller.parentObj.Loaner_cancel_reason__c = '重复申请';
        // 20210916 ljh update end
        controller.cancelSet();
        Test.stopTest();

        //System.assertEquals(true, controller.done_flg);
        //System.debug('init Success');

    }

    @isTest
    public static void test_cancelSet_error() {

        List<Rental_Apply__c> esList = [SELECT Id FROM Rental_Apply__c];

        PageReference ref = new PageReference('/apex/RentalFixtureSetAssignAgency?pt_recid=' + esList[0].Id);
        Test.setCurrentPage(ref);

        RentalFixtureSetAssignAgencyController controller = new RentalFixtureSetAssignAgencyController();

        Test.startTest();
        controller.init();
        controller.cancelSet();
        Test.stopTest();

        List<Apexpages.message> msgs = ApexPages.getMessages();
        //System.assertEquals('取消理由和取消理由备注不可为空！', msgs[0].getDetail());
        //System.debug('init Error');

    }

    @isTest
    public static void test_shipment() {

        List<Rental_Apply__c> esList = [SELECT Id FROM Rental_Apply__c];


        PageReference ref = new PageReference('/apex/RentalFixtureSetAssignAgency?pt_recid=' + esList[0].Id);
        Test.setCurrentPage(ref);

        RentalFixtureSetAssignAgencyController controller = new RentalFixtureSetAssignAgencyController();
        Test.startTest();

        controller.init();
        controller.esdList[0].checked = true;
        controller.parentObj.Asset_loaner_start_day__c = Date.today();
        controller.applyDate();
        controller.stepName = '分配';
        controller.qrcode = '51110001';
        controller.scanAndMatch();
        controller.assign();

        // controller.stepName = '下架';
        // controller.esdList[0].checked = true;
        // controller.stockDown();// 20210624 ljh update SFDC-C448GR

        controller.stepName = '发货';
        controller.esdList[0].checked = true;
        controller.slip.Name='123';
        controller.slip.Distributor_method__c = '陆运';
        controller.slip.DeliveryCompany__c = 'Fedex';
        controller.parentObj.Delivery_Agency__c = '北京市';
        controller.shipment();
        Test.stopTest();
        //System.assertEquals(true, controller.done_flg);

        //List<Apexpages.message> msgs = ApexPages.getMessages();
        //System.assertEquals('必须填写【物流公司】', msgs[0].getDetail());
        //System.debug('init Error');

    }

    @isTest
    public static void test_shipment_error2() {

        List<Rental_Apply__c> esList = [SELECT Id FROM Rental_Apply__c];

        PageReference ref = new PageReference('/apex/RentalFixtureSetAssignAgency?pt_recid=' + esList[0].Id);
        Test.setCurrentPage(ref);

        RentalFixtureSetAssignAgencyController controller = new RentalFixtureSetAssignAgencyController();

        Test.startTest();
        controller.init();
        controller.esdList[0].checked = true;
        controller.parentObj.Asset_loaner_start_day__c = Date.today();
        controller.applyDate();
        controller.stepName = '分配';
        controller.qrcode = '51110001';
        controller.scanAndMatch();
        controller.assign();
        // controller.stepName = '下架';
        // controller.esdList[0].checked = true;
        // controller.stockDown();// 20210624 ljh update SFDC-C448GR
        controller.stepName = '发货';
        controller.esdList[0].checked = true;
        controller.slip.Name = 'abcd';
        controller.slip.DeliveryCompany__c = 'Fedex';
        controller.esdList[0].rec.Select_Time__c = null;
        controller.shipment();
        Test.stopTest();

        List<Apexpages.message> msgs = ApexPages.getMessages();
        //System.assertEquals('n01未分配不可发货！', msgs[0].getDetail());
        System.debug('init Success');

    }

    @isTest
    public static void test_searchSlip() {

        List<Rental_Apply__c> esList = [SELECT Id FROM Rental_Apply__c];


        PageReference ref = new PageReference('/apex/RentalFixtureSetAssignAgency?pt_recid=' + esList[0].Id);
        Test.setCurrentPage(ref);

        RentalFixtureSetAssignAgencyController controller = new RentalFixtureSetAssignAgencyController();

        Test.startTest();
        controller.init();
        controller.esdList[0].checked = true;
        controller.parentObj.Asset_loaner_start_day__c = Date.today();
        controller.applyDate();
        controller.stepName = '分配';
        controller.qrcode = '51110001';
        controller.scanAndMatch();
        controller.assign();
        // controller.stepName = '下架';// 20210624 ljh update SFDC-C448GR
        // controller.esdList[0].checked = true;// 20210624 ljh update SFDC-C448GR
        // controller.stockDown();// 20210624 ljh update SFDC-C448GR
        controller.stepName = '发货';
        controller.esdList[0].checked = true;
        controller.slip.Name = 'abcd';
        controller.slip.DeliveryCompany__c = 'Fedex';
        controller.shipment();
        controller.searchSlip();

        System.assertEquals('发货', controller.slip.DeliveryType__c);
        Test.stopTest();
        System.debug('init Success');
    }

     @isTest
    public static void test_assign() {

        changeData();

        List<Rental_Apply__c> esList = [SELECT Id FROM Rental_Apply__c];

        PageReference ref = new PageReference('/apex/RentalFixtureSetAssignAgency?pt_recid=' + esList[0].Id);
        Test.setCurrentPage(ref);

        RentalFixtureSetAssignAgencyController controller = new RentalFixtureSetAssignAgencyController();
        Test.startTest();

        controller.init();
        // 第一次分配
        controller.esdList[0].checked = true;
        controller.parentObj.Asset_loaner_start_day__c = Date.today()+1;//20210513 ljh update
        controller.applyDate();
        controller.stepName = '分配';
        controller.qrcode = '51110001_test';
        controller.scanAndMatch();
        controller.assign();

        controller.qrcode = '51110001';
        controller.scanAndMatch();
        controller.assign();
        // 下架
        // controller.stepName = '下架';
        // controller.esdList[0].checked = true;
        // controller.stockDown();// 20210624 ljh update SFDC-C448GR

        ref = new PageReference('/apex/RentalFixtureSetAssignAgency?pt_recid=' + esList[0].Id);
        Test.setCurrentPage(ref);
        controller = new RentalFixtureSetAssignAgencyController();
        // 下架后重新分配
        controller.init();
        controller.stepName = '分配';
        controller.esdList[0].checked = true;
        controller.qrcode = '511100011';
        controller.scanAndMatch();
        controller.assign();
        Test.stopTest();

        System.assertEquals(false, controller.done_flg);
        System.debug('init Success');
    }

     //20210521 ljh add 1829 start
    @isTest
    public static void test_assignSub() {
        List<Rental_Apply__c> esList = [SELECT Id FROM Rental_Apply__c];

        PageReference ref = new PageReference('/apex/RentalFixtureSetAssignAgency?pt_recid=' + esList[0].Id);
        Test.setCurrentPage(ref);
        List<Rental_Apply_Equipment_Set__c> raesone=[select Id from Rental_Apply_Equipment_Set__c];
        RentalFixtureSetAssignAgencyController controller = new RentalFixtureSetAssignAgencyController();
        Test.startTest();

        controller.init();
        // 第一次分配
        controller.esdList[0].checked = true;
        controller.parentObj.Asset_loaner_start_day__c = Date.today()+1;//20210513 ljh update
        controller.applyDate();
        controller.stepName = '分配';

        controller.qrcode = '511100000;2;'+controller.esdList[1].rec.Rental_Apply_Equipment_Set__c;
        //controller.qrcode = '511100000;2;'+raesone[0].Id;
        controller.scanAndMatch();
        controller.assign();

        // controller.stepName = '下架';
        // controller.esdList[0].checked = true;
        // controller.stockDown();// 20210624 ljh update SFDC-C448GR

        ref = new PageReference('/apex/RentalFixtureSetAssignAgency?pt_recid=' + esList[0].Id);
        controller = new RentalFixtureSetAssignAgencyController();
        // 下架后重新分配
        controller.init();
        controller.stepName = '分配';
        controller.esdList[0].checked = false;
        //controller.qrcode = '511100000;2;'+controller.esdList[0].rec.Rental_Apply_Equipment_Set__c;
        controller.qrcode = '511100000;2;'+raesone[0].Id;
        controller.scanAndMatch();
        controller.assign();

        Test.stopTest();

        //System.assertEquals(true, controller.done_flg);
        System.debug('init Success');
    }

  @isTest
    public static void test_assignSub03() {

        List<Rental_Apply__c> esList = [SELECT Id FROM Rental_Apply__c];

        PageReference ref = new PageReference('/apex/RentalFixtureSetAssignAgency?pt_recid=' + esList[0].Id);
        Test.setCurrentPage(ref);

        RentalFixtureSetAssignAgencyController controller = new RentalFixtureSetAssignAgencyController();
        Test.startTest();

        controller.init();
        // 第一次分配
        controller.esdList[0].checked = true;
        controller.parentObj.Asset_loaner_start_day__c = Date.today()+1;//20210513 ljh update
        controller.applyDate();
        controller.stepName = '分配';

        controller.qrcode = '511100000;2;'+controller.esdList[0].rec.Rental_Apply_Equipment_Set__c;
        controller.scanAndMatch();
        controller.assign();

        // controller.stepName = '下架';
        // controller.esdList[0].checked = true;
        // controller.stockDown();// 20210624 ljh update SFDC-C448GR

        ref = new PageReference('/apex/RentalFixtureSetAssignAgency?pt_recid=' + esList[0].Id);
        Test.setCurrentPage(ref);
        controller = new RentalFixtureSetAssignAgencyController();
        // 下架后重新分配
        controller.init();
        controller.stepName = '分配';
        controller.esdList[0].checked = true;
        controller.qrcode = '51110001;2;'+controller.esdList[0].rec.Rental_Apply_Equipment_Set__c;
        List<Asset> mainAssetList = [
                SELECT Id
                     , Fixture_Model_No_F__c
                     , Internal_asset_location__c
                     , SerialNumber
                     , Salesdepartment__c
                     , SalesProvince__c
                     , Product_category__c
                     , Equipment_Type__c
                     , Internal_Asset_number_key__c
                  FROM Asset
                 WHERE Fixture_QRCode__c ='51110001'
                   //AND Main_OneToOne__c = true //20210508 ljh add 1834
                   AND You_Xiao_Ku_Cun__c > 0
                 LIMIT 1
        ];
        List<Product2> pro2l=[select id from Product2 where  Fixture_Model_No_T__c != 'n01'];
        mainAssetList[0].Product2=pro2l[0];
        update mainAssetList;
        controller.scanAndMatch();
        controller.assign();
        Test.stopTest();
        //System.assertEquals(true, controller.done_flg);
        System.debug('init Success');
    }


     @isTest
    public static void test_assignSub04() {
        List<Rental_Apply__c> esList = [SELECT Id FROM Rental_Apply__c];

        PageReference ref = new PageReference('/apex/RentalFixtureSetAssignAgency?pt_recid=' + esList[0].Id);
        Test.setCurrentPage(ref);
        List<Rental_Apply_Equipment_Set__c> raesone=[select Id from Rental_Apply_Equipment_Set__c];
        RentalFixtureSetAssignAgencyController controller = new RentalFixtureSetAssignAgencyController();
        Test.startTest();

        controller.init();
        // 第一次分配
        controller.esdList[0].checked = true;
        controller.parentObj.Asset_loaner_start_day__c = Date.today()+1;//20210513 ljh update
        controller.applyDate();
        controller.stepName = '分配';

        controller.qrcode = '511100000;2;'+controller.esdList[1].rec.Rental_Apply_Equipment_Set__c;
        //controller.qrcode = '511100000;2;'+raesone[0].Id;
        controller.scanAndMatch();
        controller.assign();

        ref = new PageReference('/apex/RentalFixtureSetAssignAgency?pt_recid=' + esList[0].Id);
        controller = new RentalFixtureSetAssignAgencyController();
        // 下架后重新分配
        controller.init();
        controller.stepName = '分配';
        integer i=controller.esdList.size();
        for(integer j=0;i<i;j++){
            controller.esdList[j].checked = false;
        }
        //controller.qrcode = '511100000;2;'+controller.esdList[0].rec.Rental_Apply_Equipment_Set__c;
        controller.qrcode = '511100000;2;'+raesone[0].Id;
        controller.scanAndMatch();
        controller.assign();

        Test.stopTest();

        //System.assertEquals(true, controller.done_flg);
        System.debug('init Success');
    }

    
   


   

    public static void changeData(){
        List<Asset> asss=[select RecordTypeId,AccountId,Department_Class__c,Hospital__c,Product2Id from Asset];
        Asset oneAsset1=asss[0];
        Asset oneAsset2=asss[1];
        Asset oneAsset3=asss[2];
        List<Product2> pro=[select id from Product2];

        Asset asset11 = new Asset(Asset_Owner__c = 'Olympus');
        asset11.RecordTypeId = oneAsset1.recordtypeid;
        asset11.SerialNumber = 'asset11';
        asset11.Name = 'asset11';
        asset11.AccountId = oneAsset1.Accountid;
        asset11.Department_Class__c = oneAsset1.Department_Class__c;
        asset11.Hospital__c = oneAsset1.hospital__c;
        asset11.Product2Id = oneAsset1.Product2Id;
        asset11.Quantity = 1;
        asset11.Status = '不明';
        asset11.Manage_type__c = '个体管理';
        asset11.Loaner_accsessary__c = false;
        asset11.Out_of_wh__c = 0;
        asset11.Salesdepartment__c = '1.华北营业本部';
        asset11.Internal_asset_location__c = '北京 备品中心';
        asset11.Product_category__c = 'GI';
        asset11.Equipment_Type__c = '产品试用';
        asset11.SalesProvince__c = '北京';
        asset11.CompanyOfEquipment__c = '北京';
        asset11.Internal_Asset_number__c = '00011';
        asset11.Main_OneToOne__c = true;

        Asset asset22 = new Asset(Asset_Owner__c = 'Olympus');
        asset22.RecordTypeId = oneAsset2.recordtypeid;
        asset22.SerialNumber = 'asset22';
        asset22.Name = 'asset22';
        asset22.AccountId = oneAsset2.Accountid;
        asset22.Department_Class__c = oneAsset2.Department_Class__c;
        asset22.Hospital__c = oneAsset2.hospital__c;
        asset22.Product2Id = oneAsset2.Product2Id;
        asset22.Quantity = 1;
        asset22.Status = '不明';
        asset22.Manage_type__c = '个体管理';
        asset22.Loaner_accsessary__c = true;
        asset22.Out_of_wh__c = 0;
        asset22.Salesdepartment__c = '1.华北营业本部';
        asset22.Internal_asset_location__c = '北京 备品中心';
        asset22.Product_category__c = 'GI';
        asset22.Equipment_Type__c = '产品试用';
        asset22.SalesProvince__c = '北京';
        asset22.CompanyOfEquipment__c = '北京';
        asset22.Internal_Asset_number__c = '00022';

        Asset asset33 = new Asset(Asset_Owner__c = 'Olympus');
        asset33.RecordTypeId = oneAsset3.recordtypeid;
        asset33.SerialNumber = 'asset33';
        asset33.Name = 'asset33';
        asset33.AccountId = oneAsset3.Accountid;
        asset33.Department_Class__c = oneAsset3.Department_Class__c;
        asset33.Hospital__c = oneAsset3.hospital__c;
        asset33.Product2Id = oneAsset3.Product2Id;
        asset33.Quantity = 50;
        asset33.Status = '不明';
        asset33.Manage_type__c = '数量管理';
        asset33.Loaner_accsessary__c = true;
        asset33.Out_of_wh__c = 0;
        asset33.Salesdepartment__c = '1.华北营业本部';
        asset33.Internal_asset_location__c = '北京 备品中心';
        asset33.Product_category__c = 'GI';
        asset33.Equipment_Type__c = '产品试用';
        asset33.SalesProvince__c = '北京';
        asset33.CompanyOfEquipment__c = '北京';
        asset33.Internal_Asset_number__c = '00033';
        insert new Asset[] {asset11, asset22, asset33};

        // 备品一对一Link表
        Fixture_OneToOne_Link__c foLink11 = new Fixture_OneToOne_Link__c();
        Fixture_OneToOne_Link__c foLink22 = new Fixture_OneToOne_Link__c();
        foLink11.Main_Asset__c = asset11.Id;
        foLink11.Accessory_Asset__c = asset22.Id;
        foLink11.Quantity__c = 1;

        foLink22.Main_Asset__c = asset11.Id;
        foLink22.Accessory_Asset__c = asset33.Id;
        foLink22.Quantity__c = 4;
        insert new Fixture_OneToOne_Link__c[] {foLink11, foLink22};

         //20210521 ljh add 1829 start
         // 保有设备
         Asset asset00 = new Asset(Asset_Owner__c = 'Olympus');
         asset00.RecordTypeId = System.Label.Asset_RecordType;
         asset00.SerialNumber = 'asset000';
         asset00.Name = 'asset000';
         asset00.AccountId = oneAsset1.Accountid;
         asset00.Department_Class__c = oneAsset1.Department_Class__c;
         asset00.Hospital__c = oneAsset1.hospital__c;
         asset00.Product2Id = pro[3].Id;
         asset00.Quantity = 1;
         asset00.Status = '不明';
         asset00.Manage_type__c = '个体管理';
         asset00.Loaner_accsessary__c = false;
         asset00.Out_of_wh__c = 0;
         asset00.Salesdepartment__c = '1.华北营业本部';
         asset00.Internal_asset_location__c = '北京 备品中心';
         asset00.Product_category__c = 'GI';
         asset00.Equipment_Type__c = '产品试用';
         asset00.SalesProvince__c = '北京';
         asset00.CompanyOfEquipment__c = '北京';
         asset00.Internal_Asset_number__c = '00000';
         asset00.Main_OneToOne__c = true;

         Asset asset01 = new Asset(Asset_Owner__c = 'Olympus');
         asset01.RecordTypeId = System.Label.Asset_RecordType;
         asset01.SerialNumber = 'asset010';
         asset01.Name = 'asset010';
         asset01.AccountId = oneAsset1.Accountid;
         asset01.Department_Class__c = oneAsset1.Department_Class__c;
         asset01.Hospital__c = oneAsset1.hospital__c;
         asset01.Product2Id = pro[4].Id;
         asset01.Quantity = 1;
         asset01.Status = '不明';
         asset01.Manage_type__c = '个体管理';
         asset01.Loaner_accsessary__c = true;
         asset01.Out_of_wh__c = 0;
         asset01.Salesdepartment__c = '1.华北营业本部';
         asset01.Internal_asset_location__c = '北京 备品中心';
         asset01.Product_category__c = 'GI';
         asset01.Equipment_Type__c = '产品试用';
         asset01.SalesProvince__c = '北京';
         asset01.CompanyOfEquipment__c = '北京';
         asset01.Internal_Asset_number__c = '00001';

         Asset asset02 = new Asset(Asset_Owner__c = 'Olympus');
         asset02.RecordTypeId = System.Label.Asset_RecordType;
         asset02.SerialNumber = 'asset020';
         asset02.Name = 'asset020';
         asset02.AccountId = oneAsset1.Accountid;
         asset02.Department_Class__c = oneAsset1.Department_Class__c;
         asset02.Hospital__c = oneAsset1.hospital__c;
         asset02.Product2Id = pro[5].Id;
         asset02.Quantity = 50;
         asset02.Status = '不明';
         asset02.Manage_type__c = '数量管理';
         asset02.Loaner_accsessary__c = true;
         asset02.Out_of_wh__c = 0;
         asset02.Salesdepartment__c = '1.华北营业本部';
         asset02.Internal_asset_location__c = '北京 备品中心';
         asset02.Product_category__c = 'GI';
         asset02.Equipment_Type__c = '产品试用';
         asset02.SalesProvince__c = '北京';
         asset02.CompanyOfEquipment__c = '北京';
         asset02.Internal_Asset_number__c = '00002';
         insert new Asset[] {asset00, asset01, asset02};

          // 备品一对一Link表
          Fixture_OneToOne_Link__c foLink00 = new Fixture_OneToOne_Link__c();
          Fixture_OneToOne_Link__c foLink01 = new Fixture_OneToOne_Link__c();
          foLink00.Main_Asset__c = asset00.Id;
          foLink00.Accessory_Asset__c = asset01.Id;
          foLink00.Quantity__c = 1;

          foLink01.Main_Asset__c = asset00.Id;
          foLink01.Accessory_Asset__c = asset02.Id;
          foLink01.Quantity__c = 1;
          insert new Fixture_OneToOne_Link__c[] {foLink00, foLink01};
          //20210521 ljh add 1829  end
    }

}