@isTest
private class SpareIsLoanBatchTest {
    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
    @isTest
	static  void setupTestData() {
        
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        ControllerUtil.EscapeNFM001Trigger = true;
		Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
		
        List<RecordType> rectOPD = [select Id,name from RecordType where IsActive = true and SobjectType = 'Report__c' and Name = '跟台'];
        if (rectOPD.size() == 0) {
            return;
        }

        
		
        Date today = Date.today();
        Date lastDate1 = Date.today().addMonths(1);
        Date lastDate2 = Date.today().addMonths(2);
        Date lastDate3 = Date.today().addMonths(3);

        // 省
        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'];
        // 診療科を作る Department_Class_OTH
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
        dep.Name = 'test dep';
        dep.AgentCode_Ext__c = '9999998';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;

        Opportunity opp = new Opportunity();
        opp.AccountId           = dep.Id;
        opp.Department_Class__c = dep.Id;
        opp.Hospital__c         = hospital.Id;
        opp.SAP_Send_OK__c      = false;
        opp.Name                = 'GZ-SP-NFM009_0';
        opp.Trade__c            = '内貿';
        opp.StageName           = '引合';
        opp.CloseDate           = today.addDays(22);
        opp.Close_Forecasted_Date__c = lastDate1;
        opp.Stock_apply_status__c = '批准';
        insert opp;
        // 产品
        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 = 'CV290',Asset_Model_No__c = 'CV290',
                ProductCode_Ext__c='pc05',Manual_Entry__c=false);
        insert pro5;

        PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=pro5.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = 'CNY';
        insert entry;


        OpportunityLineItem oppli = new OpportunityLineItem();
        oppli.OpportunityId = opp.Id;
        oppli.Id__c = '110';
        oppli.SFDA_Status__c = '有効';
        oppli.Name__c = '一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十';
        oppli.ListPrice__c = 112;
        oppli.Quantity = 113;
        oppli.UnitPrice = 114;
        oppli.UnitPrice__c = 115;
        oppli.Qty_Unit__c = '116';
        oppli.Cost__c = 117;
        oppli.BSS_Category__c = 'G&R';
        oppli.Item_Order__c = 1;
        oppli.PricebookEntryId = entry.Id;
        insert oppli;
        
        OPDPlan__c oPDPlan = new OPDPlan__c();
        oPDPlan.Status__c = '计划中';
        oPDPlan.OPDPlan_ImplementDate__c = Date.today().addDays(5);
        oPDPlan.NoOpp_Reason__c = 'HCP对应';
        oPDPlan.Rental_Apply2__c =null;
        oPDPlan.Rental_Apply1__c =null;
        oPDPlan.Rental_Apply3__c = null;
        insert oPDPlan;
        System.Test.StartTest();
        // 备品配套(只有附属品)
        Fixture_Set__c fsObjC1 = new Fixture_Set__c();
        fsObjC1.Name = 'setC1';
        fsObjC1.Fixture_Set_Body_Model_No__c = 'modelNoC1';
        fsObjC1.Loaner_name__c = 'nameC1';
        insert fsObjC1;
        
        //备品借出申请
        Rental_Apply__c raObj = new Rental_Apply__c();
        raObj.Name = 'testra';
        raObj.OPDPlan__c = oPDPlan.Id;
        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.Follow_UP_Opp__c = opp.Id;
        raObj.Status_Text__c = '已批准';
        // 2023-01-06 验证规则跳过，需要改成有询价 ssm
        raObj.demo_purpose2__c = FixtureUtil.raDemo_purpose2MAP.get('shiyongyouxunjia');
        raObj.direct_send__c = '医疗机构';
        insert raObj;

