@isTest
private class AssetUpdateLastContractbatchTest {

	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;
    }


    @isTest
    static 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);

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

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

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

        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 = contract1.Id;

        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 = contract2.Id;
        
        insert new Maintenance_Contract_Asset__c[] {mca1,mca2};
        System.Test.startTest();
        List<String> ll = new List<String>();
        List<asset> ass = [select id from asset WHERE id = : asset01.Id];
        System.assertEquals(1, ass.size());
        ll.add(ass[0].Id);
        Id execBTId = Database.executeBatch(new AssetUpdateLastContractbatch(ll), 100);

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