@isTest
private class RentalApplyShareHandlerTest {
    @TestSetup
    static void makeData(){
        TestDataUtility.CreatePIPolicyConfiguration();
    }
    static Account createHospital( String hospitalName) {
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        // 病院を作る
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE DeveloperName = 'HP'].id;
        hospital.Name = hospitalName;
        insert hospital;
        StaticParameter.EscapeAccountTrigger = true;
        return hospital;
    }
    static List<Account> selectStrategicDep( Account hospital) {
        // 戦略科室を得る
        List<Account> strategicDep = [SELECT ID, Name,OwnerId 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.State_Text__c = '上海市';
        insert dep;
        return dep;
    }
    static testMethod void testMethod1() {    
        System.runAs(new User(Id = Userinfo.getUserId())) { 
           // 病院、戦略科室、診療科の情報を作成します
            Account hospital = createHospital( 'test hospital');
            Account[] strategicDep = selectStrategicDep( hospital);
            Account dep = createDep( hospital, strategicDep[0]);
    
    
            Rental_Apply__c ra1 = new Rental_Apply__c();
            ra1.Name = 'test';
            //ra1.applyUser__c = user.Id;
            ra1.Status__c = FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Cao_An_Zhong.ordinal());
            ra1.demo_purpose1__c = '其他';
            ra1.demo_purpose2__c = '其他';
            ra1.Request_shipping_day__c = Date.today().addDays(3);
            ra1.Hope_Lonaer_date_Num__c = 6;
            ra1.Approver_of_Service_Department__c = null;
            ra1.Hospital__c = hospital.Id;
            ra1.Strategic_dept__c = strategicDep[0].Id;
            ra1.Account__c = dep.Id;
            
            insert ra1;
            
             Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
            // ユーザー作成
            User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
            insert hpOwner;
            
            List<Rental_Apply__c> raList = [SELECT id,OwnerId,Approver_of_Service_Department__c,Strategic_user__c,Strategic_dept__c,ShareStrategicUser__c FROM Rental_Apply__c WHERE Id = :ra1.Id ];
            System.debug('===+++'+raList);
            System.assertEquals(1, raList.size());
            //List<User> user = [select Id,Name from User where IsActive = true];
            strategicDep[0].OwnerId = hpOwner.Id;
            update strategicDep;
            update raList;
         }
    }

