高章伟
2023-03-03 d8dc84a3d56df839895f1c417a4d9cbee763d262
force-app/main/default/classes/ConsumablesApplyOrLendBatch.cls
@@ -67,7 +67,7 @@
            query += 'OCM_dept_category__c,ShareFlag__c,Job_Category__c,Strategic_userFSEGI__c,hospitalLeaderGI__c,Strategic_userFSESP__c,hospitalLeaderSP__c, ';
            query += 'hospitalLeaderGIchange__c,hospitalLeaderSPchange__c, OPDPlan__r.Status__c,Shippment_ng_num__c,ProducLeader_Share__c, ';
            query += ' Hospital__r.GI_Product_Leader__c,Hospital__r.BF_Product_Leader__c,Hospital__r.GS_Product_Leader__c,Hospital__r.URO_Produc_Leader__c,Hospital__r.GYN_Product_Leader__c,Hospital__r.ENT_Product_Leader__c,Hospital__r.ET_Product_Leader__c ';
            query += ' FROM Rental_Apply__c WHERE (ShareFlag__c = true or hospitalLeaderGIchange__c = true or hospitalLeaderSPchange__c = true or (Shippment_ng_num__c = 0 and OPDPlan__c != null and OPDPlan__r.Status__c IN (\'完毕未报告\',\'待提交报告\'))) AND Strategic_dept__c= \''+strategicDept+'\' ORDER BY Strategic_dept__c';
            query += ' FROM Rental_Apply__c WHERE (ShareFlag__c = true or hospitalLeaderGIchange__c = true or hospitalLeaderSPchange__c = true or (Shippment_ng_num__c = 0 and OPDPlan__c != null and OPDPlan__r.Status__c IN (\'完毕未报告\',\'待提交报告\'))) AND  Strategic_dept__c = \''+strategicDept+'\' ORDER BY Strategic_dept__c';
        }
        //update  wangweipeng   新增备品的产品担当人共享     2021/10/15      end
        //update by rentx 2020-11-16 SFDC-BVA5VJ end
