@isTest
private class RentalApplyBeforeUpdateTriggerTest {
    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;

    static testMethod void myUnitTest() {
        OlympusCalendar__c oc1 = new OlympusCalendar__c(Date__c = Date.today().addDays(1), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc2 = new OlympusCalendar__c(Date__c = Date.today().addDays(2), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc3 = new OlympusCalendar__c(Date__c = Date.today().addDays(3), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc4 = new OlympusCalendar__c(Date__c = Date.today().addDays(4), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc5 = new OlympusCalendar__c(Date__c = Date.today().addDays(5), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc6 = new OlympusCalendar__c(Date__c = Date.today().addDays(6), ChangeToHoliday__c=true, ChangeToWorkday__c=false);
        OlympusCalendar__c oc7 = new OlympusCalendar__c(Date__c = Date.today().addDays(7), ChangeToHoliday__c=true, ChangeToWorkday__c=false);
        OlympusCalendar__c oc8 = new OlympusCalendar__c(Date__c = Date.today().addDays(8), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc9 = new OlympusCalendar__c(Date__c = Date.today().addDays(9), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc10 = new OlympusCalendar__c(Date__c = Date.today().addDays(10), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc11 = new OlympusCalendar__c(Date__c = Date.today().addDays(11), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc12 = new OlympusCalendar__c(Date__c = Date.today().addDays(12), ChangeToHoliday__c=false, ChangeToWorkday__c=true);

        insert new OlympusCalendar__c[] {oc1,oc2,oc3,oc4,oc5,oc6,oc7,oc8,oc9,oc10,oc11,oc12};

        Rental_Apply__c ra = new Rental_Apply__c();
        ra.Request_shipping_day__c = Date.today().addDays(3);
        ra.Request_return_day__c= Date.today().addDays(8);
        ra.demo_purpose2__c = '其他';
        insert ra;

        Rental_Apply__c result = [select loaner_date__c from Rental_Apply__c where id = :ra.Id];
        //System.assertEquals(3, result.loaner_date__c);
    }

    // 测试删除check
    static testMethod void myUnitTest2() {
        Oly_TriggerHandler.bypass('RentalApplyTriggerHandler');
        Rental_Apply__c rentalApply =  new Rental_Apply__c();
        rentalApply.Status__c = FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Yi_Chu_Ku_Zhi_Shi.ordinal());
        rentalApply.Request_return_day__c= Date.today().addDays(32);
        insert rentalApply;
        Oly_TriggerHandler.clearAllBypasses();

        try {
            delete rentalApply;
        } catch (Exception e) {

        }
        Rental_Apply__c[] raList = [select Id from Rental_Apply__c where Id = :rentalApply.Id];
        System.assertEquals(1, raList.size());
    }

    // 测试申请分割
    static testMethod void myUnitTest3() {
        Rental_Apply__c ra = new Rental_Apply__c();
        ra.Name = '*';
        ra.Request_shipping_day__c = Date.today().addDays(3);
        ra.Request_return_day__c= Date.today().addDays(8);
        ra.demo_purpose2__c = '其他';
        insert ra;

        List<Rental_Apply__c> raList = [select Id, Name from Rental_Apply__c where Id = :ra.Id];
        String n = raList[0].Name;

        Rental_Apply__c ra1 = new Rental_Apply__c();
        ra1.Name = n;
        ra1.Request_shipping_day__c = Date.today().addDays(3);
        ra1.Request_return_day__c= Date.today().addDays(8);
        ra1.Old_Rental_Apply__c = ra.Id;
        ra1.demo_purpose2__c = '其他';
        insert ra1;

        List<Rental_Apply__c> raList1 = [select Id, Name from Rental_Apply__c where Id = :ra1.Id];
        System.assertEquals(n, raList1[0].Name);

        Rental_Apply__c ra2 = new Rental_Apply__c();
        ra2.Name = n;
        ra2.Request_shipping_day__c = Date.today().addDays(3);
        ra2.Request_return_day__c= Date.today().addDays(8);
        ra2.Old_Rental_Apply__c = ra.Id;
        ra2.demo_purpose2__c = '其他';
        insert ra2;

        List<Rental_Apply__c> raList2 = [select Id, Name from Rental_Apply__c where Id = :ra2.Id];
        System.assertEquals(n, raList2[0].Name);
    }

    // 测试修理申请check
    static testMethod void myUnitTest4() {
        // 病院を作る
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE 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 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;

        // 製品を作る
        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;

        // 修理做成
        Repair__c repair01 = new Repair__c();
        repair01.Account__c = dep.Id;
        repair01.Department_Class__c = strategicDep[0].Id;
        repair01.Hospital__c = hospital.Id;
        repair01.Delivered_Product__c = asset.Id;
        repair01.SERVICE_CONTRACT_JUDEGE_DAY__C = Date.today().addDays(0);  // 维修合同判断日がサービス契約の中間辺りの日付
        insert repair01;

        Rental_Apply__c ra = new Rental_Apply__c();
        ra.Name = 'test';
        ra.Request_shipping_day__c = Date.today().addDays(3);
        ra.Request_return_day__c= Date.today().addDays(8);
        ra.Repair__c = repair01.Id;
        ra.demo_purpose2__c = '其他';
        insert ra;

        Rental_Apply__c ra1 = new Rental_Apply__c();
        ra1.Name = 'test1';
        ra1.Request_shipping_day__c = Date.today().addDays(3);
        ra1.Request_return_day__c= Date.today().addDays(8);
        ra1.Repair__c = repair01.Id;
        ra1.demo_purpose2__c = '其他';
        try {
            insert ra1;
        } catch (Exception ex) {
            // 同一个型号的修理记录不能重复申请备品
        }

        Rental_Apply__c ra2 = new Rental_Apply__c();
        ra2.Name = 'test2';
        ra2.Request_shipping_day__c = Date.today().addDays(3);
        ra2.Request_return_day__c= Date.today().addDays(8);
        ra2.demo_purpose2__c = '其他';
        insert ra2;

        ra2.Repair__c = repair01.Id;
        ra2.HP_received_sign_rich__c = 'hhh';
        ra2.HP_received_sign_NG__c = false;
        try {
            update ra2;
        } catch (Exception ex) {
            // 同一个型号的修理记录不能重复申请备品
        }
    }
    // 测试QIS申请check
    static testMethod void myUnitTest5() {

        // 病院を作る
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE 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 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;

        // 製品を作る
        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;

        // QIS做成
        QIS_Report__c qis = new QIS_Report__c();
        qis.name = '1';
        qis.RecordTypeID = [Select Id FROM RecordType WHERE DeveloperName = 'RC'].id;
        insert qis;

        Rental_Apply__c ra = new Rental_Apply__c();
        ra.Name = 'test';
        ra.Request_shipping_day__c = Date.today().addDays(3);
        ra.Request_return_day__c= Date.today().addDays(8);
        ra.QIS_number__c = qis.Id;
        ra.demo_purpose2__c = '其他';
        insert ra;

        Rental_Apply__c ra1 = new Rental_Apply__c();
        ra1.Name = 'test1';
        ra1.Request_shipping_day__c = Date.today().addDays(3);
        ra1.Request_return_day__c= Date.today().addDays(8);
        ra1.QIS_number__c = qis.Id;
        ra1.demo_purpose2__c = '其他';
        try {
            insert ra1;
        } catch (Exception ex) {
            // 同一个QIS记录不能重复申请备品
        }

        Rental_Apply__c ra2 = new Rental_Apply__c();
        ra2.Name = 'test2';
        ra2.Request_shipping_day__c = Date.today().addDays(3);
        ra2.Request_return_day__c= Date.today().addDays(8);
        ra2.demo_purpose2__c = '其他';
        ra2.HP_received_sign_rich__c = 'hhh';
        insert ra2;

        ra2.QIS_number__c = qis.Id;
        ra2.HP_received_sign_NG__c = true;
        ra2.HP_received_sign_rich__c = 'aaaaa';
        try {
            update ra2;
        } catch (Exception ex) {
            // 同一个QIS记录不能重复申请备品
        }
    }
}