/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class RentalHpDeptUpdTriggerTest {

    static testMethod void myUnitTest() {
        
        ControllerUtil.EscapeNFM001Trigger = true;
        ControllerUtil.EscapeMaintenanceContractAfterUpdateTrigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
        StaticParameter.EscapeMaintenanceContractAfterUpdateTrigger = true;
        Oly_TriggerHandler.bypass('RentalApplyTriggerHandler');
        // recode type を取得
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科') order by Name desc];
        if (rectDpt.size() == 0) {
            return;
        }

        // insert
        Account company1 = new Account();
        Account company2 = new Account();
        company1.RecordTypeId = rectCo[0].Id;
        company1.Name = '病院テスト1';
        company2.RecordTypeId = rectCo[0].Id;
        company2.Name = '病院テスト2';
        List<Account> hps = new Account[] {company1, company2};
        insert hps;
  
        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c];
        List<Account> dc2s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company2.Id order by Department_Class_Label__c];

        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = company1.Id;
        
        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[1].Id;
        depart2.Name         = '*';
        depart2.Department_Name__c  = '診療科2';
        depart2.ParentId            = dc2s[1].Id;
        depart2.Department_Class__c = dc2s[1].Id;
        depart2.Hospital__c         = company2.Id;

        insert new Account[] {depart1, depart2};

        //add by rentx 2020-11-18 
        OPDPlan__c oPDPlan = new OPDPlan__c();
        oPDPlan.Status__c = '计划中';
        //注释 by deloitte-allen 触发验证规则报错
        //oPDPlan.OPDPlan_ImplementDate__c = Date.today();
        oPDPlan.NoOpp_Reason__c = 'HCP对应';
        oPDPlan.OPDLendSortDraft__c = 1;
        oPDPlan.OPDType__c = '事件';
        //insert oPDPlan;
        
        
        // 備品作成
        Rental_Apply__c ra1 = new Rental_Apply__c(
                Hospital__c = company1.Id,
                // Demo_purpose1__c = '产品试用',
                // demo_purpose2__c = '试用（无询价）'
                Demo_purpose1__c='其他',
                demo_purpose2__c='其他'
              , Strategic_dept__c = dc1s[0].Id
              , Account__c = depart1.Id
              ,Request_return_day__c = Date.today()+28,
              //OPDPlan__c = oPDPlan.Id,
            Hope_Lonaer_date_Num__c = 10
        );
        insert ra1;

        OPDPlan__c oPDPlan1 = new OPDPlan__c();
        oPDPlan1.Status__c = '计划中';
        oPDPlan1.OPDPlan_ImplementDate__c = Date.today();
        oPDPlan1.NoOpp_Reason__c = 'HCP对应';
        oPDPlan1.Rental_Apply2__c = ra1.Id;
        oPDPlan1.OPDType__c = '日报';
        //insert oPDPlan1;

        Rental_Apply__c ra2 = new Rental_Apply__c(
                Hospital__c = company2.Id,
                // Demo_purpose1__c = '产品试用',
                // demo_purpose2__c = '试用（无询价）'
                Demo_purpose1__c='其他',
                demo_purpose2__c='其他'
              , Strategic_dept__c = dc2s[1].Id
              , Account__c = depart2.Id
              ,Request_return_day__c =Date.today()+28
              //OPDPlan__c =oPDPlan1.Id
        );
        insert ra2;
        // 产品
        Product2 pro5 = new Product2(Name='name05',IsActive=true,Family='SP',
                Fixture_Model_No__c='n05',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'n05',
                ProductCode_Ext__c='pc05',Manual_Entry__c=false);
        insert pro5;

        // 备品配套(只有附属品)
        Fixture_Set__c fsObjC1 = new Fixture_Set__c();
        fsObjC1.Name = 'setC1';
        fsObjC1.Fixture_Set_Body_Model_No__c = 'modelNoC1';
        fsObjC1.Loaner_name__c = 'nameC1';
        insert fsObjC1;
        // 备品配套明细(只有附属品)
        Fixture_Set_Detail__c fsdObjC1 = new Fixture_Set_Detail__c();
        fsdObjC1.Name = 'detailAccessory';
        fsdObjC1.Name_CHN_Created__c = 'detailAccessoryName';
        fsdObjC1.Product2__c = pro5.Id;
        fsdObjC1.Fixture_Set__c = fsObjC1.Id;
        fsdObjC1.Is_Body__c = false;
        fsdObjC1.Is_Optional__c = true;
        fsdObjC1.UniqueKey__c = fsObjC1.Id + ':' + pro5.Id;
        fsdObjC1.SortInt__c = 1;

        insert fsdObjC1;
        
        Rental_Apply_Equipment_Set__c raesObjC1 = new Rental_Apply_Equipment_Set__c();
        raesObjC1.Rental_Apply__c = ra1.Id;
        raesObjC1.Fixture_Set__c = fsObjC1.Id;
        raesObjC1.Cancel_Select__c = false;
        raesObjC1.Shipment_request_time__c = Date.toDay();
        raesObjC1.Rental_Start_Date__c = Date.toDay();
        raesObjC1.Rental_End_Date__c = Date.toDay();
        raesObjC1.IndexFromUniqueKey__c = 1;
        raesObjC1.UniqueKey__c = '1:'+ fsObjC1.Id + ':1';
        insert raesObjC1;

        Rental_Apply_Equipment_Set_Detail__c raesd = new Rental_Apply_Equipment_Set_Detail__c();
        raesd.name='test';
        raesd.Rental_Apply_Equipment_Set__c =raesObjC1.id;
        raesd.Rental_Apply__c = ra1.Id;
        raesd.Fixture_Set_Detail__c = fsdObjC1.Id;
        raesd.Cancel_Select__c=false;
        //raesd.Shippment_loaner_time__c =Date.today();
        raesd.IndexFromUniqueKey__c = 1;
        raesd.UniqueKey__c = '1:'+ raesObjC1.Id + ':' + fsdObjC1.Id + ':1';
        raesd.Salesdepartment_before__c = '0.备品中心';
        raesd.Internal_asset_location_before__c = '北京 备品中心';
        raesd.Product_category_text__c = 'GI';
        raesd.Equipment_Type_text__c = '产品试用';
        //insert raesd;
        
        //raesd.Shippment_loaner_time__c =Date.today();
        //update raesd;
        ra1.Account__c              = depart2.Id;
        ra2.Account__c              = depart1.Id;
        //update by rentx 2020-11-18 start
        ra2.Status__c               = '取消';
        ra2.Cancel_Reason__c        = '主动取消';
        ra2.RA_Status_Text__c       = '取消';
        //update by rentx 2020-11-18 end

        //update new Rental_Apply__c[] {ra1, ra2};

        
        //2021-11-23  mzy  SWAG-C8E6LE 【委托】OPD计划状态调整改善  start
        ra1.Asset_return_time_OK__c = Date.today();
        update new Rental_Apply__c[] {ra1};
        //2021-11-23  mzy  SWAG-C8E6LE 【委托】OPD计划状态调整改善  end
        
        //まずひとつ
      //  Rental_Apply__c raChanged1 = [Select Id, Name, Strategic_dept__c, Hospital__c from Rental_Apply__c where Id = :ra1.Id ];
      //  System.assertEquals(raChanged1.Strategic_dept__c, dc2s[1].Id);
       // System.assertEquals(raChanged1.Hospital__c , company2.Id);

      //  Rental_Apply__c raChanged2 = [Select Id, Name, Strategic_dept__c, Hospital__c from Rental_Apply__c where Id = :ra2.Id];        
      //  System.assertEquals(raChanged2.Strategic_dept__c, dc1s[0].Id);
      //  System.assertEquals(raChanged2.Hospital__c , company1.Id);
    }
}