//add by rentx 2020-12-04 start
    static testMethod void testMethod2(){
    // 病院、戦略科室、診療科の情報を作成します
        Account hospital = createHospital( 'test hospital');
        Account[] strategicDep = selectStrategicDep( hospital);
        Account dep = createDep( hospital, strategicDep[0]);

		Test.startTest();
        
        Rental_Apply__c ra1 = new Rental_Apply__c();
        ra1.Name = 'test';
        ra1.Status__c = FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Cao_An_Zhong.ordinal());
        ra1.demo_purpose1__c = '其他';
        ra1.demo_purpose2__c = '其他';
        ra1.Request_shipping_day__c = Date.today().addDays(3);
        ra1.Hope_Lonaer_date_Num__c = 6;
        ra1.Hospital__c = hospital.Id;
        ra1.Strategic_dept__c = strategicDep[0].Id;
        ra1.Account__c = dep.Id;
        
        insert ra1;
        List<Rental_Apply__c> raList = [SELECT id,OwnerId,Approver_of_Service_Department__c,Strategic_user__c,Strategic_dept__c,ShareStrategicUser__c FROM Rental_Apply__c WHERE Id = :ra1.Id ];
        System.assertEquals(1, raList.size());

        

        List<User> user = [select Id,Name from User where IsActive = true AND Job_Category__c = '销售服务'];
        // List<User> user1 = [select Id,Name from User where IsActive = true AND Job_Category__c = '销售服务'];
        strategicDep[0].OwnerId = user[0].Id;
        strategicDep[0].Department_Class_Label__c = '消化科';

        update strategicDep;

        hospital.FSE_GI_Main_Leader__c = user[0].Id;
        hospital.FSE_SP_Main_Leader__c = user[1].Id;
        update hospital;

        ra1.Person_In_Charge__c = user[2].Id;
        ra1.hospitalLeaderSP__c = '12';
        ra1.hospitalLeaderGI__c = '34';
        update ra1;

        strategicDep[0].OwnerId = user[1].Id;
        // strategicDep[0].Department_Class_Label__c = '其他';
        update strategicDep;//RecordType.Name == Parent.Department_Class_Label__c

        ra1.Strategic_dept__c = strategicDep[0].Id;
        update ra1;
        Test.stopTest();
    }

    @isTest
    static  void insertRentalApply_Test() {
        OPDPlan__c oPDPlan0 = new OPDPlan__c();
        oPDPlan0.Status__c = '计划中';
        oPDPlan0.OPDPlan_ImplementDate__c = Date.today();
        oPDPlan0.NoOpp_Reason__c = 'HCP对应';
        oPDPlan0.OPDType__c = '事件';
        insert oPDPlan0;
        Rental_Apply__c raObj = new Rental_Apply__c();
        raObj.Name = 'testra';
        raObj.Product_category__c = 'GI';
        raObj.Demo_purpose1__c = '其他';
        raObj.demo_purpose2__c = FixtureUtil.raDemo_purpose2MAP.get('baoxiuyonghu');
        raObj.direct_send__c = '医疗机构';
        raObj.Loaner_received_staff__c = '王五';
        raObj.Loaner_received_staff_phone__c = '110';
        raObj.direct_shippment_address__c = '北京市';
        raObj.ApplyPerson_Phone__c = '12345';

        raObj.Request_shipping_day__c = Date.toDay();  // 希望到货日
        raObj.Phone_number__c = '1234567890';
        raObj.OPDPlan__c = oPDPlan0.Id;
        raObj.demo_purpose2__c = 'CFDA注册用于测试';
        insert raObj;

        OPDPlan__c oPDPlan0Clone = new OPDPlan__c();
        oPDPlan0Clone = oPDPlan0.clone();
        oPDPlan0Clone.Id = null;
        oPDPlan0Clone.RentalReson__c = '追加配套';
        //oPDPlan0Clone.OriginalOpdPlanApp109lication__c = oPDPlan0.Id;
        insert oPDPlan0Clone;
        //新增不变更验证字段-变更时触发
        Rental_Apply__c raObjClone = new Rental_Apply__c();
        raObjClone = raObj.clone();
        raObjClone.Id  = null;
        raObjClone.OPDPlan__c = oPDPlan0Clone.Id;
        raObjClone.SupplementCreated__c = true;
        insert raObjClone;
        Rental_Apply__c ress = [select Id,Name from Rental_Apply__c where Id = :raObjClone.Id];
        system.debug('ress---->'+ress.Name); 
        //新增时触发验证规则
        OPDPlan__c oPDPlan0Clone2 = new OPDPlan__c();
        oPDPlan0Clone2 = oPDPlan0.clone();
        oPDPlan0Clone2.Id = null;
        oPDPlan0Clone2.RentalReson__c = '追加配套';
        oPDPlan0Clone2.OriginalOpdPlanApplication__c = oPDPlan0.Id;
        insert oPDPlan0Clone2;
        Rental_Apply__c raObjClone2 = new Rental_Apply__c();
        raObjClone2 = raObj.clone();
        raObjClone2.Id  = null;
        raObjClone2.Phone_number__c ='0522';
        raObjClone2.OPDPlan__c = oPDPlan0Clone2.Id;
        raObjClone2.SupplementCreated__c = true;
        insert raObjClone2;

        /*raObjClone.Phone_number__c = '000000';
        system.debug('raObjClone---->'+raObjClone.Name);
        raObjClone.SupplementCreated__c  = true;
        update raObjClone;*/

        Test.startTest();
        //OpdPlanWebService.insertRentalApply(raObj.Id, oPDPlan0Clone.Id);
        Test.stopTest();
    }

}