@isTest
private class RepairTriggerTest {
   private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
    
    static testMethod void test01() {
        
        // 病院を作る
        Account 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;
        
        // 戦略科室を得る
        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;
        dep.State_Text__c = '海南省';
        insert 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[0].Id;
        asset.Hospital__c = hospital.Id;
        asset.SerialNumber = 'testserial';
        insert asset;
        
         Asset asset2 = new Asset();
        asset2.Name = 'テスト機器1';
        asset2.AccountId = dep.Id;
        asset2.Department_Class__c = strategicDep[0].Id;
        asset2.Hospital__c = hospital.Id;
        asset2.SerialNumber = 'testserial1';
        insert asset2;
        // 维修合同を作成する
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.Hospital__c = hospital.Id;
        contract.Department_Class__c = strategicDep[0].Id;
        contract.Contract_Start_Date__c = Date.today();
        contract.Status__c = '契約';
        contract.Contract_End_Date__c = Date.today().addMonths(12);
        contract.Department__c = dep.Id;
        contract.RecordTypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('VM_Contract').getRecordTypeId();
        insert contract;
        
        Maintenance_Contract__c contract2 = new Maintenance_Contract__c();
        contract2.Name = 'tect contract2';
        contract2.Hospital__c = hospital.Id;
        contract2.Department_Class__c = strategicDep[0].Id;
        contract2.Department__c = dep.Id;
        contract2.Status__c = '契約';
        contract2.Maintenance_Contract_No__c  = 'BJ-RS-SC0076290111';
        contract2.Contract_Start_Date__c = Date.today();
        contract2.Contract_End_Date__c = Date.today().addMonths(12);
        contract2.URF_Contract__c = true;
        contract2.agree_Upper_limit__c=true;
        contract2.RecordTypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('NewMaintenance_Contract').getRecordTypeId();
        insert contract2;
        MaintanceContractPack__c mcp = new MaintanceContractPack__c();
        Maintenance_Contract__c contract23 = new Maintenance_Contract__c();
        contract23.Name = 'tect contract2';
        contract23.Hospital__c = hospital.Id;
        contract23.Department_Class__c = strategicDep[0].Id;
        contract23.Department__c = dep.Id;
        contract23.Status__c = '契約';
        contract23.Maintenance_Contract_No__c  = 'BJ-RS-SC007629011111';
        contract23.Contract_Start_Date__c = Date.today();
        contract23.Contract_End_Date__c = Date.today().addMonths(12);
        contract23.agree_Upper_limit__c=true;
        contract23.Limit_Price__c=true;
        contract23.MaintanceContractPack__c = mcp.id;
        contract23.RecordTypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('NewMaintenance_Contract').getRecordTypeId();
        insert contract23;
        Maintenance_Contract__c contract3 = new Maintenance_Contract__c();
        contract3.Name = 'tect contract2';
        contract3.Hospital__c = hospital.Id;
        contract3.Department_Class__c = strategicDep[0].Id;
        contract3.Department__c = dep.Id;
        contract3.Status__c = '契約';
        contract3.Maintenance_Contract_No__c  = 'BJ-RS-SC0076290111111';
        contract3.Contract_Start_Date__c = Date.today();
        contract3.Contract_End_Date__c = Date.today().addMonths(12);
        contract3.agree_Upper_limit__c=true;
        contract3.Limit_Price__c=true;
        contract3.RecordTypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('NewMaintenance_Contract').getRecordTypeId();
        insert contract3;
        Maintenance_Contract_Asset__c middleTable = new Maintenance_Contract_Asset__c();
        middleTable.Asset__c = asset.Id;
        middleTable.Maintenance_Contract__c = contract.id;
        middleTable.endDateGurantee_Text__c = Date.today().addDays(1);
        insert middleTable;
        Maintenance_Contract_Asset__c middleTable2 = new Maintenance_Contract_Asset__c();
        middleTable2.Asset__c = asset.Id;
        middleTable2.Maintenance_Contract__c = contract2.id;
        middleTable2.endDateGurantee_Text__c = Date.today().addDays(1);
        insert middleTable2;
        
        // 修理を作成する
        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 = asset.Id;
        repair.NewProductGuarante_Txt__c = '2: 服务多年保修';
        repair.Service_contract_judege_day__c = Date.today().addMonths(1);
        repair.Aware_date__c = Date.today();
        insert repair;
        // Repair__c repair2 = new Repair__c();
        // repair2.Account__c = dep.Id;
        // repair2.Department_Class__c = strategicDep[0].Id;
        // repair2.Hospital__c = hospital.Id;
        // repair2.Delivered_Product__c = asset.Id;
        // insert repair2;
        
        // 未決
        
    }
    static testMethod void test02() {
          // 病院を作る
        Account 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;
        
        // 戦略科室を得る
        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;
        dep.State_Text__c = '海南省';
        insert 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[0].Id;
        asset.Hospital__c = hospital.Id;
        asset.SerialNumber = 'testserial';
        insert asset;
        
         Asset asset2 = new Asset();
        asset2.Name = 'テスト機器1';
        asset2.AccountId = dep.Id;
        asset2.Department_Class__c = strategicDep[0].Id;
        asset2.Hospital__c = hospital.Id;
        asset2.SerialNumber = 'testserial1';
        insert asset2;
        MaintanceContractPack__c mcp = new MaintanceContractPack__c();
        Maintenance_Contract__c contract23 = new Maintenance_Contract__c();
        contract23.Name = 'tect contract2';
        contract23.Hospital__c = hospital.Id;
        contract23.Department_Class__c = strategicDep[0].Id;
        contract23.Department__c = dep.Id;
        contract23.Status__c = '契約';
        contract23.Maintenance_Contract_No__c  = 'BJ-RS-SC007629011111';
        contract23.Contract_Start_Date__c = Date.today();
        contract23.Contract_End_Date__c = Date.today().addMonths(12);
        contract23.agree_Upper_limit__c=true;
        contract23.Limit_Price__c=true;
        contract23.MaintanceContractPack__c = mcp.id;
        contract23.RecordTypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('NewMaintenance_Contract').getRecordTypeId();
        insert contract23;
        Maintenance_Contract__c contract3 = new Maintenance_Contract__c();
        contract3.Name = 'tect contract2';
        contract3.Hospital__c = hospital.Id;
        contract3.Department_Class__c = strategicDep[0].Id;
        contract3.Department__c = dep.Id;
        contract3.Status__c = '契約';
        contract3.Maintenance_Contract_No__c  = 'BJ-RS-SC0076290111111';
        contract3.Contract_Start_Date__c = Date.today();
        contract3.Contract_End_Date__c = Date.today().addMonths(12);
        contract3.agree_Upper_limit__c=true;
        contract3.Limit_Price__c=true;
        contract3.RecordTypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('NewMaintenance_Contract').getRecordTypeId();
        insert contract3;
        Maintenance_Contract_Asset__c middleTable3 = new Maintenance_Contract_Asset__c();
        middleTable3.Asset__c = asset.Id;
        middleTable3.Maintenance_Contract__c = contract23.id;
        middleTable3.endDateGurantee_Text__c = Date.today().addDays(1);
        insert middleTable3;
        Maintenance_Contract_Asset__c middleTable4 = new Maintenance_Contract_Asset__c();
        middleTable4.Asset__c = asset.Id;
        middleTable4.Maintenance_Contract__c = contract3.id;
        middleTable4.endDateGurantee_Text__c = Date.today().addDays(1);
        insert middleTable4;

        // 修理を作成する
        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 = asset.Id;
        repair.NewProductGuarante_Txt__c = '2: 服务多年保修';
        repair.Service_contract_judege_day__c = Date.today().addMonths(1);
        repair.Aware_date__c = Date.today();
        insert repair;
    }
    static testMethod void test03() {
// 病院を作る
        Account 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;
        
        // 戦略科室を得る
        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;
        dep.State_Text__c = '海南省';
        insert 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[0].Id;
        asset.Hospital__c = hospital.Id;
        asset.SerialNumber = 'testserial';
        insert asset;
        
         Asset asset2 = new Asset();
        asset2.Name = 'テスト機器1';
        asset2.AccountId = dep.Id;
        asset2.Department_Class__c = strategicDep[0].Id;
        asset2.Hospital__c = hospital.Id;
        asset2.SerialNumber = 'testserial1';
        insert asset2;
        // 维修合同を作成する
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.Hospital__c = hospital.Id;
        contract.Department_Class__c = strategicDep[0].Id;
        contract.Contract_Start_Date__c = Date.today();
        contract.Status__c = '契約';
        contract.Contract_End_Date__c = Date.today().addMonths(12);
        contract.Department__c = dep.Id;
        contract.RecordTypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('VM_Contract').getRecordTypeId();
        insert contract;
        
        Maintenance_Contract__c contract2 = new Maintenance_Contract__c();
        contract2.Name = 'tect contract2';
        contract2.Hospital__c = hospital.Id;
        contract2.Department_Class__c = strategicDep[0].Id;
        contract2.Department__c = dep.Id;
        contract2.Status__c = '契約';
        contract2.Maintenance_Contract_No__c  = 'BJ-RS-SC0076290111';
        contract2.Contract_Start_Date__c = Date.today();
        contract2.Contract_End_Date__c = Date.today().addMonths(12);
        contract2.URF_Contract__c = true;
        contract2.agree_Upper_limit__c=true;
        contract2.RecordTypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('NewMaintenance_Contract').getRecordTypeId();
        insert contract2;
      
        Maintenance_Contract_Asset__c middleTable = new Maintenance_Contract_Asset__c();
        middleTable.Asset__c = asset.Id;
        middleTable.Maintenance_Contract__c = contract.id;
        middleTable.endDateGurantee_Text__c = Date.today().addDays(1);
        insert middleTable;
        Maintenance_Contract_Asset__c middleTable2 = new Maintenance_Contract_Asset__c();
        middleTable2.Asset__c = asset.Id;
        middleTable2.Maintenance_Contract__c = contract2.id;
        middleTable2.endDateGurantee_Text__c = Date.today().addDays(1);
        insert middleTable2;
        RepairSubOrder__c subOrder = new RepairSubOrder__c();
        subOrder.AttachmentDownload__c = true;
        subOrder.AttachmentLink__c = 'https://albsylfw.s3.cn-northwest-1.amazonaws.com.cn/20211220/14/11/30/lessthan12/5e8cb69e-f935-48f5-825e-b8344d60f6d4.zip';
        subOrder.AttachmentName__c = '测试附件下载';
        subOrder.Status__c = '待处理';
        insert subOrder;
        // 修理を作成する
        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 = asset.Id;
        repair.AwaitToSendAWS__c = false;
        repair.RepairSubOrder__c = subOrder.id;
        repair.engineerSendDate__c = Date.today();
        repair.DescriptionName__c = 'xxxxxxxxxxxxx';
        insert repair;
    }
     static testMethod void test04() {
// 病院を作る
        Account 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;
        
        // 戦略科室を得る
        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;
        dep.State_Text__c = '海南省';
        insert 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[0].Id;
        asset.Hospital__c = hospital.Id;
        asset.SerialNumber = 'testserial';
        insert asset;
        
         Asset asset2 = new Asset();
        asset2.Name = 'テスト機器1';
        asset2.AccountId = dep.Id;
        asset2.Department_Class__c = strategicDep[0].Id;
        asset2.Hospital__c = hospital.Id;
        asset2.SerialNumber = 'testserial1';
        insert asset2;
        // 维修合同を作成する
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.Hospital__c = hospital.Id;
        contract.Department_Class__c = strategicDep[0].Id;
        contract.Contract_Start_Date__c = Date.today();
        contract.Status__c = '契約';
        contract.Contract_End_Date__c = Date.today().addMonths(12);
        contract.Department__c = dep.Id;
        contract.RecordTypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('VM_Contract').getRecordTypeId();
        insert contract;
        
        Maintenance_Contract__c contract2 = new Maintenance_Contract__c();
        contract2.Name = 'tect contract2';
        contract2.Hospital__c = hospital.Id;
        contract2.Department_Class__c = strategicDep[0].Id;
        contract2.Department__c = dep.Id;
        contract2.Status__c = '契約';
        contract2.Maintenance_Contract_No__c  = 'BJ-RS-SC0076290111';
        contract2.Contract_Start_Date__c = Date.today();
        contract2.Contract_End_Date__c = Date.today().addMonths(12);
        contract2.URF_Contract__c = true;
        contract2.agree_Upper_limit__c=true;
        contract2.RecordTypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('NewMaintenance_Contract').getRecordTypeId();
        insert contract2;
      
        Maintenance_Contract_Asset__c middleTable = new Maintenance_Contract_Asset__c();
        middleTable.Asset__c = asset.Id;
        middleTable.Maintenance_Contract__c = contract.id;
        middleTable.endDateGurantee_Text__c = Date.today().addDays(1);
        insert middleTable;
        Maintenance_Contract_Asset__c middleTable2 = new Maintenance_Contract_Asset__c();
        middleTable2.Asset__c = asset.Id;
        middleTable2.Maintenance_Contract__c = contract2.id;
        middleTable2.endDateGurantee_Text__c = Date.today().addDays(1);
        insert middleTable2;
        RepairSubOrder__c subOrder = new RepairSubOrder__c();
        subOrder.AttachmentDownload__c = true;
        subOrder.AttachmentLink__c = 'https://albsylfw.s3.cn-northwest-1.amazonaws.com.cn/20211220/14/11/30/lessthan12/5e8cb69e-f935-48f5-825e-b8344d60f6d4.zip';
        subOrder.AttachmentName__c = '测试附件下载';
        subOrder.Status__c = '待处理';
        insert subOrder;
        // 修理を作成する
        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 = asset.Id;
        repair.AwaitToSendAWS__c = false;
        repair.RepairSubOrder__c = subOrder.id;
        repair.engineerSendDate__c = Date.today();
        repair.DescriptionName__c = 'xxxxxxxxxxxxx';
        repair.Exc_work_location__c = false;
        insert repair;
    }
}