@isTest
private class InsReToMaintenanceBatchTest {

    static Asset createAsset(String input, String accountid, String dcId, String hpId, String serialNo, String prId, String mark ,String Product_Serial_No) {
        Asset asset = new Asset();
        asset.Name = input;
        asset.AccountId = accountid;
        asset.Department_Class__c = dcId;
        asset.Hospital__c = hpId;
        asset.SerialNumber = serialNo;
        asset.Product2Id = prId;
        asset.InstallDate = Date.today();
        asset.Product_Serial_No__c = Product_Serial_No;
        //JZHG-BSDUT4 ---20200825---update By rentongxiao---Start
        //在接收参数的时候新增了String类型的Mark字段,以及在调用该方法时传递了String类型的Mark字段
        asset.AssetMark__c = mark;
        //JZHG-BSDUT4 ---20200825---update By rentongxiao---End 
        insert asset;
        return asset;
    }


    static testMethod void testMethod1() {
        // Implement test code
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'];
        if (rectHp.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'];
        if (rectDpt.size() == 0) {
            return;
        }
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];

        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', Work_Location__c = '北京', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;

        // 病院作成
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'hp', OwnerId = hpOwner.Id);
        insert hp;

        Account dc = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_GI'];
        
        // 診療科を作る
        Account dpt1 = new Account(RecordTypeId = rectDpt[0].Id);
        dpt1.Name         = '*';
        dpt1.Department_Name__c  = 'TestDepart1';
        dpt1.ParentId            = dc.Id;
        dpt1.Department_Class__c = dc.Id;
        dpt1.Hospital__c         = hp.Id;
        insert dpt1;

        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;
        
        // 戦略科室を得る
        List<Account> strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
        StaticParameter.EscapeSyncProduct2Trigger = true;
        StaticParameter.EscapeAccountTrigger = true;
        StaticParameter.EscapeMaintenanceContractAfterUpdateTrigger = true;

         // 診療科を作る
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'].id;
        dep.Name = 'test dep';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;
        
        // 製品を作る
        Product2 productA = new Product2( Name='テスト商品', Maintenance_Price_Year__c = 12000, Manual_Entry__c = false);
        insert productA;
        
        
        // 納入機器を作成する
        Asset asset01 = createAsset('asset01', dep.Id, strategicDep[0].Id, hospital.Id, '00001', productA.Id,'主机','123');
        Asset asset02 = createAsset('asset02', dep.Id, strategicDep[0].Id, hospital.Id, '00002', productA.Id,'主机','456');
        Asset asset03 = createAsset('asset03', dep.Id, strategicDep[0].Id, hospital.Id, '00003', productA.Id,'耗材','789');
        Asset asset04 = createAsset('asset04', dep.Id, strategicDep[0].Id, hospital.Id, '00004', productA.Id,'耗材','101');
        Asset asset05 = createAsset('asset05', dep.Id, strategicDep[0].Id, hospital.Id, '00005', productA.Id,'耗材','102');

        // 维修合同を作成する
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.Not_Upper_limit_reason__c = 'tect contract';
        contract.RecordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Maintenance_Contract__c' and DeveloperName = 'NewMaintenance_Contract'].id;
        contract.Hospital__c = hospital.Id;
        contract.Department_Class__c = strategicDep[0].Id;
        contract.Department__c = dep.Id;
        contract.Service_Contract_Staff__c = hpOwner.Id;
        contract.Status__c = '契約';
        contract.Contract_Start_Date__c = Date.today().addYears(-1);
        contract.Contract_End_Date__c = Date.today().addYears(1);
        contract.Maintenance_Contract_No__c = 'tect contract';
        insert contract;
        
        // 维修合同报价を作成する
        Maintenance_Contract_Estimate__c contactEsti1 = new Maintenance_Contract_Estimate__c(
            Name = 'contract estimate 1',
            RecordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Maintenance_Contract_Estimate__c' and DeveloperName = 'NewMaintenance_Quote'].id,
            Contract_Esti_Start_Date__c = Date.today(),
            Contract_Range__c = 2,
            Maintenance_Contract__c = contract.Id,
            Request_quotation_Amount__c = 1,
            EndUserType__c = '既有用户',
            mainTalksTime__c = 1,
            talksStartDate__c = Date.today(),
            Discount_reason__c = 'test',
            Improve_ConsumptionRate_Idea__c = 'test'
        );
        insert contactEsti1;

