@isTest
private class AssetHandlerTest {

    private static User user1;
    private static List<Rental_Apply__c> setupRAList;

    static Asset setupTestData() {
        // 省
        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;

        // 病院を作る
        ControllerUtil.EscapeNFM001Trigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        StaticParameter.EscapeAccountTrigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;

        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        System.debug('病院完了!');
        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;
        System.debug('病院 insert 完了!');

        // 戦略科室を得る
        Account strategicDep = new Account();
        strategicDep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_Class_OTH'].id;
        strategicDep.Name = 'Olympus社内 其他';
        strategicDep.Department_Class_Label__c = '其他';
        strategicDep.Hospital__c = hospital.Id;
        strategicDep.ParentId = hospital.Id;
        insert strategicDep;

        // 診療科を作る
        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.Id;
        dep.Department_Class__c = strategicDep.Id;
        dep.Hospital__c = hospital.Id;
        insert dep;
        System.debug('診療科 insert 完了!');

        // 产品
        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);

        insert new Product2[] {pro1};

        // 保有设备A (主体)
        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.Id;
        assetA1.Hospital__c = hospital.Id;
        assetA1.Product2Id = pro1.Id;
        assetA1.Quantity = 1;
        assetA1.Status = '有库存';
        assetA1.Manage_type__c = '个体管理';
        assetA1.Loaner_accsessary__c = false;
        assetA1.Out_of_wh__c = 0;
        assetA1.Salesdepartment__c = '1.华北营业本部';
        assetA1.Internal_asset_location__c = '北京 备品中心';
        assetA1.Product_category__c = 'GI';
        assetA1.Equipment_Type__c = '产品试用';
        assetA1.SalesProvince__c = '北京';
        insert new Asset[] {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 = 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;
        insert new Fixture_Set_Detail__c[] {fsdObjA1};

        //备品借出申请
        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.Person_In_Charge__c = Userinfo.getUserId();
        raObj.applyUser__c = Userinfo.getUserId();
        raObj.Demo_purpose_text__c = '123';
        raObj.Request_shipping_day__c = Date.toDay();
        raObj.Request_return_day__c = Date.toDay();
        raObj.direct_send__c = '上门自提';
        raObj.Loaner_received_staff__c = '王五';
        raObj.Loaner_received_staff_phone__c = '110';
        raObj.pickup_time__c = System.now();

        Rental_Apply__c raObj1 = new Rental_Apply__c();
        raObj1.Name = 'testra1';
        raObj1.Product_category__c = 'GI';
        raObj1.Demo_purpose1__c = '其他';
        raObj1.demo_purpose2__c = '其他';
        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.Loaner_received_staff__c = '王五';
        raObj1.Loaner_received_staff_phone__c = '110';
        raObj1.pickup_time__c = System.now();
        setupRAList = new Rental_Apply__c[]{raObj,raObj1};
        insert setupRAList;

        // 借出备品配套一览
        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.Cancel_Select__c = false;
        raesObj.Shipment_request_time__c = Date.toDay();
        raesObj.Rental_Start_Date__c = Date.toDay();
        raesObj.Rental_End_Date__c = Date.toDay();
        raesObj.IndexFromUniqueKey__c = 1;
        raesObj.UniqueKey__c = '1:'+ fsObjA1.Id + ':1';

        Rental_Apply_Equipment_Set__c raesObj1 = new Rental_Apply_Equipment_Set__c();
        raesObj1.Rental_Apply__c = raObj1.Id;
        raesObj1.Fixture_Set__c = fsObjA1.Id;
        raesObj1.Cancel_Select__c = false;
        raesObj1.Shipment_request_time__c = Date.toDay();
        raesObj1.Rental_Start_Date__c = Date.toDay().addDays(1);
        raesObj1.Rental_End_Date__c = Date.toDay().addDays(1);
        raesObj1.IndexFromUniqueKey__c = 2;
        raesObj1.UniqueKey__c = '1:'+ fsObjA1.Id + ':2';
        insert new Rental_Apply_Equipment_Set__c[]{raesObj, raesObj1};

        // 借出备品配套一览明细
        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.IndexFromUniqueKey__c = 1;
        raesdObj1.UniqueKey__c = '1:'+ raesObj.Id + ':' + fsdObjA1.Id + ':1';
        raesdObj1.Rental_Apply_Equipment_Set__c = raesObj.Id;

        Rental_Apply_Equipment_Set_Detail__c raesdObj2 = new Rental_Apply_Equipment_Set_Detail__c();
        raesdObj2.Rental_Apply__c = raObj1.Id;
        raesdObj2.Fixture_Set_Detail__c = fsdObjA1.Id;
        raesdObj2.Rental_Num__c = 1;
        raesdObj2.Queue_Number__c = null;
        raesdObj2.Is_Body__c = true;
        raesdObj2.IndexFromUniqueKey__c = 1;
        raesdObj2.UniqueKey__c = '1:'+ raesObj1.Id + ':' + fsdObjA1.Id + ':1';
        raesdObj2.Rental_Apply_Equipment_Set__c = raesObj1.Id;
        insert new Rental_Apply_Equipment_Set_Detail__c[] {raesdObj1, raesdObj2};

        return assetA1;
    }

