@isTest
private class ConsumablesApplyOrLendBatchTest {
    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.GI_Product_Leader__c = '张三，陈华君，赵四,王五';
        hospital.BF_Product_Leader__c = '张三，陈华君，赵四,王五';
        hospital.GS_Product_Leader__c = '张三，陈华君，赵四,王五';
        hospital.URO_Produc_Leader__c = '张三，陈华君，赵四,王五';
        hospital.GYN_Product_Leader__c = '张三，陈华君，赵四,王五';
        hospital.ENT_Product_Leader__c = '张三，陈华君，赵四,王五';
        hospital.ET_Product_Leader__c = '张三，陈华君，赵四,王五';
        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() {     
       // 病院、戦略科室、診療科の情報を作成します
        Account hospital = createHospital( 'test hospital');
        Account[] strategicDep = selectStrategicDep( hospital);
        Account dep = createDep( hospital, strategicDep[0]);

        OPDPlan__c oPDPlan0 = new OPDPlan__c();
        oPDPlan0.Status__c = '完毕未报告';
        oPDPlan0.OPDPlan_ImplementDate__c = Date.today().addDays(1);
        oPDPlan0.NoOpp_Reason__c = 'HCP对应';
        insert oPDPlan0;

        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.Repair__c = repair01.Id;
        ra1.Hospital__c = hospital.Id;
        ra1.Strategic_dept__c = strategicDep[0].Id;
        ra1.Account__c = dep.Id;
        ra1.OPDPlan__c = oPDPlan0.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];
        strategicDep[0].OwnerId = user[0].Id;
        update strategicDep;
        System.Test.startTest();
        Database.executeBatch(new ConsumablesApplyOrLendBatch(),1);
        strategicDep[0].OwnerId = user[1].Id;
        update strategicDep;
        Database.executeBatch(new ConsumablesApplyOrLendBatch(strategicDep[0].id),1);
        System.Test.stopTest();

    }


    //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]);

        OPDPlan__c oPDPlan0 = new OPDPlan__c();
        oPDPlan0.Status__c = '完毕未报告';
        oPDPlan0.OPDPlan_ImplementDate__c = Date.today().addDays(1);
        oPDPlan0.NoOpp_Reason__c = 'HCP对应';
        insert oPDPlan0;

        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;
        ra1.OPDPlan__c = oPDPlan0.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,Job_Category__c 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[1].Id;
        hospital.FSE_SP_Main_Leader__c = user[1].Id;
        update hospital;

        // User user1 = new User();
        // for (User u :user ) {
        //     if (u.Job_Category__c== '销售服务') {
        //         user1 = u;
        //     }
        // }

        ra1.Person_In_Charge__c = user[2].Id;
        update ra1;

        System.debug('OCM_dept_category__c::'+ra1.OCM_dept_category__c);
        System.debug('Job_Category__c::'+ra1.Job_Category__c);
        System.debug('Strategic_userFSEGI__c::'+ra1.Strategic_userFSEGI__c);

        System.Test.startTest();
            Database.executeBatch(new ConsumablesApplyOrLendBatch(),1);
            strategicDep[0].OwnerId = user[1].Id;
            strategicDep[0].Department_Class_Label__c = '其他';
            update strategicDep;
            Database.executeBatch(new ConsumablesApplyOrLendBatch(strategicDep[0].id),1);
        System.Test.stopTest();
    }

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


        OPDPlan__c oPDPlan0 = new OPDPlan__c();
        oPDPlan0.Status__c = '完毕未报告';
        oPDPlan0.OPDPlan_ImplementDate__c = Date.today().addDays(1);
        oPDPlan0.NoOpp_Reason__c = 'HCP对应';
        insert oPDPlan0;


        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;
        ra1.OPDPlan__c = oPDPlan0.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,Job_Category__c 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[1].Id;
        hospital.FSE_SP_Main_Leader__c = user[1].Id;
        update hospital;


        ra1.Person_In_Charge__c = user[2].Id;
        update ra1;

        System.debug('OCM_dept_category__c::'+ra1.OCM_dept_category__c);
        System.debug('Job_Category__c::'+ra1.Job_Category__c);
        System.debug('Strategic_userFSEGI__c::'+ra1.Strategic_userFSEGI__c);

        System.Test.startTest();
            Database.executeBatch(new ConsumablesApplyOrLendBatch(),1);
            strategicDep[0].OwnerId = user[1].Id;
            strategicDep[0].Department_Class_Label__c = '其他';
            update strategicDep;
            Database.executeBatch(new ConsumablesApplyOrLendBatch(strategicDep[0].id),1);
        System.Test.stopTest();
    }

    //add by rentx 2020-12-04 end
    //add by wangweipeng 2021-10-19 start
    static testMethod void testMethod4(){
         //简档
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        //用户
        User u1 = new User(Test_staff__c = true, LastName = '张', FirstName = '三', Alias = '张 三',Work_Location__c = '北京', 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 u1;
        User u2 = new User(Test_staff__c = true, LastName = '赵', FirstName = '四', Alias = '赵 四',Work_Location__c = '重庆', CommunityNickname = 'hpOwner2', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner2@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert u2;

        User u3 = new User(Test_staff__c = true, LastName = '王', FirstName = '五', Alias = '王 五',Work_Location__c = '石家庄', CommunityNickname = 'hpOwner3', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner3@sunbridge.com', IsActive = false, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert u3;

    // 病院、戦略科室、診療科の情報を作成します
        Account hospital = createHospital( 'test hospital');
        Account[] strategicDep = selectStrategicDep( hospital);
        Account dep = createDep( hospital, strategicDep[0]);

        OPDPlan__c oPDPlan0 = new OPDPlan__c();
        oPDPlan0.Status__c = '完毕未报告';
        oPDPlan0.OPDPlan_ImplementDate__c = Date.today().addDays(1);
        oPDPlan0.NoOpp_Reason__c = 'HCP对应';
        insert oPDPlan0;


        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.OPDPlan__c = oPDPlan0.id;
        ra1.Account__c = dep.Id;
        ra1.ProducLeader_Share__c = '';
        
        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 ];

        strategicDep[0].OwnerId = u1.Id;
        strategicDep[0].Department_Class_Label__c = '普外科';
        update strategicDep;


        hospital.FSE_GI_Main_Leader__c = u2.Id;
        hospital.FSE_SP_Main_Leader__c = u2.Id;
        update hospital;

        //备品借出申请 共享计划创建
        Rental_Apply__Share rass = new Rental_Apply__Share();
        rass.RowCause= 'Office_Assistant__c';//手动
        rass.ParentId = ra1.id;
        rass.UserOrGroupId = u2.Id;
        rass.AccessLevel = 'Read';//编辑
        insert rass;

        //备品借出申请 共享计划创建
        Rental_Apply__Share rasss = new Rental_Apply__Share();
        rasss.RowCause= 'RentalApplyProducLeaderShare__c';//手动
        rasss.ParentId = ra1.id;
        rasss.UserOrGroupId = u1.Id;
        rasss.AccessLevel = 'Read';//编辑
        insert rasss;


        Database.executeBatch(new ConsumablesApplyOrLendBatch(),1);

        ra1.ShareStrategicUser__c = u2.Id;
        ra1.hospitalLeaderGI__c = u2.Id;
        ra1.hospitalLeaderSP__c = u2.Id;
        ra1.ProducLeader_Share__c = u1.Id;
        update ra1;


        System.Test.startTest();
            Database.executeBatch(new ConsumablesApplyOrLendBatch(),1);
        System.Test.stopTest();
    }

    //add by wangweipeng 2021-10-19 end
}