        Rental_Apply_Equipment_Set__c raesObjC1 = new Rental_Apply_Equipment_Set__c();
        raesObjC1.Rental_Apply__c = raObj.Id;
        raesObjC1.Fixture_Set__c = fsObjC1.Id;
        raesObjC1.Cancel_Select__c = false;
        raesObjC1.Shipment_request_time__c = Date.toDay();
        raesObjC1.Rental_Start_Date__c = Date.toDay();
        raesObjC1.Rental_End_Date__c = Date.toDay();
        raesObjC1.IndexFromUniqueKey__c = 1;
        raesObjC1.UniqueKey__c = '1:'+ fsObjC1.Id + ':1';
        insert raesObjC1;

        // 保有设备
        Asset asset = new Asset(Asset_Owner__c = 'Olympus');
        asset.RecordTypeId = System.Label.Asset_RecordType;
        asset.SerialNumber = 'asset';
        asset.Name = 'asset';
        asset.AccountId = dep.Id;
        asset.Department_Class__c = strategicDep[0].Id;
        asset.Hospital__c = hospital.Id;
        asset.Product2Id = pro5.Id;
        asset.Quantity = 1;
        asset.Status = '有库存';
        asset.Manage_type__c = '个体管理';
        asset.Internal_asset_location__c = '北京 备品中心';
        asset.Loaner_accsessary__c = true;
        asset.Delete_Flag__c = false;
        asset.Freeze_sign__c = false;
        asset.Out_of_wh__c = 0;
        insert new Asset[] {asset};

        // 备品配套明细(只有附属品)
        Fixture_Set_Detail__c fsdObjC1 = new Fixture_Set_Detail__c();
        fsdObjC1.Name = 'detailAccessory';
        fsdObjC1.Name_CHN_Created__c = 'detailAccessoryName';
        fsdObjC1.Product2__c = pro5.Id;
        fsdObjC1.Fixture_Set__c = fsObjC1.Id;
        fsdObjC1.Is_Body__c = false;
        fsdObjC1.Is_Optional__c = true;
        fsdObjC1.UniqueKey__c = fsObjC1.Id + ':' + pro5.Id;
        fsdObjC1.SortInt__c = 1;
        // fsdObjC1.Fixture_Model_No_text__c = 'CV290';
        insert fsdObjC1;

        

        // 借出备品配套一览明细
        Rental_Apply_Equipment_Set_Detail__c raesdObjC1 = new Rental_Apply_Equipment_Set_Detail__c();
        raesdObjC1.Rental_Apply__c = raObj.Id;
        raesdObjC1.Fixture_Set_Detail__c = fsdObjC1.Id;
        raesdObjC1.Rental_Num__c = 1;
        raesdObjC1.Queue_Number__c = null;
        raesdObjC1.Is_Body__c = false;
        raesdObjC1.Rental_Apply_Equipment_Set__c = raesObjC1.Id;
        raesdObjC1.Asset__c = asset.Id;
        raesdObjC1.IndexFromUniqueKey__c = 1;
        raesdObjC1.UniqueKey__c = '1:'+ raesObjC1.Id + ':' + fsdObjC1.Id + ':1';
        raesdObjC1.Salesdepartment_before__c = '1.华北营业本部';
        raesdObjC1.Internal_asset_location_before__c = '北京 备品中心';
        raesdObjC1.Product_category_text__c = 'GI';
        raesdObjC1.Equipment_Type_text__c = '产品试用';
        raesdObjC1.Cancel_Select__c = true;
        raesdObjC1.Arrival_in_wh__c = false;
        raesdObjC1.Check_lost_Item_Final__c = '欠品';
        raesdObjC1.Check_lost_Item__c = '欠品';
        raesdObjC1.Lost_item_giveup__c = false;
        raesdObjC1.Cancel_Reason__c = '主动取消';
        raesdObjC1.Shippment_loaner_time__c = Date.toDay();
        // raesdObjC1.Fixture_Model_No_text__c = 'CV290';
        insert raesdObjC1;

        Daily_Report__c dr1 = new Daily_Report__c();
        dr1.Reporter__c = UserInfo.getUserId();
        dr1.Reported_Date__c = date.today();
        dr1.Status__c = '批准';
        
