@isTest
private class RepairWorkdayBatchTest {
    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;

    static Account createHospital( String hospitalName) {
        // 病院を作る
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE DeveloperName = 'HP'].id;
        hospital.Name = hospitalName;
        insert hospital;
        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;
        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';
        insert asset;

        return asset;
    }

	static testMethod void testExecute() {
        // 测试需要，插入固定日历时间，强制设定工作日和假日
        OlympusCalendar__c oc0 = new OlympusCalendar__c(Date__c = Date.today().addDays(0), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc1 = new OlympusCalendar__c(Date__c = Date.today().addDays(-1), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc2 = new OlympusCalendar__c(Date__c = Date.today().addDays(-2), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc3 = new OlympusCalendar__c(Date__c = Date.today().addDays(-3), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc4 = new OlympusCalendar__c(Date__c = Date.today().addDays(-4), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc5 = new OlympusCalendar__c(Date__c = Date.today().addDays(-5), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc6 = new OlympusCalendar__c(Date__c = Date.today().addDays(-6), ChangeToHoliday__c=true, ChangeToWorkday__c=false);
        OlympusCalendar__c oc7 = new OlympusCalendar__c(Date__c = Date.today().addDays(-7), ChangeToHoliday__c=true, ChangeToWorkday__c=false);
        OlympusCalendar__c oc8 = new OlympusCalendar__c(Date__c = Date.today().addDays(-8), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc9 = new OlympusCalendar__c(Date__c = Date.today().addDays(-9), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc10 = new OlympusCalendar__c(Date__c = Date.today().addDays(-10), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc11 = new OlympusCalendar__c(Date__c = Date.today().addDays(-11), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc12 = new OlympusCalendar__c(Date__c = Date.today().addDays(-12), ChangeToHoliday__c=false, ChangeToWorkday__c=true);

        insert new OlympusCalendar__c[] {oc0,oc1,oc2,oc3,oc4,oc5,oc6,oc7,oc8,oc9,oc10,oc11,oc12};

        // 病院、戦略科室、診療科の情報を作成します
        Account hospital = createHospital( 'test hospital');
        Account[] strategicDep = selectStrategicDep( hospital);
        Account dep = createDep( hospital, strategicDep[0]);

        // 納入機器を作る
        Asset asset = createAsset( hospital, strategicDep[0], dep);

        // 修理を作成する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(-9);
        repair01.Repair_Final_Inspection_Date__c = null;
        insert repair01;

		System.Test.StartTest();
        Id execBTId = Database.executeBatch(new RepairWorkdayBatch(repair01.Id), 20);
        System.Test.StopTest();

        List<Repair__c> rpList = [select id, TAT_elapsed_workday__c from Repair__c where id =:repair01.Id];
        System.assertEquals(8, rpList[0].TAT_elapsed_workday__c);
	}
}