高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
public without sharing class RentalApplyShareHandler extends Oly_TriggerHandler {
 
    private Map<Id, Rental_Apply__c> newMap;
    private Map<Id, Rental_Apply__c> oldMap;
    private List<Rental_Apply__c> newList;
    private List<Rental_Apply__c> oldList;
    public RentalApplyShareHandler() {
        this.newMap = (Map<Id, Rental_Apply__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Rental_Apply__c>) Trigger.oldMap;
        this.newList = (List<Rental_Apply__c>) Trigger.new;
        this.oldList = (List<Rental_Apply__c>) Trigger.old;
    }
    protected override void afterInsert() {
        addStrategiccShare();
    }
    protected override void afterUpdate() {
        addStrategiccShare();
    }
 
    protected override void beforeInsert() {
        dataCheck();
    }
    protected override void beforeUpdate() {
        dataCheck();
    }
 
 
    //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 新建共享数据
 
 
 
    private void addStrategiccShare() {
        //add by rentx 2020-11-16 SFDC-BVA5VJ start
        // List<Id> aids = new List<Id>();//20210728 ljh SFDC-C5645S start
        // List<Rental_Apply__c> ratList = new List<Rental_Apply__c>();//20210728 ljh SFDC-C5645S start
        // List<Rental_Apply__c> raoList = new List<Rental_Apply__c>();//20210728 ljh SFDC-C5645S start
        //add by rentx 2020-11-16 SFDC-BVA5VJ end
 
        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>();
        //20210728 ljh SFDC-C5645S start
        List<Rental_Apply__Share> addStrategListL = 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>();
        Set<String> ParentIdSet = new Set<String>();
        Set<String> UserOrGroupIdSet = new Set<String>();
        Set<String> UserOrGroupIdOldSet = new Set<String>();
        Map<String, Rental_Apply__c> PUMap = new Map<String, Rental_Apply__c>();
        Map<String, Rental_Apply__c> RAUMap = new Map<String, Rental_Apply__c>();
        List<String> IsActiveNoList = new List<String>();
        //20210728 ljh SFDC-C5645S end
        for (Rental_Apply__c nObj : newList) {
            if (nObj.Strategic_user__c != null && (Trigger.isInsert || (nObj.ShareFlag__c == true && Trigger.isUpdate))) {
                // 20210924 ljh SFDC-C758NB update substring(0,15) start
                // if (!nObj.Strategic_user__c.equals(nObj.OwnerId)
                if (!String.valueOf(nObj.Strategic_user__c).substring(0,15).equals(String.valueOf(nObj.OwnerId).substring(0,15))
                        && (nObj.Approver_of_Service_Department__c == null 
                            || ( nObj.Approver_of_Service_Department__c != null && !String.valueOf(nObj.Strategic_user__c).substring(0,15).equals(String.valueOf(nObj.Approver_of_Service_Department__c).substring(0,15)) )
                        )
                   ) {
                    // 20210924 ljh SFDC-C758NB update substring(0,15) end
                    Rental_Apply__Share ras = new Rental_Apply__Share(
                        RowCause = 'Manual',
                        ParentId = nObj.Id,
                        UserOrGroupId = nObj.Strategic_user__c,
                        AccessLevel = 'read'
                    );
                    addStrategList.add(ras);
                }
                //20210728 ljh SFDC-C5645S start
                Rental_Apply__c ra = new Rental_Apply__c();
                ra.Id = nObj.Id;
                ra.ShareStrategicUser__c = nObj.Strategic_user__c;
                raList.add(ra);
                //20210728 ljh SFDC-C5645S end
            }
            //判断战略科室主担当是否发生更改
            if (nObj.ShareFlag__c == true && Trigger.isUpdate) {
                //删除原来的共享
                if (String.isNotBlank(nObj.ShareStrategicUser__c)) {
                    PUMap.put(nObj.Id, nObj);
                    ParentIdSet.add(nObj.Id);
                    UserOrGroupIdOldSet.add(nObj.ShareStrategicUser__c);
                }
            }
            //20210728 ljh SFDC-C5645S 加入共享前判断用户是否可用 start
            //如果 执行的Insert操作或者发生GI担当改变,设置共享\
            //如果执行的是insert操作
            //判断GI是否发生改变
            /*if (nObj.hospitalLeaderGIchange__c == true && String.isNotBlank(nobj.Strategic_userFSEGI__c) && !nObj.OwnerId.equals(nObj.Strategic_userFSEGI__c)) {
                //加入共享
                Rental_Apply__Share ras = makeData(nObj.Id,nObj.Strategic_userFSEGI__c);
                addStrategList.add(ras);
                //设置hospitalLeaderGI__c的值
                Rental_Apply__c ra = new Rental_Apply__c();
                ra.Id = nObj.Id;
                ra.hospitalLeaderGI__c = nObj.Strategic_userFSEGI__c;
                ratList.add(ra);
            }
 
            //判断SP是否发生改变
            if (nObj.hospitalLeaderSPchange__c && String.isNotBlank(nobj.Strategic_userFSESP__c) && !nObj.OwnerId.equals(nObj.Strategic_userFSESP__c)) {
                //加入共享
                Rental_Apply__Share ras = makeData(nObj.Id,nObj.Strategic_userFSESP__c);
                addStrategList.add(ras);
                //设置hospitalLeaderSP__c的值
                Rental_Apply__c ra = new Rental_Apply__c();
                ra.Id = nObj.Id;
                ra.hospitalLeaderSP__c = nObj.Strategic_userFSESP__c;
                raoList.add(ra);
            }
 
            //判断战略科室主担当是否和 SP/GI之前的值一样
            if ((nObj.hospitalLeaderSPchange__c && String.isNotBlank(nObj.hospitalLeaderSP__c) && String.isNotBlank(nObj.Strategic_user__c) && nObj.Strategic_user__c.equals(nObj.hospitalLeaderSP__c)) ||
                (nObj.hospitalLeaderGIchange__c && String.isNotBlank(nObj.hospitalLeaderGI__c) && String.isNotBlank(nObj.Strategic_user__c) && nObj.Strategic_user__c.equals(nObj.hospitalLeaderGI__c))) {
                Rental_Apply__Share ras = new Rental_Apply__Share(
                                RowCause = 'Manual',
                                ParentId = nObj.Id,
                                UserOrGroupId = nObj.Strategic_user__c,
                                AccessLevel = 'read'
                            );
                addStrategList.add(ras);
            }
 
            //如果是新建备品或者战略科室分类发生改变
            if (Trigger.isInsert || (Trigger.isUpdate && nObj.OCM_dept_category__c != oldMap.get(nObj.Id).OCM_dept_category__c) ) {
                //判断新的战略科室是什么
                String dept = nObj.OCM_dept_category__c;
                if(string.isNotBlank(dept)){
                    if ((nObj.Job_Category__c =='销售服务') && (dept.equals('消化科') ||dept.equals('呼吸科') || dept.equals('ET') || dept.equals('其他'))
                        && String.isNotBlank(nobj.Strategic_userFSEGI__c) && !nObj.OwnerId.equals(nObj.Strategic_userFSEGI__c)) {
                        //加入共享
                        Rental_Apply__Share ras = makeData(nObj.Id,nObj.Strategic_userFSEGI__c);
                        addStrategList.add(ras);
                    }
                    if ((nObj.Job_Category__c =='销售服务') &&
                        (dept.equals('普外科') ||dept.equals('泌尿科') || dept.equals('妇科') || dept.equals('耳鼻喉科') || dept.equals('其他'))
                        && String.isNotBlank(nobj.Strategic_userFSESP__c) && !nObj.OwnerId.equals(nObj.Strategic_userFSESP__c)) {
                        //加入共享
                        Rental_Apply__Share ras = makeData(nObj.Id,nObj.Strategic_userFSESP__c);
                        addStrategList.add(ras);
 
                    }
                }
            }
 
            //判断现在执行的操作是否为 更新
            if (Trigger.isUpdate) {
                //换换姐的
                //删除原来的共享 --战略科室主担当
                if(nObj.ShareFlag__c==true &&String.isNotBlank(nObj.ShareStrategicUser__c)){
                    PUMap.put(nObj.Id,nObj.ShareStrategicUser__c);
                    ParentIdList.add(nObj.Id);
                    UserOrGroupIdList.add(nObj.ShareStrategicUser__c);
                }
                //如果战略科室发生改变
                if (nObj.OCM_dept_category__c != oldMap.get(nObj.Id).OCM_dept_category__c) {
                    if (String.isNotBlank(nobj.hospitalLeaderGI__c)) {
                        PUMap.put(nObj.Id,nObj.hospitalLeaderGI__c);
                        UserOrGroupIdList.add(nObj.hospitalLeaderGI__c);
                    }
                    if (String.isNotBlank(nObj.hospitalLeaderSP__c)) {
                        PUMap.put(nObj.Id,nObj.hospitalLeaderSP__c);
                        UserOrGroupIdList.add(nObj.hospitalLeaderSP__c);
                    }
                    ParentIdList.add(nObj.Id);
                }
 
                //战略科室FSEGI担当,如果发生改变
                if (nObj.hospitalLeaderGIchange__c == true && String.isNotBlank(nObj.hospitalLeaderGI__c)) {
                    PUMap.put(nObj.Id,nObj.hospitalLeaderGI__c);
                    ParentIdList.add(nObj.Id);
                    UserOrGroupIdList.add(nObj.hospitalLeaderGI__c);
                }
 
                //战略科室FSESP担当,如果发生改变
                if (nObj.hospitalLeaderSPchange__c == true && String.isNotBlank(nObj.hospitalLeaderSP__c)) {
                    PUMap.put(nObj.Id,nObj.hospitalLeaderSP__c);
                    ParentIdList.add(nObj.Id);
                    UserOrGroupIdList.add(nObj.hospitalLeaderSP__c);
                }
                //判断战略科室主担当是否和 SP/GI之前的值一样
                if ((nObj.hospitalLeaderSPchange__c && String.isNotBlank(nObj.hospitalLeaderSP__c) && String.isNotBlank(nObj.Strategic_user__c) && nObj.Strategic_user__c.equals(nObj.hospitalLeaderSP__c)) ||
                    (nObj.hospitalLeaderGIchange__c && String.isNotBlank(nObj.hospitalLeaderGI__c) && String.isNotBlank(nObj.Strategic_user__c) && nObj.Strategic_user__c.equals(nObj.hospitalLeaderGI__c))) {
                    PUMap.put(nObj.Id,nObj.Strategic_user__c);
                    ParentIdList.add(nObj.Id);
                    UserOrGroupIdList.add(nObj.Strategic_user__c);
                }
 
            }20210728 ljh SFDC-C5645S 注释*/
            // System.debug('zheli00:'+nObj.OwnerId.equals(nObj.Strategic_userFSEGI__c)+'~'+nObj.Strategic_userFSEGI__c+'~'+nObj.OwnerId);
            // System.debug('zheli11:'+nObj.OwnerId.equals(nObj.Strategic_userFSESP__c)+'~'+nObj.Strategic_userFSESP__c+'~'+nObj.OwnerId+'||'+nObj.hospitalLeaderGIchange__c+'~'+nObj.hospitalLeaderSPchange__c);
            // System.debug('zheli22:'+nObj.hospitalLeaderGIchange__c+'~'+nObj.hospitalLeaderSPchange__c);
            // 20210924 ljh SFDC-C758NB update substring(0,15) start
            // if (
            //     (nObj.hospitalLeaderGIchange__c  && String.isNotBlank(nobj.Strategic_userFSEGI__c) && !nObj.OwnerId.equals(nObj.Strategic_userFSEGI__c))
            //     || (nObj.hospitalLeaderSPchange__c && String.isNotBlank(nobj.Strategic_userFSESP__c) && !nObj.OwnerId.equals(nObj.Strategic_userFSESP__c))
            // ) {
            if (
                (nObj.hospitalLeaderGIchange__c  && String.isNotBlank(nobj.Strategic_userFSEGI__c) && !String.valueOf(nObj.OwnerId).substring(0,15).equals(String.valueOf(nObj.Strategic_userFSEGI__c).substring(0,15)))
                || (nObj.hospitalLeaderSPchange__c && String.isNotBlank(nobj.Strategic_userFSESP__c) && !String.valueOf(nObj.OwnerId).substring(0,15).equals(String.valueOf(nObj.Strategic_userFSESP__c).substring(0,15)))
            ) {
                // if (nObj.hospitalLeaderGIchange__c) {
                if (nObj.hospitalLeaderGIchange__c && !String.valueOf(nObj.OwnerId).substring(0,15).equals(String.valueOf(nObj.Strategic_userFSEGI__c).substring(0,15))) {
                    Rental_Apply__Share ras = makeData(nObj.Id, nObj.Strategic_userFSEGI__c);
                    PUMap.put(nObj.Id, nObj);
                    ParentIdSet.add(nObj.Id);
                    UserOrGroupIdSet.add(nObj.Strategic_userFSEGI__c);
                    UserOrGroupIdOldSet.add(nObj.hospitalLeaderGI__c);
                    addStrategListL.add(ras);
                }
                // if (nObj.hospitalLeaderSPchange__c) {
                if (nObj.hospitalLeaderSPchange__c && !String.valueOf(nObj.OwnerId).substring(0,15).equals(String.valueOf(nObj.Strategic_userFSESP__c).substring(0,15))) {
                    Rental_Apply__Share ras = makeData(nObj.Id, nObj.Strategic_userFSESP__c);
                    PUMap.put(nObj.Id, nObj);
                    ParentIdSet.add(nObj.Id);
                    UserOrGroupIdSet.add(nObj.Strategic_userFSESP__c);
                    UserOrGroupIdOldSet.add(nObj.hospitalLeaderSP__c);
                    addStrategListL.add(ras);
                }
            }
            //20210728 ljh SFDC-C5645S 加入共享前判断用户是否可用 end
        }
        // 20210924 ljh SFDC-C758NB update substring(0,15) end
        if(UserOrGroupIdSet.size() > 0){
            for (User user : [Select Id, Name, IsActive From User WHERE Id in:UserOrGroupIdSet]) {
                //未启用
                if (!user.IsActive) {
                    String  userId = (String)user.Id;
                    userId = userId.substring(0, 15);
                    IsActiveNoList.add(userId);
                }
            }
        }
            //add by rentx 2020-11-26 SFDC-BVA5VJ end
        // System.debug('zheli22:'+UserOrGroupIdOldSet.size()+'~'+ParentIdSet.size());
        if(UserOrGroupIdOldSet.size() > 0 && ParentIdSet.size() > 0 ){
            //删除原来的共享
            delStrategList  = [SELECT Id, ParentId, UserOrGroupId FROM Rental_Apply__Share WHERE  RowCause = 'Manual' AND ParentId in :ParentIdSet AND UserOrGroupId in :UserOrGroupIdOldSet];
            for (Integer m = 0; m < delStrategList.size(); m++) {
                if (PUMap.get(delStrategList[m].ParentId) == null) {
                    delStrategList.remove(m);
                }
            }
            if (delStrategList.size() > 0) {
                // System.debug('zheli11:'+delStrategList);
                delete delStrategList;
            }
        }   
        //增加
        if (addStrategListL.size() > 0) {
            //20210728 ljh SFDC-C5645S
            for (Rental_Apply__Share ras : addStrategListL) {
                String temp = (String)ras.UserOrGroupId;
                if ( IsActiveNoList.size() == 0 ||
                        (IsActiveNoList.size() > 0 && !IsActiveNoList.contains(temp.substring(0, 15)))
                   ) {
                    addStrategList.add(ras);
                }
 
                Rental_Apply__c Ra = PUMap.get(ras.ParentId);
                Rental_Apply__c raU = new Rental_Apply__c();
                if (Ra != null ) {
                    // System.debug('zheli00:'+Ra.hospitalLeaderGIchange__c+'~'+Ra.hospitalLeaderSPchange__c);
                    raU.Id = Ra.Id;
                    String tempGI = Ra.Strategic_userFSEGI__c;
                    String tempSP = Ra.Strategic_userFSESP__c;
                    if (Ra.hospitalLeaderGIchange__c && String.isNotBlank(tempGI)) {
                        raU.hospitalLeaderGI__c = Ra.Strategic_userFSEGI__c;
                    } else {
                        raU.hospitalLeaderGI__c = '';
                    }
                    if (Ra.hospitalLeaderSPchange__c && String.isNotBlank(tempSP)) {
                        raU.hospitalLeaderSP__c = Ra.Strategic_userFSESP__c;
                    } else {
                        raU.hospitalLeaderSP__c = '';
                    }
                    RAUMap.put(raU.Id, raU);
                }
            }
            //20210728 ljh SFDC-C5645S end
        }
        // System.debug('zheli00:'+addStrategList);
        // 共享表
        if (addStrategList.size() > 0) {
            insert addStrategList;
        }
        // 更新
        // 20210924 ljh SFDC-C758NB update 优化 start
        if (raList.size() > 0) {
            // update raList;
            for(Rental_Apply__c ra:raList){
                if(RAUMap.containsKey(ra.Id)){
                    RAUMap.get(ra.Id).ShareStrategicUser__c = ra.ShareStrategicUser__c;
                }else{
                    RAUMap.put(ra.Id, ra);
                }
            }
        }
        // 20210924 ljh SFDC-C758NB update 优化 end
        
        //更新
        if (RAUMap.size() > 0) {
            update RAUMap.values();
        }
        
        /*
        //更新
        if(ratList.size()>0){
            update ratList;
        }
        //更新
        if(raoList.size()>0){
            update raoList;
        }//20210728 ljh SFDC-C5645S 注释*/
    }
 
    private void dataCheck() {
        Map<Id, Rental_Apply__c> dataCheckMap = new Map<Id, Rental_Apply__c>();
        List<OPDPlan__c> oPDPlanList = new List<OPDPlan__c>();
        Map<Id, OPDPlan__c> dataOPDMap = new Map<Id, OPDPlan__c>();
        for (Rental_Apply__c newObj : newList) {
 
            Rental_Apply__c oldObj = oldMap == null ? null : oldMap.get(newObj.Id);
            Boolean changeFlag = false;
            if (oldObj == null) {
 
                //新增
                //1.获取 opd计划上记录的 原备品借出申请Id 获取31个相关字段的值 与当前值作比较
                dataCheckMap.put(newObj.OPDPlan__c, newObj);
            } else {
                //应该有一个 补充申请创建 判断
                //变更
                //有31个字段的值不可修改
                if (newObj.SupplementCreated__c) {
                    changeFlag = RentalApplydataCheck(null, newObj, oldObj);
                    if (System.Test.isRunningTest() == false && changeFlag) {
                        newObj.addError('关联的OPD计划为补充OPD计划 不能编辑备品借用信息');
                    }
                    //system.debug('changeFlag---->'+changeFlag);
                }
            }
        }
 
        if (dataCheckMap.size() > 0) {
            //查询 OPD计划
            oPDPlanList = [SELECT Id, Name, supplementaryApplication__c, originalOpdRentalApply__c,
                           OriginalOpdPlanApplication__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Person_In_Charge__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.applyUser__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.ApplyPerson_Phone__c ,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Hospital__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Strategic_dept__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Account__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Loaner_medical_Staff__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Phone_number__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Demo_purpose1__c ,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.demo_purpose2__c ,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Product_category__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.AnimalExerimentFlag__c ,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Campaign__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Demo_purpose_text__c ,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Repair__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Follow_UP_Opp__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.CrinicalTrialName__c ,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.DB_loaner_request__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Request_demo_detail__c ,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Request_shipping_day__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Request_return_day__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Shipment_address__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Dealer__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Loaner_received_staff__c ,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.direct_shippment_address__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.direct_send__c ,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.pickup_time__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Loaner_received_staff_phone__c ,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Post_Code__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.GI_Diff__c ,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Hope_Lonaer_date_Num__c,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Name,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Id,
                           OriginalOpdPlanApplication__r.Rental_Apply2__r.Rental_Apply_Equipment_Set_Cnt__c
                           FROM OPDPlan__c
                           WHERE Id In:dataCheckMap.keySet()
                          ];
            String sqlName = '';
            String newName = '';
            for (OPDPlan__c opd : oPDPlanList) {
                //OPD计划 通过 补充申请创建
                if (opd.supplementaryApplication__c) {
                    dataOPDMap.put(opd.Id, opd);
                    String oldRaName = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Name;
                    sqlName = oldRaName.indexOf('_') >= 0 ? oldRaName.split('_')[0] : oldRaName;
                    sqlName = sqlName + '%';
                }
            }
            if (dataOPDMap.size() > 0) {
                String rootRentalApplyId = null;
                if (String.isNotBlank(sqlName)) {
                    List<Rental_Apply__c> oldRaList = new List<Rental_Apply__c>();
                    oldRaList = [select Id, Name from Rental_Apply__c where Name  like :sqlName order by Name];
                    if (oldRaList.size() > 0) {
                        rootRentalApplyId = oldRaList[0].Id;
                        Integer num = oldRaList.size();
                        newName = sqlName.split('%')[0] + '_' + num;
                    } else {
                        newName = sqlName.split('%')[0] + '_' + 1;
                    }
 
                }
 
                for (Rental_Apply__c newObj : newList) {
 
                    Rental_Apply__c oldObj = oldMap == null ? null : oldMap.get(newObj.Id);
                    Boolean changeFlag = false;
                    if (oldObj == null) {
                        //通过补充申请创建 备品借出申请
                        if (dataOPDMap.containskey(newObj.OPDPlan__c)) {
                            OPDPlan__c opd = dataOPDMap.get(newObj.OPDPlan__c);
                            newObj.Request_return_day__c = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Request_return_day__c;
                            changeFlag = RentalApplydataCheck(opd, newObj, null);
 
                            if (System.Test.isRunningTest() == false && changeFlag) {
                                newObj.addError('关联的OPD计划为补充OPD计划 不能编辑备品借用信息');
                            }
                            newObj.Old_Rental_Apply__c = opd.OriginalOpdPlanApplication__r.Rental_Apply2__c;
                            newObj.Split_Apply_Reason__c = '补充借用';
                            newObj.SupplementCreated__c = true;
                            newObj.Name = newName;
                            newObj.Root_Rental_Apply__c = rootRentalApplyId;
                            newObj.OriginalOpdPlanApplication__c = opd.OriginalOpdPlanApplication__c;
                        }
 
                    }
                }
            }
 
        }
    }
 
    private Boolean RentalApplydataCheck(OPDPlan__c opd, Rental_Apply__c newObj, Rental_Apply__c oldObj) {
        //新增
        Boolean changeFlag = false;
        if (opd != null ) {
            Boolean Person_In_Charge = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Person_In_Charge__c != newObj.Person_In_Charge__c;
            system.debug('Person_In_Charge__c--->' + Person_In_Charge);
            Boolean applyUser = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.applyUser__c != newObj.applyUser__c;
            system.debug('applyUser--->' + applyUser);
 
            Boolean ApplyPerson_Phone = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.ApplyPerson_Phone__c != newObj.ApplyPerson_Phone__c;
            system.debug('ApplyPerson_Phone--->' + ApplyPerson_Phone);
 
            boolean Hospital =  opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Hospital__c != newObj.Hospital__c;
            system.debug('Hospital__c--->' + Hospital);
 
            boolean Strategic_dept = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Strategic_dept__c != newObj.Strategic_dept__c;
            system.debug('Strategic_dept__c--->' + Strategic_dept);
 
            boolean Account = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Account__c != newObj.Account__c;
            system.debug('Account__c--->' + Account);
 
            boolean Loaner_medical_Staff = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Loaner_medical_Staff__c != newObj.Loaner_medical_Staff__c;
            system.debug('Loaner_medical_Staff__c--->' + Loaner_medical_Staff);
 
            boolean Phone_number = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Phone_number__c  != newObj.Phone_number__c;
            system.debug('Phone_number__c--->' + Phone_number);
 
            boolean Demo_purpose1 = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Demo_purpose1__c  != newObj.Demo_purpose1__c;
            system.debug('Demo_purpose1__c--->' + Demo_purpose1);
 
            boolean demo_purpose2 = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.demo_purpose2__c  != newObj.demo_purpose2__c;
            system.debug('demo_purpose2__c--->' + demo_purpose2);
 
            boolean Product_category = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Product_category__c != newObj.Product_category__c;
            system.debug('Product_category__c--->' + Product_category);
 
            boolean AnimalExerimentFlag = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.AnimalExerimentFlag__c != newObj.AnimalExerimentFlag__c;
            system.debug('AnimalExerimentFlag__c--->' + AnimalExerimentFlag);
 
            boolean Campaign = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Campaign__c != newObj.Campaign__c;
            system.debug('Campaign__c--->' + Campaign);
 
            boolean Demo_purpose_text = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Demo_purpose_text__c != newObj.Demo_purpose_text__c;
            system.debug('Demo_purpose_text__c--->' + Demo_purpose_text);
 
            boolean Repair = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Repair__c != newObj.Repair__c;
            system.debug('Repair__c--->' + Repair);
 
            boolean Follow_UP_Opp = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Follow_UP_Opp__c != newObj.Follow_UP_Opp__c;
            system.debug('Follow_UP_Opp__c--->' + Follow_UP_Opp);
 
            boolean CrinicalTrialName = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.CrinicalTrialName__c  != newObj.CrinicalTrialName__c;
            system.debug('CrinicalTrialName__c--->' + CrinicalTrialName);
 
            boolean DB_loaner_request = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.DB_loaner_request__c != newObj.DB_loaner_request__c;
            system.debug('DB_loaner_request__c--->' + DB_loaner_request);
 
            boolean Request_demo_detail = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Request_demo_detail__c  != newObj.Request_demo_detail__c;
            system.debug('Request_demo_detail__c--->' + Request_demo_detail);
 
//boolean Request_shipping_day = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Request_shipping_day__c != newObj.Request_shipping_day__c;
//system.debug('Request_shipping_day__c--->'+Request_shipping_day);
 
            boolean Request_return_day = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Request_return_day__c != newObj.Request_return_day__c;
            system.debug('Request_return_day__c--->' + Request_return_day);
 
            boolean Shipment_address = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Shipment_address__c != newObj.Shipment_address__c;
            system.debug('Shipment_address__c--->' + Shipment_address);
 
            boolean Dealer = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Dealer__c != newObj.Dealer__c;
            system.debug('Dealer__c--->' + Dealer);
 
            boolean Loaner_received_staff = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Loaner_received_staff__c  != newObj.Loaner_received_staff__c;
            system.debug('Loaner_received_staff__c--->' + Loaner_received_staff);
 
            boolean direct_shippment_address = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.direct_shippment_address__c  != newObj.direct_shippment_address__c;
            system.debug('direct_shippment_address__c--->' + direct_shippment_address);
 
            boolean direct_send = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.direct_send__c  != newObj.direct_send__c;
            system.debug('direct_send__c--->' + direct_send);
 
            boolean pickup_time = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.pickup_time__c != newObj.pickup_time__c;
            system.debug('pickup_time__c--->' + pickup_time);
 
            boolean Loaner_received_staff_phone = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Loaner_received_staff_phone__c  != newObj.Loaner_received_staff_phone__c;
            system.debug('Loaner_received_staff_phone__c--->' + Loaner_received_staff_phone);
 
            boolean Post_Code = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Post_Code__c  != newObj.Post_Code__c;
            system.debug('Post_Code__c--->' + Post_Code);
 
            boolean GI_Diff = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.GI_Diff__c  != newObj.GI_Diff__c;
            system.debug('GI_Diff__c--->' + GI_Diff);
 
            boolean Hope_Lonaer_date_Num = opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Hope_Lonaer_date_Num__c != newObj.Hope_Lonaer_date_Num__c ;
            system.debug('Hope_Lonaer_date_Num__c--->' + Hope_Lonaer_date_Num);
            changeFlag  =  opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Person_In_Charge__c != newObj.Person_In_Charge__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.applyUser__c != newObj.applyUser__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.ApplyPerson_Phone__c != newObj.ApplyPerson_Phone__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Hospital__c != newObj.Hospital__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Strategic_dept__c != newObj.Strategic_dept__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Account__c != newObj.Account__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Loaner_medical_Staff__c != newObj.Loaner_medical_Staff__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Phone_number__c  != newObj.Phone_number__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Demo_purpose1__c  != newObj.Demo_purpose1__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.demo_purpose2__c  != newObj.demo_purpose2__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Product_category__c != newObj.Product_category__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.AnimalExerimentFlag__c != newObj.AnimalExerimentFlag__c  ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Campaign__c != newObj.Campaign__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Demo_purpose_text__c != newObj.Demo_purpose_text__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Repair__c != newObj.Repair__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Follow_UP_Opp__c != newObj.Follow_UP_Opp__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.CrinicalTrialName__c  != newObj.CrinicalTrialName__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.DB_loaner_request__c != newObj.DB_loaner_request__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Request_demo_detail__c  != newObj.Request_demo_detail__c ||
                           //opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Request_shipping_day__c != newObj.Request_shipping_day__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Request_return_day__c != newObj.Request_return_day__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Shipment_address__c != newObj.Shipment_address__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Dealer__c != newObj.Dealer__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Loaner_received_staff__c  != newObj.Loaner_received_staff__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.direct_shippment_address__c  != newObj.direct_shippment_address__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.direct_send__c  != newObj.direct_send__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.pickup_time__c != newObj.pickup_time__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Loaner_received_staff_phone__c  != newObj.Loaner_received_staff_phone__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Post_Code__c  != newObj.Post_Code__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.GI_Diff__c  != newObj.GI_Diff__c ||
                           opd.OriginalOpdPlanApplication__r.Rental_Apply2__r.Hope_Lonaer_date_Num__c != newObj.Hope_Lonaer_date_Num__c ;
        } else {
            changeFlag  =  newObj.Person_In_Charge__c != oldObj.Person_In_Charge__c ||
                           newObj.applyUser__c != oldObj.applyUser__c ||
                           newObj.ApplyPerson_Phone__c != oldObj.ApplyPerson_Phone__c ||
                           newObj.Hospital__c != oldObj.Hospital__c ||
                           newObj.Strategic_dept__c != oldObj.Strategic_dept__c ||
                           newObj.Account__c != oldObj.Account__c ||
                           newObj.Loaner_medical_Staff__c != oldObj.Loaner_medical_Staff__c ||
                           newObj.Phone_number__c != oldObj.Phone_number__c ||
                           newObj.Demo_purpose1__c  != oldObj.Demo_purpose1__c ||
                           newObj.demo_purpose2__c != oldObj.demo_purpose2__c ||
                           newObj.Product_category__c != oldObj.Product_category__c ||
                           newObj.AnimalExerimentFlag__c != oldObj.AnimalExerimentFlag__c ||
                           newObj.Campaign__c != oldObj.Campaign__c ||
                           newObj.Demo_purpose_text__c != oldObj.Demo_purpose_text__c ||
                           newObj.Repair__c != oldObj.Repair__c ||
                           newObj.Follow_UP_Opp__c != oldObj.Follow_UP_Opp__c ||
                           newObj.CrinicalTrialName__c != oldObj.CrinicalTrialName__c ||
                           newObj.DB_loaner_request__c != oldObj.DB_loaner_request__c ||
                           newObj.Request_demo_detail__c != oldObj.Request_demo_detail__c ||
                           //newObj.Request_shipping_day__c != oldObj.Request_shipping_day__c ||
                           newObj.Request_return_day__c != oldObj.Request_return_day__c ||
                           newObj.Shipment_address__c != oldObj.Shipment_address__c ||
                           newObj.Dealer__c != oldObj.Dealer__c ||
                           newObj.Loaner_received_staff__c != oldObj.Loaner_received_staff__c ||
                           newObj.direct_shippment_address__c != oldObj.direct_shippment_address__c ||
                           newObj.direct_send__c != oldObj.direct_send__c ||
                           newObj.pickup_time__c != oldObj.pickup_time__c ||
                           newObj.Loaner_received_staff_phone__c != oldObj.Loaner_received_staff_phone__c ||
                           newObj.Post_Code__c != oldObj.Post_Code__c ||
                           newObj.GI_Diff__c !=  oldObj.GI_Diff__c ||
                           newObj.Hope_Lonaer_date_Num__c !=  oldObj.Hope_Lonaer_date_Num__c;
        }
        return changeFlag;
 
    }
}