        insert dr1;

        Report__c rep1 = new Report__c();
        rep1.CurrencyIsoCode         = 'CNY';
        rep1.RecordTypeId            = rectOPD[0].Id;
        rep1.Status__c               = '申請中';
        rep1.Daily_Report__c         = dr1.Id;
        rep1.Purpose1__c             = 'OPD';
        rep1.Purpose2__c             = '引合追及活動';
        rep1.Reason__c               = '実施背景';
        rep1.Appeal_Point__c         = '実施目的・訴求点';
        rep1.Strategic_Department_Class__c = '1.消化科';
        rep1.Technical_Category1__c  = '消化道检查';
        rep1.Technical_Category2__c  = '普通胃镜检查';
        rep1.Patient_Status__c       = '患者状況';
        rep1.Operation_Process__c    = '手術プロセス';
        rep1.Operation_Task__c       = '手術上の課題';
        rep1.Product_Evaluation_Advantage__c = '製品評価利点';
        rep1.Product_Evaluation_Faults__c = '製品評価欠点';
        rep1.OPD_Summarize__c        = 'OPD実施総括';
        rep1.Activity_Plan__c        = '今後の活動計画';
        rep1.Opportunity_Situation__c = '引合無';
        rep1.OPD_ProductCategory1__c ='3D System';
        rep1.OPD_ProductCategory2__c ='OR Imaging Products';
        rep1.Hospital_Reference__c   = hospital.Id;
        rep1.Department_Class_Ref__c = strategicDep[0].Id;
        rep1.Hospital_Department__c  = dep.Id;
        rep1.Date__c                 = Date.today().addDays(-1);
        rep1.Loaner_request_no1__c = raObj.Id;
        rep1.Status__c = '批准';
        insert rep1;
        raObj.Status__c = '已批准';
        update raObj;
        