    // 已分配的数据节点
    static Id setupTestData2() {
        // OLY_OCM-643 追加ControllerUtil.EscapeNFM001Trigger
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        StaticParameter.EscapeAccountTrigger = true;
        ControllerUtil.EscapeNFM001Trigger = true;

        Rental_Apply__c raObj = new Rental_Apply__c();
        Fixture_Set_Detail__c fsdObjA1 = new Fixture_Set_Detail__c();
        Fixture_Set_Detail__c fsdObjA2 = new Fixture_Set_Detail__c();

        Rental_Apply_Equipment_Set__c raesObj = new Rental_Apply_Equipment_Set__c();
        Asset asset1 = new Asset(Asset_Owner__c = 'Olympus');
        Asset asset2 = new Asset(Asset_Owner__c = 'Olympus');

        Fixture_OneToOne_Link__c foLink1 = new Fixture_OneToOne_Link__c();

        System.runAs(new User(Id = Userinfo.getUserId())) {
            StaticParameter.EscapeNFM001AgencyContractTrigger = true;
            StaticParameter.EscapeNFM001Trigger = true;
            StaticParameter.EscapeAccountTrigger = true;
            ControllerUtil.EscapeNFM001Trigger = true;

            Oly_TriggerHandler.bypass(ContactTriggerHandler.class.getName());
            Oly_TriggerHandler.bypass(AgencyHospitalHandler.class.getName());
            // 省
            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 = new Account();
            strategicDep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_Class_OTH'].id;
            strategicDep.Name = 'Olympus社内 其他';
            strategicDep.Department_Class_Label__c = '其他';
            strategicDep.Hospital__c = hospital.Id;
            strategicDep.ParentId = hospital.Id;
            insert strategicDep;

            // 診療科を作る
            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.Id;
            dep.Department_Class__c = strategicDep.Id;
            dep.Hospital__c = hospital.Id;
            insert dep;

            // 产品
            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', 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);

            insert new Product2[] {pro1, pro2};

            //备品借出申请
            raObj.Name = 'testra';
            raObj.Product_category__c = 'GI';
            raObj.Demo_purpose1__c = '其他';
            raObj.demo_purpose2__c = '其他';
            raObj.Person_In_Charge__c = Userinfo.getUserId();
            raObj.applyUser__c = Userinfo.getUserId();
            raObj.Demo_purpose_text__c = '123';
            raObj.Request_shipping_day__c = Date.toDay();
            raObj.Request_return_day__c = Date.toDay();
            raObj.direct_send__c = '上门自提';
            raObj.Loaner_received_staff__c = '王五';
            raObj.Loaner_received_staff_phone__c = '110';
            raObj.pickup_time__c = System.now();
            insert raObj;

            // 保有设备
            asset1.RecordTypeId = System.Label.Asset_RecordType;
            asset1.SerialNumber = 'asset1';
            asset1.Name = 'asset1';
            asset1.AccountId = dep.Id;
            asset1.Department_Class__c = strategicDep.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';

            asset2.RecordTypeId = System.Label.Asset_RecordType;
            asset2.SerialNumber = 'asset2';
            asset2.Name = 'asset2';
            asset2.AccountId = dep.Id;
            asset2.Department_Class__c = strategicDep.Id;
            asset2.Hospital__c = hospital.Id;
            asset2.Product2Id = pro2.Id;
            asset2.Quantity = 100;
            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';

            insert new Asset[] {asset1, asset2};

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

            insert new Fixture_OneToOne_Link__c[] {foLink1};

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

            // 备品配套明细
            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;

            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;

            insert new Fixture_Set_Detail__c[] {fsdObjA1,fsdObjA2};

            // 借出备品配套一览
            raesObj.Rental_Apply__c = raObj.Id;
            raesObj.Fixture_Set__c = fsObj1.Id;
            raesObj.Cancel_Select__c = false;
            raesObj.Rental_Start_Date__c = Date.toDay();
            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();
        Rental_Apply_Equipment_Set_Detail__c raesdObj2 = new Rental_Apply_Equipment_Set_Detail__c();

        System.runAs(new User(Id = Userinfo.getUserId())) {

            // 借出备品配套一览明细
            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 = 1;
            raesdObj1.FSD_Is_Optional__c = false;
            raesdObj1.FSD_Is_OneToOne__c = true;
            raesdObj1.ApplyPersonAppended__c = false;
            raesdObj1.Fixture_OneToOne_Link_Id__c = foLink1.Id;
            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 = '产品试用';
            // 借出备品配套一览明细
            raesdObj2.Rental_Apply__c = raObj.Id;
            raesdObj2.Fixture_Set_Detail__c = fsdObjA2.Id;
            raesdObj2.Rental_Num__c = 2;
            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 = 2;
            raesdObj2.FSD_Is_Optional__c = true;
            raesdObj2.FSD_Is_OneToOne__c = true;
            raesdObj2.ApplyPersonAppended__c = false;
            raesdObj2.Cancel_Select__c = false;
            raesdObj2.Fixture_OneToOne_Link_Id__c = foLink1.Id;
            raesdObj2.FSD_Fixture_Model_No__c = 'n04';
            raesdObj2.Fixture_Model_No_text__c = 'n04';
            raesdObj2.Salesdepartment_before__c = '1.华北营业本部';
            raesdObj2.Internal_asset_location_before__c = '北京 备品中心';
            raesdObj2.Product_category_text__c = 'GI';
            raesdObj2.Equipment_Type_text__c = '产品试用';

            insert new Rental_Apply_Equipment_Set_Detail__c[] {raesdObj1,raesdObj2};

        }
        System.runAs(new User(Id = Userinfo.getUserId())) {
            // 申请单
            raObj.Status__c = FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Yi_Pi_Zhun.ordinal());
            raObj.Status_Text__c = FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Yi_Pi_Zhun.ordinal());
            raObj.NotWatch_RA_Status__c = true;
            update raObj;

            // 借出备品配套一览明细
            raesdObj1.Select_Time__c = Date.toDay();
            raesdObj1.Asset__c = asset1.Id;
            raesdObj1.Loaner_accsessary__c = false;
            raesdObj1.FSD_Name_CHN__c = 'name01';
            raesdObj1.Shipment_Status_Text__c = FixtureUtil.raesdStatusMap.get(FixtureUtil.HistoryStatus.Yi_Fen_Pei.ordinal());

            raesdObj2.Select_Time__c = Date.toDay();
            raesdObj2.Asset__c = asset2.Id;
            raesdObj2.Loaner_accsessary__c = true;
            raesdObj2.FSD_Name_CHN__c = 'name02';
            raesdObj2.Shipment_Status_Text__c = FixtureUtil.raesdStatusMap.get(FixtureUtil.HistoryStatus.Yi_Fen_Pei.ordinal());

            update new Rental_Apply_Equipment_Set_Detail__c[] {raesdObj1,raesdObj2};

            raesObj.First_RAESD__c = raesdObj2.Id;
            update raesObj;

        }
        return raObj.Id;
    }

    private static User getUser() {
        if (user1 != null) {
            return user1;
        }
        String timenow = Datetime.now().format('yyyyMMddHHmmss');
        User hpOwner = new User(Test_staff__c = true, LastName = 'TestMao', FirstName = 'TestMaoF',
                Alias = 'hp', CommunityNickname = 'TestMao', Email = 'Test@sunbridge.com',
                Username = 'Test' + timenow + '@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP',
                TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = System.Label.ProfileId_SystemAdmin,
                Dept__c = '医疗华北营业本部', Province__c = '北京');
        insert hpOwner;
        return hpOwner;
    }

    /*
    * Asset新規作成する際の暂定分配テスト
    * TODO 20210208 报101，需要修改
    */
    static void newAssetQueueRedistri() {

        system.runAs(getUser()){
            Asset aa = setupTestData();
            List<Rental_Apply__c> raObjs = setupRAList;
            System.assertEquals(raObjs.size(), 2);
            Rental_Apply__c raObj = raObjs[0];
            raObj.Status__c = FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Yi_Chu_Ku_Zhi_Shi.ordinal());
            update raObj;

// cancelAssetQueueRedistriTest() なとで assert 済み
//            List<Asset> ass = [Select Id, Last_Reserve_RAES_Detail__c From Asset];
//            System.assertEquals(ass.size(), 1);
//
//            List<Rental_Apply_Equipment_Set_Detail__c> raesds = [Select Id From Rental_Apply_Equipment_Set_Detail__c];
//            System.assertEquals(raesds.size(), 2);

            List<Rental_Apply_Equipment_Set__c> raess = [Select Id From Rental_Apply_Equipment_Set__c Where Rental_Start_Date__c = toDay];
            System.assertEquals(raess.size(), 1);
            PageReference page = new PageReference('/apex/MainFixtureSelect?pt_recid=' + raess[0].Id);
            System.Test.setCurrentPage(page);

            Test.startTest();
            MainFixtureSelectController mh = new MainFixtureSelectController();
            CreateRelationListPagingCmpCtrl cmp = new CreateRelationListPagingCmpCtrl();
            cmp.pageController = mh;
            mh.myComponentController.init();
            mh.bieCunFangDi = '北京 备品中心';
            mh.bieBenBu = '1.华北营业本部';
            mh.bieChanPinFenLei = 'GI';
            mh.bieBeiPinFenLei = '产品试用';

            mh.searchOpp();
            mh.init();
            System.assertEquals(mh.viewList.size(), 1);
            Rental_Apply_Equipment_Set_Detail__c robj = (Rental_Apply_Equipment_Set_Detail__c) mh.viewList[0].sobj;
// cancelAssetQueueRedistriTest() なとで assert 済み
//            System.assertEquals(robj.Asset__c, ass[0].Id);
            mh.viewList[0].check = true;
            mh.save();
            List<Asset> ass = [Select Id, Last_Reserve_RAES_Detail__c From Asset];
// cancelAssetQueueRedistriTest() なとで assert 済み
//            raesds = [Select Id, Asset__c From Rental_Apply_Equipment_Set_Detail__c Where Rental_Apply_Equipment_Set__c = :raess[0].Id];
//            System.assertEquals(raesds.size(), 1);
//            System.assertNotEquals(raesds[0].Asset__c, null);

//            System.assertEquals(ass[0].Last_Reserve_RAES_Detail__c, raesds[0].Id);
//            System.assertEquals(ass.size(), 1);
//            System.assertEquals(ass[0].Last_Reserve_RAES_Detail__c, raesds[0].Id);
            Date d1 = Date.today().addDays(1);
            raess = [Select Id From Rental_Apply_Equipment_Set__c Where Rental_Start_Date__c = :d1];
            System.assertEquals(raess.size(), 1);
            PageReference page1 = new PageReference('/apex/MainFixtureSelect?pt_recid=' + raess[0].Id);
            System.Test.setCurrentPage(page1);
            mh = new MainFixtureSelectController();
            cmp = new CreateRelationListPagingCmpCtrl();
            cmp.pageController = mh;
            mh.myComponentController.init();
            mh.bieCunFangDi = '北京 备品中心';
            mh.bieBenBu = '1.华北营业本部';
            mh.bieChanPinFenLei = 'GI';
            mh.bieBeiPinFenLei = '产品试用';
            mh.searchOpp();
            mh.init();
            Test.stopTest();
            System.assertEquals(mh.viewList.size(), 1);
            robj = (Rental_Apply_Equipment_Set_Detail__c) mh.viewList[0].sobj;
            System.assertEquals(robj.Asset__c, ass[0].Id);
            mh.viewList[0].check = true;
            MainFixtureSelectController.queue(raess[0].Id, ass[0].Id);
/**
            raesds = [Select Id, Asset__c, Queue_Day__c, Queue_Number__c From Rental_Apply_Equipment_Set_Detail__c Where Rental_Apply_Equipment_Set__c = :raess[0].Id];
            System.assertEquals(raesds.size(), 1);
            System.assertEquals(raesds[0].Queue_Day__c, Date.today());
            System.assertEquals(raesds[0].Queue_Number__c, 1);
**/
            Asset aa2 = aa.clone(false);
            aa2.SerialNumber = 'ass02';
            aa2.Name = 'ass02';
            insert aa2;

            List<Rental_Apply_Equipment_Set_Detail__c> raesds = [Select Id, Asset__c, Queue_Day__c, Queue_Number__c From Rental_Apply_Equipment_Set_Detail__c Where Rental_Apply_Equipment_Set__c = :raess[0].Id];
            System.assertEquals(raesds.size(), 1);
            System.assertEquals(raesds[0].Queue_Day__c, Date.today());
            System.assertEquals(raesds[0].Queue_Number__c, 0);
            System.assertEquals(raesds[0].Asset__c, aa2.Id);

            ass = [Select Id, Last_Reserve_RAES_Detail__c From Asset Where id =:aa2.Id];
            System.assertEquals(ass[0].Last_Reserve_RAES_Detail__c, raesds[0].Id);
        }
    }

    /*
    * 明細キャンセルの暂定分配テスト
    */
    static testMethod void cancelAssetQueueRedistriTest() {

        system.runAs(getUser()){
            Asset aa = setupTestData();
            List<Rental_Apply__c> raObjs = setupRAList;
            System.assertEquals(raObjs.size(), 2);
            Rental_Apply__c raObj = raObjs[0];
            raObj.Status__c = FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Yi_Chu_Ku_Zhi_Shi.ordinal());
            update raObj;

            List<Asset> ass = [Select Id, Last_Reserve_RAES_Detail__c From Asset];
            System.assertEquals(ass.size(), 1);

            List<Rental_Apply_Equipment_Set_Detail__c> raesds = [Select Id From Rental_Apply_Equipment_Set_Detail__c];
            System.assertEquals(raesds.size(), 2);
            //Test.startTest();
            List<Rental_Apply_Equipment_Set__c> raess = [Select Id From Rental_Apply_Equipment_Set__c Where Rental_Start_Date__c = toDay];
            Rental_Apply_Equipment_Set__c raess1 = raess[0];
            System.assertEquals(raess.size(), 1);
            PageReference page = new PageReference('/apex/MainFixtureSelect?pt_recid=' + raess[0].Id);
            System.Test.setCurrentPage(page);
            Test.startTest();
            MainFixtureSelectController mh = new MainFixtureSelectController();
            CreateRelationListPagingCmpCtrl cmp = new CreateRelationListPagingCmpCtrl();
            cmp.pageController = mh;
            mh.myComponentController.init();
            mh.bieCunFangDi = '北京 备品中心';
            mh.bieBenBu = '1.华北营业本部';
            mh.bieChanPinFenLei = 'GI';
            mh.bieBeiPinFenLei = '产品试用';
            mh.searchOpp();
            mh.init();
            System.assertEquals(mh.viewList.size(), 1);
            Rental_Apply_Equipment_Set_Detail__c robj = (Rental_Apply_Equipment_Set_Detail__c) mh.viewList[0].sobj;
            System.assertEquals(robj.Asset__c, ass[0].Id);
            mh.viewList[0].check = true;
            mh.save();
            ass = [Select Id, Last_Reserve_RAES_Detail__c From Asset];

            raesds = [Select Id, Asset__c From Rental_Apply_Equipment_Set_Detail__c Where Rental_Apply_Equipment_Set__c = :raess[0].Id];
            System.assertEquals(raesds.size(), 1);
            System.assertNotEquals(raesds[0].Asset__c, null);

            System.assertEquals(ass[0].Last_Reserve_RAES_Detail__c, raesds[0].Id);
            System.assertEquals(ass.size(), 1);
            System.assertEquals(ass[0].Last_Reserve_RAES_Detail__c, raesds[0].Id);

            Date d1 = Date.today().addDays(1);
            raess = [Select Id From Rental_Apply_Equipment_Set__c Where Rental_Start_Date__c = :d1];
            Rental_Apply_Equipment_Set__c raess2 = raess[0];
            System.assertEquals(raess.size(), 1);
            PageReference page1 = new PageReference('/apex/MainFixtureSelect?pt_recid=' + raess[0].Id);
            System.Test.setCurrentPage(page1);
            mh = new MainFixtureSelectController();
            cmp = new CreateRelationListPagingCmpCtrl();
            cmp.pageController = mh;
            mh.myComponentController.init();
            mh.bieCunFangDi = '北京 备品中心';
            mh.bieBenBu = '1.华北营业本部';
            mh.bieChanPinFenLei = 'GI';
            mh.bieBeiPinFenLei = '产品试用';
            mh.searchOpp();
            mh.init();
            System.Test.stopTest();
            System.assertEquals(mh.viewList.size(), 1);
            robj = (Rental_Apply_Equipment_Set_Detail__c) mh.viewList[0].sobj;
            System.assertEquals(robj.Asset__c, ass[0].Id);
            mh.viewList[0].check = true;
            MainFixtureSelectController.queue(raess[0].Id, ass[0].Id);
/**
            raesds = [Select Id, Asset__c, Queue_Day__c, Queue_Number__c From Rental_Apply_Equipment_Set_Detail__c Where Rental_Apply_Equipment_Set__c = :raess[0].Id];
            System.assertEquals(raesds.size(), 1);
            System.assertEquals(raesds[0].Queue_Day__c, Date.today());
            System.assertEquals(raesds[0].Queue_Number__c, 1);

            raesds = [Select Id, Asset__c, Queue_Day__c, Queue_Number__c From Rental_Apply_Equipment_Set_Detail__c Where Rental_Apply_Equipment_Set__c = :raess1.Id];
            raesds[0].Cancel_Select__c = true;
            raesds[0].Cancel_Reason__c = '主动取消';
            update raesds;


            raesds = [Select Id, Asset__c, Queue_Day__c, Queue_Number__c From Rental_Apply_Equipment_Set_Detail__c Where Rental_Apply_Equipment_Set__c = :raess2.Id];
            System.assertEquals(raesds.size(), 1);
            System.assertEquals(raesds[0].Queue_Day__c, Date.today());
            System.assertEquals(raesds[0].Queue_Number__c, 0);
            System.assertEquals(raesds[0].Asset__c, aa.Id);

            ass = [Select Id, Last_Reserve_RAES_Detail__c From Asset Where id =:aa.Id];
            System.assertEquals(ass[0].Last_Reserve_RAES_Detail__c, raesds[0].Id);
**/
        }
    }


    /**
     *
     * @description 明細キャンセルの暂定分配テスト 排的是别的Asset
     * @example
     * Asset A
     * Asset B
     * RaesdAA 分配Asset A
     * RaesdBB 排队Asset B
     * RaesdAA取消分配后RaesdBB为暂定分配
     *
     */
    static testMethod void cancelAssetQueueRedistriTest2() {

        system.runAs(getUser()){
            Asset aa = setupTestData();

             // 产品
            Product2 pro1 = new Product2(Name='name02',IsActive=true,Family='SP',
                    Fixture_Model_No__c='n02',Serial_Lot_No__c='S/N tracing',
                    Fixture_Model_No_T__c = 'n02',
                    ProductCode_Ext__c='pc02',Manual_Entry__c=false);

            insert new Product2[] {pro1};

            Asset a2 = aa.clone(false);
            System.assertEquals(null, a2.Id);
            a2.Product2Id = pro1.Id;
            a2.Salesdepartment__c = '4.华东营业本部';
            a2.Internal_asset_location__c = '上海 备品中心';
            a2.Product_category__c = 'SP';
            a2.Equipment_Type__c = '产品试用';
            a2.SalesProvince__c = '上海';
            a2.SerialNumber = 'ass02';
            a2.Name = 'ass02';
            insert a2;

            List<Rental_Apply__c> raObjs = [Select Id From Rental_Apply__c];
            System.assertEquals(raObjs.size(), 2);
            Rental_Apply__c raObj = raObjs[0];
            raObj.Status__c = FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Yi_Chu_Ku_Zhi_Shi.ordinal());
            update raObj;

            List<Asset> ass = [SELECT Id, Last_Reserve_RAES_Detail__c
                                 FROM Asset
                                WHERE id = :a2.Id];
            System.assertEquals(ass.size(), 1);

            List<Rental_Apply_Equipment_Set_Detail__c> raesds = [Select Id From Rental_Apply_Equipment_Set_Detail__c];
            System.assertEquals(raesds.size(), 2);
            //Test.startTest();
            List<Rental_Apply_Equipment_Set__c> raess = [Select Id From Rental_Apply_Equipment_Set__c Where Rental_Start_Date__c = toDay];
            Rental_Apply_Equipment_Set__c raess1 = raess[0];
            System.assertEquals(raess.size(), 1);
            PageReference page = new PageReference('/apex/MainFixtureSelect?pt_recid=' + raess1.Id);
            System.Test.setCurrentPage(page);
            MainFixtureSelectController mh = new MainFixtureSelectController();
            CreateRelationListPagingCmpCtrl cmp = new CreateRelationListPagingCmpCtrl();
            cmp.pageController = mh;
            mh.myComponentController.init();
            mh.bieCunFangDi = '上海 备品中心';
            mh.bieBenBu = '4.华东营业本部';
            mh.bieChanPinFenLei = 'SP';
            mh.bieBeiPinFenLei = '产品试用';
            mh.keyword = 'n02';
            mh.init();
            Test.startTest();
            mh.searchOpp();
            System.assertEquals(mh.viewList.size(), 1);
            Rental_Apply_Equipment_Set_Detail__c robj = (Rental_Apply_Equipment_Set_Detail__c) mh.viewList[0].sobj;
            System.assertEquals(robj.Asset__c, ass[0].Id);
            mh.viewList[0].check = true;
            mh.save();

            ass = [SELECT Id, Last_Reserve_RAES_Detail__c
                     FROM Asset
                    WHERE id = :a2.Id];

            raesds = [Select Id, Asset__c, Fixture_Model_No_text__c
                        FROM Rental_Apply_Equipment_Set_Detail__c
                       WHERE Rental_Apply_Equipment_Set__c = :raess1.Id];
            System.assertEquals(raesds.size(), 1);
            System.assertNotEquals(raesds[0].Asset__c, null);
            System.assertEquals('n02', raesds[0].Fixture_Model_No_text__c);

            System.assertEquals(ass[0].Last_Reserve_RAES_Detail__c, raesds[0].Id);
            System.assertEquals(ass.size(), 1);

            Date d1 = Date.today().addDays(1);
            raess = [Select Id From Rental_Apply_Equipment_Set__c Where Rental_Start_Date__c = :d1];
            Rental_Apply_Equipment_Set__c raess2 = raess[0];
            System.assertEquals(raess.size(), 1);
            PageReference page1 = new PageReference('/apex/MainFixtureSelect?pt_recid=' + raess2.Id);
            System.Test.setCurrentPage(page1);
            mh = new MainFixtureSelectController();
            cmp = new CreateRelationListPagingCmpCtrl();
            cmp.pageController = mh;
            mh.myComponentController.init();
            mh.bieCunFangDi = '上海 备品中心';
            mh.bieBenBu = '4.华东营业本部';
            mh.bieChanPinFenLei = 'SP';
            mh.bieBeiPinFenLei = '产品试用';
            mh.keyword = 'n02';
            mh.searchOpp();
            mh.init();
            Test.stopTest();
            System.assertEquals(mh.viewList.size(), 1);
            robj = (Rental_Apply_Equipment_Set_Detail__c) mh.viewList[0].sobj;
            System.assertEquals(robj.Asset__c, ass[0].Id);
            mh.viewList[0].check = true;
/**
            raesds = [SELECT Id, Asset__c, Queue_Day__c, Queue_Number__c, Fixture_Model_No_text__c
                        FROM Rental_Apply_Equipment_Set_Detail__c
                      WHERE Rental_Apply_Equipment_Set__c = :raess2.Id
                        AND Is_Body__c = true];

            System.assertEquals(raesds.size(), 1);
            System.assertEquals(raesds[0].Queue_Day__c, null);
            System.assertEquals(raesds[0].Queue_Number__c, null);

            Map<String, String> mm = MainFixtureSelectController.queue(raess2.Id, a2.Id);

            raesds = [SELECT Id, Asset__c, Queue_Day__c, Queue_Number__c, Fixture_Model_No_text__c
                        FROM Rental_Apply_Equipment_Set_Detail__c
                      WHERE Rental_Apply_Equipment_Set__c = :raess2.Id
                        AND Is_Body__c = true];
            ass = [SELECT Id, Last_Reserve_RAES_Detail__c, Fixture_Model_No_F__c
                     FROM Asset
                    WHERE id = :a2.Id];

            System.assertEquals('n02', ass[0].Fixture_Model_No_F__c);

            System.assertEquals(raesds.size(), 1);
            System.assertEquals(raesds[0].Queue_Day__c, Date.today());
            System.assertEquals(raesds[0].Queue_Number__c, 1);
            System.assertEquals('n02', raesds[0].Fixture_Model_No_text__c);

            raesds = [Select Id, Asset__c, Queue_Day__c, Queue_Number__c From Rental_Apply_Equipment_Set_Detail__c Where Rental_Apply_Equipment_Set__c = :raess1.Id];
            raesds[0].Cancel_Select__c = true;
            raesds[0].Cancel_Reason__c = '主动取消';
            update raesds;


            raesds = [Select Id, Asset__c, Queue_Day__c, Queue_Number__c From Rental_Apply_Equipment_Set_Detail__c Where Rental_Apply_Equipment_Set__c = :raess2.Id];
            System.assertEquals(raesds.size(), 1);
            System.assertEquals(raesds[0].Queue_Day__c, Date.today());
            System.assertEquals(raesds[0].Queue_Number__c, 0);
            System.assertEquals(raesds[0].Asset__c, a2.Id);

            ass = [SELECT Id, Last_Reserve_RAES_Detail__c
                     FROM Asset
                    WHERE id =:a2.Id];
            System.assertEquals(ass[0].Last_Reserve_RAES_Detail__c, raesds[0].Id);
**/
        }
    }


    /**
     *
     * @description mainAssetUpdateed_OneToOneRecord测试Case
     *
     */
    static testMethod void TestmainAssetUpdateed_OneToOneRecord() {

        system.runAs(getUser()){
            Id raId = setupTestData2();
            System.Test.startTest();
            List<Fixture_OneToOne_Link__c> otoList = [SELECT Id
                        , In_wh_Fu_Shu_Pin_You_Xiao_Ku_Cun_F__c
                        , In_wh_Fu_Shu_Pin_You_Xiao_Ku_Cun__c
                     FROM Fixture_OneToOne_Link__c];
            // for (Fixture_OneToOne_Link__c oto : otoList) {
            //     System.assertEquals(oto.In_wh_Fu_Shu_Pin_You_Xiao_Ku_Cun__c, oto.In_wh_Fu_Shu_Pin_You_Xiao_Ku_Cun_F__c);
            // }
            update otoList;
            List<Rental_Apply_Equipment_Set_Detail__c> raesdList = [SELECT Id
                        , Manage_type_F__c
                        , Asset__r.Last_Reserve_RAES_Detail__c
                        , Asset__r.SerialNumber
                     FROM Rental_Apply_Equipment_Set_Detail__c
                    WHERE Rental_Apply__c = :raId];
            for (Rental_Apply_Equipment_Set_Detail__c raesd : raesdList) {
                raesd.Cancel_Select__c = true;
                raesd.Cancel_Mem__c = UserInfo.getUserId();
                raesd.Cancel_Date__c = Date.today();
                raesd.Cancel_Time__c = MainFixtureSelectController.getCurrentTime();
                raesd.Cancel_Reason__c = '主动取消';
                raesd.Loaner_cancel_Remarks__c = 'TestClass';
                if (raesd.Asset__r.SerialNumber == 'asset2') {
                    System.assertEquals('数量管理', raesd.Manage_type_F__c);
                    System.assertEquals(null, raesd.Asset__r.Last_Reserve_RAES_Detail__c);
                }
                else {
                    System.assertEquals('个体管理', raesd.Manage_type_F__c);
                    System.assertNOTEquals(null, raesd.Asset__r.Last_Reserve_RAES_Detail__c);
                }
            }
            update raesdList;
            otoList = [SELECT Id
                        , In_wh_Fu_Shu_Pin_You_Xiao_Ku_Cun_F__c
                        , In_wh_Fu_Shu_Pin_You_Xiao_Ku_Cun__c
                     FROM Fixture_OneToOne_Link__c];
            System.assertEquals(1, otoList.size());
            for (Fixture_OneToOne_Link__c oto : otoList) {
                System.assertEquals(oto.In_wh_Fu_Shu_Pin_You_Xiao_Ku_Cun__c, oto.In_wh_Fu_Shu_Pin_You_Xiao_Ku_Cun_F__c);
            }
            System.Test.stopTest();
        }
    }



    /**
     *
     * @description 暂定分配是Asset修理暂定分配取消
     * @example
     * Asset A
     * RaesdAA 暂定分配Asset A
     * RaesdBB排队 Asset A
     * AssetA 修理
     * RaesdAA取消分配
     * RaesdBB为排队中
     *
     * 注释掉理由：
     * 明细暂定分配的时候创建修理的话会报以下错误
     * 借出_分配_数 不能大于 设备的总数 ------> Asset 入力规则 You_Xiao_Ku_Cun_chk
     * 暂定分配-1
     * 修理-1
     * 数量1
     * 1-1-1 = -1
     *
     * 如果暂定分配想修理的话分配暂定分配的那一条明细
     * 然后再出出库前检测的时候选择NG修理
     *
     */
    // static testMethod void cancelAssetQueueRedistriTest3() {

    //     system.runAs(getUser()){
    //         Asset aa = setupTestData();
    //         List<Rental_Apply__c> raObjs = [Select Id From Rental_Apply__c];
    //         System.assertEquals(raObjs.size(), 2);
    //         Rental_Apply__c raObj = raObjs[0];
    //         raObj.Status__c = FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Yi_Chu_Ku_Zhi_Shi.ordinal());
    //         update raObj;

    //         List<Asset> ass = [Select Id, Last_Reserve_RAES_Detail__c From Asset];
    //         System.assertEquals(ass.size(), 1);

    //         List<Rental_Apply_Equipment_Set_Detail__c> raesds = [Select Id From Rental_Apply_Equipment_Set_Detail__c];
    //         System.assertEquals(raesds.size(), 2);
    //         Test.startTest();
    //         List<Rental_Apply_Equipment_Set__c> raess = [SELECT Id
    //                                                        FROM Rental_Apply_Equipment_Set__c
    //                                                       WHERE Rental_Start_Date__c = toDay];
    //         Rental_Apply_Equipment_Set__c raess1 = raess[0];


    //         raesds = [SELECT Id, Asset__c FROM Rental_Apply_Equipment_Set_Detail__c
    //                    WHERE Rental_Apply_Equipment_Set__c = :raess1.Id];
    //         System.assertEquals(raesds.size(), 1);
    //         raesds[0].Asset__c = ass[0].Id;
    //         raesds[0].Queue_Number__c = 0;
    //         raesds[0].Queue_Day__c = Date.today();
    //         update raesds[0];

    //         raesds = [SELECT Id, Asset__c, RAESD_Status__c
    //                     FROM Rental_Apply_Equipment_Set_Detail__c
    //                    WHERE Rental_Apply_Equipment_Set__c = :raess1.Id];
    //         System.assertEquals(raesds.size(), 1);
    //         System.assertEquals(FixtureUtil.raesdStatusMap.get(FixtureUtil.HistoryStatus.Zan_Ding_Fen_Pei.ordinal()) ,raesds[0].RAESD_Status__c);

    //         List<Asset> aset = [select Id from Asset where Loaner_accsessary__c = false];
    //         System.assertEquals(aset.size(), 1);

    //         List<Account> hospitals = [SELECT Id
    //                                  FROM Account
    //                                 WHERE Name = 'test hospital'];
    //         System.assertEquals(1, hospitals.size());

    //         // 戦略科室を得る
    //         Account[] strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospitals[0].Id AND recordType.DeveloperName = 'Department_Class_OTH'];

    //         List<Account> deps = [SELECT Id
    //                                      FROM Account
    //                                     WHERE AgentCode_Ext__c = '9999998'];
    //         System.assertEquals(1, deps.size());

    //         // 修理を作成する
    //         Repair__c repair = new Repair__c();
    //         repair.Account__c = deps[0].Id;
    //         repair.Department_Class__c = strategicDep[0].Id;
    //         repair.Hospital__c = hospitals[0].Id;
    //         repair.Delivered_Product__c = aset[0].Id;
    //         insert repair;

    //         Date d1 = Date.today().addDays(1);
    //         raess = [Select Id From Rental_Apply_Equipment_Set__c Where Rental_Start_Date__c = :d1];
    //         Rental_Apply_Equipment_Set__c raess2 = raess[0];
    //         System.assertEquals(raess.size(), 1);
    //         PageReference page1 = new PageReference('/apex/MainFixtureSelect?pt_recid=' + raess[0].Id);
    //         System.Test.setCurrentPage(page1);
    //         MainFixtureSelectController mh = new MainFixtureSelectController();
    //         CreateRelationListPagingCmpCtrl cmp = new CreateRelationListPagingCmpCtrl();
    //         cmp.pageController = mh;
    //         mh.myComponentController.init();
    //         mh.bieCunFangDi = '北京 备品中心';
    //         mh.bieBenBu = '1.华北营业本部';
    //         mh.bieChanPinFenLei = 'GI';
    //         mh.bieBeiPinFenLei = '动物试验用';
    //         mh.searchOpp();
    //         mh.init();
    //         System.assertEquals(mh.viewList.size(), 1);
    //         Rental_Apply_Equipment_Set_Detail__c robj = (Rental_Apply_Equipment_Set_Detail__c) mh.viewList[0].sobj;
    //         System.assertEquals(robj.Asset__c, ass[0].Id);
    //         mh.viewList[0].check = true;
    //         MainFixtureSelectController.queue(raess[0].Id, ass[0].Id);

    //         raesds = [Select Id, Asset__c, Queue_Day__c, Queue_Number__c From Rental_Apply_Equipment_Set_Detail__c Where Rental_Apply_Equipment_Set__c = :raess[0].Id];
    //         System.assertEquals(raesds.size(), 1);
    //         System.assertEquals(raesds[0].Queue_Day__c, Date.today());
    //         System.assertEquals(raesds[0].Queue_Number__c, 1);

    //         raesds = [Select Id, Asset__c, Queue_Day__c, Queue_Number__c From Rental_Apply_Equipment_Set_Detail__c Where Rental_Apply_Equipment_Set__c = :raess1.Id];
    //         raesds[0].Cancel_Select__c = true;
    //         raesds[0].Cancel_Reason__c = '主动取消';
    //         update raesds;


    //         raesds = [Select Id, Asset__c, Queue_Day__c, Queue_Number__c From Rental_Apply_Equipment_Set_Detail__c Where Rental_Apply_Equipment_Set__c = :raess2.Id];
    //         System.assertEquals(1, raesds.size());
    //         System.assertEquals(Date.today(), raesds[0].Queue_Day__c);
    //         System.assertEquals(1, raesds[0].Queue_Number__c);
    //         System.assertEquals(null, raesds[0].Asset__c);

    //         ass = [Select Id, Last_Reserve_RAES_Detail__c From Asset Where id =:aa.Id];
    //         System.assertEquals(null, ass[0].Last_Reserve_RAES_Detail__c);
    //     }
    // }

    /**
     * Insert Asset 找不到 Account
     * 做 Account, 做 Group, 设定 AccountShare
     */
    @isTest
    static void beforeSetAccountId_InsertAsset_FeatureInsertAcc() {
        // 做数据
        setupAccount(new List<String> {});

        //  Olympus社内
        List<Account> olympusAccount = [SELECT Id
                 FROM Account
                WHERE Name LIKE 'Olympus社内%'
                ORDER BY Name];

        AssetHandler.testTargetDepts = new Set<String> {'1.华北Test', '4.华东Test'};
        List<String> insertAccShareNameList = new List<String> {'备品共享_北京_华北Test', '备品共享_上海_华东Test'};

        // 产品
        Product2 pro5 = new Product2(Name='name05',IsActive=true,Family='SP',
                Fixture_Model_No__c='n05',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'n05',
                ProductCode_Ext__c='pc05',Manual_Entry__c=false);
        insert pro5;

        Asset assetC1 = new Asset(Asset_Owner__c = 'Olympus');
        assetC1.RecordTypeId = System.Label.Asset_RecordType;
        assetC1.AssetManageConfirm__c = true;
        assetC1.SerialNumber = 'assetC1';
        assetC1.Name = 'assetC1';
        assetC1.Hospital__c = olympusAccount[0].Id;
        assetC1.Department_Class__c = olympusAccount[1].Id;
        assetC1.AccountId = olympusAccount[2].Id;
        assetC1.Product2Id = pro5.Id;
        assetC1.Quantity = 1;
        assetC1.Status = '有库存';
        assetC1.Manage_type__c = '个体管理';
        assetC1.Internal_asset_location__c = '北京';
        assetC1.Salesdepartment__c = '1.华北Test';
        assetC1.Loaner_accsessary__c = true;
        assetC1.Delete_Flag__c = false;
        assetC1.Freeze_sign__c = false;
        assetC1.Out_of_wh__c = 0;

        Asset assetC2 = new Asset(Asset_Owner__c = 'Olympus');
        assetC2.RecordTypeId = System.Label.Asset_RecordType;
        assetC2.AssetManageConfirm__c = true;
        assetC2.SerialNumber = 'assetC2';
        assetC2.Name = 'assetC2';
        assetC2.Hospital__c = olympusAccount[0].Id;
        assetC2.Department_Class__c = olympusAccount[1].Id;
        assetC2.AccountId = olympusAccount[2].Id;
        assetC2.Product2Id = pro5.Id;
        assetC2.Quantity = 1;
        assetC2.Status = '有库存';
        assetC2.Manage_type__c = '个体管理';
        assetC2.Internal_asset_location__c = '上海';
        assetC2.Salesdepartment__c = '4.华东Test';
        assetC2.Loaner_accsessary__c = true;
        assetC2.Delete_Flag__c = false;
        assetC2.Freeze_sign__c = false;
        assetC2.Out_of_wh__c = 0;

        Test.startTest();
        insert new List<Asset> {assetC1, assetC2};
        Test.stopTest();

        List<Asset> astList = [SELECT Id, AccountId, OlympusAccShareName__c, Account.Department_Name__c FROM Asset ORDER BY SerialNumber];
        System.assertEquals(2, astList.size());
        System.assertEquals('备品共享_北京_华北Test', astList[0].OlympusAccShareName__c);
        System.assertEquals('备品共享_北京_华北Test', astList[0].Account.Department_Name__c);
        System.assertEquals('备品共享_上海_华东Test', astList[1].OlympusAccShareName__c);
        System.assertEquals('备品共享_上海_华东Test', astList[1].Account.Department_Name__c);

        Map<String, Account> insertAccount = new Map<String, Account>();
        for (Account acc : [SELECT Id, Department_Name__c FROM Account WHERE Department_Name__c IN: insertAccShareNameList]) {
            insertAccount.put(acc.Department_Name__c, acc);
        }
        System.assertEquals(2, insertAccount.size());

        astList = [SELECT Id, AccountId FROM Asset WHERE Account.Department_Name__c IN: insertAccShareNameList];
        System.assertEquals(2, astList.size());

        Map<String, Group> insertGroup = new Map<String, Group>();
        for (Group grp : [SELECT Id, Name FROM Group WHERE Type = 'Regular' AND Name IN: insertAccShareNameList]) {
            insertGroup.put(grp.Name, grp);
        }
        System.assertEquals(2, insertGroup.size());

        for (String accShareName : insertAccShareNameList) {
            AccountShare accShare = [SELECT AccountAccessLevel, UserOrGroupId, AccountId FROM AccountShare WHERE UserOrGroupId =: insertGroup.get(accShareName).Id AND AccountId =: insertAccount.get(accShareName).Id];
            System.assertEquals('Edit', accShare.AccountAccessLevel);
        }
    }

    /**
     * 做数据
     * Insert Asset 找到 Account
     * 只设定 AccountId  "Olympus社内 其他 备品" => "Olympus社内 其他 备品共享_XXX"
     */
    static List<Asset> beforeSetAccountId_InsertAsset_date(List<String> insertAccShareNameList) {
        setupAccount(insertAccShareNameList);

        Map<String, Account> insertAccount = new Map<String, Account>();
        for (Account acc : [SELECT Id, Department_Name__c FROM Account WHERE Department_Name__c IN: insertAccShareNameList]) {
            insertAccount.put(acc.Department_Name__c, acc);
        }
        System.assertEquals(insertAccShareNameList.size(), insertAccount.size());

        //  Olympus社内
        List<Account> olympusAccount = [SELECT Id
                 FROM Account
                WHERE Name LIKE 'Olympus社内%'
                ORDER BY Name];

        // 产品
        Product2 pro5 = new Product2(Name='name05',IsActive=true,Family='SP',
                Fixture_Model_No__c='n05',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'n05',
                ProductCode_Ext__c='pc05',Manual_Entry__c=false);
        insert pro5;
        pro5 = [SELECT Id, ProductCode_Ext__c FROM Product2 WHERE Fixture_Model_No__c = 'n05'];

        Asset assetC1 = new Asset(Asset_Owner__c = 'Olympus');
        assetC1.RecordTypeId = System.Label.Asset_RecordType;
        assetC1.AssetManageConfirm__c = true;
        assetC1.SerialNumber = 'assetC1';
        assetC1.Name = 'assetC1';
        assetC1.Hospital__c = olympusAccount[0].Id;
        assetC1.Department_Class__c = olympusAccount[1].Id;
        assetC1.AccountId = olympusAccount[2].Id;       // Olympus社内 其他 备品
        assetC1.Product2Id = pro5.Id;
        assetC1.Quantity = 1;
        assetC1.Status = '有库存';
        assetC1.Manage_type__c = '个体管理';
        assetC1.Internal_asset_location__c = '北京';
        assetC1.Salesdepartment__c = '1.华北Test';
        assetC1.Loaner_accsessary__c = true;
        assetC1.Delete_Flag__c = false;
        assetC1.Freeze_sign__c = false;
        assetC1.Out_of_wh__c = 0;

        Asset assetC2 = new Asset(Asset_Owner__c = 'Olympus');
        assetC2.RecordTypeId = System.Label.Asset_RecordType;
        assetC2.AssetManageConfirm__c = true;
        assetC2.SerialNumber = 'assetC2';
        assetC2.Name = 'assetC2';
        assetC2.Hospital__c = olympusAccount[0].Id;
        assetC2.Department_Class__c = olympusAccount[1].Id;
        assetC2.AccountId = olympusAccount[2].Id;       // Olympus社内 其他 备品
        assetC2.Product2Id = pro5.Id;
        assetC2.Quantity = 1;
        assetC2.Status = '有库存';
        assetC2.Manage_type__c = '个体管理';
        assetC2.Internal_asset_location__c = '上海';
        assetC2.Salesdepartment__c = '4.华东Test';
        assetC2.Loaner_accsessary__c = true;
        assetC2.Delete_Flag__c = false;
        assetC2.Freeze_sign__c = false;
        assetC2.Out_of_wh__c = 0;

        return new List<Asset> {assetC1, assetC2};
    }

    /**
     * Insert Asset 找到 Account
     * 只设定 AccountId  "Olympus社内 其他 备品" => "Olympus社内 其他 备品共享_XXX"
     */
    @isTest
    static void beforeSetAccountId_InsertAsset() {
        // 做数据
        List<String> insertAccShareNameList = new List<String> {'备品共享_北京_华北Test', '备品共享_上海_华东Test'};
        List<Asset> insAssetList = beforeSetAccountId_InsertAsset_date(insertAccShareNameList);

        Test.startTest();
        AssetHandler.testTargetDepts = new Set<String> {'1.华北Test', '4.华东Test'};
        insert insAssetList;
        Test.stopTest();

        List<Asset> astList = [SELECT Id, AccountId, OlympusAccShareName__c, Account.Department_Name__c FROM Asset ORDER BY  SerialNumber];
        System.assertEquals(2, astList.size());
        System.assertEquals('备品共享_北京_华北Test', astList[0].OlympusAccShareName__c);
        System.assertEquals('备品共享_北京_华北Test', astList[0].Account.Department_Name__c);
        System.assertEquals('备品共享_上海_华东Test', astList[1].OlympusAccShareName__c);
        System.assertEquals('备品共享_上海_华东Test', astList[1].Account.Department_Name__c);
    }

    /**
     * Insert Asset 找到 Account
     * 只设定 AccountId  "Olympus社内 其他 备品"
     */
    @isTest
    static void beforeSetAccountId_InsertAsset0() {
        // 做数据 testTargetDepts を指定しない "备品" に
        // List<String> insertAccShareNameList = new List<String> {'备品共享_北京_华北Test', '备品共享_上海_华东Test'};
        List<Asset> insAssetList = beforeSetAccountId_InsertAsset_date(new List<String>());

        Test.startTest();
        insert insAssetList;
        Test.stopTest();

        List<Asset> astList = [SELECT Id, AccountId, OlympusAccShareName__c, Account.Department_Name__c FROM Asset ORDER BY  SerialNumber];
        System.assertEquals(2, astList.size());
        System.assertEquals(null, astList[0].OlympusAccShareName__c);
        System.assertEquals('备品', astList[0].Account.Department_Name__c);
        System.assertEquals(null, astList[1].OlympusAccShareName__c);
        System.assertEquals('备品', astList[1].Account.Department_Name__c);
    }

    /**
     * Update Asset 找不到 Account
     * 做 Account, 做 Group, 设定 AccountShare
     */
    @isTest
    static void beforeSetAccountId_UpdateAsset_FeatureInsertAcc() {
        // 做数据
        List<String> dataAccShareNameList = new List<String> {'备品共享_北京_华北Test', '备品共享_上海_华东Test'};
        List<Asset> insAssetList = beforeSetAccountId_InsertAsset_date(dataAccShareNameList);
        AssetHandler.testTargetDepts = new Set<String> {'1.华北Test', '4.华东Test'};
        insert insAssetList;

        //  Olympus社内
        List<Account> olympusAccount = [SELECT Id
                 FROM Account
                WHERE Name LIKE 'Olympus社内%'
                ORDER BY Name];

        AssetHandler.testTargetDepts = new Set<String> {'2.东北Test', '3.西北Test'};
        List<String> insertAccShareNameList = new List<String> {'备品共享_黑龙江_东北Test', '备品共享_宁夏_西北Test'};

        // 产品
        Product2 pro5 = [SELECT Id, ProductCode_Ext__c FROM Product2 WHERE Fixture_Model_No__c = 'n05'];

        Asset assetC1 = [SELECT Id FROM Asset WHERE SerialNumber='assetC1'];
        assetC1.Internal_asset_location__c = '哈尔滨';
        assetC1.Salesdepartment__c = '2.东北Test';

        Asset assetC2 = [SELECT Id FROM Asset WHERE SerialNumber='assetC2'];
        assetC2.Internal_asset_location__c = '银川';
        assetC2.Salesdepartment__c = '3.西北Test';

        Test.startTest();
        update new List<Asset> {assetC1, assetC2};
        Test.stopTest();

        List<Asset> astList = [SELECT Id, AccountId, OlympusAccShareName__c, Account.Department_Name__c FROM Asset ORDER BY SerialNumber];
        System.assertEquals(2, astList.size());
        System.assertEquals('备品共享_黑龙江_东北Test', astList[0].OlympusAccShareName__c);
        System.assertEquals('备品共享_黑龙江_东北Test', astList[0].Account.Department_Name__c);
        System.assertEquals('备品共享_宁夏_西北Test', astList[1].OlympusAccShareName__c);
        System.assertEquals('备品共享_宁夏_西北Test', astList[1].Account.Department_Name__c);

        Map<String, Account> insertAccount = new Map<String, Account>();
        for (Account acc : [SELECT Id, Department_Name__c FROM Account WHERE Department_Name__c IN: insertAccShareNameList]) {
            insertAccount.put(acc.Department_Name__c, acc);
        }
        System.assertEquals(2, insertAccount.size());

        astList = [SELECT Id, AccountId FROM Asset WHERE Account.Department_Name__c IN: insertAccShareNameList];
        System.assertEquals(2, astList.size());

        Map<String, Group> insertGroup = new Map<String, Group>();
        for (Group grp : [SELECT Id, Name FROM Group WHERE Type = 'Regular' AND Name IN: insertAccShareNameList]) {
            insertGroup.put(grp.Name, grp);
        }
        System.assertEquals(2, insertGroup.size());

        for (String accShareName : insertAccShareNameList) {
            AccountShare accShare = [SELECT AccountAccessLevel, UserOrGroupId, AccountId FROM AccountShare WHERE UserOrGroupId =: insertGroup.get(accShareName).Id AND AccountId =: insertAccount.get(accShareName).Id];
            System.assertEquals('Edit', accShare.AccountAccessLevel);
        }
    }

    /**
     * Update Asset 找到 Account
     */
    @isTest
    static void beforeSetAccountId_UpdateAsset() {
        // 做数据
        List<String> dataAccShareNameList = new List<String> {'备品共享_北京_华北Test', '备品共享_上海_华东Test'};
        List<Asset> insAssetList = beforeSetAccountId_InsertAsset_date(dataAccShareNameList);
        AssetHandler.testTargetDepts = new Set<String> {'1.华北Test', '4.华东Test'};
        insert insAssetList;
        // 做数据 的 assert
        List<Asset> astList = [SELECT Id, AccountId, OlympusAccShareName__c, Account.Department_Name__c FROM Asset ORDER BY  SerialNumber];
        System.assertEquals(2, astList.size());
        System.assertEquals('备品共享_北京_华北Test', astList[0].OlympusAccShareName__c);
        System.assertEquals('备品共享_北京_华北Test', astList[0].Account.Department_Name__c);
        System.assertEquals('备品共享_上海_华东Test', astList[1].OlympusAccShareName__c);
        System.assertEquals('备品共享_上海_华东Test', astList[1].Account.Department_Name__c);

        //  Olympus社内
        List<Account> olympusAccount = [SELECT Id
                 FROM Account
                WHERE Name LIKE 'Olympus社内%'
                ORDER BY Name];

        // 产品
        Product2 pro5 = [SELECT Id, ProductCode_Ext__c FROM Product2 WHERE Fixture_Model_No__c = 'n05'];

        Asset assetC1 = [SELECT Id FROM Asset WHERE SerialNumber='assetC1'];
        assetC1.Internal_asset_location__c = '上海';
        assetC1.Salesdepartment__c = '4.华东Test';

        Asset assetC2 = [SELECT Id FROM Asset WHERE SerialNumber='assetC2'];
        assetC2.Internal_asset_location__c = '北京';
        assetC2.Salesdepartment__c = '1.华北Test';

        Test.startTest();
        update new List<Asset> {assetC1, assetC2};
        Test.stopTest();

        astList = [SELECT Id, AccountId, OlympusAccShareName__c, Account.Department_Name__c FROM Asset ORDER BY SerialNumber];
        System.assertEquals(2, astList.size());
        System.assertEquals('备品共享_上海_华东Test', astList[0].OlympusAccShareName__c);
        System.assertEquals('备品共享_上海_华东Test', astList[0].Account.Department_Name__c);
        System.assertEquals('备品共享_北京_华北Test', astList[1].OlympusAccShareName__c);
        System.assertEquals('备品共享_北京_华北Test', astList[1].Account.Department_Name__c);
    }

    /**
     * Update Asset 到 '0.备品中心'
     */
    @isTest
    static void beforeSetAccountId_UpdateAssetTo0() {
        // 做数据
        List<String> dataAccShareNameList = new List<String> {'备品共享_北京_华北Test', '备品共享_上海_华东Test'};
        List<Asset> insAssetList = beforeSetAccountId_InsertAsset_date(dataAccShareNameList);
        AssetHandler.testTargetDepts = new Set<String> {'1.华北Test', '4.华东Test'};
        insert insAssetList;
        // 做数据 的 assert
        List<Asset> astList = [SELECT Id, AccountId, OlympusAccShareName__c, Account.Department_Name__c FROM Asset ORDER BY  SerialNumber];
        System.assertEquals(2, astList.size());
        System.assertEquals('备品共享_北京_华北Test', astList[0].OlympusAccShareName__c);
        System.assertEquals('备品共享_北京_华北Test', astList[0].Account.Department_Name__c);
        System.assertEquals('备品共享_上海_华东Test', astList[1].OlympusAccShareName__c);
        System.assertEquals('备品共享_上海_华东Test', astList[1].Account.Department_Name__c);

        //  Olympus社内
        List<Account> olympusAccount = [SELECT Id
                 FROM Account
                WHERE Name LIKE 'Olympus社内%'
                ORDER BY Name];

        // 产品
        Product2 pro5 = [SELECT Id, ProductCode_Ext__c FROM Product2 WHERE Fixture_Model_No__c = 'n05'];

        Asset assetC1 = [SELECT Id FROM Asset WHERE SerialNumber='assetC1'];
        assetC1.Internal_asset_location__c = '上海';
        assetC1.Salesdepartment__c = '0.备品中心';

        Asset assetC2 = [SELECT Id FROM Asset WHERE SerialNumber='assetC2'];
        assetC2.Internal_asset_location__c = '北京';
        assetC2.Salesdepartment__c = '0.备品中心';

        Test.startTest();
        update new List<Asset> {assetC1, assetC2};
        Test.stopTest();

        astList = [SELECT Id, Salesdepartment__c, OlympusAccShareName__c, Account.Department_Name__c FROM Asset ORDER BY SerialNumber];
        System.assertEquals(2, astList.size());
        System.assertEquals('0.备品中心', astList[0].Salesdepartment__c);
        System.assert(String.isBlank(astList[0].OlympusAccShareName__c), 'OlympusAccShareName__c Expected isBlank, Actual:' + astList[0].OlympusAccShareName__c);
        System.assertEquals('备品', astList[0].Account.Department_Name__c);
        System.assertEquals('0.备品中心', astList[1].Salesdepartment__c);
        System.assert(String.isBlank(astList[1].OlympusAccShareName__c), 'OlympusAccShareName__c Expected isBlank, Actual:' + astList[1].OlympusAccShareName__c);
        System.assertEquals('备品', astList[1].Account.Department_Name__c);
    }

    /**
     * Clone Asset 到 '备品' 中心共享
     */
    @isTest
    static void beforeSetAccountId_CloneAssetTo0() {
        // 做数据
        List<String> dataAccShareNameList = new List<String> {'备品共享_北京_华北Test', '备品共享_上海_华东Test'};
        List<Asset> insAssetList = beforeSetAccountId_InsertAsset_date(dataAccShareNameList);
        AssetHandler.testTargetDepts = new Set<String> {'1.华北Test', '4.华东Test'};
        insert insAssetList;
        // 做数据 的 assert
        List<Asset> astList = [SELECT Id, AccountId, OlympusAccShareName__c, Account.Department_Name__c FROM Asset ORDER BY  SerialNumber];
        System.assertEquals(2, astList.size());
        System.assertEquals('备品共享_北京_华北Test', astList[0].OlympusAccShareName__c);
        System.assertEquals('备品共享_北京_华北Test', astList[0].Account.Department_Name__c);
        System.assertEquals('备品共享_上海_华东Test', astList[1].OlympusAccShareName__c);
        System.assertEquals('备品共享_上海_华东Test', astList[1].Account.Department_Name__c);

        //  Olympus社内
        List<Account> olympusAccount = [SELECT Id
                 FROM Account
                WHERE Name LIKE 'Olympus社内%'
                ORDER BY Name];

        // 产品
        Product2 pro5 = [SELECT Id, ProductCode_Ext__c FROM Product2 WHERE Fixture_Model_No__c = 'n05'];

        Asset assetC1 = [SELECT Id, RecordTypeId
                     , Name
                     , AccountId
                     , Quantity
                     , Status
                     , Manage_type__c
                     , Abandoned_Inventory__c
                     , Abandoned_RealThing__c
                     , CountForRepair__c
                     , Confirm_Lost_Count__c
                     , Consumable_Guaranteen_end__c
                     , Fixture_Model_No_F__c
                     , SerialNumber
                     , Asset_loaner_category__c
                     , Product2Id
                     , unknow_serial_NO_product__c
                     , Product_Serial_No__c
                     , AssetMark__c
                     , Internal_asset_location__c
                     , Salesdepartment__c
                     , SalesProvince__c
                     , Equipment_Type__c
                     , EquipmentSet_Managment_Code__c
                     , WH_location__c
                     , TransferFrozenQuantity__c
                     , TransferFrozenAbandon__c
                     , TransferFrozenRepair__c
                     , TransferFrozenLost__c
                 FROM Asset WHERE SerialNumber='assetC1'];
        assetC1 = assetC1.clone(false);
        assetC1.Internal_asset_location__c = '上海';
        assetC1.Salesdepartment__c = '0.备品中心';
        assetC1.unknow_serial_NO_product__c = true;
        assetC1.SerialNumber = 'assetC1_c';
        assetC1.Product_Serial_No__c = 'assetC1_c';
        assetC1.Loaner_accsessary__c = true;
        assetC1.AssetManageConfirm__c = true;
        assetC1.Abandoned_RealThing__c = 0;
        assetC1.Abandoned_Inventory__c = 0;
        assetC1.Frozen_Quantity__c = 0;
        assetC1.Out_of_wh__c = 0;
        assetC1.Rental_Count__c = 0;
        assetC1.MaintainCount_For_Processing__c = 0;
        assetC1.CountForRepair__c = 0;
        assetC1.Consumed_Count__c = 0;
        assetC1.TransferFrozenQuantity__c = 0;
        assetC1.TransferFrozenAbandon__c = 0;
        assetC1.TransferFrozenLost__c = 0;
        assetC1.TransferFrozenRepair__c = 0;
        assetC1.Confirm_Lost_Count__c = 0;
        assetC1.Inventory_Frozen_Quantity__c = 0;
        assetC1.Appended_Inventory_Frozen_Quantity__c = 0;
        assetC1.Inventory_Profit_Quantity__c = 0;
        assetC1.Appended_Inventory_Profit_Quantity__c = 0;

        Asset assetC2 = [SELECT Id, RecordTypeId
                     , Name
                     , AccountId
                     , Quantity
                     , Status
                     , Manage_type__c
                     , Abandoned_Inventory__c
                     , Abandoned_RealThing__c
                     , CountForRepair__c
                     , Confirm_Lost_Count__c
                     , Consumable_Guaranteen_end__c
                     , Fixture_Model_No_F__c
                     , SerialNumber
                     , Asset_loaner_category__c
                     , Product2Id
                     , unknow_serial_NO_product__c
                     , Product_Serial_No__c
                     , AssetMark__c
                     , Internal_asset_location__c
                     , Salesdepartment__c
                     , SalesProvince__c
                     , Equipment_Type__c
                     , EquipmentSet_Managment_Code__c
                     , WH_location__c
                     , TransferFrozenQuantity__c
                     , TransferFrozenAbandon__c
                     , TransferFrozenRepair__c
                     , TransferFrozenLost__c
                 FROM Asset WHERE SerialNumber='assetC2'];
        assetC2 = assetC2.clone(false);
        assetC2.Internal_asset_location__c = '北京';
        assetC2.Salesdepartment__c = '0.备品中心';
        assetC2.unknow_serial_NO_product__c = true;
        assetC2.SerialNumber = null;
        assetC2.Product_Serial_No__c = null; // workflow会重新赋值
        assetC2.Loaner_accsessary__c = true;
        assetC2.AssetManageConfirm__c = true;
        assetC2.Abandoned_RealThing__c = 0;
        assetC2.Abandoned_Inventory__c = 0;
        assetC2.Frozen_Quantity__c = 0;
        assetC2.Out_of_wh__c = 0;
        assetC2.Rental_Count__c = 0;
        assetC2.MaintainCount_For_Processing__c = 0;
        assetC2.CountForRepair__c = 0;
        assetC2.Consumed_Count__c = 0;
        assetC2.TransferFrozenQuantity__c = 0;
        assetC2.TransferFrozenAbandon__c = 0;
        assetC2.TransferFrozenLost__c = 0;
        assetC2.TransferFrozenRepair__c = 0;
        assetC2.Confirm_Lost_Count__c = 0;
        assetC2.Inventory_Frozen_Quantity__c = 0;
        assetC2.Appended_Inventory_Frozen_Quantity__c = 0;
        assetC2.Inventory_Profit_Quantity__c = 0;
        assetC2.Appended_Inventory_Profit_Quantity__c = 0;

        Test.startTest();
        insert new List<Asset> {assetC1, assetC2};
        Test.stopTest();

        astList = [SELECT Id, Salesdepartment__c, OlympusAccShareName__c, Account.Department_Name__c
                 FROM Asset
                WHERE SerialNumber != 'assetC1'
                  AND SerialNumber != 'assetC2'
                ORDER BY SerialNumber];
        System.assertEquals(2, astList.size(), astList);
        System.assertEquals('0.备品中心', astList[0].Salesdepartment__c);
        System.assert(String.isBlank(astList[0].OlympusAccShareName__c), 'OlympusAccShareName__c Expected isBlank, Actual:' + astList[0].OlympusAccShareName__c);
        System.assertEquals('备品', astList[0].Account.Department_Name__c);
        System.assertEquals('0.备品中心', astList[1].Salesdepartment__c);
        System.assert(String.isBlank(astList[1].OlympusAccShareName__c), 'OlympusAccShareName__c Expected isBlank, Actual:' + astList[1].OlympusAccShareName__c);
        System.assertEquals('备品', astList[1].Account.Department_Name__c);
    }

    @isTest
    static void futureInsertAccShare() {
        // 做数据
        setupAccount(new List<String> {});

        Test.startTest();
        Datetime execNow = System.now();
        List<String> insertAccShareNameList = new List<String> {'备品共享_北京_华北Test', '备品共享_上海_华东Test'};
        System.enqueueJob(new AssetHandler.MyQueueableClass(JSON.serialize(insertAccShareNameList), execNow));
        Test.stopTest();

        Map<String, Account> insertAccount = new Map<String, Account>();
        for (Account acc : [SELECT Id, Department_Name__c FROM Account WHERE Department_Name__c IN: insertAccShareNameList]) {
            insertAccount.put(acc.Department_Name__c, acc);
        }
        System.assertEquals(2, insertAccount.size());

        Map<String, Group> insertGroup = new Map<String, Group>();
        for (Group grp : [SELECT Id, Name FROM Group WHERE Type = 'Regular' AND Name IN: insertAccShareNameList]) {
            insertGroup.put(grp.Name, grp);
        }
        System.assertEquals(2, insertGroup.size());

        for (String accShareName : insertAccShareNameList) {
            AccountShare accShare = [SELECT AccountAccessLevel, UserOrGroupId, AccountId FROM AccountShare WHERE UserOrGroupId =: insertGroup.get(accShareName).Id AND AccountId =: insertAccount.get(accShareName).Id];
            System.assertEquals('Edit', accShare.AccountAccessLevel);
        }
    }

    // Olympus社内 的 数据
    private static void setupAccount(List<String> othList) {
        ControllerUtil.EscapeNFM001Trigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        StaticParameter.EscapeAccountTrigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;

        System.runAs(new User(Id = Userinfo.getUserId())) {
            // 省
            Address_Level__c al1 = new Address_Level__c();
            al1.Name = '東京';
            al1.Level1_Code__c = 'CN-99';
            al1.Level1_Sys_No__c = '999999';
            insert al1;
            // 市
            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 = al1.id;
            insert al2;

            // Olympus社内 9999999
            Account hospital = new Account();
            hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
            hospital.Name = 'Olympus社内';
            hospital.AgentCode_Ext__c = System.Label.Account_Asset_Hospatal;
            hospital.Is_Active__c = '有効';
            hospital.Attribute_Type__c = '其他';
            hospital.Speciality_Type__c = 'その他';
            hospital.Grade__c = '一般';
            hospital.OCM_Category__c = 'L';
            hospital.Is_Medical__c = '非医疗机构';
            hospital.State_Master__c = al1.id;
            hospital.City_Master__c = al2.id;
            hospital.Town__c = '东京';
            insert hospital;

            // 戦略科室を得る
            Account strategicDep = new Account();
            strategicDep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_Class_OTH'].id;
            strategicDep.Name = 'Olympus社内 其他';
            strategicDep.Department_Class_Label__c = '其他';
            strategicDep.Hospital__c = hospital.Id;
            strategicDep.ParentId = hospital.Id;
            insert strategicDep;

            // 备品中心:9999998
            List<Account> departmentList = new List<Account>();
            Account accountDep = new Account();
            accountDep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
            accountDep.Name = 'Olympus社内 其他 备品';
            accountDep.AgentCode_Ext__c = System.Label.Account_Asset;
            accountDep.ParentId = strategicDep.Id;
            accountDep.Department_Class__c = strategicDep.Id;
            accountDep.Department_Name__c = '备品';
            accountDep.Hospital__c = hospital.Id;
            departmentList.add(accountDep);

            for (String dptName : othList) {
                Account accountOth = new Account(
                    recordtypeId = accountDep.recordtypeId,
                    Name = 'Olympus社内 其他 ' + dptName,
                    ParentId = strategicDep.Id,
                    Department_Class__c = strategicDep.Id,
                    Department_Name__c = dptName,
                    Hospital__c = hospital.Id
                );
                departmentList.add(accountOth);
            }
            insert departmentList;
        }
    }
}