        contract.Estimation_Id__c = contactEsti1.Id;
        
        update contract;

        //维修合同报价/保有设备
        Maintenance_Contract_Asset_Estimate__c mcae1 = new Maintenance_Contract_Asset_Estimate__c();
        mcae1.Asset__c = asset01.Id;
        mcae1.Maintenance_Contract_Estimate__c = contactEsti1.Id;
        mcae1.ifHaveleftInPrevious__c = true;
        mcae1.Estimate_List_Price__c = 1000;
        mcae1.Check_Result__c = 'OK';

        Maintenance_Contract_Asset_Estimate__c mcae2 = new Maintenance_Contract_Asset_Estimate__c();
        mcae2.Asset__c = asset02.Id;
        mcae2.Maintenance_Contract_Estimate__c = contactEsti1.Id;
        mcae2.ifHaveleftInPrevious__c = true;
        mcae2.Estimate_List_Price__c = 1000;
        mcae2.Check_Result__c = 'OK';

        Maintenance_Contract_Asset_Estimate__c mcae3 = new Maintenance_Contract_Asset_Estimate__c();
        mcae3.Asset__c = asset03.Id;
        mcae3.Maintenance_Contract_Estimate__c = contactEsti1.Id;
        mcae3.ifHaveleftInPrevious__c = true;
        mcae3.Estimate_List_Price__c = 1000;
        mcae3.Check_Result__c = 'OK';

        insert new Maintenance_Contract_Asset_Estimate__c[] {mcae1,mcae2,mcae3};
         

        //维修合同/保有设备
        Maintenance_Contract_Asset__c mca1 = new Maintenance_Contract_Asset__c();
        mca1.Asset__c = asset01.Id;
        mca1.Estimate_List_Price__c = 100;
        mca1.Check_object__c = true;
        mca1.Maintenance_Contract__c = contract.Id;
        Maintenance_Contract_Asset__c mca2 = new Maintenance_Contract_Asset__c();
        mca2.Asset__c = asset02.Id;
        mca2.Estimate_List_Price__c = 100;            
        mca2.Check_object__c = true;
        mca2.Maintenance_Contract__c = contract.Id;
        Maintenance_Contract_Asset__c mca3 = new Maintenance_Contract_Asset__c();
        mca3.Asset__c = asset03.Id;
        mca3.Estimate_List_Price__c = 100;
        mca3.Check_object__c = true;
        mca3.Maintenance_Contract__c = contract.Id;
        
        insert new Maintenance_Contract_Asset__c[] {mca1,mca2,mca3};

        //点检计划
        Inspectup_Plan__c inplan = new Inspectup_Plan__c();
        inplan.Planned_Start_Date__c = Date.today().addMonths(-1).addDays(1);
        inplan.Planned_End_Date__c = Date.today().addMonths(2).addDays(1);
        inplan.InspctupDate__c = Date.today().addDays(1);
        inplan.Actual_Execution_Quantity__c = 0;
        inplan.Maintenance_Contract__c = contract.Id;
        inplan.Task_sending_time__c = Date.today();
        insert inplan;

        Inspection_Report__c ir0 = new Inspection_Report__c(Name = '*',
        Hospital__c = hospital.Id,
        //Reporter__c = hpOwner.id,
        Approved_date__c = Date.today().addDays(-1),
        Inspectup_Plan__c = inplan.id,
        Contract__c = contract.id,
        Inspection_Date__c = Date.today()
        );
        insert ir0;

        Inspection_Item__c ii0 = new Inspection_Item__c(
            Inspection_ReportId__c = ir0.Id,
            AssetId__c = asset03.Id
        );
        insert ii0;
        Database.executeBatch(new InsReToMaintenanceBatch(), 1);
        
    }
}