@isTest
public with sharing class RepairReceiptTriggerTest {
    public RepairReceiptTriggerTest() {

    }

    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
    static Account createHospital( String hospitalName , String Sales) {
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        // 病院を作る
        Account hospital = new Account();
        Id AgencyRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        hospital.recordtypeId = AgencyRecordTypeId;
        hospital.Name = hospitalName;
        hospital.Sales_Shop_Class__c = Sales;
        insert hospital;
        StaticParameter.EscapeAccountTrigger = true;
        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;
        dep.Sales_Shop_Class__c = '特約販売店（区域）';
        // dep.State_Text__c = '上海市';
        insert dep;
        return dep;
    }

    // static Asset createAsset( Account hospital, Account strategicDep, Account dep) {
    static Asset createAsset( Account hospital) {

        // 製品を作る
        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.AccountId = hospital.Id;
        asset.SerialNumber = 'testserial';
        asset.Quantity = 3;
        // asset.Extend_Gurantee_DateTo_Text__c =Date.today().addDays(30);
        // asset.IS_Extend_Gurantee_Txt__c =true;
        // asset.Order_No__c = 'BJ_2020';

        insert asset;

        return asset;
    }

    @isTest
    static void test(){
        ControllerUtil.EscapeNFM001Trigger = true;
        // 病院、戦略科室、診療科の情報を作成します
        Account hospital1 = createHospital( 'test hospital1' , '特約販売店（区域）');
        Account hospital2 = createHospital( 'test hospital2' , '医疗修理经销商');

        Account hospital3 = createHospital( 'test hospital3test hospital3test hospital3test hospital3test hospital3test hospital3test hospital3test hospital3test hospital3test hospital3test hospital3' , '特約販売店（区域）');
        Account hospital4 = createHospital( 'test hospital4' , '一級販売店');
        // Account[] strategicDep = selectStrategicDep( hospital1);
        // Account dep = createDep( hospital1, strategicDep[0]);

        // 納入機器を作る
        Asset asset = createAsset( hospital1);
        // 维修合同を作成する
        // Maintenance_Contract__c contract = createMaintenanceContract( hospital1, strategicDep[0], dep);
        // 中間テーブルを設ける
        // Maintenance_Contract_Asset__c middleTable = createMiddleTable( asset, contract);


        // 修理を作成する01
        Repair__c repair01 = new Repair__c();
        // repair01.Account__c = dep.Id;
        // repair01.Department_Class__c = strategicDep[0].Id;
        repair01.Dealer__c = hospital1.Id;
        repair01.Delivered_Product__c = asset.Id;
        repair01.SERVICE_CONTRACT_JUDEGE_DAY__C = Date.today().addDays( -1000); // 维修合同判断日がサービス契約のだいぶ前
        repair01.Failure_Occurrence_Date__c  =Date.today();
        repair01.BilltoPartyNoAccount__c = '';
        insert repair01;

        // 修理を作成する02
        Repair__c repair02 = new Repair__c();
        // repair02.Account__c = dep.Id;
        // repair02.Department_Class__c = strategicDep[0].Id;
        repair02.Dealer__c = hospital3.Id;
        repair02.Delivered_Product__c = asset.Id;
        repair02.SERVICE_CONTRACT_JUDEGE_DAY__C = Date.today().addDays( -1000); // 维修合同判断日がサービス契約のだいぶ前
        repair02.Failure_Occurrence_Date__c  =Date.today();
        repair02.BilltoPartyNoAccount__c = '南京乐硕贸易有限公司/南京乐硕贸易有限公司/南京乐硕贸易有限公司/南京乐硕贸易有限公司/南京乐硕贸易有限公司/南京乐硕贸易有限公司/南京乐硕贸易有限公司/南京乐硕贸易有限公司/南京乐硕贸易有限公司/南京乐硕贸易有限公司/南京乐硕贸易有限公司/南京乐硕贸易有限公司/南京乐硕贸易有限公司';
        insert repair02;

        Date toDate = Date.today();
        Date expirationDate = toDate.toStartOfMonth()-1;
        Oly_TriggerHandler.bypass('RepairReceiptHandler');
        Repair_receipt__c rr = new Repair_receipt__c();
        rr.Name = '123456';
        rr.UniqueKey__c = '1234567';
        rr.Repair__c = System.Label.Olympus_Dummy_Repair;
        rr.Maintenance_Contract__c =  System.Label.Olympus_Dummy_MC;
        rr.PaymentAmount__c = Decimal.valueOf('1235');
        //rr.PaymentDate__c = NFMUtil.parseStr2Date('20190422');
        rr.PaymentDate__c = expirationDate;
        rr.Repair__c = repair01.Id;
        rr.BilltoPartyNoAccount__c = hospital1.Id;
        rr.DeadlineLastMonthPeriodPaymentAmountFigu__c = 2;

        Repair_receipt__c rr2 = new Repair_receipt__c();
        rr2.Name = '123456-1';
        rr2.UniqueKey__c = '20200813';
        rr2.Repair__c = System.Label.Olympus_Dummy_Repair;
        rr2.Maintenance_Contract__c =  System.Label.Olympus_Dummy_MC;
        rr2.PaymentAmount__c = Decimal.valueOf('1235');
        //rr.PaymentDate__c = NFMUtil.parseStr2Date('20190422');
        rr2.PaymentDate__c = expirationDate;
        rr2.Repair__c = repair01.Id;
        rr2.BilltoPartyNoAccount__c = hospital2.Id;
        rr2.DeadlineLastMonthPeriodPaymentAmountFigu__c = 2;

        Repair_receipt__c rr3 = new Repair_receipt__c();
        rr3.Name = '123456-11';
        rr3.UniqueKey__c = '20200815';
        rr3.Repair__c = System.Label.Olympus_Dummy_Repair;
        rr3.Maintenance_Contract__c =  System.Label.Olympus_Dummy_MC;
        rr3.PaymentAmount__c = Decimal.valueOf('1235');
        //rr3.PaymentDate__c = NFMUtil.parseStr2Date('20190422');
        rr3.PaymentDate__c = expirationDate;
        rr3.Repair__c = repair02.Id;
        rr3.BilltoPartyNoAccount__c = hospital3.Id;
        rr3.DeadlineLastMonthPeriodPaymentAmountFigu__c = 2;

        Repair_receipt__c rr4 = new Repair_receipt__c();
        rr4.Name = '123456-12';
        rr4.UniqueKey__c = '20200825';
        rr4.Repair__c = System.Label.Olympus_Dummy_Repair;
        rr4.Maintenance_Contract__c =  System.Label.Olympus_Dummy_MC;
        rr4.PaymentAmount__c = Decimal.valueOf('1235');
        //rr4.PaymentDate__c = NFMUtil.parseStr2Date('20190422');
        rr4.PaymentDate__c = expirationDate;
        rr4.Repair__c = repair02.Id;
        rr4.BilltoPartyNoAccount__c = hospital4.Id;
        rr4.DeadlineLastMonthPeriodPaymentAmountFigu__c = 2;

        List<Repair_receipt__c> rcs = new List<Repair_receipt__c>();
        rcs.add(rr);
        rcs.add(rr2);
        rcs.add(rr3);
        rcs.add(rr4);
        

        System.debug('repair01+++'+repair01);
        insert rcs;
        update rcs;

        // repair01 = [select id,name,BilltoPartyNoAccount__c from Repair__c where id = :repair01.Id];
        repair01 = [select id,name from Repair__c where id = :repair01.Id];
        System.debug('repair  01+++'+repair01);
        System.debug('111111111');
        // System.assertEquals(repair01.BilltoPartyNoAccount__c, 'test hospital1/test hospital2');
    } 



}