@isTest
private class ConUpdateLastContractbatchTest {

	private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
	static Asset createAsset(String input, String accountid, String dcId, String hpId, String serialNo, String prId) {
		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();
		insert asset;
		return asset;
	}

	static testMethod void testMethod1() {
		ControllerUtil.EscapeNFM001Trigger = true;
		StaticParameter.EscapeSyncProduct2Trigger = true;
		//StaticParameter.EscapeAccountTrigger = true;
		StaticParameter.EscapeMaintenanceContractAfterUpdateTrigger = true;

		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 = '有效';
		insert hospital;

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

		// 価格表エントリを作成する
		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 asset01 = createAsset('asset01', dep.Id, strategicDep[0].Id, hospital.Id, '00001', productA.Id);
		// 納入機器を作成する
		Asset asset02 = createAsset('asset02', dep.Id, strategicDep[0].Id, hospital.Id, '00002', productA.Id);

		// 第一期合同
		Maintenance_Contract__c mc1 = new Maintenance_Contract__c();
		mc1.Name = 'tect mc1';
		mc1.Not_Upper_limit_reason__c = 'tect mc1';
		mc1.RecordtypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('NewMaintenance_Contract').getRecordTypeId();
		mc1.Hospital__c = hospital.Id;
		mc1.Switch_TimeBase_WF__c = true;
		mc1.Department_Class__c = strategicDep[0].Id;
		mc1.Department__c = dep.Id;
		mc1.Service_Contract_Staff__c = UserInfo.getUserId();
		mc1.Maintenance_Contract_No__c = 'tect mc1';
		mc1.Status__c = '契約満了';
		mc1.Contract_Start_Date__c = Date.today().addMonths(-14);
		mc1.Contract_End_Date__c = Date.today().addMonths(-2);

		// 第二期合同
		Maintenance_Contract__c mc2 = new Maintenance_Contract__c();
		mc2.Name = 'tect mc2';
		mc2.Not_Upper_limit_reason__c = 'tect mc2';
		mc2.RecordtypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('NewMaintenance_Contract').getRecordTypeId();
		mc2.Hospital__c = hospital.Id;
		mc2.Switch_TimeBase_WF__c = true;
		mc2.Department_Class__c = strategicDep[0].Id;
		mc2.Department__c = dep.Id;
		mc2.Service_Contract_Staff__c = UserInfo.getUserId();
		mc2.Maintenance_Contract_No__c = 'tect mc2';
		mc2.Status__c = '契約満了';
		mc2.Contract_Start_Date__c = Date.today().addMonths(-23);
		mc2.Contract_End_Date__c = Date.today().addMonths(-15);

		// 第三期合同
		Maintenance_Contract__c mc3 = new Maintenance_Contract__c();
		mc3.Name = 'tect mc2';
		mc3.Not_Upper_limit_reason__c = 'tect mc2';
		mc3.RecordtypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('NewMaintenance_Contract').getRecordTypeId();
		mc3.Hospital__c = hospital.Id;
		mc3.Switch_TimeBase_WF__c = true;
		mc3.Department_Class__c = strategicDep[0].Id;
		mc3.Department__c = dep.Id;
		mc3.Service_Contract_Staff__c = UserInfo.getUserId();
		mc3.Maintenance_Contract_No__c = 'tect mc2';
		mc3.Status__c = '契約満了';
		mc3.Contract_Start_Date__c = Date.today().addMonths(-23);
		// mc2.Contract_End_Date__c = Date.today().addMonths(-15);
		insert new Maintenance_Contract__c[] {mc1,mc2,mc3};

		mc1.Status__c = '契約満了';
		update mc1;

		mc2.Status__c = '契約満了';
		update mc2;

		Maintenance_Contract_Asset__c mca1 = new Maintenance_Contract_Asset__c();
		mca1.Asset__c = asset01.Id;
		mca1.Estimate_List_Price__c = 100;
		mca1.Maintenance_Contract__c = mc1.Id;
		insert mca1;

		Maintenance_Contract_Asset__c mca2 = new Maintenance_Contract_Asset__c();
		mca2.Asset__c = asset01.Id;
		mca2.Estimate_List_Price__c = 100;
		mca2.Maintenance_Contract__c = mc2.Id;
		mca2.LastMContract__c = mc1.Id;
		insert mca2;

		Maintenance_Contract_Asset__c mca3 = new Maintenance_Contract_Asset__c();
		mca3.Asset__c = asset02.Id;
		mca3.Estimate_List_Price__c = 100;
		mca3.Maintenance_Contract__c = mc1.Id;
		mca3.LastMContract__c = mc2.Id;
        insert mca3;

		Maintenance_Contract_Asset__c mca4 = new Maintenance_Contract_Asset__c();
		mca4.Asset__c = asset02.Id;
		mca4.Estimate_List_Price__c = 100;
		mca4.Maintenance_Contract__c = mc1.Id;
		mca4.LastMContract__c = mc2.Id;
        insert mca4;

        Maintenance_Contract_Asset__c mca5 = new Maintenance_Contract_Asset__c();
		mca5.Asset__c = asset02.Id;
		mca5.Estimate_List_Price__c = 100;
		mca5.Maintenance_Contract__c = mc2.Id;
		mca5.LastMContract__c = mc1.Id;
        insert mca5;

        Maintenance_Contract_Asset__c mca6 = new Maintenance_Contract_Asset__c();
		mca6.Asset__c = asset02.Id;
		mca6.Estimate_List_Price__c = 100;
		mca6.Maintenance_Contract__c = mc2.Id;
		mca6.LastMContract__c = mc3.Id;
        insert mca6;
		// insert new Maintenance_Contract_Asset__c[] {mca1,mca2,mca3};

		System.Test.startTest();
		List<String> ll = new List<String>();
		Maintenance_Contract_Asset__c ass = [select id,LastMContract__c,Maintenance_Contract__r.Status__c from Maintenance_Contract_Asset__c WHERE id = : mca2.Id];
		System.assertEquals(mc1.Id, ass.LastMContract__c);
		System.assertEquals('契約満了', ass.Maintenance_Contract__r.Status__c);
		ll.add(mc2.Id);
        Database.executeBatch(new ConUpdateLastContractbatch(ll), 100);
		Id execBTId = Database.executeBatch(new ConUpdateLastContractbatch(), 100);

		// Maintenance_Contract_Asset__c mca =  [select Id,LastMContract__c
		//                             from Maintenance_Contract_Asset__c WHERE Id = : mca2.Id];
		// System.assertEquals(mc1.Id, mca.LastMContract__c);
		System.Test.stopTest();
	}
}