@@ -75,177 +75,12 @@
        update iflog;
        return Database.getQueryLocator(query);
    }
    //注释 by rentx 2021-01-04 SFDC-BWQ3MX
    /*global void execute(Database.BatchableContext BC, list<Sobject> scope) {
        //add by rentx 2020-11-17 start
        List<Rental_Apply__c> ratList = new List<Rental_Apply__c>();
        List<Rental_Apply__c> raoList = new List<Rental_Apply__c>();
        //add by rentx 2020-11-17 end
    global void execute(Database.BatchableContext BC, list<Rental_Apply__c> RentalApply) {
        // 20220412 ljh 整理 start
        // ShareFlag__c hospitalLeaderGIchange__c hospitalLeaderSPchange__c 为真的时候空更新即可
        Map<String,Rental_Apply__c> raMap = new Map<String,Rental_Apply__c>();
        // 20220412 ljh 整理 end
        //共享权限变更
        List<Rental_Apply__c> RentalApply = new List<Rental_Apply__c>();
        List<Rental_Apply__c> raList = new List<Rental_Apply__c>();
        List<Rental_Apply__Share> delStrategList = new List<Rental_Apply__Share>();
        List<Rental_Apply__Share> addStrategList = new List<Rental_Apply__Share>();
        List<String> ParentIdList = new List<String>();
        List<String> UserOrGroupIdList = new List<String>();
        Map<String, String> PUMap = new Map<String, String>();
        List<String> UserIDList = new List<String>();
        List<String> IsActiveNoList = new List<String>();
        RentalApply = scope;
        if(RentalApply.size()>0){
            //用户未启用无法插入到共享表 start
            for(Rental_Apply__c ra:RentalApply){
                if(ra.Strategic_user__c !=null){
                    UserIDList.add(ra.Strategic_user__c);
                }
                //add by rentx 2020-11-27
                if (ra.Strategic_userFSEGI__c != null) {
                    UserIDList.add(ra.Strategic_userFSEGI__c);
                }
                if (ra.Strategic_userFSESP__c != null) {
                    UserIDList.add(ra.Strategic_userFSESP__c);
                }
                //add by rentx 2020-11-27
            }
            for(User user:[Select Id,Name,IsActive From User WHERE Id in:UserIDList]){
                //未启用
                if(!user.IsActive){
                    String  userId = (String)user.Id;
                    userId = userId.substring(0,15);
                    IsActiveNoList.add(userId);
                }
            }
            //用户未启用无法插入到共享表  end
            for(Rental_Apply__c ra:RentalApply){
                //删除原来的共享
                if(String.isNotBlank(ra.ShareStrategicUser__c)){
                    PUMap.put(ra.Id,ra.ShareStrategicUser__c);
                    ParentIdList.add(ra.Id);
                    UserOrGroupIdList.add(ra.ShareStrategicUser__c);
                }
                //增加新的
                //所有者还有服务服务部审批人相同不用加 用户未启用无法插入到共享表
                if(ra.Strategic_user__c !=null&& !IsActiveNoList.contains(ra.Strategic_user__c)){
                    if(
                        ((ra.Approver_of_Service_Department__c !=null&&!ra.Strategic_user__c.equals(ra.Approver_of_Service_Department__c))||ra.Approver_of_Service_Department__c ==null)
                        &&!ra.Strategic_user__c.equals(ra.OwnerId)){
                        Rental_Apply__Share ras = new Rental_Apply__Share(
                                RowCause = 'Manual',
                                ParentId = ra.Id,
                                UserOrGroupId = ra.Strategic_user__c,
                                AccessLevel = 'read'
                            );
                        addStrategList.add(ras);
                    }
                    Rental_Apply__c r_a = new Rental_Apply__c();
                    r_a.Id = ra.Id;
                    r_a.ShareStrategicUser__c = ra.Strategic_user__c;
                    raList.add(r_a);
                }
                //add by rentx 2020-11-16 SFDC-BVA5VJ start
                //判断战略科室  直接删了再加
                String dept = ra.OCM_dept_category__c;
                if ((ra.Job_Category__c =='销售服务') && (dept.equals('消化科') ||dept.equals('呼吸科') || dept.equals('ET') || dept.equals('其他'))
                     && ra.Strategic_userFSEGI__c != null && !IsActiveNoList.contains(ra.Strategic_userFSEGI__c) && !ra.OwnerId.equals(ra.Strategic_userFSEGI__c)) {
                    //加入共享
                    Rental_Apply__Share ras = makeData(ra.Id,ra.Strategic_userFSEGI__c);
                    addStrategList.add(ras);
                    //删除原来的共享
                    PUMap.put(ra.Id,ra.hospitalLeaderGI__c);
                    ParentIdList.add(ra.Id);
                    UserOrGroupIdList.add(ra.hospitalLeaderGI__c);
                    if (String.isNotBlank(ra.hospitalLeaderSP__c)) {
                        PUMap.put(ra.Id,ra.hospitalLeaderSP__c);
                        UserOrGroupIdList.add(ra.hospitalLeaderSP__c);
                    }
                    //更新字段
                    Rental_Apply__c rac = new Rental_Apply__c();
                    rac.Id = ra.Id;
                    rac.hospitalLeaderGI__c = ra.Strategic_userFSEGI__c;
                    ratList.add(rac);
                }
                if ((ra.Job_Category__c =='销售服务') &&
                    (dept.equals('普外科') ||dept.equals('泌尿科') || dept.equals('妇科') || dept.equals('耳鼻喉科') || dept.equals('其他'))
                     && ra.Strategic_userFSESP__c != null && !IsActiveNoList.contains(ra.Strategic_userFSESP__c) && !ra.OwnerId.equals(ra.Strategic_userFSESP__c)) {
                    //加入共享
                    Rental_Apply__Share ras = makeData(ra.Id,ra.Strategic_userFSESP__c);
                    addStrategList.add(ras);
                    //删除原来的共享
                    PUMap.put(ra.Id,ra.hospitalLeaderSP__c);
                    ParentIdList.add(ra.Id);
                    UserOrGroupIdList.add(ra.hospitalLeaderSP__c);
                    if (String.isNotBlank(ra.hospitalLeaderGI__c)) {
                        PUMap.put(ra.Id,ra.hospitalLeaderGI__c);
                        UserOrGroupIdList.add(ra.hospitalLeaderGI__c);
                    }
                    //更新字段
                    Rental_Apply__c rac = new Rental_Apply__c();
                    rac.Id = ra.Id;
                    rac.hospitalLeaderSP__c = ra.Strategic_userFSESP__c;
                    raoList.add(rac);
                }
            }
            //删除原来的共享
            delStrategList  = [SELECT Id,ParentId,UserOrGroupId FROM Rental_Apply__Share WHERE  RowCause ='Manual' AND ParentId in :ParentIdList AND UserOrGroupId in :UserOrGroupIdList];
            for(Integer m=0; m<delStrategList.size(); m++){
                if(String.isBlank(PUMap.get(delStrategList[m].ParentId))){
                    delStrategList.remove(m);
                }
            }
            if(delStrategList.size()>0){
                delete delStrategList;
            }
            //增加
            if (addStrategList.size() > 0) {
                insert addStrategList;
            }
            //更新备品借出
            if(raList.size()>0){
                update raList;
            }
            //更新备品借出 -- hospleaderGI字段
            if (ratList.size() > 0) {
                update ratList;
            }
            //更新备品借出 -- hospleaderSP字段
            if (raoList.size() > 0) {
                update raoList;
            }
        }
    }*/
    //update by rentx 2021-01-04 SFDC-BWQ3MX
    global void execute(Database.BatchableContext BC, list<Sobject> scope) {
        //原来的是将战略科室所有人加入到共享表中  现在需要将GI/SP主担当也加入并且删除之前的共享
        //add by rentx 2020-11-17 start
        List<Rental_Apply__c> ratList = new List<Rental_Apply__c>();
        List<Rental_Apply__c> raoList = new List<Rental_Apply__c>();
        //add by rentx 2020-11-17 end
        //共享权限变更
        List<Rental_Apply__c> RentalApply = new List<Rental_Apply__c>();
        List<Rental_Apply__c> raList = new List<Rental_Apply__c>();
        List<Rental_Apply__Share> delStrategList = new List<Rental_Apply__Share>();
        List<Rental_Apply__Share> addStrategList = new List<Rental_Apply__Share>();
        List<String> ParentIdList = new List<String>();
        List<String> UserOrGroupIdList = new List<String>();
        Map<String, String> PUMap = new Map<String, String>();
        List<String> UserIDList = new List<String>();
        List<String> IsActiveNoList = new List<String>();
        //add  wangweipeng   新增备品的产品担当人共享     2021/10/15      start
        //收集产品担当人中文名
        List<String> productLeaderNameList = new List<String>();
@@ -257,29 +92,15 @@
        //新增借出备品申请产品担当共享人
        List<Rental_Apply__Share> insRentalApplyShare = new List<Rental_Apply__Share>();
        //修改借出备品申请的产品担当人共享记录
        List<Rental_Apply__c> updateRac = new List<Rental_Apply__c>();
        // List<Rental_Apply__c> updateRac = new List<Rental_Apply__c>();
        //获取所有满足条件的备品id
        List<String> raIdList = new List<String>();
        //获取备品的产品担当人共享数据
        Map<String,List<Rental_Apply__Share>> raShareMap = new Map<String,List<Rental_Apply__Share>>();
        //add  wangweipeng   新增备品的产品担当人共享     2021/10/15      end
        RentalApply = scope;
        System.debug('zheli000');
        if(RentalApply.size()>0){
            //用户未启用无法插入到共享表 start
            for(Rental_Apply__c ra:RentalApply){
                if(ra.Strategic_user__c !=null){
                    UserIDList.add(ra.Strategic_user__c);
                }
                //add by rentx 判断用户是否启用
                if (ra.Strategic_userFSEGI__c != null) {
                    UserIDList.add(ra.Strategic_userFSEGI__c);
                }
                if (ra.Strategic_userFSESP__c != null) {
                    UserIDList.add(ra.Strategic_userFSESP__c);
                }
                //add by rentx
                //add  wangweipeng   新增备品的产品担当人共享     2021/10/15      start
                //收集产品担当人   只收集 opd计划为完毕未报告   备品 出库未完了数  等于0(已出库)
                //收集的产品担当人为中文名
@@ -297,22 +118,13 @@
                }
                //add  wangweipeng   新增备品的产品担当人共享     2021/10/15      end
            }
            for(User user:[Select Id,Name,IsActive From User WHERE Id in:UserIDList]){
                //未启用
                if(!user.IsActive){
                    String  userId = (String)user.Id;
                    userId = userId.substring(0,15);
                    IsActiveNoList.add(userId);
                }
            }
            //add   wangweipeng   新增备品的产品担当人共享     2021/10/15      start
            //由于产品担当获取的都是中文名字,所以需要获取到他的id,并且去掉没启用的人员
            List<User> userList;
            if(productLeaderNameList.size() > 0){
                for(String ss : productLeaderNameList){
                    // System.debug('---------------'+ss);
                }
                // for(String ss : productLeaderNameList){
                //     // System.debug('---------------'+ss);
                // }
                //获取担当人的user对象
                userList = [select id,name,Alias,Alias__c from user where IsActive = true and Alias__c != null and Alias__c = :productLeaderNameList order by CreatedDate desc];
            }else{
@@ -334,70 +146,25 @@
                    }
                }
            }
            System.debug('zheli00');
            //add  wangweipeng   新增备品的产品担当人共享     2021/10/15      end
            //用户未启用无法插入到共享表  end
            for(Rental_Apply__c ra:RentalApply){
                //如果战略科室主担当发生变更 --之前的逻辑 update by rentx 2021-01-04 SFDC-BWQ3MX
                //如果战略科室主担当发生变更
                if (ra.ShareFlag__c == true) {
                    //删除原来的共享
                    if(String.isNotBlank(ra.ShareStrategicUser__c)){
                        PUMap.put(ra.Id,ra.ShareStrategicUser__c);
                        ParentIdList.add(ra.Id);
                        UserOrGroupIdList.add(ra.ShareStrategicUser__c);
                    raMap.put(ra.Id,new Rental_Apply__c(id=ra.Id)); // 20220412 ljh add
                    //add   wangweipeng   新增备品的产品担当人共享     2021/10/15      start
                    //权限表中的战略科室主担当 字段发生变化,那么记录一下当前修改的内容
                    if(RentalApplyMapData.containsKey(ra.Id)){
                        Rental_Apply__c raa = RentalApplyMapData.get(ra.Id);
                        raa.ShareStrategicUser__c = ra.Strategic_user__c;
                        RentalApplyMapData.put(raa.id,raa);
                    }
                    //增加新的
                    //所有者还有服务服务部审批人相同不用加 用户未启用无法插入到共享表
                    if(ra.Strategic_user__c !=null&& (IsActiveNoList.size() ==0 || !IsActiveNoList.contains(ra.Strategic_user__c))){
                        if(
                            ((ra.Approver_of_Service_Department__c !=null&&!ra.Strategic_user__c.equals(ra.Approver_of_Service_Department__c))||ra.Approver_of_Service_Department__c ==null)
                            &&!ra.Strategic_user__c.equals(ra.OwnerId)){
                            Rental_Apply__Share ras = new Rental_Apply__Share(
                                    RowCause = 'Manual',
                                    ParentId = ra.Id,
                                    UserOrGroupId = ra.Strategic_user__c,
                                    AccessLevel = 'read'
                                );
                            addStrategList.add(ras);
                        }
                        Rental_Apply__c r_a = new Rental_Apply__c();
                        r_a.Id = ra.Id;
                        r_a.ShareStrategicUser__c = ra.Strategic_user__c;
                        raList.add(r_a);
                        //add   wangweipeng   新增备品的产品担当人共享     2021/10/15      start
                        //权限表中的战略科室主担当 字段发生变化,那么记录一下当前修改的内容
                        if(RentalApplyMapData.containsKey(ra.Id)){
                            Rental_Apply__c raa = RentalApplyMapData.get(ra.Id);
                            raa.ShareStrategicUser__c = ra.Strategic_user__c;
                            RentalApplyMapData.put(raa.id,raa);
                        }
                        //add   wangweipeng   新增备品的产品担当人共享     2021/10/15      end
                    }
                    //add   wangweipeng   新增备品的产品担当人共享     2021/10/15      end
                }
                //update by rentx 2021-01-04 SFDC-BWQ3MX  
                //如果战略科室GI担当发生变更
                if (ra.hospitalLeaderGIchange__c == true) {
                    //删除原来的共享
                    if(String.isNotBlank(ra.hospitalLeaderGI__c)){
                        PUMap.put(ra.Id,ra.hospitalLeaderGI__c);
                        ParentIdList.add(ra.Id);
                        UserOrGroupIdList.add(ra.hospitalLeaderGI__c);
                    }
                    //判断
                    String dept = ra.OCM_dept_category__c;
                    if (ra.Strategic_userFSEGI__c != null && (IsActiveNoList.size() ==0 || !IsActiveNoList.contains(ra.Strategic_userFSEGI__c)) && !ra.OwnerId.equals(ra.Strategic_userFSEGI__c)) {
                        //加入共享
                        Rental_Apply__Share ras = makeData(ra.Id,ra.Strategic_userFSEGI__c);
                        addStrategList.add(ras);
                    }
                    //更新字段
                    Rental_Apply__c rac = new Rental_Apply__c();
                    rac.Id = ra.Id;
                    rac.hospitalLeaderGI__c = ra.Strategic_userFSEGI__c;
                    ratList.add(rac);
                    raMap.put(ra.Id,new Rental_Apply__c(id=ra.Id)); // 20220412 ljh add
                    //add   wangweipeng   新增备品的产品担当人共享     2021/10/15      start
                    //战略科室FSEGI担当 发生变化,记录一下备品修改的内容
                    if(RentalApplyMapData.containsKey(ra.Id)){
@@ -410,24 +177,7 @@
                //update by rentx 2021-01-04 SFDC-BWQ3MX 
                //如果战略科室SP担当发生变更
                if (ra.hospitalLeaderSPchange__c == true) {
                    //删除原来的共享
                    if(String.isNotBlank(ra.hospitalLeaderSP__c)){
                        PUMap.put(ra.Id,ra.hospitalLeaderSP__c);
                        ParentIdList.add(ra.Id);
                        UserOrGroupIdList.add(ra.hospitalLeaderSP__c);
                    }
                    //判断
                    if (ra.Strategic_userFSESP__c != null && (IsActiveNoList.size() ==0 || !IsActiveNoList.contains(ra.Strategic_userFSESP__c)) && !ra.OwnerId.equals(ra.Strategic_userFSESP__c)) {
                        //加入共享
                        Rental_Apply__Share ras = makeData(ra.Id,ra.Strategic_userFSESP__c);
                        addStrategList.add(ras);
                    }
                    //更新字段
                    Rental_Apply__c rac = new Rental_Apply__c();
                    rac.Id = ra.Id;
                    rac.hospitalLeaderSP__c = ra.Strategic_userFSESP__c;
                    raoList.add(rac);
                    raMap.put(ra.Id,new Rental_Apply__c(id=ra.Id)); // 20220412 ljh add
                    //add   wangweipeng   新增备品的产品担当人共享     2021/10/15      start
                    //战略科室FSESP担当 发生变化,记录一下修改内容
                    if(RentalApplyMapData.containsKey(ra.Id)){
@@ -543,16 +293,22 @@
                                                insRentalApplyShare.add(makeData3(newRA.id,userId));
                                            }
                                        }
                                        // 20220413 ljh update start
                                        //修改备品的产品担当人字段值
                                        updateRac.add(new Rental_Apply__c(id=newRA.id,ProducLeader_Share__c=productLeaderUserIdStr));
                                        // updateRac.add(new Rental_Apply__c(id=newRA.id,ProducLeader_Share__c=productLeaderUserIdStr));
                                        raMap.put(newRA.id,new Rental_Apply__c(id=newRA.id,ProducLeader_Share__c=productLeaderUserIdStr));
                                        // 20220413 ljh update end
                                    }
                                }
                            }else{
                                //如果 ProducLeader_Share__c 字段不为空,并且没有找到当前备品需要共享的产品担当人
                                //证明以前共享了产品担当人,但是现在产品担当人做了修改(系统中没找到或此人已经通过其它共享原因共享了)或制空了,所以直接把以前共享的人删除掉就行
                                delRentalApplyShare.add(newRA.id);
                                // 20220413 ljh update start
                                //修改备品的产品担当人字段值
                                updateRac.add(new Rental_Apply__c(id=newRA.id,ProducLeader_Share__c=''));
                                // updateRac.add(new Rental_Apply__c(id=newRA.id,ProducLeader_Share__c=''));
                                raMap.put(newRA.id,new Rental_Apply__c(id=newRA.id,ProducLeader_Share__c=''));
                                // 20220413 ljh update end
                            }
                        }else{
                            //如果ProducLeader_Share__c字段为空,并且当前备品的产品担当人需要共享,那么就需要我们新增共享,并把共享人id,记录到当前备品上
@@ -566,8 +322,11 @@
                                            insRentalApplyShare.add(makeData3(newRA.id,userId));
                                        }
                                    }
                                    // 20220413 ljh update start
                                    //修改备品的产品担当人字段值
                                    updateRac.add(new Rental_Apply__c(id=newRA.id,ProducLeader_Share__c=productLeaderUserIdStr));
                                    // updateRac.add(new Rental_Apply__c(id=newRA.id,ProducLeader_Share__c=productLeaderUserIdStr));
                                    raMap.put(newRA.id,new Rental_Apply__c(id=newRA.id,ProducLeader_Share__c=productLeaderUserIdStr));
                                    // 20220413 ljh update end
                                }
                            }
                        }
