高章伟
2023-03-03 d8dc84a3d56df839895f1c417a4d9cbee763d262
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
global class ConsumablesApplyOrLendBatch implements Database.Batchable<sObject> {
    private String query;
    private String strategicDept;
    private BatchIF_Log__c iflog;
    private String errStr;
 
    global ConsumablesApplyOrLendBatch() {
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Is_Error__c = 0;
        iflog.Log__c = 'ConsumablesApplyOrLendBatch start1\n';
        iflog.ErrorLog__c = '';
        insert iflog;
    }
 
    //2021-1-4 update by rentx SFDC-BWQ3MX start 
    // global ConsumablesApplyOrLendBatch(String dePartment) {
    global ConsumablesApplyOrLendBatch(String strategicDept) {
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Is_Error__c = 0;
        iflog.Log__c = 'ConsumablesApplyOrLendBatch start2\n';
        iflog.ErrorLog__c = '';
        insert iflog;
        // this.strategicDept = dePartment;
        this.strategicDept = strategicDept;
    }
    //2021-1-4 update by rentx SFDC-BWQ3MX end
 
    //add by rentx 2020-12-03 start 新建共享数据
    private Rental_Apply__Share makeData(String parentId,String userId){
        Rental_Apply__Share ras = new Rental_Apply__Share();
        ras.RowCause= 'Manual';
        ras.ParentId = parentId;
        ras.UserOrGroupId = userId;
        ras.AccessLevel = 'Edit';
        return ras;
    }
    //add by rentx 2020-12-03 end 新建共享数据
 
    //add  wangweipeng   新增备品的产品担当人共享     2021/10/15   start
    //产品担当人共享原因的对象
    private Rental_Apply__Share makeData3(String parentId,String userId){
        Rental_Apply__Share ras = new Rental_Apply__Share();
        ras.RowCause= 'RentalApplyProducLeaderShare__c';//手动
        ras.ParentId = parentId;//opd id
        ras.UserOrGroupId = userId;//共享给谁
        ras.AccessLevel = 'Read';//只读
        return ras;
    }
    //add  wangweipeng   新增备品的产品担当人共享     2021/10/15   end
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
 
        if (strategicDept == null || strategicDept == '') {
            //update by rentx 2020-11-16 SFDC-BVA5VJ start
            // query = 'SELECT id,OwnerId,Approver_of_Service_Department__c,Strategic_user__c,Strategic_dept__c,ShareStrategicUser__c FROM Rental_Apply__c WHERE ShareFlag__c =true  ORDER BY Strategic_dept__c';
            //update  wangweipeng   新增备品的产品担当人共享     2021/10/15      start
            query = 'SELECT id,OwnerId,Approver_of_Service_Department__c,Strategic_user__c,Strategic_dept__c,ShareStrategicUser__c, ';
            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 (\'完毕未报告\',\'待提交报告\'))) ORDER BY Strategic_dept__c';
        }else{
            // query = 'SELECT id,OwnerId,Approver_of_Service_Department__c,Strategic_user__c,Strategic_dept__c,ShareStrategicUser__c FROM Rental_Apply__c WHERE ShareFlag__c =true AND Strategic_dept__c= \''+strategicDept+'\' ORDER BY Strategic_dept__c';
            query = 'SELECT id,OwnerId,Approver_of_Service_Department__c,Strategic_user__c,Strategic_dept__c,ShareStrategicUser__c, ';
            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';
        }
        //update  wangweipeng   新增备品的产品担当人共享     2021/10/15      end
        //update by rentx 2020-11-16 SFDC-BVA5VJ end
        iflog.Log__c += query+'\n';
        update iflog;
        return Database.getQueryLocator(query);
    }
    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
        //共享权限变更
        //add  wangweipeng   新增备品的产品担当人共享     2021/10/15      start
        //收集产品担当人中文名
        List<String> productLeaderNameList = new List<String>();
        //收集所有的数据,注意:最新的共享情况,包括当前类所处理的备品数据
        //使用map,方便后期处理数据
        Map<String,Rental_Apply__c> RentalApplyMapData = new Map<String,Rental_Apply__c>();
        //删除掉备品的产品担当人共享人
        List<String> delRentalApplyShare = new List<String>();
        //新增借出备品申请产品担当共享人
        List<Rental_Apply__Share> insRentalApplyShare = new List<Rental_Apply__Share>();
        //修改借出备品申请的产品担当人共享记录
        // 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
        System.debug('zheli000');
        if(RentalApply.size()>0){
            for(Rental_Apply__c ra:RentalApply){
                //add  wangweipeng   新增备品的产品担当人共享     2021/10/15      start
                //收集产品担当人   只收集 opd计划为完毕未报告   备品 出库未完了数  等于0(已出库)
                //收集的产品担当人为中文名
                if(('完毕未报告'.equals(ra.OPDPlan__r.Status__c) || '待提交报告'.equals(ra.OPDPlan__r.Status__c)) && ra.Shippment_ng_num__c == 0){
                    List<String> getProductLeaderNameList = getProductLeaderName(ra);
                    if(getProductLeaderNameList != null && getProductLeaderNameList.size() > 0){
                        for(String productLeaderName : getProductLeaderNameList){
                            if(productLeaderName != ''){
                                productLeaderNameList.add(productLeaderName);
                            }
                        }
                        RentalApplyMapData.put(ra.id,ra);
                        raIdList.add(ra.id);
                    }
                }
                //add  wangweipeng   新增备品的产品担当人共享     2021/10/15      end
            }
            //add   wangweipeng   新增备品的产品担当人共享     2021/10/15      start
            //由于产品担当获取的都是中文名字,所以需要获取到他的id,并且去掉没启用的人员
            List<User> userList;
            if(productLeaderNameList.size() > 0){
                // 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{
                userList = new List<User>();
            }
            if(raIdList.size() > 0){
                List<Rental_Apply__Share> raShareList = [SELECT Id, ParentId, UserOrGroupId, AccessLevel, RowCause, LastModifiedDate, LastModifiedById, IsDeleted FROM Rental_Apply__Share where RowCause not in ('RentalApplyProducLeaderShare__c','Manual','Owner','Rule') and ParentId in :raIdList];
                //处理数据,方便后期获取数据,不用一直循环查询数据
                if(raShareList != null && raShareList.size() > 0){
                    for(Rental_Apply__Share ras : raShareList){
                        List<Rental_Apply__Share> oneRas;
                        if(raShareMap.containsKey(ras.ParentId)){
                            oneRas = raShareMap.get(ras.ParentId);
                        }else{
                            oneRas = new List<Rental_Apply__Share>();
                        }
                        oneRas.add(ras);                        
                        raShareMap.put(ras.ParentId,oneRas);
                    }
                }
            }
            System.debug('zheli00');
            //add  wangweipeng   新增备品的产品担当人共享     2021/10/15      end
            for(Rental_Apply__c ra:RentalApply){
                //如果战略科室主担当发生变更   
                if (ra.ShareFlag__c == true) {
                    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);
                    }
                    //add   wangweipeng   新增备品的产品担当人共享     2021/10/15      end
                }
                //update by rentx 2021-01-04 SFDC-BWQ3MX  
                //如果战略科室GI担当发生变更
                if (ra.hospitalLeaderGIchange__c == true) {
                    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)){
                        Rental_Apply__c raa = RentalApplyMapData.get(ra.Id);
                        raa.hospitalLeaderGI__c = ra.Strategic_userFSEGI__c;
                        RentalApplyMapData.put(raa.id,raa);
                    }
                    //add   wangweipeng   新增备品的产品担当人共享     2021/10/15      end
                }
                //update by rentx 2021-01-04 SFDC-BWQ3MX 
                //如果战略科室SP担当发生变更
                if (ra.hospitalLeaderSPchange__c == true) {
                    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)){
                        Rental_Apply__c raa = RentalApplyMapData.get(ra.Id);
                        raa.hospitalLeaderSP__c = ra.Strategic_userFSESP__c;
                        RentalApplyMapData.put(raa.id,raa);
                    }
                    //add   wangweipeng   新增备品的产品担当人共享     2021/10/15      end
                }
 
                //add   wangweipeng   新增备品的产品担当人共享     2021/10/15      start
                if(('完毕未报告'.equals(ra.OPDPlan__r.Status__c) || '待提交报告'.equals(ra.OPDPlan__r.Status__c)) && ra.Shippment_ng_num__c == 0){
                    //当前备品的产品担当中文名,可能是多个或一个
                    List<String> getProductLeaderNameList = getProductLeaderName(ra);
                    //判断当前备品是否有产品担当人
                    if(getProductLeaderNameList != null && getProductLeaderNameList.size() > 0){
                        String productLeaderUserIdStr = '';
                        //获取最新的备品信息,用于判断当前的产品担当人是否已经有权限查看这条备品信息
                        Rental_Apply__c newRA = RentalApplyMapData.get(ra.Id);
                        //判断当前产品担当人是否已经在共享表里面存在(去除产品担当人共享、手动共享的人、规则和所有人)
                        //去掉手动共享原因:因为此类中会重新更新共享人,并且以上代码已经对新的共享人做了逻辑判断,不需要重新循环判断
                        boolean shareFalg = raShareMap.containsKey(newRA.Id);
                        //循环当前产品担当人,判断他是否需要新添加共享
                        for(String productLeaderName : getProductLeaderNameList){
                            if(userList != null && userList.size() > 0){
                                //循环已经查找到的的用户信息
                                for(User u : userList){
                                    //判断循环的人和当前备品的产品担当人是否中文名相同
                                    //可能出现同名民,如果出现那么这几个同名民的都加到这个备品中
                                    if(u.Alias__c == productLeaderName){
                                        String userId = u.Id;
                                        userId = userId.substring(0,15);
                                        //由于可能出现产品担当人和战略科室所有人、战略科室FSEGI担当人、战略科室FSESP担当人、服务部审批人相同,所以需要判断,如果相同,那么不用加
                                        //备品所有人还有服务部审批人相同不用加
                                        String ownerIdStr = newRA.OwnerId;
                                        ownerIdStr = ownerIdStr.substring(0,15);
                                        if(ownerIdStr.equals(userId)){
                                            //由于系统可能出现同名民的人,所以我们只是跳出本次循环
                                            continue;
                                        }else if(String.isNotBlank(newRA.Approver_of_Service_Department__c)){
                                            String approver_of_Service_Department = newRA.Approver_of_Service_Department__c;
                                            approver_of_Service_Department = approver_of_Service_Department.substring(0,15);
                                            if(userId == approver_of_Service_Department){
                                                continue;
                                            }
                                        }
                                        //战略科室客户所人
                                        if(String.isNotBlank(newRA.ShareStrategicUser__c)){
                                            if(userId == (newRA.ShareStrategicUser__c.substring(0,15))){
                                                //由于系统可能出现同名民的人,所以我们只是跳出本次循环
                                                continue;
                                            }
                                        }
                                        //战略科室FSEGI担当
                                        if(String.isNotBlank(newRA.hospitalLeaderGI__c)){
                                            if(userId == (newRA.hospitalLeaderGI__c.substring(0,15))){
                                                //由于系统可能出现同名民的人,所以我们只是跳出本次循环
                                                continue;
                                            }
                                        }
                                        //战略科室FSESP担当
                                        if(String.isNotBlank(newRA.hospitalLeaderSP__c)){
                                            if(userId == (newRA.hospitalLeaderSP__c.substring(0,15))){
                                                //由于系统可能出现同名民的人,所以我们只是跳出本次循环
                                                continue;
                                            }
                                        }
 
                                        //判断当前产品担当人是否已经在共享表里面存在(去除产品担当人共享和手动共享的人)
                                        //去掉手动共享原因:因为此类中会重新更新共享人,并且以上代码已经对新的共享人做了逻辑判断,不需要重新循环判断
                                        if(shareFalg){
                                            List<Rental_Apply__Share> rasList = raShareMap.get(newRA.Id);
                                            if(rasList != null && rasList.size() > 0){
                                                //为了更方便的跳出循环,如果在共享表里面找到当前人,证明此人已经共享不需要重新添加共享,那么就需要跳出这次循环
                                                boolean flag = false;
                                                for(Rental_Apply__Share rasO : rasList){
                                                    String rasOId1 = rasO.UserOrGroupId;
                                                    rasOId1 = rasOId1.substring(0,15);
                                                    String userId1 = u.id;
                                                    userId1 = userId1.substring(0,15);
                                                    if(rasOId1 == userId1){
                                                        flag = true;
                                                        break;
                                                    }
                                                }
                                                if(flag){
                                                    continue;
                                                }
                                            }
                                        }
                                        
                                        //如果以上条件都没满足,那么需要把这些人员信息收集,用于后期处理
                                        productLeaderUserIdStr += ','+userId;
                                    }
                                }
                            }
                        }
 
                        //判断 ProducLeader_Share__c 是否为空
                        if(String.isNotBlank(newRA.ProducLeader_Share__c)){
                            //当前备品是否有需要共享的担当人不
                            if(String.isNotBlank(productLeaderUserIdStr)){
                                productLeaderUserIdStr = productLeaderUserIdStr.substring(1,productLeaderUserIdStr.length());
                                //如果相同,那么什么也不做
                                if(!newRA.ProducLeader_Share__c.equals(productLeaderUserIdStr)){
                                    //备品表记录担当人共享的字段不为空,现在也有需要共享的担当人,并且原来备品记录的担当人和当前需要共享的担当人不是一个人,那么需要删除原来的担当人共享,重新添加共享,并且把备品记录担当人共享的字段重新更新一下
                                    delRentalApplyShare.add(newRA.id);//删除全部的产品担当共享,重新添加共享
                                    List<String> userListId = productLeaderUserIdStr.split(',');
                                    if(userListId.size() > 0){
                                        for(String userId : userListId){
                                            if(userId != ''){
                                                //重新添加共享
                                                insRentalApplyShare.add(makeData3(newRA.id,userId));
                                            }
                                        }
                                        // 20220413 ljh update start 
                                        //修改备品的产品担当人字段值
                                        // 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=''));
                                raMap.put(newRA.id,new Rental_Apply__c(id=newRA.id,ProducLeader_Share__c='')); 
                                // 20220413 ljh update end 
                            }
                        }else{
                            //如果ProducLeader_Share__c字段为空,并且当前备品的产品担当人需要共享,那么就需要我们新增共享,并把共享人id,记录到当前备品上
                            if(String.isNotBlank(productLeaderUserIdStr)){
                                productLeaderUserIdStr = productLeaderUserIdStr.substring(1,productLeaderUserIdStr.length());
                                List<String> userListId = productLeaderUserIdStr.split(',');
                                if(userListId.size() > 0){
                                    for(String userId : userListId){
                                        if(userId != ''){
                                            //重新添加共享
                                            insRentalApplyShare.add(makeData3(newRA.id,userId));
                                        }
                                    }
                                    // 20220413 ljh update start
                                    //修改备品的产品担当人字段值
                                    // 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{
                        //如果没有产品当但人,但是字段不为空,那么证明,当前备品的产品担当人有变动值设置为空了,那么我们需要处理以前已经共享的产品担当人
                        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=''));
                            raMap.put(ra.Id,new Rental_Apply__c(id=ra.id,ProducLeader_Share__c='')); 
                            // 20220413 ljh update end
                        }
                    }
                }
                //add   wangweipeng   新增备品的产品担当人共享     2021/10/15      end
            }
            Savepoint sp = Database.setSavepoint();
            try{                
                //add   wangweipeng   新增备品的产品担当人共享     2021/10/18      start
                //以下是备品借出申请的产品担当人共享
                //删除原来的产品担当共享人
                if(delRentalApplyShare != null && delRentalApplyShare.size() > 0){
                    List<Rental_Apply__Share> delentalApplyShareList = [SELECT Id,ParentId,UserOrGroupId FROM Rental_Apply__Share WHERE RowCause ='RentalApplyProducLeaderShare__c' AND ParentId in :delRentalApplyShare];
                    if(delentalApplyShareList != null && delentalApplyShareList.size() > 0){
                        delete delentalApplyShareList;
                    }
                }
                //新增产品担当共享人
                if(insRentalApplyShare != null && insRentalApplyShare.size() > 0){
                    insert insRentalApplyShare;
                }
                // 20220413 ljh update start
                //修改产品担当共享人
                // 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){
                errStr = e.getMessage()+'\n';
                errStr += e.getLineNumber()+'\n';
                Database.rollback(sp);
            }
                //add by rentx 2021-01-04 SFDC-BWQ3MX end
        }  
    }
 
    //add  wangweipeng   新增备品的产品担当人共享     2021/10/15   start
    //获取对应的产品担当人中文名
    public List<String> getProductLeaderName(Rental_Apply__c rentalApplyOneData){
        List<String> productLeaderNameList = new List<String>();//产品担当中文名
        if(rentalApplyOneData != null){
            String productLeader = '';
            String departmentType = rentalApplyOneData.OCM_dept_category__c;//战略科室分类
            if(departmentType != ''){
                if('消化科'.equals(departmentType)){productLeader = rentalApplyOneData.Hospital__r.GI_Product_Leader__c;}
                if('呼吸科'.equals(departmentType)){productLeader = rentalApplyOneData.Hospital__r.BF_Product_Leader__c;}
                if('普外科'.equals(departmentType)){productLeader = rentalApplyOneData.Hospital__r.GS_Product_Leader__c;}
                if('泌尿科'.equals(departmentType)){productLeader = rentalApplyOneData.Hospital__r.URO_Produc_Leader__c;}
                if('妇科'.equals(departmentType)){productLeader = rentalApplyOneData.Hospital__r.GYN_Product_Leader__c;}
                if('耳鼻喉科'.equals(departmentType)){productLeader = rentalApplyOneData.Hospital__r.ENT_Product_Leader__c;}
                if('ET'.equals(departmentType)){productLeader = rentalApplyOneData.Hospital__r.ET_Product_Leader__c;}
                //由于担当人可能是多个人所以需要循环一下
                if(productLeader != '' && productLeader != null){
                    if(productLeader.indexOf(',') != -1){
                        productLeader = productLeader.replace(',',',');
                    }
                    if(productLeader.indexOf(' ') != -1){
                        productLeader = productLeader.replace(' ','');
                    }
                    if(productLeader.indexOf(',') != -1){
                        for (String usrName1 : productLeader.split(',')) {
                            if(!(''.equals(usrName1))){
                                productLeaderNameList.add(usrName1);
                            }
                        }
                    }else{
                        productLeaderNameList.add(productLeader);
                    }
                }
            }
        }
        return productLeaderNameList;
    }
 
    //add  wangweipeng   新增备品的产品担当人共享     2021/10/15   end
 
 
    global void finish(Database.BatchableContext BC) {
        String tmp = '';
        if (String.isNotBlank(errStr)) {
            tmp = errStr;
        }
        iflog.Log__c += 'ConsumablesApplyOrLendBatch finish()\n';
        iflog.Log__c += '\nConsumablesApplyOrLendBatch end';
 
        tmp += iflog.ErrorLog__c;
        if (tmp.length() > 60000) {
          tmp = tmp.substring(0, 60000);
          tmp += ' ...have more lines...';
          iflog.ErrorLog__c = tmp;
        }
        String tmp2 = iflog.Log__c;
        if (tmp2.length() > 60000) {
          tmp2 = tmp2.substring(0, 60000);
          tmp2 += ' ...have more lines...';
          iflog.Log__c = tmp2;
        }
        if (System.Label.Log_IO_Flag == 'Keep') {
          update iflog;
        } else if (System.Label.Log_IO_Flag == 'Auto') {
          if (iflog.Is_Error__c > 0) {
            update iflog;
          }
        }
        //wangweipeng   20210707   start
        if(strategicDept == null || strategicDept == ''){
            //opd计划模块共享
            //如果战略科室所有人和opd计划所有人不是一个人,那么给这个战略科室所有人加一个共享
            Id execBTId = Database.executeBatch(new OPDStrategyPersonShareBatch(),50);  
        } 
        //wangweipeng   20210707   end  
    }
}