@isTest
private class EnquiryBoostTypeBatchTest {
    static testMethod void testMethod1() {
        RecordType rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity' ];
        // 病院を作る
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital.Name = 'test hospital';
        insert hospital;
        // 戦略科室を得る
        Account[] strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_OTH'];
            // 診療科を作る
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
        dep.Name = 'test dep';
        dep.AgentCode_Ext__c = '9999998';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;

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

        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           =  Date.today().addDays(22);
        opp.Close_Forecasted_Date__c = Date.today().addMonths(1);
        opp.Stock_apply_status__c = '批准';
        insert opp;

         // 保有设备C (只有附属品 个体管理)
        Asset assetC1 = new Asset(Asset_Owner__c = 'Olympus');
        assetC1.RecordTypeId = System.Label.Asset_RecordType;
        assetC1.SerialNumber = 'assetC1';
        assetC1.Name = 'assetC1';
        assetC1.AccountId = dep.Id;
        assetC1.Department_Class__c = strategicDep[0].Id;
        assetC1.Hospital__c = hospital.Id;
        assetC1.Product2Id = pro5.Id;
        assetC1.Quantity = 1;
        assetC1.Status = '有库存';
        assetC1.Manage_type__c = '个体管理';
        assetC1.Internal_asset_location__c = '北京 备品中心';
        assetC1.Loaner_accsessary__c = true;
        assetC1.Delete_Flag__c = false;
        assetC1.Freeze_sign__c = false;
        assetC1.Out_of_wh__c = 0;
        // 保有设备
        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[] {assetC1, asset};

        //备品借出申请
        Rental_Apply__c raObj = new Rental_Apply__c();
        raObj.Name = 'testra';
        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.Hope_Lonaer_date_Num__c = 1;  // 希望借用天数
        raObj.Phone_number__c = '1234567890';
        raObj.Follow_pcl_status2_Text__c = '引合 : -';
        raObj.demo_purpose2__c = '试用（有询价）' ;
        raObj.Follow_UP_Opp__c = opp.Id;
        insert raObj;

        // 备品配套(只有附属品)
        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_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;

        // 备品配套明细(只有附属品)
        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_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_Apply_Equipment_Set__c = raesObjC1.Id;
        raesdObjC1.Rental_Num__c = 1;
        raesdObjC1.Queue_Number__c = null;
        raesdObjC1.Is_Body__c = false;
        raesdObjC1.Shippment_loaner_time__c = System.Now();
        raesdObjC1.Asset__c = assetC1.Id;
        raesdObjC1.IndexFromUniqueKey__c = 1;
        raesdObjC1.UniqueKey__c = '1:'+ raesObjC1.Id + ':' + fsdObjC1.Id + ':1';
        raesdObjC1.Salesdepartment_before__c = '0.备品中心';
        raesdObjC1.Internal_asset_location_before__c = '北京 备品中心';
        raesdObjC1.Product_category_text__c = 'GI';
        raesdObjC1.Equipment_Type_text__c = '产品试用';
        insert raesdObjC1;


        Database.executeBatch(new EnquiryBoostTypeBatch(), 50);
        
        System.Test.StartTest();
                 // 病院を作る
        Account hospital1 = new Account();
        hospital1.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital1.Name = 'test hospital1';
        insert hospital1;
        // 戦略科室を得る
        Account[] strategicDep1 = [SELECT ID, Name FROM Account WHERE parentId = :hospital1.Id AND recordType.DeveloperName = 'Department_Class_OTH'];
            // 診療科を作る
        Account dep1 = new Account();
        dep1.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
        dep1.Name = 'test dep1';
        dep1.AgentCode_Ext__c = '9999997';
        dep1.ParentId = strategicDep1[0].Id;
        dep1.Department_Class__c = strategicDep1[0].Id;
        dep1.Hospital__c = hospital1.Id;
        insert dep1;

        // 产品
        Product2 pro4 = new Product2(Name='name04',IsActive=true,Family='SP',
                Fixture_Model_No__c='n04',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'n04',
                ProductCode_Ext__c='pc04',Manual_Entry__c=false);
        insert pro4;

        Opportunity opp1 = new Opportunity();
        opp1.AccountId           = dep1.Id;
        opp1.Department_Class__c = dep1.Id;
        opp1.Hospital__c         = hospital1.Id;
        opp1.SAP_Send_OK__c      = false;
        opp1.Name                = 'GZ-SP-NFM009_1';
        opp1.Trade__c            = '内貿';
        opp1.StageName           = '削除';
        opp1.CloseDate           =  Date.today().addDays(22);
        //opp1.Close_Forecasted_Date__c = Date.today().addMonths(1);
        opp1.Stock_apply_status__c = '批准';
        insert opp1;

         // 保有设备C (只有附属品 个体管理)
        Asset assetC2 = new Asset(Asset_Owner__c = 'Olympus');
        assetC2.RecordTypeId = System.Label.Asset_RecordType;
        assetC2.SerialNumber = 'assetC2';
        assetC2.Name = 'assetC2';
        assetC2.AccountId = dep1.Id;
        assetC2.Department_Class__c = strategicDep1[0].Id;
        assetC2.Hospital__c = hospital1.Id;
        assetC2.Product2Id = pro4.Id;
        assetC2.Quantity = 1;
        assetC2.Status = '有库存';
        assetC2.Manage_type__c = '个体管理';
        assetC2.Internal_asset_location__c = '北京 备品中心';
        assetC2.Loaner_accsessary__c = true;
        assetC2.Delete_Flag__c = false;
        assetC2.Freeze_sign__c = false;
        assetC2.Out_of_wh__c = 0;
        // 保有设备
        Asset asset1 = new Asset(Asset_Owner__c = 'Olympus');
        asset1.RecordTypeId = System.Label.Asset_RecordType;
        asset1.SerialNumber = 'asset1';
        asset1.Name = 'asset1';
        asset1.AccountId = dep1.Id;
        asset1.Department_Class__c = strategicDep1[0].Id;
        asset1.Hospital__c = hospital1.Id;
        asset1.Product2Id = pro4.Id;
        asset1.Quantity = 1;
        asset1.Status = '有库存';
        asset1.Manage_type__c = '个体管理';
        asset1.Internal_asset_location__c = '北京 备品中心';
        asset1.Loaner_accsessary__c = true;
        asset1.Delete_Flag__c = false;
        asset1.Freeze_sign__c = false;
        asset1.Out_of_wh__c = 0;
        insert new Asset[] {assetC2, asset1};

        //备品借出申请
        Rental_Apply__c raObj1 = new Rental_Apply__c();
        raObj1.Name = 'testra1';
        raObj1.Product_category__c = 'GI';
        raObj1.Demo_purpose1__c = '产品试用';
        raObj1.direct_send__c = '医疗机构';
        raObj1.Loaner_received_staff__c = '赵六';
        raObj1.Loaner_received_staff_phone__c = '111';
        raObj1.direct_shippment_address__c = '北京市';
        raObj1.Hope_Lonaer_date_Num__c = 1;  // 希望借用天数
        raObj1.Phone_number__c = '1234567890';
        raObj1.Follow_pcl_status2_Text__c = '削除 : 17 取消';
        raObj1.demo_purpose2__c = '试用（有询价）' ;
        raObj1.Follow_UP_Opp__c = opp1.Id;
        insert raObj1;

        // 备品配套(只有附属品)
        Fixture_Set__c fsObjC2 = new Fixture_Set__c();
        fsObjC2.Name = 'setC2';
        fsObjC2.Fixture_Set_Body_Model_No__c = 'modelNoC2';
        fsObjC2.Loaner_name__c = 'nameC2';
        insert fsObjC2;

        // 借出备品配套一览
        Rental_Apply_Equipment_Set__c raesObjC2 = new Rental_Apply_Equipment_Set__c();
        raesObjC2.Rental_Apply__c = raObj1.Id;
        raesObjC2.Fixture_Set__c = fsObjC2.Id;
        raesObjC2.Cancel_Select__c = false;
        raesObjC2.Shipment_request_time__c = Date.toDay();
        raesObjC2.Rental_Start_Date__c = Date.toDay();
        raesObjC2.Rental_End_Date__c = Date.toDay();
        raesObjC2.IndexFromUniqueKey__c = 1;
        raesObjC2.UniqueKey__c = '1:'+ fsObjC2.Id + ':1';
        insert raesObjC2;

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

        // 借出备品配套一览明细
        Rental_Apply_Equipment_Set_Detail__c raesdObjC2 = new Rental_Apply_Equipment_Set_Detail__c();
        raesdObjC2.Rental_Apply__c = raObj1.Id;
        raesdObjC2.Fixture_Set_Detail__c = fsdObjC2.Id;
        raesdObjC2.Rental_Apply_Equipment_Set__c = raesObjC2.Id;
        raesdObjC2.Rental_Num__c = 1;
        raesdObjC2.Queue_Number__c = null;
        raesdObjC2.Is_Body__c = false;
        raesdObjC2.Shippment_loaner_time__c = System.Now();
        raesdObjC2.Asset__c = assetC2.Id;
        raesdObjC2.IndexFromUniqueKey__c = 1;
        raesdObjC2.UniqueKey__c = '1:'+ raesObjC2.Id + ':' + fsdObjC2.Id + ':1';
        raesdObjC2.Salesdepartment_before__c = '0.备品中心';
        raesdObjC2.Internal_asset_location_before__c = '北京 备品中心';
        raesdObjC2.Product_category_text__c = 'GI';
        raesdObjC2.Equipment_Type_text__c = '产品试用';
        insert raesdObjC2;
        
        List<ID> l = new List<Id>();
        l.add(raObj.id);
        l.add(raObj1.id);
        Id execBTId = Database.executeBatch(new EnquiryBoostTypeBatch(l), 50);

        System.Test.StopTest();
    }
}