@@ -575,43 +334,18 @@
                        //如果没有产品当但人,但是字段不为空,那么证明,当前备品的产品担当人有变动值设置为空了,那么我们需要处理以前已经共享的产品担当人
                        if(String.isNotBlank(ra.ProducLeader_Share__c)){
                            delRentalApplyShare.add(ra.id);
                            // 20220413 ljh update start
                            //修改备品的产品担当人字段值
                            updateRac.add(new Rental_Apply__c(id=ra.id,ProducLeader_Share__c=''));
                            // updateRac.add(new Rental_Apply__c(id=ra.id,ProducLeader_Share__c=''));
                            raMap.put(ra.Id,new Rental_Apply__c(id=ra.id,ProducLeader_Share__c=''));
                            // 20220413 ljh update end
                        }
                    }
                }
                //add   wangweipeng   新增备品的产品担当人共享     2021/10/15      end
            }
            //删除原来的共享
            delStrategList  = [SELECT Id,ParentId,UserOrGroupId FROM Rental_Apply__Share WHERE  RowCause ='Manual' AND ParentId in :ParentIdList AND UserOrGroupId in :UserOrGroupIdList];
            for(Integer m=0; m<delStrategList.size(); m++){
                if(String.isBlank(PUMap.get(delStrategList[m].ParentId))){
                    delStrategList.remove(m);
                }
            }
            Savepoint sp = Database.setSavepoint();
            try{
                if(delStrategList.size()>0){
                    delete delStrategList;
                }
                //增加
                if (addStrategList.size() > 0) {
                    insert addStrategList;
                }
                //更新备品借出
                if(raList.size()>0){
                    update raList;
                }
                //更新备品借出GI
                if (ratList.size() > 0) {
                    update ratList;
                }
                //更新备品借出SP
                if (raoList.size() > 0) {
                    update raoList;
                }
                //add by rentx 2021-01-04 SFDC-BWQ3MX start
            try{
                //add   wangweipeng   新增备品的产品担当人共享     2021/10/18      start
                //以下是备品借出申请的产品担当人共享
                //删除原来的产品担当共享人
@@ -625,10 +359,17 @@
                if(insRentalApplyShare != null && insRentalApplyShare.size() > 0){
                    insert insRentalApplyShare;
                }
                // 20220413 ljh update start
                //修改产品担当共享人
                if(updateRac != null && updateRac.size() > 0){
                    update updateRac;
                // if(updateRac != null && updateRac.size() > 0){
                //     update updateRac;
                // }
                // System.debug('zheli:'+raMap.size());
                if(raMap != null && raMap.size() > 0){
                    // System.debug('zheli:'+raMap.values());
                    update raMap.values();
                }
                // 20220413 ljh update  end
                //add   wangweipeng   新增备品的产品担当人共享     2021/10/18      end
                
            }catch(Exception e){