@isTest
private class RepairResonHistoricalBatchTest {
     private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
    //创建医院
    static Account createHospital( String hospitalName) {
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        // 病院を作る
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE DeveloperName = :'HP'].id;
        hospital.Name = hospitalName;
        insert hospital;
        StaticParameter.EscapeAccountTrigger = true;
        return hospital;
    }

    static List<Account> selectStrategicDep( Account hospital) {
        // 戦略科室を得る
        List<Account> strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
        return strategicDep;
    }

    static Account createDep( Account hospital, Account strategicDep) {
        // 診療科を作る
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE DeveloperName = :'Department_GI'].id;
        dep.Name = 'test dep';
        dep.ParentId = strategicDep.Id;
        dep.Department_Class__c = strategicDep.Id;
        dep.Hospital__c = hospital.Id;
        // dep.State_Text__c = '上海市';
        insert dep;
        return dep;
    }

    static Asset createAsset( Account hospital, Account strategicDep, Account dep) {
        // 製品を作る
        Product2 productA = new Product2( Name = 'テスト商品');
        insert productA;

        // 価格表エントリを作成する
        PricebookEntry entry = new PricebookEntry( Pricebook2Id = pricebookId, Product2Id = productA.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = 'CNY';
        entry.Product2Id = productA.Id;
        insert entry;

        // 納入機器を作成する
        Asset asset = new Asset();
        asset.Name = 'テスト機器';
        asset.CurrencyIsoCode= 'CNY';
        asset.Reson_Can_not_Warranty__c='点检';
        asset.AccountId = dep.Id;
        asset.Department_Class__c = strategicDep.Id;
        asset.Hospital__c = hospital.Id;
        asset.SerialNumber = 'testserial';
        asset.Quantity = 3;
        // asset.Extend_Gurantee_DateTo_Text__c =Date.today().addDays(30);
        // asset.IS_Extend_Gurantee_Txt__c =true;
        // asset.Order_No__c = 'BJ_2020';

        insert asset;
        return asset;
    }

    static Maintenance_Contract__c createMaintenanceContract( Account hospital, Account strategicDep, Account dep) {
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.status__c = '契約';
        contract.Maintenance_Contract_No__c = 'Kami_Contract_No';
        contract.Contract_Conclusion_Date__c = Date.today();
        contract.Hospital__c = hospital.Id;
        contract.Department_Class__c = strategicDep.Id;
        contract.Department__c = dep.Id;
        contract.Contract_Start_Date__c = Date.today().addDays( -10);   // 10日前
        contract.Contract_End_Date__c = Date.today().addDays( 5);       // 5日後
        contract.SalesOfficeCode_selection__c = '北京RC';

        insert contract;
        return contract;
    }

    static Maintenance_Contract__c createMaintenanceContract1( Account hospital, Account strategicDep, Account dep) {
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.status__c = '契約';
        contract.Maintenance_Contract_No__c = 'Kami_Contract_No';
        contract.Contract_Conclusion_Date__c = Date.today();
        contract.Hospital__c = hospital.Id;
        contract.Department_Class__c = strategicDep.Id;
        contract.Department__c = dep.Id;
        contract.Contract_Start_Date__c = Date.today().addDays( -10);   // 10日前
        contract.Contract_End_Date__c = Date.today().addDays( 5);       // 5日後
        contract.SalesOfficeCode_selection__c = '上海RC';

        insert contract;
        return contract;
    }

    static Maintenance_Contract__c createMaintenanceContract2( Account hospital, Account strategicDep, Account dep) {
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.status__c = '契約';
        contract.Maintenance_Contract_No__c = 'Kami_Contract_No';
        contract.Contract_Conclusion_Date__c = Date.today();
        contract.Hospital__c = hospital.Id;
        contract.Department_Class__c = strategicDep.Id;
        contract.Department__c = dep.Id;
        contract.Contract_Start_Date__c = Date.today().addDays( -10);   // 10日前
        contract.Contract_End_Date__c = Date.today().addDays( 5);       // 5日後
        contract.SalesOfficeCode_selection__c = '北京RC';
        ID RecordTypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByName().get('多年保合同').getRecordTypeId();
        // contract.RecordTypeId='01210000000gTYv';
        contract.RecordTypeId=RecordTypeId;

        insert contract;
        return contract;
    }
    static Maintenance_Contract__c createMaintenanceContract3( Account hospital, Account strategicDep, Account dep) {
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.status__c = '契約';
        contract.Maintenance_Contract_No__c = 'Kami_Contract_No';
        contract.Contract_Conclusion_Date__c = Date.today();
        contract.Hospital__c = hospital.Id;
        contract.Department_Class__c = strategicDep.Id;
        contract.Department__c = dep.Id;
        contract.Contract_Start_Date__c = Date.today().addDays( -10);   // 10日前
        contract.Contract_End_Date__c = Date.today().addDays( 5);       // 5日後
        contract.SalesOfficeCode_selection__c = '沈阳RC';

        insert contract;
        return contract;
    }
    //维修合同/保有设备
    static Maintenance_Contract_Asset__c createMiddleTable( Asset asset, Maintenance_Contract__c contract) {
        // 中間テーブルを設ける
        Maintenance_Contract_Asset__c middleTable = new Maintenance_Contract_Asset__c();
        middleTable.Asset__c = asset.Id;
        middleTable.Maintenance_Contract__c = contract.id;
        middleTable.CurrencyIsoCode= 'CNY';
        middleTable.startDateGurantee_Text__c =Date.today().addDays( 5);
        middleTable.endDateGurantee_Text__c =Date.today().addDays( 30);
        insert middleTable;
        return middleTable;
    }
    static testMethod void testMethod1() {
        Oly_TriggerHandler.bypass('AccountTrigger');
        Oly_TriggerHandler.bypass('RepairHandler');
        Oly_TriggerHandler.bypass('RepairBeforeInsertHandler');
        Oly_TriggerHandler.bypass('UpdateContractAimAmountHandler');
        Oly_TriggerHandler.bypass('PaymentMaintenanceContractTriggerHandler');
        Oly_TriggerHandler.bypass('SetContractEstimatePriceHandler');
        Oly_TriggerHandler.bypass('SyncRepairFromMCAETrigger');
        Oly_TriggerHandler.bypass('ContactTriggerHandler');
        StaticParameter.EscapeMaintenanceContractAfterUpdateTrigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
        StaticParameter.EscapeSyncProduct2Trigger = true;
        StaticParameter.EscapeContactInsUpdUser = true;
        StaticParameter.EscapeContactToUser = true;
        StaticParameter.EscapeNFM010UpsertStatuAchievementsTrigger = true;
        StaticParameter.EscapeAccountTrigger = true;
        StaticParameter.EscapeNFM106Trigger = true;
        //客户
        // Account hospital = createHospital( 'test hospital');
        // Account[] strategicDep = selectStrategicDep( hospital);
        // Account dep = createDep( hospital, strategicDep[0]);
        // 保有设备
        // Asset asset = createAsset( hospital, strategicDep[0], strategicDep[0]);
        // 病院を作る
        // 省
        Address_Level__c al = new Address_Level__c();
        al.Name = '東京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;
        // 市
        Address_Level2__c al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-99';
        al2.Level1_Sys_No__c = '999999';
        al2.Level1_Name__c = '東京';
        al2.Name = '渋谷区';
        al2.Level2_Code__c = 'CN-9999';
        al2.Level2_Sys_No__c = '9999999';
        al2.Address_Level__c = al.id;
        insert al2;
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital.Name = 'test hospital';
        hospital.Is_Active__c = '有効';
        hospital.Attribute_Type__c = '卫生部';
        hospital.Speciality_Type__c = '综合医院';
        hospital.Grade__c = '一级';
        hospital.OCM_Category__c = 'SLTV';
        hospital.Is_Medical__c = '医疗机构';
        hospital.State_Master__c = al.id;
        hospital.City_Master__c = al2.id;
        hospital.Town__c = '东京';
        insert hospital;

        // 戦略科室を得る
        Account[] strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_OTH'];
        // 診療科を作る
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
        dep.Name = 'test dep';
        dep.AgentCode_Ext__c = '9999998';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;
        Asset asset = new Asset();
        asset.Name = 'テスト機器';
        asset.AccountId = dep.Id;
        asset.Department_Class__c = strategicDep[0].Id;
        asset.Hospital__c = hospital.Id;
        asset.SerialNumber = 'testserial';
        insert asset;
        
        // 创建维修合同
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        ID RecordTypeId1 = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByName().get('服务合同').getRecordTypeId();
        ID RecordTypeId2 = Schema.SObjectType.Maintenance_Contract_Estimate__c.getRecordTypeInfosByName().get('服务合同报价').getRecordTypeId();
        contract.Name = 'tect contract';
        contract.status__c = '契約';
        contract.URF_Contract__c=true;
        contract.Maintenance_Contract_No__c = 'Kami_Contract_No';
        contract.Contract_Conclusion_Date__c = Date.today();
        contract.Hospital__c = hospital.Id;
        contract.Department_Class__c = strategicDep[0].Id;
        contract.Department__c = dep.Id;
        contract.Contract_Start_Date__c = Date.today().addDays( -10);   // 10日前
        contract.Contract_End_Date__c = Date.today().addDays( 5);       // 5日後
        contract.SalesOfficeCode_selection__c = '北京RC';
        //contract.RecordTypeId='01210000000gTYq';
        contract.RecordTypeId=RecordTypeId1;
        contract.URF_Contract__c = true;
        contract.agree_Upper_limit__c = true;
        insert contract;
        //创建维修合同报价
        Maintenance_Contract_Estimate__c Estimate = new Maintenance_Contract_Estimate__c();
        Estimate.Maintenance_Contract__c = contract.Id;
        //Estimate.recordtypeId = '01210000000gTZA';
        Estimate.recordtypeId = RecordTypeId2;
        Estimate.Name = '报价1';
        insert Estimate;
        //创建维修合同报价/保有设备
        Maintenance_Contract_Asset_Estimate__c  Asset_Estimate = new Maintenance_Contract_Asset_Estimate__c();
        Asset_Estimate.Maintenance_Contract_Estimate__c = Estimate.Id;
        Asset_Estimate.CurrencyIsoCode = 'CNY';
        Asset_Estimate.Series_MaxRepairCount__c = 10;
        Asset_Estimate.Series_RepairCount__c = 5;
        insert Asset_Estimate;
        //创建维修合同/保有设备
        Maintenance_Contract_Asset__c middleTable = new Maintenance_Contract_Asset__c();
        middleTable.Asset__c = asset.Id;
        middleTable.Maintenance_Contract__c = contract.Id;
        middleTable.CurrencyIsoCode= 'CNY';
        middleTable.startDateGurantee_Text__c =Date.today().addDays( 5);
        middleTable.endDateGurantee_Text__c =Date.today().addDays( 30);
        middleTable.Maintenance_Contract_Asset_Estimate__c = Asset_Estimate.Id;
        insert middleTable;
        // 修理を作成する01
        Test.startTest();
        Repair__c repair01 = new Repair__c();
        repair01.Account__c = strategicDep[0].Id;
        repair01.Department_Class__c = dep.Id;
        repair01.Hospital__c = hospital.Id;
        repair01.Return_Without_Repair_Reason__c = 'I/F理由、確認';
        repair01.CancellationReason__c = '报价单到期';
        repair01.Return_Without_Repair_IF__c = true;
        repair01.SERVICE_CONTRACT_JUDEGE_DAY__C = Date.today(); // 维修合同判断日在合同开始日与合同结束日之间
        repair01.Failure_Occurrence_Date__c  =Date.today();
        repair01.Delivered_Product__c = asset.Id;
        repair01.Status__c = '草案中';
        repair01.SalesOfficeCode_selection__c = '西安RC';
        insert repair01;
        
        Repair__c checkRepair = null;
        checkRepair = [SELECT Id, Name , Maintenance_Contract__c,MaintenanceContractType__c FROM Repair__c WHERE ID = :repair01.id];
        System.assertEquals( contract.Id, checkRepair.Maintenance_Contract__c);

        // 修理を作成する01
        Repair__c repair02 = new Repair__c();
        repair02.Account__c = strategicDep[0].Id;
        repair02.Department_Class__c = strategicDep[0].Id;
        repair02.Hospital__c = hospital.Id;
        repair02.SERVICE_CONTRACT_JUDEGE_DAY__C = Date.today(); // 维修合同判断日在合同开始日与合同结束日之间
        repair02.Failure_Occurrence_Date__c  =Date.today();
        repair02.Delivered_Product__c = asset.Id;
        repair02.Return_Without_Repair_Reason__c = 'I/F理由、確認';
        repair02.CancellationReason__c = '报价单到期';
        repair02.Return_Without_Repair_IF__c = true;
        insert repair02;
        delete repair02;
        checkRepair.NewProductGuarante_Txt__c = '2: 服务多年保修';
        update checkRepair;
        checkRepair.NewProductGuarante_Txt__c = '8: 市场多年保修';
        checkRepair.On_site_repair__c = 'RC修理';
        checkRepair.Maintenance_Contract__c = contract.Id;
        checkRepair.SalesOfficeCode_selection__c = '杭州RC';
        update checkRepair;
        contract.SalesOfficeCode_selection__c = '上海RC';
        update contract;
        checkRepair.SalesOfficeCode_selection__c = '杭州RC';
        update checkRepair;
        checkRepair.SalesOfficeCode_selection__c = '杭州RC';
        checkRepair.NewProductGuarante_Txt__c = '2: 服务多年保修';
        update checkRepair;
        Test.stopTest();
        asset.Order_No__c = '上海分公司';
        update asset;
        checkRepair.SalesOfficeCode_selection__c = '杭州';
        update checkRepair;
        asset.Order_No__c = '广州分公司';
        update asset;
        checkRepair.On_site_repair__c = '办事处修理';
        checkRepair.SalesOfficeCode_selection__c = '杭州';
        checkRepair.NewProductGuarante_Txt__c = '8: 市场多年保修';
        update checkRepair;

        contract.SalesOfficeCode_selection__c = '广州RC';
        update contract;
        database.executeBatch(new RepairResonCannotWarrantyHistoricalBatch(),200);
    }
}