@isTest
private class OncallFinalStatusManagerTester {
    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
    private static Account hospital = null;
    private static List<Account> strategicDep = null;
    private static Product2 productA = null;
    private static Account dep = null;
    private static PricebookEntry entry = null;
    private static Asset asset = null;
    private static Maintenance_Contract__c contract = null;
    static {

        Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_GI').getRecordTypeId();

        // 病院を作る
        hospital = new Account();
        hospital.recordtypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
        hospital.Name = 'test hospital';
        insert hospital;
        /*
        为了对应0914的Opptrigger改变，新增
            测试User
        */
        //RecordType rt = Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('Target').getRecordTypeId();
        
        // システム管理者
       
       
        // 戦略科室を得る
        strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
        StaticParameter.EscapeAccountTrigger = true;
        StaticParameter.EscapeSyncProduct2Trigger = true;
        StaticParameter.EscapeMaintenanceContractAfterUpdateTrigger = true;
        // 診療科を作る
        dep = new Account();
        dep.recordtypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_GI').getRecordTypeId();
        dep.Name = 'test dep';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;
        
        // 製品を作る
        productA = new Product2( Name='テスト商品');
        insert productA;
        
        // 価格表エントリを作成する     
        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 = new Asset();
        asset.Name = 'テスト機器';
        asset.AccountId = dep.Id;
        asset.Department_Class__c = strategicDep[0].Id;
        asset.Hospital__c = hospital.Id;
        asset.SerialNumber = 'testserial';
        asset.Guarantee_period_for_products__c = Date.today();
        asset.InstallDate                      = Date.today();
        insert asset;
        
        // 维修合同を作成する
        contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.Hospital__c = hospital.Id;
        contract.Department_Class__c = strategicDep[0].Id;
        contract.Department__c = dep.Id;
        insert contract;
    }

    static testMethod void test01() {
System.debug( '-----:test01');
      
        // On-Callをつくる
        On_Call__c onCall = new On_Call__c();
        onCall.segment__c = dep.Id;
        onCall.Hospital__c = hospital.Id;
        insert onCall;
        
        OncallFinalStatusManager.callCount = 0;
        
        // 修理を作成する
        Repair__c repair = new Repair__c();
        repair.Account__c = dep.Id;
        repair.Department_Class__c = strategicDep[0].Id;
        repair.Hospital__c = hospital.Id;
        repair.On_Call_ID__c = onCall.Id;
        repair.Delivered_Product__c = asset.Id;
        insert repair;

		OncallFinalStatusManager.callCount = 0;

        // QISを作る
        QIS_Report__c qis = new QIS_Report__c();
        qis.Source_OnCall__c = onCall.Id;
        qis.nonyushohin__c = asset.Id;
        qis.Hospital__c = hospital.Id;
        qis.RC__c = UserInfo.getUserId();
        qis.Department_Class__c = strategicDep[0].Id;
        qis.Hospital_Department__c = dep.Id;
        qis.Damage_For_Doc_Or_Pat__c = '有';
        qis.Relation_With_The_Problem__c = '有可能';
        qis.Report_For_Goz__c = '不知道';
        insert qis;
    
    }

    static testMethod void test02() {
System.debug( '-----:test02');
        // On-Callをつくる
        System.Test.startTest();
        On_Call__c onCall = new On_Call__c();
        onCall.segment__c = dep.Id;
        onCall.Hospital__c = hospital.Id;
        insert onCall;
        
        OncallFinalStatusManager.callCount = 0;
        
        // QISを作る
        QIS_Report__c qis = new QIS_Report__c();
        qis.Source_OnCall__c = onCall.Id;
        qis.nonyushohin__c = asset.Id;
        qis.Hospital__c = hospital.Id;
        qis.RC__c = UserInfo.getUserId();
        qis.Department_Class__c = strategicDep[0].Id;
        qis.Hospital_Department__c = dep.Id;
        qis.Damage_For_Doc_Or_Pat__c = '有';
        qis.Relation_With_The_Problem__c = '有可能';
        qis.Report_For_Goz__c = '不知道';
        insert qis;

		OncallFinalStatusManager.callCount = 0;

        // 修理を作成する
        Repair__c repair = new Repair__c();
        repair.Account__c = dep.Id;
        repair.Department_Class__c = strategicDep[0].Id;
        repair.Hospital__c = hospital.Id;
        repair.On_Call_ID__c = onCall.Id;
        repair.Delivered_Product__c = asset.Id;
        insert repair;

		// delete repair;
		// delete qis;
		update onCall;
        System.Test.stopTest();
}
	static testMethod void test03() {
System.debug( '-----:test03');
        // On-Callをつくる
        On_Call__c onCall = new On_Call__c();
        onCall.segment__c = dep.Id;
        onCall.Hospital__c = hospital.Id;
        insert onCall;
        OncallFinalStatusManager.callCount = 0;
        update onCall;
	}
}