        List<String> rA_statusList = new List<String>();
        rA_statusList.add('已出库');rA_statusList.add('医院已装机确认');rA_statusList.add('已回寄');
        rA_statusList.add('欠品中');rA_statusList.add('申请者已收货');rA_statusList.add('已回收');
        rA_statusList.add('完了');
        List<Rental_Apply__c> raList =[Select Id,Follow_UP_Opp__c,RA_Status__c,ShelfUp_ng_num__c,Follow_UP_Opp__r.Spare_Application_Singular__c 
			from Rental_Apply__c where Follow_UP_Opp__c = :opp.Id ];
        System.assertEquals('完了', raList[0].RA_Status__c);
        System.assertEquals(1, raList.size());
        
        
        Id execBTId = null;
        List<String> oppIdList = new List<String>();
        oppIdList.add(opp.Id);
        execBTId = Database.executeBatch(new SpareIsLoanBatch(oppIdList));
        System.Test.StopTest(); 
        Opportunity  opp2 = [select Id,Spare_Application_Singular__c 
            from Opportunity where Id = :opp.Id];
        System.assertEquals(1, opp2.Spare_Application_Singular__c);
        
    
    }	
    @isTest
	static  void setupTestData1() {
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        ControllerUtil.EscapeNFM001Trigger = true;
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        
        List<RecordType> rectOPD = [select Id,name from RecordType where IsActive = true and SobjectType = 'Report__c' and Name = '跟台'];
        if (rectOPD.size() == 0) {
            return;
        }
        
        Date today = Date.today();
        Date lastDate1 = Date.today().addMonths(1);
        Date lastDate2 = Date.today().addMonths(2);
        Date lastDate3 = Date.today().addMonths(3);

        // 省
        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'];
        // 診療科を作る Department_Class_OTH
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
        dep.Name = 'test dep';
        dep.AgentCode_Ext__c = '9999998';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;

        Opportunity opp = new Opportunity();
        opp.AccountId           = dep.Id;
        opp.Department_Class__c = dep.Id;
        opp.Hospital__c         = hospital.Id;
        opp.SAP_Send_OK__c      = false;
        opp.Name                = 'GZ-SP-NFM009_0';
        opp.Trade__c            = '内貿';
        opp.StageName           = '引合';
        opp.CloseDate           = today.addDays(22);
        opp.Close_Forecasted_Date__c = lastDate1;
        opp.Stock_apply_status__c = '批准';
        opp.Spare_Application_Singular__c = 5;
        insert opp;

        

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

        OPDPlan__c oPDPlan = new OPDPlan__c();
        oPDPlan.Status__c = '计划中';
        oPDPlan.OPDPlan_ImplementDate__c = Date.today().addDays(5);
        oPDPlan.NoOpp_Reason__c = 'HCP对应';
        oPDPlan.Rental_Apply2__c =null;
        oPDPlan.Rental_Apply1__c =null;
        oPDPlan.Rental_Apply3__c = null;
        insert oPDPlan;
        Test.startTest();
        //备品借出申请
        Rental_Apply__c raObj = new Rental_Apply__c();
        raObj.Name = 'testra';
        raObj.OPDPlan__c = oPDPlan.Id;
        raObj.Product_category__c = 'GI';
        raObj.Demo_purpose1__c = '产品试用';
        // 2023-01-06 验证规则限制，改为有询价 ssm
        raObj.demo_purpose2__c = FixtureUtil.raDemo_purpose2MAP.get('shiyongyouxunjia');
        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.Follow_UP_Opp__c = opp.Id;
        raObj.Status_Text__c = '已批准';
        raObj.OPDAmount__c = 1;
        raObj.direct_send__c = '医疗机构';
        insert raObj;

        // 保有设备
        Asset asset = new Asset(Asset_Owner__c = 'Olympus');
        asset.RecordTypeId = System.Label.Asset_RecordType;
        asset.SerialNumber = 'asset';
        asset.Name = 'asset';
        asset.AccountId = dep.Id;
        asset.Department_Class__c = strategicDep[0].Id;
        asset.Hospital__c = hospital.Id;
        asset.Product2Id = pro5.Id;
        asset.Quantity = 1;
        asset.Status = '有库存';
        asset.Manage_type__c = '个体管理';
        asset.Internal_asset_location__c = '北京 备品中心';
        asset.Loaner_accsessary__c = true;
        asset.Delete_Flag__c = false;
        asset.Freeze_sign__c = false;
        asset.Out_of_wh__c = 0;
        insert new Asset[] {asset};

        // 备品配套(只有附属品)
        Fixture_Set__c fsObjC1 = new Fixture_Set__c();
        fsObjC1.Name = 'setC1';
        fsObjC1.Fixture_Set_Body_Model_No__c = 'modelNoC1';
        fsObjC1.Loaner_name__c = 'nameC1';
        insert fsObjC1;

        // 备品配套明细(只有附属品)
        Fixture_Set_Detail__c fsdObjC1 = new Fixture_Set_Detail__c();
        fsdObjC1.Name = 'detailAccessory';
        fsdObjC1.Name_CHN_Created__c = 'detailAccessoryName';
        fsdObjC1.Product2__c = pro5.Id;
        fsdObjC1.Fixture_Set__c = fsObjC1.Id;
        fsdObjC1.Is_Body__c = false;
        fsdObjC1.Is_Optional__c = true;
        fsdObjC1.UniqueKey__c = fsObjC1.Id + ':' + pro5.Id;
        fsdObjC1.SortInt__c = 1;
        insert fsdObjC1;

        // 借出备品配套一览
        Rental_Apply_Equipment_Set__c raesObjC1 = new Rental_Apply_Equipment_Set__c();
        raesObjC1.Rental_Apply__c = raObj.Id;
        raesObjC1.Fixture_Set__c = fsObjC1.Id;
        raesObjC1.Shipment_request_time__c = Date.toDay();
        raesObjC1.Rental_Start_Date__c = Date.toDay();
        raesObjC1.Rental_End_Date__c = Date.toDay();
        raesObjC1.IndexFromUniqueKey__c = 1;
        raesObjC1.UniqueKey__c = '1:'+ fsObjC1.Id + ':1';
        raesObjC1.Cancel_Reason__c = '主动取消';
        insert raesObjC1;

        // 借出备品配套一览明细
        Rental_Apply_Equipment_Set_Detail__c raesdObjC1 = new Rental_Apply_Equipment_Set_Detail__c();
        raesdObjC1.Rental_Apply__c = raObj.Id;
        raesdObjC1.Fixture_Set_Detail__c = fsdObjC1.Id;
        raesdObjC1.Rental_Num__c = 1;
        raesdObjC1.Queue_Number__c = null;
        raesdObjC1.Is_Body__c = false;
        raesdObjC1.Rental_Apply_Equipment_Set__c = raesObjC1.Id;
        raesdObjC1.Asset__c = asset.Id;
        raesdObjC1.IndexFromUniqueKey__c = 1;
        raesdObjC1.UniqueKey__c = '1:'+ raesObjC1.Id + ':' + fsdObjC1.Id + ':1';
        raesdObjC1.Salesdepartment_before__c = '1.华北营业本部';
        raesdObjC1.Internal_asset_location_before__c = '北京 备品中心';
        raesdObjC1.Product_category_text__c = 'GI';
        raesdObjC1.Equipment_Type_text__c = '产品试用';
        raesdObjC1.Cancel_Select__c = true;
        raesdObjC1.Arrival_in_wh__c = false;
        raesdObjC1.Check_lost_Item_Final__c = '欠品';
        raesdObjC1.Check_lost_Item__c = '欠品';
        raesdObjC1.Lost_item_giveup__c = false;
        raesdObjC1.Shippment_loaner_time__c = Date.toDay();
         raesdObjC1.Cancel_Reason__c = '主动取消';
        insert raesdObjC1;

        Daily_Report__c dr1 = new Daily_Report__c();
        dr1.Reporter__c = UserInfo.getUserId();
        dr1.Reported_Date__c = date.today();
        dr1.Status__c = '批准';
        
        insert dr1;

        Report__c rep1 = new Report__c();
        rep1.CurrencyIsoCode         = 'CNY';
        rep1.RecordTypeId            = rectOPD[0].Id;
        rep1.Status__c               = '申請中';
        rep1.Daily_Report__c         = dr1.Id;
        rep1.Purpose1__c             = 'OPD';
        rep1.Purpose2__c             = '引合追及活動';
        rep1.Reason__c               = '実施背景';
        rep1.Appeal_Point__c         = '実施目的・訴求点';
        rep1.Strategic_Department_Class__c = '1.消化科';
        rep1.Technical_Category1__c  = '消化道检查';
        rep1.Technical_Category2__c  = '普通胃镜检查';
        rep1.Patient_Status__c       = '患者状況';
        rep1.Operation_Process__c    = '手術プロセス';
        rep1.Operation_Task__c       = '手術上の課題';
        rep1.Product_Evaluation_Advantage__c = '製品評価利点';
        rep1.Product_Evaluation_Faults__c = '製品評価欠点';
        rep1.OPD_Summarize__c        = 'OPD実施総括';
        rep1.Activity_Plan__c        = '今後の活動計画';
        rep1.Opportunity_Situation__c = '引合無';
        rep1.OPD_ProductCategory1__c ='3D System';
        rep1.OPD_ProductCategory2__c ='OR Imaging Products';
        rep1.Hospital_Reference__c   = hospital.Id;
        rep1.Department_Class_Ref__c = strategicDep[0].Id;
        rep1.Hospital_Department__c  = dep.Id;
        rep1.Date__c                 = Date.today().addDays(-1);
        rep1.Loaner_request_no1__c = raObj.Id;
        rep1.Status__c = '批准';
        insert rep1;
        raObj.Status__c = '已批准';
        update raObj;
        
        List<String> rA_statusList = new List<String>();
        rA_statusList.add('已出库');rA_statusList.add('医院已装机确认');rA_statusList.add('已回寄');
        rA_statusList.add('欠品中');rA_statusList.add('申请者已收货');rA_statusList.add('已回收');
        rA_statusList.add('完了');
        List<Rental_Apply__c> raList =[Select Id,Follow_UP_Opp__c,RA_Status__c,ShelfUp_ng_num__c,Follow_UP_Opp__r.Spare_Application_Singular__c 
            from Rental_Apply__c where Follow_UP_Opp__c = :opp.Id ];
        
        System.assertEquals('完了', raList[0].RA_Status__c);
        System.assertEquals(1, raList.size());
        Id execBTId = null;
        // 2023-01-06 提高覆盖率 测试isneedexecute分支 ssm
        execBTId = Database.executeBatch(new SpareIsLoanBatch(true));
        Opportunity  opp2 = [select Id,Spare_Application_Singular__c 
            from Opportunity where Id = :opp.Id];
        System.assertEquals(5, opp2.Spare_Application_Singular__c);
    }	
    @isTest
	static  void setupTestData3() {
		StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        ControllerUtil.EscapeNFM001Trigger = true;
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        
        List<RecordType> rectOPD = [select Id,name from RecordType where IsActive = true and SobjectType = 'Report__c' and Name = '跟台'];
        if (rectOPD.size() == 0) {
            return;
        }
        
        Date today = Date.today();
        Date lastDate1 = Date.today().addMonths(1);
        Date lastDate2 = Date.today().addMonths(2);
        Date lastDate3 = Date.today().addMonths(3);

        // 省
        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'];
        // 診療科を作る Department_Class_OTH
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
        dep.Name = 'test dep';
        dep.AgentCode_Ext__c = '9999998';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;

        Opportunity opp = new Opportunity();
        opp.AccountId           = dep.Id;
        opp.Department_Class__c = dep.Id;
        opp.Hospital__c         = hospital.Id;
        opp.SAP_Send_OK__c      = false;
        opp.Name                = 'GZ-SP-NFM009_0';
        opp.Trade__c            = '内貿';
        opp.StageName           = '引合';
        opp.CloseDate           = today.addDays(22);
        opp.Close_Forecasted_Date__c = lastDate1;
        opp.Stock_apply_status__c = '批准';
        insert opp;
        // 产品
        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;

        OPDPlan__c oPDPlan = new OPDPlan__c();
        oPDPlan.Status__c = '计划中';
        oPDPlan.OPDPlan_ImplementDate__c = Date.today().addDays(5);
        oPDPlan.NoOpp_Reason__c = 'HCP对应';
        oPDPlan.Rental_Apply2__c =null;
        oPDPlan.Rental_Apply1__c =null;
        oPDPlan.Rental_Apply3__c = null;
        insert oPDPlan;
        System.Test.StartTest();
        //备品借出申请
        Rental_Apply__c raObj = new Rental_Apply__c();
        raObj.Name = 'testra';
        raObj.OPDPlan__c = oPDPlan.Id;

        raObj.OPDPlan__c = oPDPlan.Id;
        raObj.Product_category__c = 'GI';
        raObj.Demo_purpose1__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.Follow_UP_Opp__c = opp.Id;
        raObj.Status_Text__c = '已批准';
        // 2023-01-06 验证规则限制，需要改成有询价 ssm
        raObj.demo_purpose2__c = FixtureUtil.raDemo_purpose2MAP.get('shiyongyouxunjia');
        // raObj.Bollow_Date__c = Date.today();
        raObj.direct_send__c = '医疗机构';
        //raObj.Status__c = '已批准';
        raObj.OPDAmount__c = 1;
        insert raObj;

        // 保有设备
        Asset asset = new Asset(Asset_Owner__c = 'Olympus');
        asset.RecordTypeId = System.Label.Asset_RecordType;
        asset.SerialNumber = 'asset';
        asset.Name = 'asset';
        asset.AccountId = dep.Id;
        asset.Department_Class__c = strategicDep[0].Id;
        asset.Hospital__c = hospital.Id;
        asset.Product2Id = pro5.Id;
        asset.Quantity = 1;
        asset.Status = '有库存';
        asset.Manage_type__c = '个体管理';
        asset.Internal_asset_location__c = '北京 备品中心';
        asset.Loaner_accsessary__c = true;
        asset.Delete_Flag__c = false;
        asset.Freeze_sign__c = false;
        asset.Out_of_wh__c = 0;
        insert new Asset[] {asset};

        // 备品配套(只有附属品)
        Fixture_Set__c fsObjC1 = new Fixture_Set__c();
        fsObjC1.Name = 'setC1';
        fsObjC1.Fixture_Set_Body_Model_No__c = 'modelNoC1';
        fsObjC1.Loaner_name__c = 'nameC1';
        insert fsObjC1;

        // 备品配套明细(只有附属品)
        Fixture_Set_Detail__c fsdObjC1 = new Fixture_Set_Detail__c();
        fsdObjC1.Name = 'detailAccessory';
        fsdObjC1.Name_CHN_Created__c = 'detailAccessoryName';
        fsdObjC1.Product2__c = pro5.Id;
        fsdObjC1.Fixture_Set__c = fsObjC1.Id;
        fsdObjC1.Is_Body__c = false;
        fsdObjC1.Is_Optional__c = true;
        fsdObjC1.UniqueKey__c = fsObjC1.Id + ':' + pro5.Id;
        fsdObjC1.SortInt__c = 1;
        insert fsdObjC1;

        // 借出备品配套一览
        Rental_Apply_Equipment_Set__c raesObjC1 = new Rental_Apply_Equipment_Set__c();
        raesObjC1.Rental_Apply__c = raObj.Id;
        raesObjC1.Fixture_Set__c = fsObjC1.Id;
        //raesObjC1.Cancel_Select__c = false;
        raesObjC1.Shipment_request_time__c = Date.toDay();
        raesObjC1.Rental_Start_Date__c = Date.toDay();
        raesObjC1.Rental_End_Date__c = Date.toDay();
        raesObjC1.IndexFromUniqueKey__c = 1;
        raesObjC1.UniqueKey__c = '1:'+ fsObjC1.Id + ':1';
        raesObjC1.Cancel_Reason__c = '主动取消';
        insert raesObjC1;

        // 借出备品配套一览明细
        Rental_Apply_Equipment_Set_Detail__c raesdObjC1 = new Rental_Apply_Equipment_Set_Detail__c();
        raesdObjC1.Rental_Apply__c = raObj.Id;
        raesdObjC1.Fixture_Set_Detail__c = fsdObjC1.Id;
        raesdObjC1.Rental_Num__c = 1;
        raesdObjC1.Queue_Number__c = null;
        raesdObjC1.Is_Body__c = false;
        raesdObjC1.Rental_Apply_Equipment_Set__c = raesObjC1.Id;
        raesdObjC1.Asset__c = asset.Id;
        raesdObjC1.IndexFromUniqueKey__c = 1;
        raesdObjC1.UniqueKey__c = '1:'+ raesObjC1.Id + ':' + fsdObjC1.Id + ':1';
        raesdObjC1.Salesdepartment_before__c = '1.华北营业本部';
        raesdObjC1.Internal_asset_location_before__c = '北京 备品中心';
        raesdObjC1.Product_category_text__c = 'GI';
        raesdObjC1.Equipment_Type_text__c = '产品试用';
        raesdObjC1.Cancel_Select__c = true;
        raesdObjC1.Arrival_in_wh__c = false;
        raesdObjC1.Check_lost_Item_Final__c = '欠品';
        raesdObjC1.Check_lost_Item__c = '欠品';
        raesdObjC1.Lost_item_giveup__c = false;
        raesdObjC1.Shippment_loaner_time__c = Date.toDay();
        //raesdObjC1.Detail_Finish__c = false;
         raesdObjC1.Cancel_Reason__c = '主动取消';
        insert raesdObjC1;

        Daily_Report__c dr1 = new Daily_Report__c();
        dr1.Reporter__c = UserInfo.getUserId();
        dr1.Reported_Date__c = date.today();
        dr1.Status__c = '批准';
        
        insert dr1;

        Report__c rep1 = new Report__c();
        rep1.CurrencyIsoCode         = 'CNY';
        rep1.RecordTypeId            = rectOPD[0].Id;
        rep1.Status__c               = '申請中';
        rep1.Daily_Report__c         = dr1.Id;
        rep1.Purpose1__c             = 'OPD';
        rep1.Purpose2__c             = '引合追及活動';
        rep1.Reason__c               = '実施背景';
        rep1.Appeal_Point__c         = '実施目的・訴求点';
        rep1.Strategic_Department_Class__c = '1.消化科';
        rep1.Technical_Category1__c  = '消化道检查';
        rep1.Technical_Category2__c  = '普通胃镜检查';
        rep1.Patient_Status__c       = '患者状況';
        rep1.Operation_Process__c    = '手術プロセス';
        rep1.Operation_Task__c       = '手術上の課題';
        rep1.Product_Evaluation_Advantage__c = '製品評価利点';
        rep1.Product_Evaluation_Faults__c = '製品評価欠点';
        rep1.OPD_Summarize__c        = 'OPD実施総括';
        rep1.Activity_Plan__c        = '今後の活動計画';
        rep1.Opportunity_Situation__c = '引合無';
        rep1.OPD_ProductCategory1__c ='3D System';
        rep1.OPD_ProductCategory2__c ='OR Imaging Products';
        rep1.Hospital_Reference__c   = hospital.Id;
        rep1.Department_Class_Ref__c = strategicDep[0].Id;
        rep1.Hospital_Department__c  = dep.Id;
        rep1.Date__c                 = Date.today().addDays(-1);
        rep1.Loaner_request_no1__c = raObj.Id;
        rep1.Status__c = '申请中';
        insert rep1;
        raObj.Status__c = '已批准';
        update raObj;
        
        List<String> rA_statusList = new List<String>();
        rA_statusList.add('已出库');rA_statusList.add('医院已装机确认');rA_statusList.add('已回寄');
        rA_statusList.add('欠品中');rA_statusList.add('申请者已收货');rA_statusList.add('已回收');
        rA_statusList.add('完了');
        List<Rental_Apply__c> raList =[Select Id,Follow_UP_Opp__c,RA_Status__c,ShelfUp_ng_num__c,Follow_UP_Opp__r.Spare_Application_Singular__c 
            from Rental_Apply__c where Follow_UP_Opp__c = :opp.Id ];
        System.assertEquals('完了', raList[0].RA_Status__c);
        System.assertEquals(1, raList.size());
        
        Id execBTId = null;
        // 2023-01-06 新构造函数测试 ssm
        execBTId = Database.executeBatch(new SpareIsLoanBatch(2));
        Rental_Apply__c rac = [select Id,OPDAmount__c from Rental_Apply__c where Id = :raObj.Id ];
        System.assertEquals(1, rac.OPDAmount__c);
        System.Test.StopTest();
        // 2023-01-06 业务确认不会出现状态回退的问题，不考虑以下情况 ssm
        // List<String> oppIdList = new List<String>();
        // rep1.Status__c = '申请中';
        // update rep1;
        // raObj.OPDAmount__c = 5;
        // oppIdList.add(raObj.Follow_UP_Opp__c);
        // execBTId = Database.executeBatch(new SpareIsLoanBatch(oppIdList));
        // rac = [select Id,OPDAmount__c from Rental_Apply__c where Id = :raObj.Id];
        // System.assertEquals(0, rac.OPDAmount__c);
        
    }
    
    
}