@isTest
private class MaintenanceContractAssetTriggerTester {
	
    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 asset01 = null;
    private static Asset asset02 = null;
    private static Asset asset03 = null;
    private static Maintenance_Contract__c contract = null;
    private static Maintenance_Contract__c contract1 = null;
    static {
        // 病院を作る
        hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital.Name = 'test hospital';
        insert hospital;
        
        // 戦略科室を得る
        strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
        
        // 診療科を作る
        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;
        
        // 製品を作る
        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;
        
        // 納入機器を作成する
        asset01 = new Asset();
        asset01.Name = 'テスト機器';
        asset01.AccountId = dep.Id;
        asset01.Department_Class__c = strategicDep[0].Id;
        asset01.Hospital__c = hospital.Id;
        asset01.SerialNumber = 'testserial';
        insert asset01;
        asset02 = new Asset();
        asset02.Name = 'テスト機器';
        asset02.AccountId = dep.Id;
        asset02.Department_Class__c = strategicDep[0].Id;
        asset02.Hospital__c = hospital.Id;
        asset02.SerialNumber = 'testserial';
        insert asset02;
        asset03 = new Asset();
        asset03.Name = 'テスト機器';
        asset03.AccountId = dep.Id;
        asset03.Department_Class__c = strategicDep[0].Id;
        asset03.Hospital__c = hospital.Id;
        asset03.SerialNumber = 'testserial';
        insert asset03;
        
        // 维修合同を作成する
        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;
        // contract.RecordType.name == '服务合同';
        contract.Contract_Start_Date__c = Date.today() - 10; // 2000/07/09
        contract.Contract_End_Date__c = Date.today() + 10;
        insert contract;

        contract1 = new Maintenance_Contract__c();
        contract1.Name = 'tect1 contract';
        contract1.Hospital__c = hospital.Id;
        contract1.Department_Class__c = strategicDep[0].Id;
        contract1.Department__c = dep.Id;
        contract1.Contract_Start_Date__c = Date.today() - 10;
        contract1.Contract_End_Date__c = Date.today() + 10;

        insert contract1;
        
    }

    static testMethod void myTest01() {
        // 修理を作成する
        Repair__c repair = new Repair__c();
        repair.Account__c = dep.Id;
        repair.Department_Class__c = strategicDep[0].Id;
        repair.Hospital__c = hospital.Id;
        repair.Delivered_Product__c = asset01.Id;
        insert repair;
    	
        // 维修合同に納入商品を関連付ける
        List<Maintenance_Contract_Asset__c> insertTarget = new List<Maintenance_Contract_Asset__c>();
        
        Maintenance_Contract_Asset__c target01 = new Maintenance_Contract_Asset__c();
        target01.Maintenance_Contract__c = contract.Id;
        target01.Asset__c = asset01.Id;
        target01.LastMContract__c = contract1.Id;
        insertTarget.add( target01);
        
        // Maintenance_Contract_Asset__c target02 = new Maintenance_Contract_Asset__c();
        // target02.Maintenance_Contract__c = contract.Id;
        // target02.Asset__c = asset02.Id;
        // // target02.LastMContract__c = contract1.Id;
        // insertTarget.add( target02);
        
        // Maintenance_Contract_Asset__c target03 = new Maintenance_Contract_Asset__c();
        // target03.Maintenance_Contract__c = contract.Id;
        // target03.Asset__c = asset03.Id;
        // // target03.LastMContract__c = contract1.Id;
        // insertTarget.add( target03);
        
        insert insertTarget;
        
    }
}