@isTest
private class SyncRepairFromMCAETriggerTest {
	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 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 testMethod void test_method_one() {
		// Implement test code
		// 病院、戦略科室、診療科の情報を作成します
        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 contract = createMaintenanceContract( hospital, strategicDep[0], dep);

        // 维修合同报价作成
        Maintenance_Contract_Estimate__c contactEsti = new Maintenance_Contract_Estimate__c(
            Name = 'contract estimate 1',
            Maintenance_Contract__c = contract.Id,
            Contract_Esti_Start_Date__c = Date.today(),
            Contract_Start_Date__c = Date.today(),
            Contract_Range__c = 12,
            IsSyncing__c = True,
            Maintenance_Price__c = 1500,
            Estimate_Trial_Money__c = 1500,
            NotUse_Oxygenated_Water__c = true
        );
        insert contactEsti;

        // 维修合同更新
        contract.Estimation_Id__c = contactEsti.Id;
        update contract;

        Maintenance_Contract_Asset_Estimate__c mcae1 = new Maintenance_Contract_Asset_Estimate__c(
            Asset__c = asset.Id,
            Maintenance_Contract_Estimate__c = contactEsti.Id,
            Estimate_List_Price__c = 100,
            Comment__c = '一二三四五六七八九十一二三四五'
        );
        insert mcae1;

        ControllerUtil.EscapeNFM001Trigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
        StaticParameter.EscapeNFM007Trigger = true;
        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
        StaticParameter.EscapeOpportunityHpDeptUpdTrigger = true;
        StaticParameter.EscapeSyncOpportunityTrigger = true;
        StaticParameter.EscapeMaintenanceContractAfterUpdateTrigger = true;
        StaticParameter.EscapeSyncOpportunityTrigger = true;
        StaticParameter.EscapeSyncProduct2Trigger = true;
        StaticParameter.EscapeLeadTrigger = true;
        StaticParameter.EscapeAccountTrigger = true;
        StaticParameter.EscapeConsumableOrderDetail2Trigger = true;
        StaticParameter.EscapeOrderDetail2Trigger = true;
        StaticParameter.EscapeNFM010UpsertStatuAchievementsTrigger = true;

        // 修理を作成する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.Maintenance_Contract__c = contract.Id;
        repair01.Delivered_Product__c = asset.Id;
        repair01.Status__c = '2.维修报价阶段';
        repair01.Repair_Detail__c = '12';
        repair01.SERVICE_CONTRACT_JUDEGE_DAY__C = Date.today().addDays(0);  // 维修合同判断日がサービス契約の中間辺りの日付
        insert repair01;
        
        Asset asset02 = createAsset( hospital, strategicDep[0], dep);
//System.Test.startTest();
        Repair__c repair02 = new Repair__c();
        repair02.Account__c = dep.Id;
        repair02.Department_Class__c = strategicDep[0].Id;
        repair02.Hospital__c = hospital.Id;
        repair02.Maintenance_Contract__c = contract.Id;
        repair02.Delivered_Product__c = asset02.Id;
        repair02.Status__c = '2.维修报价阶段';
        repair02.SERVICE_CONTRACT_JUDEGE_DAY__C = Date.today().addDays(0);  // 维修合同判断日がサービス契約の中間辺りの日付
        insert repair02;
//        System.Test.stopTest();
	}
}