liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
public without sharing class RentalApplyFaultHandler extends Oly_TriggerHandler {
    private Map<Id, Rental_Apply_Fault__c> newMap;
    private Map<Id, Rental_Apply_Fault__c> oldMap;
    private List<Rental_Apply_Fault__c> newList;
    private List<Rental_Apply_Fault__c> oldList;
 
    public RentalApplyFaultHandler(){
        this.newMap =  (Map<Id, Rental_Apply_Fault__c>) Trigger.newMap;
        this.oldMap =  (Map<Id, Rental_Apply_Fault__c>) Trigger.oldMap;
        this.newList = (List<Rental_Apply_Fault__c>) Trigger.new;
        this.oldList = (List<Rental_Apply_Fault__c>) Trigger.old;
 
        
    }
 
 
    protected override void beforeInsert() {
 
        //List 备品借出申请
        List<String> raListIds = new List<String>();
        Set<String> radListIds = new Set<String>();
        //申请单Id, List<故障报告>
        Map<Id, List<Rental_Apply_Fault__c>> raMap = new Map<Id, List<Rental_Apply_Fault__c>>();
 
        //Name 规则 : 备品借出申请单 + 001 ~999
        for(Rental_Apply_Fault__c raf: newList){
            //获取所有的申请单Id
            radListIds.add(raf.Rental_Apply_Equipment_Set_Detail__c);
        }
 
        
        // 获取申请单下所有明细的Id
        List<Rental_Apply_Equipment_Set_Detail__c> raesdList = [SELECT Id,Rental_Apply__c from Rental_Apply_Equipment_Set_Detail__c WHERE Id in :radListIds];
        for(Rental_Apply_Equipment_Set_Detail__c rae : raesdList){
            raListIds.add(rae.Rental_Apply__c);
        }
 
        raesdList = [SELECT Id,Rental_Apply__c from Rental_Apply_Equipment_Set_Detail__c WHERE Rental_Apply__c in :raListIds];
        for(Rental_Apply_Equipment_Set_Detail__c rea : raesdList){
            radListIds.add(rea.Id);
        }
 
        system.debug('radListIds===='+radListIds);
        
        //查出 申请单下全部的检测报告
        List<Rental_Apply_Fault__c> raList = [SELECT Id, Rental_Apply_Equipment_Set_Detail__c, Rental_Apply_Equipment_Set_Detail__r.Rental_Apply__r.Id,Rental_Apply_Equipment_Set_Detail__r.Rental_Apply__r.Name from Rental_Apply_Fault__c where Rental_Apply_Equipment_Set_Detail__c in :radListIds];
 
        //查出 所有申请单明细
        Map<Id,Rental_Apply_Equipment_Set_Detail__c> radMap = new Map<Id,Rental_Apply_Equipment_Set_Detail__c>([
                Select Id,Rental_Apply__r.Name, Rental_Apply__r.Id, Rental_Apply__r.Person_In_Charge__c, Rental_Apply__r.demo_purpose2__c,
                        Rental_Apply__r.Rental_Assistant__c, Rental_Apply__r.Rental_Assistant2__c, Rental_Apply__r.Loaner_Storage_mail_address__c,
                        Rental_Apply__r.ZongjianApprovalManager__c, Rental_Apply__r.BuchangApprovalManagerSales__c, SerialNumber_F__c,
                        Rental_Apply__r.SalesManager__c, Rental_Apply__r.Loaner_centre_mail_address__c, Rental_Apply__r.Hospital__r.Name,
                        Fixture_Model_No__c, Rental_Apply__r.OCM_dept_category__c, Rental_Apply__r.WorkPlace__c, (select id from Rental_Apply_Equipment_Set_Detail_Fault__r)
                from Rental_Apply_Equipment_Set_Detail__c 
                where id in :radListIds]);
 
        system.debug('radMap===='+radMap);
        for (Rental_Apply_Fault__c raf : raList) {
            system.debug('raf.Rental_Apply_Equipment_Set_Detail__c==='+raf.Rental_Apply_Equipment_Set_Detail__c);
            system.debug('radMap.get(raf.Rental_Apply_Equipment_Set_Detail__c).Rental_Apply__r.Id===' + radMap.get(raf.Rental_Apply_Equipment_Set_Detail__c).Rental_Apply__r.Id);
            if(raMap.containsKey(radMap.get(raf.Rental_Apply_Equipment_Set_Detail__c).Rental_Apply__r.Id)){
                raMap.get(radMap.get(raf.Rental_Apply_Equipment_Set_Detail__c).Rental_Apply__r.Id).add(raf);
            } else {
                List<Rental_Apply_Fault__c> tempList = new List<Rental_Apply_Fault__c>();
                tempList.add(raf);
                raMap.put(radMap.get(raf.Rental_Apply_Equipment_Set_Detail__c).Rental_Apply__r.Id,tempList);
            }
        }
 
        system.debug('raMap===='+raMap);
 
        for(Rental_Apply_Fault__c raf : newList){
 
            //当前借出明细Id
            String radId = raf.Rental_Apply_Equipment_Set_Detail__c;
            //当前申请单Id
            String raId = radMap.get(radId).Rental_Apply__r.Id;
 
            if( radMap.containsKey(raf.Rental_Apply_Equipment_Set_Detail__c) && radMap.get(raf.Rental_Apply_Equipment_Set_Detail__c).Rental_Apply_Equipment_Set_Detail_Fault__r.size() >= 1 ){
                raf.addError('已存在检测分析报告');
            }
 
            if(raMap.containsKey(raId)){
                
                Integer rafSize = raMap.get(raId).size() + 1;
                system.debug('rafSize===='+rafSize);
                String rafSizeString = '000' + rafSize;
                String cutString = rafSizeString.substring(rafSizeString.length()-3, rafSizeString.length());
 
                if(rafSize == 0) {
                    raf.Name = radMap.get(radId).Rental_Apply__r.Name + '-JCBG-001';
                }else{
                    raf.Name = radMap.get(radId).Rental_Apply__r.Name + '-JCBG-' + cutString;
                }
            }else{
                raf.Name = radMap.get(radId).Rental_Apply__r.Name + '-JCBG-001';
            }
 
            raf.RentalApplyNo__c = radMap.get(radId).Rental_Apply__r.Name;
            raf.demo_purpose2__c = radMap.get(radId).Rental_Apply__r.demo_purpose2__c;
            raf.HospitalText__c = radMap.get(radId).Rental_Apply__r.Hospital__r.Name;
            raf.Repair_product_body_number__c = radMap.get(radId).SerialNumber_F__c;
            raf.Fixture_Model_No__c = radMap.get(radId).Fixture_Model_No__c;
            raf.OCM_dept_category__c = radMap.get(radId).Rental_Apply__r.OCM_dept_category__c;
            raf.WorkPlace__c = radMap.get(radId).Rental_Apply__r.WorkPlace__c;
            raf.Person_In_Charge_User__c = radMap.get(radId).Rental_Apply__r.Person_In_Charge__c;
            raf.Rental_Assistant__c = radMap.get(radId).Rental_Apply__r.Rental_Assistant__c;
            raf.Rental_Assistant2__c = radMap.get(radId).Rental_Apply__r.Rental_Assistant2__c;
            raf.ZongjianApprovalManager__c = radMap.get(radId).Rental_Apply__r.ZongjianApprovalManager__c;
            raf.BuchangApprovalManagerSales__c = radMap.get(radId).Rental_Apply__r.BuchangApprovalManagerSales__c;
            raf.SalesManager__c = radMap.get(radId).Rental_Apply__r.SalesManager__c;
            raf.Loaner_centre_mail_address__c = radMap.get(radId).Rental_Apply__r.Loaner_centre_mail_address__c;
            raf.Loaner_Storage_mail_address__c = radMap.get(radId).Rental_Apply__r.Loaner_Storage_mail_address__c;
            
            raf.status__c = '草案中';
        }
    }
 
    protected override void afterInsert() {
        // 给备品出借担当增加读写权限
        List<Rental_Apply_Fault__Share> rafShareList = new List<Rental_Apply_Fault__Share>();
        for (Rental_Apply_Fault__c raf: newList) {
            // 备品出借担当
            if (raf.Person_In_Charge_User__c != null) {
                Rental_Apply_Fault__Share rafShare = new Rental_Apply_Fault__Share();
                rafShare.AccessLevel = 'Edit';
                rafShare.UserOrGroupId = raf.Person_In_Charge_User__c;
                rafShare.ParentID = raf.Id;
                rafShareList.add(rafShare);
            }
 
            Rental_Apply_Fault__c tempRaf = [select id, ZongjianApprovalManager__r.isActive, BuchangApprovalManagerSales__r.isActive, SalesManager__r.isActive from Rental_Apply_Fault__c where Id =:raf.id ];
 
            System.debug('-------------temp.ZongjianApprovalManager__r.isActive-------------' + tempRaf.ZongjianApprovalManager__r.isActive);
            System.debug('-------------raf.ZongjianApprovalManager__r.isActive--------------' + raf.ZongjianApprovalManager__r.isActive);
 
            // CL4 高级总监
            if (raf.ZongjianApprovalManager__c != null && tempRaf.ZongjianApprovalManager__r.isActive) {
                Rental_Apply_Fault__Share rafShare = new Rental_Apply_Fault__Share();
                rafShare.AccessLevel = 'READ';
                rafShare.UserOrGroupId = raf.ZongjianApprovalManager__c;
                rafShare.ParentID = raf.Id;
                rafShareList.add(rafShare);
            }
 
            // CL5 总监级
            if (raf.BuchangApprovalManagerSales__c != null && tempRaf.BuchangApprovalManagerSales__r.isActive) {
                Rental_Apply_Fault__Share rafShare = new Rental_Apply_Fault__Share();
                rafShare.AccessLevel = 'READ';
                rafShare.UserOrGroupId = raf.BuchangApprovalManagerSales__c;
                rafShare.ParentID = raf.Id;
                rafShareList.add(rafShare);
            }
 
            // CL6 经理级
            if (raf.SalesManager__c != null && tempRaf.SalesManager__r.isActive) {
                Rental_Apply_Fault__Share rafShare = new Rental_Apply_Fault__Share();
                rafShare.AccessLevel = 'READ';
                rafShare.UserOrGroupId = raf.SalesManager__c;
                rafShare.ParentID = raf.Id;
                rafShareList.add(rafShare);
            }
        }
 
        if (!rafShareList.isEmpty()) {
            insert rafShareList;
        }
        
    }
 
    protected override void beforeUpdate() {
        Date today = Date.today();
        List<Rental_Apply_Fault__c> deleteAttachmentIds = new List<Rental_Apply_Fault__c>();
        Set<Id> deleteIds = new Set<Id>();
        for(Rental_Apply_Fault__c raf: newList){
            Rental_Apply_Fault__c oldRaf = oldMap.get(raf.Id);
 
            if (raf.status__c == '已发送' && oldRaf.status__c != '已发送') {
                raf.SendDate__c = today;
            }
 
            if (raf.status__c == '已反馈' && oldRaf.status__c != '已反馈') {
                raf.LoanerFeedback_date__c = today;
 
                if (raf.UseExplain__c == null) {
                    raf.adderror('【备品使用情况说明】不能为空');
                    return;
                }
            }
 
            if ((String.isBlank(raf.AttachmentId1__c) && !String.isBlank(oldRaf.AttachmentId1__c)) 
                || (String.isBlank(raf.AttachmentId2__c) && !String.isBlank(oldRaf.AttachmentId2__c))) {
                deleteAttachmentIds.add(raf);
                if (String.isBlank(raf.AttachmentId1__c) && !String.isBlank(oldRaf.AttachmentId1__c)) {
                    deleteIds.add(oldRaf.AttachmentId1__c);
                }
                if (String.isBlank(raf.AttachmentId2__c) && !String.isBlank(oldRaf.AttachmentId2__c)) {
                    deleteIds.add(oldRaf.AttachmentId2__c);
                }
            }
        }
 
 
        if (!deleteAttachmentIds.isEmpty()) {
            // //20231027 ymh添加注释  lighting修改附件上传 start
            // List<Attachment> attList = [SELECT Id,ParentId FROM Attachment WHERE ParentId IN: deleteAttachmentIds Order by ParentId,Id];
            // Map<String, List<Attachment>> parentMap = new Map<String, List<Attachment>>();
            // for (Attachment att : attList) {
            //     if (!deleteIds.contains(att.Id)) {
            //         if (!parentMap.isEmpty() && parentMap.containskey(att.ParentId)) {
            //             List<Attachment> attTemp = parentMap.get(att.ParentId);
            //             attTemp.add(att);
            //             parentMap.put(att.ParentId, attTemp);
            //         } else {
            //             List<Attachment> attTemp = new List<Attachment>();
            //             attTemp.add(att);
            //             parentMap.put(att.ParentId, attTemp);
            //         }
            //     }
            // }
            
            List<Id> reList = new List<Id>();
            for(Rental_Apply_Fault__c  re: deleteAttachmentIds){
                reList.add(re.Id);
            }
            List<ContentDocumentLink> linkId = [SELECT ContentDocumentId,LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId IN: reList Order by LinkedEntityId];
            List<Id> idList = new List<Id>();
            for (ContentDocumentLink link : linkId) {
                idList.add(link.ContentDocumentId);
            }
            Map<String, List<ContentVersion>> parentMap = new Map<String, List<ContentVersion>>();
            List<ContentVersion> versionList = [SELECT ContentDocumentId,Id FROM ContentVersion where ContentDocumentId in: idList];
            Map<String,ContentVersion> shareMap = new Map<String,ContentVersion>();
            for (ContentVersion version : versionList) {
                for(ContentDocumentLink link: linkId){
                    System.debug('conId  ' + link.ContentDocumentId);
                    System.debug('verId  ' + version.ContentDocumentId);
                    if(link.ContentDocumentId == version.ContentDocumentId){
                        shareMap.put(link.ContentDocumentId,version);
                        
                    }
                }
            }
            for (ContentDocumentLink link : linkId) {
                if (!deleteIds.contains(link.ContentDocumentId)) {
                    if (!parentMap.isEmpty() && parentMap.containskey(link.LinkedEntityId)) {
                        List<ContentVersion> linkTemp = parentMap.get(link.LinkedEntityId);
                        linkTemp.add(shareMap.get(link.ContentDocumentId));
                        parentMap.put(link.LinkedEntityId, linkTemp);
                    } else {
                        List<ContentVersion> linkTemp = new List<ContentVersion>();
                        linkTemp.add(shareMap.get(link.ContentDocumentId));
                        parentMap.put(link.LinkedEntityId, linkTemp);
                    }
                }
            }
 
 
            for (Rental_Apply_Fault__c raf : deleteAttachmentIds) {
                if (!parentMap.isEmpty() && parentMap.containskey(raf.Id)) {
                    Rental_Apply_Fault__c oldObj = oldMap.get(raf.Id);
                    System.debug('oldObj' + oldObj);
                    // List<Attachment> attTemp = parentMap.get(raf.Id);
                    List<ContentVersion> cVersionTemp = parentMap.get(raf.Id);
                    System.debug('cVersionTemp' + cVersionTemp);
                    for(Integer i = 0;i < cVersionTemp.size();i++){
                        System.debug('attId1:' + raf.AttachmentId1__c);
                        System.debug('attId2:' + raf.AttachmentId2__c);
                        System.debug('oldattId1:' + oldObj.AttachmentId1__c);
                        System.debug('oldattId2:' + oldObj.AttachmentId2__c);
                        if(oldObj.AttachmentId1__c == cVersionTemp[i].Id && raf.AttachmentId1__c == null){
                            System.debug('123456:' + cVersionTemp[i].Id);
                            cVersionTemp.remove(i);
                        }else if(oldObj.AttachmentId2__c == cVersionTemp[i].Id && raf.AttachmentId2__c == null){
                            System.debug('123456:' + cVersionTemp[i].Id);
                            cVersionTemp.remove(i);
                        }
 
                    }
                    System.debug('========================================' + cVersionTemp);
                    if(cVersionTemp.size() == 0){
                    }else if (cVersionTemp.size() == 1) {
                        // raf.AttachmentId1__c = attTemp[0].Id;
                        raf.AttachmentId1__c = cVersionTemp[0].Id;
                        raf.AttachmentId2__c = null;
                    } else {
                        // raf.AttachmentId1__c = attTemp[0].Id;
                        // raf.AttachmentId2__c = attTemp[1].Id;
                        raf.AttachmentId1__c = cVersionTemp[0].Id;
                        raf.AttachmentId2__c = cVersionTemp[1].Id;
                    }
            // //20231027 ymh添加注释  lighting修改附件上传 end
                }
            }
        }
    }
 
    protected override void afterUpdate() {
        Set<Id> feedbackIds = new Set<Id>();
        Set<Id> sendReportIds = new Set<Id>();
        for(Rental_Apply_Fault__c raf: newList){
            Rental_Apply_Fault__c oldRaf = oldMap.get(raf.Id);
 
            if (raf.status__c == '已发送' && oldRaf.status__c != '已发送') {
                sendReportIds.add(raf.Id);
            }
 
            if (raf.status__c == '已反馈' && oldRaf.status__c != '已反馈') {
                feedbackIds.add(raf.Id);
            }
        }
 
        if (!sendReportIds.isEmpty()) {
            //备品中心推送邮件给现场
            sendEmail(sendReportIds, '1');
        }
        
        if (!feedbackIds.isEmpty()) {
            //现场回复后推送的邮件
            sendEmail(feedbackIds, '2');
        }
    }
 
    //推送邮件处理
    public void sendEmail(Set<Id> rafIds, String flag) {
        List<Rental_Apply_Fault__c> rafList = [
                SELECT Id, Person_In_Charge_User__r.Email, Owner.Email, Rental_Assistant__r.Email, Rental_Assistant2__r.Email,
                        Loaner_centre_mail_address__c, Loaner_Storage_mail_address__c, ZongjianApprovalManager__r.Email,
                        BuchangApprovalManagerSales__r.Email, SalesManager__r.Email, Salesdept__c, StorageCheckEmail__c
                FROM Rental_Apply_Fault__c 
                WHERE Id IN: rafIds];
 
        EmailTemplate et = null;
        if (flag == '1') {
            et = [Select id from EmailTemplate where name = '备品检测分析报告' limit 1];
        } else if (flag == '2') {
            et = [Select id from EmailTemplate where name = '备品检测分析报告操作不当提醒已回复' limit 1];
        } else {
            return;
        }
        
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
 
        for (Rental_Apply_Fault__c raf : rafList) {
            String[] toAddresses = new String[]{};
            String[] ccAddresses = new String[]{};
 
            if (flag == '1') {
                //To 申请人
                toAddresses.add(raf.Person_In_Charge_User__r.Email);
                //To 备品跟进助理
                if(raf.Rental_Assistant__r.Email != null) {
                    toAddresses.add(raf.Rental_Assistant__r.Email);
                }
                if(raf.Rental_Assistant2__r.Email != null){
                    toAddresses.add(raf.Rental_Assistant2__r.Email);
                }
 
                //CC   部外 备品审批流人员及所属总监---cl4总监邮箱 备品总窗口
                //部内 白总、古部长,林部长   白科,古芳丽,林芳
                //     ocmasset公共邮箱
                //     检测仓库公共邮箱
                ccAddresses = System.Label.RentalApplyFaultReplyToBuNei.split(';');
 
                //cc 部外 备品审批流人员及所属总监---cl4总监邮箱  备品总窗口
                setEmailAddress(raf, ccAddresses);
                // ljh  修改空指针 start
                if(raf.Loaner_Storage_mail_address__c!=null){
                    ccAddresses.add(raf.Loaner_Storage_mail_address__c);
                }
                 // ljh  修改空指针 end
                ccAddresses.add(raf.Loaner_centre_mail_address__c);
                //ccAddresses.add(raf.StorageCheckEmail__c);
 
            } else if (flag == '2') {
                //To ocmasset公共邮箱 检测仓库公共邮箱 检测担当
                toAddresses.add(raf.Loaner_centre_mail_address__c);
                toAddresses.add(raf.Loaner_Storage_mail_address__c);
                //toAddresses.add(raf.StorageCheckEmail__c);
                toAddresses.add(raf.Owner.Email);
 
                //部内  白总、古部长,林部长
                ccAddresses = System.Label.RentalApplyFaultReplyToBuNei.split(';');
 
                //cc 部外 备品审批流人员及所属总监---cl4总监邮箱  备品总窗口
                setEmailAddress(raf, ccAddresses);
 
                //Cc 备品跟进助理
                if(raf.Rental_Assistant__r.Email != null) {
                    ccAddresses.add(raf.Rental_Assistant__r.Email);
                }
                if(raf.Rental_Assistant2__r.Email != null){
                    ccAddresses.add(raf.Rental_Assistant2__r.Email);
                }
            }
            
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail = Messaging.renderStoredEmailTemplate(et.Id, null, raf.Id);
            mail.setSaveAsActivity(false);
            mail.setToAddresses(toAddresses);
            System.debug('=============' + ccAddresses);
            mail.setCcAddresses(ccAddresses);
            emails.add(mail);
        }
 
        Messaging.sendEmail(emails);
    }
 
    public void setEmailAddress(Rental_Apply_Fault__c rafObj, String[] ccAddr) {
        //本部                窗口    CL5总监(备品审批) CL4高级总监(备品审批)   建议抄送人1  建议抄送人2  备注
        //华北                赵靖苹  是                  否                      慕海霞       李鸷
        //东北                关贝妮  是                  否                      赵志成       李鸷 
        //西北                张媛媛  是                  否                      颜大庆       宋琰
        //西南                赵雯雯  是                  是
        //华东                徐明楠  是                  否                      宋真         杨剑清  
        //华南                夏俊洁  是                  是                      俞澜         严蕊芳  
        //MA                  无     是                  否                       陆吉       
        //SOLUTION            殷佳骊  是                  否
        //医疗服务本部         无      是                                         陆吉(理论上没有)
        //外科事业本部         李海红  是                  否 
        //市场企划本部         无      是                                         陆吉(理论上没有) 
        //消化.呼吸内镜事业本部 无      是                 否                      陆吉  
        //消化.呼吸领域解决方案本部   无   是                                      陆吉(理论上没有)  
        if (rafObj.Salesdept__c == '医疗华北营业本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_Huabei) && System.Label.RentalApplyFaultReplyTo_Huabei != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_Huabei.split(';'));
            }
        } else if (rafObj.Salesdept__c == '医疗东北营业本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_Dongbei) && System.Label.RentalApplyFaultReplyTo_Dongbei != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_Dongbei.split(';'));
            }
        } else if (rafObj.Salesdept__c == '医疗西北营业本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_Xibei) && System.Label.RentalApplyFaultReplyTo_Xibei != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_Xibei.split(';'));
            }
        } else if (rafObj.Salesdept__c == '医疗西南营业本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (rafObj.ZongjianApprovalManager__c != null) {
                ccAddr.add(rafObj.ZongjianApprovalManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_Xinan) && System.Label.RentalApplyFaultReplyTo_Xinan != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_Xinan.split(';'));
            }
        } else if (rafObj.Salesdept__c == '医疗华东营业本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_Huadong) && System.Label.RentalApplyFaultReplyTo_Huadong != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_Huadong.split(';'));
            }
        } else if (rafObj.Salesdept__c == '医疗华南营业本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (rafObj.ZongjianApprovalManager__c != null) {
                ccAddr.add(rafObj.ZongjianApprovalManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_Huanan) && System.Label.RentalApplyFaultReplyTo_Huanan != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_Huanan.split(';'));
            }
        } else if (rafObj.Salesdept__c == 'MA本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_MA) && System.Label.RentalApplyFaultReplyTo_MA != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_MA.split(';'));
            }
        } else if (rafObj.Salesdept__c == 'Solution本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_SOLUTION) && System.Label.RentalApplyFaultReplyTo_SOLUTION != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_SOLUTION.split(';'));
            }
        } else if (rafObj.Salesdept__c == '医疗服务本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_Yiliao) && System.Label.RentalApplyFaultReplyTo_Yiliao != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_Yiliao.split(';'));
            }
        } else if (rafObj.Salesdept__c == '外科事业本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_Waike) && System.Label.RentalApplyFaultReplyTo_Waike != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_Waike.split(';'));
            }
        } else if (rafObj.Salesdept__c == '市场企划本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_Shichang) && System.Label.RentalApplyFaultReplyTo_Shichang != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_Shichang.split(';'));
            }
        } else if (rafObj.Salesdept__c == '消化·呼吸内镜事业本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_HXNJ) && System.Label.RentalApplyFaultReplyTo_HXNJ != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_HXNJ.split(';'));
            }
        } else if (rafObj.Salesdept__c == '消化·呼吸领域解决方案本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_HXLY) && System.Label.RentalApplyFaultReplyTo_HXLY != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_HXLY.split(';'));
            }
        }
    }
 
    // 测试类  add ymh  20231101补充
    @TestVisible
    public void runningline() {
        Integer i = 0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
    }
}