@isTest
private class MoreMaintenanceContractBatchTest {
    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.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_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;
        insert middleTable;
        return middleTable;
    }
    static testMethod void testMethod1() {
    	ControllerUtil.EscapeNFM001Trigger = true;
        StaticParameter.EscapeSyncProduct2Trigger = true;
        //StaticParameter.EscapeAccountTrigger = true;
        StaticParameter.EscapeMaintenanceContractAfterUpdateTrigger = true;
    	//维修合同
		// 病院、戦略科室、診療科の情報を作成します
        Account hospital = createHospital( 'test hospital');
        Account[] strategicDep = selectStrategicDep( hospital);
        Account dep = createDep( hospital, strategicDep[0]);
        // 納入機器を作る
        Asset asset = createAsset( hospital, strategicDep[0], dep);

        //维修合同
		Maintenance_Contract__c contract1 = new Maintenance_Contract__c();
        contract1.Name = 'tect contract1';
        contract1.status__c = '契約';
        contract1.Maintenance_Contract_No__c = 'Kami_Contract_No';
        contract1.Contract_Conclusion_Date__c = Date.today();
        contract1.Hospital__c = hospital.Id;
        contract1.Department_Class__c = strategicDep[0].Id;
        contract1.Department__c = dep.Id;
        contract1.Contract_Start_Date__c = Date.today().addDays( -10);   // 10日前
        contract1.Contract_End_Date__c = Date.today().addDays( 5);       // 5日後
        contract1.SalesOfficeCode_selection__c = '北京RC';
        contract1.hospitalQuotationTextSum__c = 0.00;
        contract1.Estimate_Target__c = '经销商';
        contract1.RecordTypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('NewMaintenance_Contract').getRecordTypeId();
        insert new Maintenance_Contract__c[] {contract1};

        Maintenance_Contract_Asset__c mca1 = new Maintenance_Contract_Asset__c();
        mca1.Asset__c = asset.Id;
        mca1.Estimate_List_Price__c = 100;
        mca1.Maintenance_Contract__c = contract1.Id;
        insert new Maintenance_Contract_Asset__c[] {mca1};

		// 修理を作成する01
        Repair__c repair01 = new Repair__c();
        repair01.Account__c = dep.Id;
        repair01.Department_Class__c = strategicDep[0].Id;
        repair01.Hospital__c = hospital.Id;
        repair01.Delivered_Product__c = asset.Id;
        repair01.SERVICE_CONTRACT_JUDEGE_DAY__C = Date.today().addDays( -11);   // 维修合同判断日がサービス契約開始日の前日
        repair01.Repair_Start_Date__c = Date.today().addDays(-10);
        repair01.Repair_Final_Inspection_Date__c = Date.today().addDays(-3);
        repair01.Repair_List_Price__c = 100.00;
        repair01.RepairOrder_Complete_Day__c = Date.today().addDays(-1);
        repair01.Maintenance_Contract__c = contract1.Id;
        insert repair01;
    	
        System.Test.StartTest();
        List<Maintenance_Contract__c> Mc1_1 = [Select Id,Name,RecordType.DeveloperName from Maintenance_Contract__c where RecordType.DeveloperName='NewMaintenance_Contract'];
    	system.assertEquals(1,Mc1_1.size());
      	Database.executeBatch(new MoreMaintenanceContractBatch(Mc1_1[0].Id), 1);     
        System.Test.StopTest();
    }
    static testMethod void testMethod2() {
    	ControllerUtil.EscapeNFM001Trigger = true;
        StaticParameter.EscapeSyncProduct2Trigger = true;
        //StaticParameter.EscapeAccountTrigger = true;
        StaticParameter.EscapeMaintenanceContractAfterUpdateTrigger = true;
    	//维修合同
		// 病院、戦略科室、診療科の情報を作成します
        Account hospital = createHospital( 'test hospital');
        Account[] strategicDep = selectStrategicDep( hospital);
        Account dep = createDep( hospital, strategicDep[0]);
        // 納入機器を作る
        Asset asset = createAsset( hospital, strategicDep[0], dep);

        //维修合同
		Maintenance_Contract__c contract1 = new Maintenance_Contract__c();
        contract1.Name = 'tect contract1';
        contract1.status__c = '契約';
        contract1.Maintenance_Contract_No__c = 'Kami_Contract_No';
        contract1.Contract_Conclusion_Date__c = Date.today();
        contract1.Hospital__c = hospital.Id;
        contract1.Department_Class__c = strategicDep[0].Id;
        contract1.Department__c = dep.Id;
        contract1.Contract_Start_Date__c = Date.today().addDays( -10);   // 10日前
        contract1.Contract_End_Date__c = Date.today().addDays( 5);       // 5日後
        contract1.SalesOfficeCode_selection__c = '北京RC';
        contract1.hospitalQuotationTextSum__c = 0.00;
        contract1.Estimate_Target__c = '经销商';
        contract1.RecordTypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('VM_Contract').getRecordTypeId();
        insert new Maintenance_Contract__c[] {contract1};

        Maintenance_Contract_Asset__c mca1 = new Maintenance_Contract_Asset__c();
        mca1.Asset__c = asset.Id;
        mca1.Estimate_List_Price__c = 100;
        mca1.Maintenance_Contract__c = contract1.Id;
        insert new Maintenance_Contract_Asset__c[] {mca1};

		// 修理を作成する01
        Repair__c repair01 = new Repair__c();
        repair01.Account__c = dep.Id;
        repair01.Department_Class__c = strategicDep[0].Id;
        repair01.Hospital__c = hospital.Id;
        repair01.Delivered_Product__c = asset.Id;
        repair01.SERVICE_CONTRACT_JUDEGE_DAY__C = Date.today().addDays( -11);   // 维修合同判断日がサービス契約開始日の前日
        repair01.Repair_Start_Date__c = Date.today().addDays(-10);
        repair01.Repair_Final_Inspection_Date__c = Date.today().addDays(-3);
        repair01.Repair_List_Price__c = 100.00;
        repair01.RepairOrder_Complete_Day__c = Date.today().addDays(-1);
        repair01.Maintenance_Contract__c = contract1.Id;
        insert repair01;
    	
        System.Test.StartTest();
        List<Maintenance_Contract__c> Mc1_1 = [Select Id,Name,RecordType.DeveloperName from Maintenance_Contract__c where RecordType.DeveloperName='VM_Contract'];
    	system.assertEquals(1,Mc1_1.size());
      	Database.executeBatch(new MoreMaintenanceContractBatch(Mc1_1[0].Id), 1);     
        System.Test.StopTest();
    }

}