binxie
2024-01-18 0e0dd1e20e7211f3c3c11d77a41090d998dfd06c
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
trigger ContentDocumentLink on ContentDocumentLink (before insert,after insert,before delete) {
    
    //deloitte-zhj 20231124 本地化导入 start
    if((!Test.isRunningTest())&&System.Label.ByPassTrigger.contains(UserInfo.getUserId())){
        return;
    }
    //deloitte-zhj 20231124 本地化导入 end
    
    
    // 20231103 陈京武 Lightning文件修改 Start
    if(StaticParameter.ContentDocumentLink == false){
        return;
    }
    // 20231105 陈京武 紧急 Start
    // if(Trigger.isDelete && (Trigger.isBefore)){
    //     for(ContentDocumentLink link : Trigger.old){
    //         if(String.valueOf(link.LinkedEntityId).startsWith('a5C') && UserInfo.getProfileId() != (ID)'00e10000000Y3o5AAC'){
    //             link.addError('请联系管理员');
    //         }
    //     }
    // }
    // 20231105 陈京武 紧急 Start
    if((!Test.isRunningTest())&&UserInfo.getUserId()==System.Label.ByPassTrigger){
        return;
    }
//     if (Trigger.isAfter && Trigger.isInsert) {
//         Set<Id> Ids = new Set<id>();
//         Map<Id,ContentDocumentLink> mapcvwithid = new Map<Id,ContentDocumentLink>();
//         for(ContentDocumentLink c:Trigger.new){
//         ids.add(c.ContentDocumentId);
//         mapcvwithid.put(c.ContentDocumentId,c);
//         }
//     if(ids.size() >  0){
//         for(ContentDocument ContentDocumentObj : [select id,FileType from ContentDocument where id in: ids]){
//             if(mapcvwithid.get(ContentDocumentObj.Id).LinkedEntityId != null && ContentDocumentObj.FileType != null){
//                 String ObjectName = mapcvwithid.get(ContentDocumentObj.Id).LinkedEntityId.getSObjectType().getDescribe().getName();
//                 String filetype = ContentDocumentObj.FileType.toLowerCase();
//                 String CsName = ObjectName+'-'+filetype;
                
//                     ContentDocumentLink ContentDocumentLinkObj = mapcvwithid.get(ContentDocumentObj.Id);
//                     ContentDocumentLinkObj.addError('Testing error');
//                     return;
//             }
//         }
//     }
// }
    List<String> rentalIds = new List<String>();
    List<String> attIds = new List<String>();
    List<String> rentalApplyIds = new List<String>();
    List<String> accIds = new List<String>();
    //20221014 yc 医院审批优化 start
    List<String> accAppIds = new List<String>();
    //20221014 yc 医院审批优化 end
    // LHJ CHAN-BCLD4P 20190618 Start
    List<String> specIds = new List<String>();
    // LHJ CHAN-BCLD4P 20190618 End
 
    // MZY SWAG-BXXBKA  2021-02-19 start
    List<String> opdIds = new List<String>();
    Map<String,String> opdMap = new Map<String,String>();
    // MZY SWAG-BXXBKA  2021-02-19 end
 
    List<String> OpporFileIds = new List<String>();
    Map<String,String> OpporFileUndeleteMap = new Map<String,String>();
    List<String> DeleteReIds = new List<String>();
    Map<String,String> OppordeleteReUserMap = new Map<String,String>();
    Map<String,Datetime> OppordeleteReTimeMap = new Map<String,Datetime>();
    //CHAN-BCNCRB ---XHL----Start-----
    List<String> opportunity_FileIds = new List<String>();
 
    List<String> opportunityFileOrderIds = new List<String>();
    List<String> oFOrderIds = new List<String>();
    Map<String,Boolean> oFOrderIdMap = new Map<String,Boolean>();
 
    // sx 20230210start 备品借出申请故障报告附件提取
    List<String> rentalApplyFaultIds = new List<String>();
    Map<String,List<Id>> trentalImages = new Map<String,List<Id>>();
    List<String> rentaldeleteApplyFaultIds = new List<String>();
    Map<String,List<Id>> deleteImages = new Map<String,List<Id>>();
    // sx 20230210end 备品借出申请故障报告附件提取
    Set<Id> docidList = new Set<Id>();
    Set<Id> delDocidList = new Set<Id>();
    if (Trigger.isBefore && Trigger.isInsert) {
        for (ContentDocumentLink link : Trigger.new) {
            if(String.valueOf(link.LinkedEntityId).startsWith(System.Label.Oppor_sepKey)){
                opportunity_FileIds.add(link.LinkedEntityId);
            }
            //deloitte-zhj 20231211 a3Z 改为 a2n
            if(String.valueOf(link.LinkedEntityId).startsWith('a2n')){
                opportunityFileOrderIds.add(link.LinkedEntityId);
            }
            //deloitte-zhj 20231211 a0J 改为 a3r
            if(!String.valueOf(link.LinkedEntityId).startsWith('a3r') 
            && !String.valueOf(link.LinkedEntityId).startsWith('a3T')){  //deloitte-zhj 20231211 a0f 改为 a3T
                docidList.add(link.ContentDocumentId);
            }
        }
    }
    if (Trigger.isAfter && Trigger.isInsert) {
        for (ContentDocumentLink link : Trigger.new) {
            if(!String.valueOf(link.LinkedEntityId).startsWith('a3r') 
            && !String.valueOf(link.LinkedEntityId).startsWith('a3T')){
                docidList.add(link.ContentDocumentId);
            }
        }
    }
    if(Trigger.isDelete && Trigger.isBefore){
        for (ContentDocumentLink link : Trigger.old) {
            if(!String.valueOf(link.LinkedEntityId).startsWith('a3r') 
            && !String.valueOf(link.LinkedEntityId).startsWith('a3T')){
                docidList.add(link.ContentDocumentId);
            }
            delDocidList.add(link.ContentDocumentId);
        }
    }
    //deloitte-zhj 20231227 改善空指针
    //ContentVersion ver = new ContentVersion();
    List<ContentVersion> ver = new List<ContentVersion>();
    System.debug('docidList.size=>' + docidList.size());
    if(docidList.size() > 0){
        System.debug('docidList = ' + docidList);
        ver = [select FileType,TextPreview,Id from ContentVersion where ContentDocumentId in: docidList limit 1];
        //deloitte-zhj 20231227 改善空指针
        if(ver.size() > 0){
            System.debug('TextPreview=>' + ver[0].TextPreview);
            System.debug('FileType=>' + ver[0].TextPreview);
            //if(ver.FileType == 'SNOTE'){
            if(ver[0].FileType == 'SNOTE'){
                System.debug('可恶的备注!!!');
                return;
            }
        }
    }
    if (Trigger.isBefore && Trigger.isDelete) {
        List<ContentDocumentLink> conDeleteList = new List<ContentDocumentLink>();
        for (ContentDocumentLink link : Trigger.old) {
            if (String.valueOf(link.LinkedEntityId).startsWith('001')) {
                accIds.add(link.LinkedEntityId);
            }
            // LHJ CHAN-BCLD4P 20190618 Start
            if(String.valueOf(link.LinkedEntityId).startsWith(System.Label.OppSpecialApply)){
                specIds.add(link.LinkedEntityId);
            }
            // LHJ CHAN-BCLD4P 20190618 End
            //20221014 yc 医院审批优化 start
            //deloitte-zhj 20231211 a5C 改为 a3h,   a5A  改为  a02
            if (String.valueOf(link.LinkedEntityId).startsWith('a3h') || String.valueOf(link.LinkedEntityId).startsWith('a02')) {
                accAppIds.add(link.LinkedEntityId);
            }
            //20221014 yc 医院审批优化 end
 
             //2023-11-06  陈京武 备品检测分析报告 start
             String parentLabel = link.LinkedEntityId.getSObjectType().getDescribe().getName();
             System.debug('...' + link.LinkedEntityId.getSObjectType().getDescribe().getName());
             if(parentLabel.equals('Rental_Apply_Fault__c')){
                 rentaldeleteApplyFaultIds.add(link.LinkedEntityId);
                 conDeleteList.add(link);
                 //deloitte-zhj 20231227 改善空指针
                //  if(deleteImages.containskey(ver.Id)){
                //      deleteImages.get(link.LinkedEntityId).add(ver.Id);
                //  }else{
                //      List<Id> temp = new List<Id>();
                //      temp.add(ver.Id);
                //      deleteImages.put(link.LinkedEntityId,temp);
                //  }
                if(ver.size() > 0){
                    if(deleteImages.containskey(ver[0].Id)){
                        deleteImages.get(link.LinkedEntityId).add(ver[0].Id);
                    }else{
                        List<Id> temp = new List<Id>();
                        temp.add(ver[0].Id);
                        deleteImages.put(link.LinkedEntityId,temp);
                    }
                }
             }
             //2023-11-06  陈京武 备品检测分析报告 end
        }
         //2023-2-10  sx备品 备品检测分析报告 start
         List<Rental_Apply_Fault__c> rafList = [SELECT Id, Name FROM Rental_Apply_Fault__c WHERE Id IN: rentaldeleteApplyFaultIds AND (status__c = '已发送' OR status__c = '已反馈')];
         for (Rental_Apply_Fault__c raf : rafList) {
             for (ContentDocumentLink link1 : conDeleteList) {
                 if (link1.LinkedEntityId == raf.Id) {
                    link1.addError('非草案中的检测分析报告不允许删除已上传的故障图片');
                 }
             }
         }
         //2023-2-10  sx备品 备品检测分析报告 end
 
    }
 
 
    //CHAN-BCNCRB ---XHL----End-----
    if (Trigger.isAfter && Trigger.isInsert) {
        List<Id> idList = new List<Id>();
        for (ContentDocumentLink link : Trigger.new) {
            idList.add(link.ContentDocumentId);
        }
        List<ContentVersion> version = [select Id,Title from ContentVersion where ContentDocumentId in: idList];
        for (ContentDocumentLink link : Trigger.new) {
            //if (att.Name.toUpperCase() == '合同书.PDF' && String.valueOf(att.ParentId).startsWith('a0t')) {
            // 添付した契約書のファイル名は指定できない
            //deloitee-zhj 20231211 a2K => a15
            if (String.valueOf(link.LinkedEntityId).startsWith('a15')) {
                rentalIds.add(link.LinkedEntityId);
                attIds.add(version[0].Id);
            }
            //deloitee-zhj 20231211 a0t => a3k
            if (String.valueOf(link.LinkedEntityId).startsWith('a3k')) {
                rentalApplyIds.add(link.LinkedEntityId); 
            }
            if (String.valueOf(link.LinkedEntityId).startsWith('001')) {
                accIds.add(link.LinkedEntityId);
            }
            //20221014 yc 医院审批优化 start
            //deloitte-zhj 20231211 a5C 改为 a3h,   a5A  改为  a02
            if (String.valueOf(link.LinkedEntityId).startsWith('a3h') || String.valueOf(link.LinkedEntityId).startsWith('a02')) {
                accAppIds.add(link.LinkedEntityId);
            }
            //20221014 yc 医院审批优化 end
            System.debug(link.LinkedEntityId +'陈京武');
            if(String.valueOf(link.LinkedEntityId).startsWith(System.Label.Oppor_sepKey)){
                OpporFileIds.add(link.LinkedEntityId);
            }
            // LHJ CHAN-BCLD4P 20190618 Start
            if(String.valueOf(link.LinkedEntityId).startsWith(System.Label.OppSpecialApply)){
                specIds.add(link.LinkedEntityId);
            }
            // LHJ CHAN-BCLD4P 20190618 End
 
            // MZY SWAG-BXXBKA  2021-02-19 start 
            //deloitte-zhj 20231211 a3E 改为 a2e
            if(String.valueOf(link.LinkedEntityId).startsWith('a2e')){
                opdIds.add(link.LinkedEntityId);
                opdMap.put(link.LinkedEntityId,version[0].Title);
             }
            // MZY SWAG-BXXBKA  2021-02-19 end
            //deloitte-zhj 20231211 a3Z 改为 a2n
            if(String.valueOf(link.LinkedEntityId).startsWith('a2n')){
                oFOrderIds.add(link.LinkedEntityId);
                oFOrderIdMap.put(link.LinkedEntityId,true);
            }
 
            //2023-2-10  sx备品 备品检测分析报告 start
            Id cid = link.LinkedEntityId;
            String parentLabel = cid.getSObjectType().getDescribe().getName();
            if(parentLabel.equals('Rental_Apply_Fault__c')){
                rentalApplyFaultIds.add(link.LinkedEntityId);
                if(trentalImages.containskey(link.LinkedEntityId)){
                    trentalImages.get(link.LinkedEntityId).add(version[0].Id);
                }else{
                    List<Id> temp = new List<Id>();
                    temp.add(version[0].Id);
                    trentalImages.put(link.LinkedEntityId,temp);
                }
            }
            //2023-2-10  sx备品 备品检测分析报告 end
        }
    }
 
 
    
    //2023-2-10  sx备品 备品检测分析报告 start
    if(rentalApplyFaultIds.size() > 0){
        List<Rental_Apply_Fault__c> renList = [SELECT Id,AttachmentId1__c,AttachmentId2__c from Rental_Apply_Fault__c where Id in :rentalApplyFaultIds];
 
        system.debug('updrentalApplyFaultList====='+renList);
        for(Rental_Apply_Fault__c ren : renList){
            if(trentalImages.containskey(ren.Id)){ 
                if (ren.AttachmentId1__c == null) {
                    ren.AttachmentId1__c = trentalImages.get(ren.id)[0];
                } else if(ren.AttachmentId2__c == null) {
                    ren.AttachmentId2__c = trentalImages.get(ren.id)[0];
                }
            }
        }
        system.debug('updrentalApplyFaultList====='+renList);
        UPDATE renList;
    }
 
    if(rentaldeleteApplyFaultIds.size() > 0){
        List<Rental_Apply_Fault__c> renList = [SELECT Id,AttachmentId1__c,AttachmentId2__c from Rental_Apply_Fault__c where Id in :rentaldeleteApplyFaultIds];
 
        system.debug('renList====='+renList);
        for(Rental_Apply_Fault__c ren : renList){
            if(deleteImages.containskey(ren.Id)){
 
                if(deleteImages.get(ren.Id).contains(ren.AttachmentId1__c)){
                    ren.AttachmentId1__c = '';
                }
                if(deleteImages.get(ren.Id).contains(ren.AttachmentId2__c)){
                    ren.AttachmentId2__c = '';
                }
            }
        }
        system.debug('renList====='+renList);
        UPDATE renList;
    }
    //2023-2-10  sx备品 备品检测分析报告 end
 
 
    
    if (rentalIds.size() > 0) {
        List<Consumable_order__c> rentalList = new List<Consumable_order__c>();
        for (Consumable_order__c ra : [select Id from Consumable_order__c where Id in :rentalIds]) {
            ra.Consumable_pdf_insert_day__c = Date.today();
            ra.Order_Attachment__c = attIds[0];
            //ra.Order_status__c = '录入完成';
            //ra.Contract_pdf_updated__c = true;
            rentalList.add(ra);
        }
        ControllerUtil.updRentalApplyList(rentalList);
    }
     if (rentalApplyIds.size() > 0) {
        List<Rental_Apply__c> rentalApplyList = new List<Rental_Apply__c>();
        for (Rental_Apply__c ra : [select Id from Rental_Apply__c where Id in :rentalApplyIds]) {
            ra.Contract_pdf_update_day__c = Date.today();
            ra.Contract_pdf_updated__c = true;
            rentalApplyList.add(ra);
        }
        ControllerUtil.updRentalApplyList(rentalApplyList);
    }
 
    if (accIds.size() > 0) {
        Map<String, String> attMap = new Map<String, String>();
        // List<Attachment> attList = [select Id, ParentId from Attachment where ParentId = :accIds order by ParentId];
        List<ContentDocumentLink> linkList = [select ContentDocumentId,LinkedEntityId from ContentDocumentLink where ContentDocument.FileType != 'SNOTE' and LinkedEntityId =: accIds order by LinkedEntityId];
        System.debug('客户Link的条数:' + linkList.size());
        for (ContentDocumentLink link : linkList) {
            if(!delDocidList.contains(link.ContentDocumentId)){
                attMap.put(link.LinkedEntityId, link.LinkedEntityId);
            }
        }
        System.debug('attMap的条数:' + attMap);
        List<Account> updAccList = new List<Account>();
 
        //20221207 lt 医院审批优化 start
        List<Account> accList = [select id,Name,
                                                         Is_Active__c
                                               from Account
                                               where id in:accIds ];
        //20221207 lt 医院审批优化 end
 
        for (String accid : accIds) {
            if(accList.size() > 0){
                // Account acc = new Account(Id = accid);
                //20221207 lt 医院审批优化 start
                for(Account acc : accList){  
                    if(Trigger.isUpdate && Trigger.isAfter){
                        for(ContentDocumentLink link : Trigger.New){
 
                            if(attMap.containsKey(link.LinkedEntityId) &&acc.Is_Active__c != '草案中'){
                                System.debug('lt123---草案中---');
                                link.addError('非草案中,附件不允许编辑。');
                
                                return;
                            }
                        }
                    }                  
                    if(Trigger.isDelete && (Trigger.isBefore)){
                        for(ContentDocumentLink link : Trigger.old){
                            if(acc.Is_Active__c != '草案中'){
                                link.addError('非草案中,附件不允许删除。');
                
                                return;
                            }
                        }
                    }
                    acc.Is_upload_file__c = attMap.containsKey(accid);
                    System.debug('是否上传附件:' + attMap.containsKey(accid));
                    updAccList.add(acc);
                }
            }
            //20221207 lt 医院审批优化 end
        }
 
        // for (String accid : accIds) {
        //     Account acc = new Account(Id = accid);
        //     acc.Is_upload_file__c = attMap.containsKey(accid);
        //     updAccList.add(acc);
        // }
        if (updAccList.size() > 0){
            StaticParameter.ContentDocumentTrigger = false;
            update updAccList;
        }
    }
 
    //20221014 yc 医院审批优化 start
    if (accAppIds.size() > 0) {
        Map<String, String> attMap = new Map<String, String>();
        // List<Attachment> attList = [select Id, ParentId from Attachment where ParentId = :accAppIds order by ParentId];
        List<ContentDocumentLink> linkList = [select ContentDocumentId,LinkedEntityId from ContentDocumentLink where  ContentDocument.FileType != 'SNOTE' and  LinkedEntityId =: accAppIds order by LinkedEntityId];
        for (ContentDocumentLink link : linkList) {
            if(!delDocidList.contains(link.ContentDocumentId)){
                attMap.put(link.LinkedEntityId, link.LinkedEntityId);
            }
        }
        List<Account_Delay_Apply__c> updAccAppList = new List<Account_Delay_Apply__c>();
        
        //20221207 lt 医院审批优化 start
        List<Account_Delay_Apply__c> adaList = [select id,Name,
                                                         Is_Active__c
                                               from Account_Delay_Apply__c 
                                               where id in:accAppIds ];
        //20221207 lt 医院审批优化 end
 
        for (String accappid : accAppIds) {
            if(adaList.size() > 0){
                // Account_Delay_Apply__c accapp = new Account_Delay_Apply__c(Id = accappid);
                //20221207 lt 医院审批优化 start
                
                for(Account_Delay_Apply__c accapp : adaList){                    
                    accapp.Is_upload_file__c = attMap.containsKey(accappid);
                    updAccAppList.add(accapp);
                }
                
            }
            
            //20221207 lt 医院审批优化 end
            
        }
        try {
            System.debug('客户变更申请');
            if (updAccAppList.size() > 0){
                StaticParameter.ContentDocumentTrigger = false;
                update updAccAppList;
            }  
        } catch (Exception e) {
            linkList[0].addError(e.getMessage());
        }
        
    }
    //20221014 yc 医院审批优化 end
 
    // LHJ CHAN-BCLD4P 20190618 Start
    if (specIds.size() > 0) {
        
        Map<String, String> attMap = new Map<String, String>();
        // List<Attachment> attList = [select Id, ParentId from Attachment where ParentId = :specIds order by ParentId];
        List<ContentDocumentLink> linkList = [select LinkedEntityId from ContentDocumentLink where  ContentDocument.FileType != 'SNOTE' and  LinkedEntityId =: specIds order by LinkedEntityId];
        for (ContentDocumentLink link : linkList) {
            attMap.put(link.LinkedEntityId, link.LinkedEntityId);
        }
        List<OpportunitySpecialApply__c> updSpecList = new List<OpportunitySpecialApply__c>();
        for (String specId : specIds) {
            OpportunitySpecialApply__c spec = new OpportunitySpecialApply__c(Id = specId);
            spec.Is_upload_file__c = attMap.containsKey(specId);
            updSpecList.add(spec);
        }
        if (updSpecList.size() > 0) update updSpecList;
    }
    // LHJ CHAN-BCLD4P 20190618 End
    
    // MZY SWAG-BXXBKA  2021-02-19 start
    System.debug('SWAG-BXXBKA start');
    if (opdIds.size()>0) {
        List<OPDPlan__c> updateOPDs = new List<OPDPlan__c>();
 
        OPDPlan__c tempOPD = new OPDPlan__c();
        for (String i :opdIds ) {
            tempOPD.id = i;
            tempOPD.AttachmentCertificate__c = opdMap.get(i);
            updateOPDs.add(tempOPD);
        }
        System.debug('临时OPD :'+tempOPD);
        if (updateOPDs.size()>0){
            update updateOPDs;
        }
    }
    System.debug('SWAG-BXXBKA end');
    // MZY SWAG-BXXBKA  2021-02-19 end
 
    if(OpporFileIds.size()>0){
        List<Opportunity_File__c> OpporList = [select id,Last_upload_time__c,
                                                            Is_Locked__c,
                                                            Last_upload_user__c,
                                                            Stock_apply_time__c
                                                        from Opportunity_File__c 
                                                        where id 
                                                        in:OpporFileIds ];
        if(OpporList.size()>0){
            for(Opportunity_File__c ofc : OpporList){
                if(!ofc.Is_Locked__c){
                    ofc.Last_upload_user__c = UserInfo.getUserId();
                    ofc.Last_upload_time__c = System.now();
                    System.debug(ofc+'23333');
                    update OpporList;
                }else{
                    OpporFileUndeleteMap.put(ofc.id,ofc.id);
                    System.debug(ofc+'23333444444');
                }
            }
            
        }
    }
    System.debug(Trigger.isDelete+'Trigger.isDelete');
    System.debug(Trigger.isBefore+'Trigger.isBefore');
   
    if(Trigger.isDelete && Trigger.isBefore){
        for(ContentDocumentLink link : Trigger.old){
            if(OpporFileUndeleteMap.containsKey(link.LinkedEntityId)){
                link.addError('备货已审批,不可删除附件');
                return;
            }else{
                DeleteReIds.add(link.LinkedEntityId);
            }
        }
    }
    
 
    if(Trigger.isInsert){
        for(ContentDocumentLink link : Trigger.new){
            if(OpporFileUndeleteMap.containsKey(link.LinkedEntityId)){
                // ContentDocument doc = [select Id from ContentDocument where Id =: link.ContentDocumentId];
                link.addError('备货已审批,不可删除附件');
                // link.addError('备货已审批,不可删除附件');
                System.debug(Trigger.isBefore+'陈京武');
                return;
            }else{
                if(link.LinkedEntityId.getSObjectType().getDescribe().getName() != 'User'){
                    DeleteReIds.add(link.LinkedEntityId);
                }
                // System.debug(Trigger.isBefore+'武');
                // DeleteReIds.add(link.LinkedEntityId);
            }
        }
    }
    if(DeleteReIds.size() > 0){
        // List<Attachment> atcList = [select id,ParentId,createddate,createdbyid from Attachment where ParentId in:DeleteReIds];
        List<ContentDocumentLink> linkList = [select LinkedEntityId,ContentDocumentId from ContentDocumentLink where LinkedEntityId in:DeleteReIds];
        List<Id> idList = new List<Id>();
        for (ContentDocumentLink link : linkList) {
            if(link.LinkedEntityId.getSObjectType().getDescribe().getName() != 'User'){
                idList.add(link.ContentDocumentId);
            }
        }
        List<ContentDocument> docList = [select Id,CreatedDate,CreatedById from ContentDocument where Id =: idList];
        System.debug(DeleteReIds+'222222222222');
        System.debug(docList + 'doclist');
        if(docList.size()>0){
            for(String ids : DeleteReIds){
                for(ContentDocument doc : docList){
                    if(OppordeleteReTimeMap.containsKey(ids)){
                        if(doc.CreatedDate > OppordeleteReTimeMap.get(ids)){
                            OppordeleteReTimeMap.put(ids,doc.CreatedDate);
                            OppordeleteReUserMap.put(ids,doc.CreatedById);
                        }else{
                            continue;
                        }
                    }else{
                        OppordeleteReTimeMap.put(ids,doc.CreatedDate);
                        OppordeleteReUserMap.put(ids,doc.CreatedById);
                    }
                }
            }
 
            System.debug(OppordeleteReTimeMap + 'OppordeleteReTimeMap');
            System.debug(OppordeleteReUserMap + 'OppordeleteReUserMap');
            List<Opportunity_File__c> reDateList = [select id,Last_upload_time__c,
                                                                    Is_Locked__c,
                                                                    Last_upload_user__c
                                                                from Opportunity_File__c 
                                                                where id 
                                                                in:DeleteReIds ];
            if(reDateList.size()>0){
                for(Opportunity_File__c ofc : reDateList){
                    ofc.Last_upload_time__c = OppordeleteReTimeMap.get(ofc.id);
                    ofc.Last_upload_user__c = OppordeleteReUserMap.get(ofc.id);
                    System.debug(ofc+'233333333');
                }
                if(Trigger.isInsert && Trigger.isAfter){
                    update reDateList;
                }
                
            }
        }else{
            List<Opportunity_File__c> reDateList = [select id,Last_upload_time__c,
                                                                    Is_Locked__c,
                                                                    Last_upload_user__c
                                                                from Opportunity_File__c 
                                                                where id 
                                                                in:DeleteReIds ];
            if(reDateList.size()>0){
                for(Opportunity_File__c ofc : reDateList){
                    if (!ofc.Is_Locked__c) {
                        ofc.Last_upload_time__c = null;
                        ofc.Last_upload_user__c = null;
                        update reDateList;
                    }
                    
                }
            }
        }
    }
 
    
 
    //CHAN-BCNCRB ---XHL----Start-----
    if (opportunity_FileIds.size() > 0) {
        List<Opportunity_File__c> opporList = [select id,Last_upload_time__c,
                                                            Is_Locked__c,
                                                            Last_upload_user__c,
                                                            Stock_apply_time__c,
                                                            Oppor_File_Stage__c
                                                        from Opportunity_File__c 
                                                        where id in:opportunity_FileIds And Oppor_File_Stage__c = 'G'];
        List<String>  ofIds = new  List<String>();                                              
        if (opporList.size() > 0) {
 
            for (Opportunity_File__c oppf : opporList) {
                ofIds.add(oppf.Id);
            }
 
            if (ofIds.size() > 0 ) {
                //  List<Attachment> atcList = [select id,ParentId,createddate,createdbyid from Attachment where ParentId in:ofIds];
                List<ContentDocumentLink> linkList = [select ContentDocumentId from ContentDocumentLink where LinkedEntityId in:ofIds];
 
                List<Id> idList = new List<Id>();
                for (ContentDocumentLink link : linkList) {
                    idList.add(link.ContentDocumentId);
                }
                List<ContentDocument> docList = [select Id from ContentDocument where Id in: idList];
                if (docList.size() > 0) {
                    delete docList;
                }
            }
           
            
        }
    }
    //CHAN-BCNCRB ---XHL----End-----
    
 
    if (opportunityFileOrderIds.size() > 0) {
        List<OpportunityFileOrder__c> ofoList = [select id,
                                                            OpporFileStage__c
                                                        from OpportunityFileOrder__c 
                                                        where Id in:opportunityFileOrderIds And OpporFileStage__c = 'G'];
        List<String>  ofoIds = new  List<String>();                                              
        if (ofoList.size() > 0) {
 
            for (OpportunityFileOrder__c ofo : ofoList) {
                ofoIds.add(ofo.Id);
            }
 
            if (ofoIds.size() > 0 ) {
                //  List<Attachment> atcList = [select id,ParentId,createddate,createdbyid from Attachment where ParentId in:ofoIds];
                List<ContentDocumentLink> linkList = [select ContentDocumentId from ContentDocumentLink where LinkedEntityId in:ofoIds];
 
                List<Id> idList = new List<Id>();
                for (ContentDocumentLink link : linkList) {
                    idList.add(link.ContentDocumentId);
                }
                List<ContentDocument> docList = [select Id from ContentDocument where Id in: idList];
                if (docList.size() > 0) {
                    delete docList;
                }
            }
           
            
        }
    }
 
    if (oFOrderIdMap.size() > 0) {
 
        List<OpportunityFileOrder__c> oFOrderList = [select Id,Name,LatestUploadUser__c,LatestUploadTime__c
                                                    from OpportunityFileOrder__c 
                                                    where Id In : oFOrderIdMap.keySet()];
        if (oFOrderList.size() > 0 ) {
 
            for (OpportunityFileOrder__c oFOrder :oFOrderList) {
                System.debug(oFOrderIdMap.get(oFOrder.Id));
                if (oFOrderIdMap.get(oFOrder.Id)) {
                    oFOrder.LatestUploadUser__c = UserInfo.getUserId();
                    oFOrder.LatestUploadTime__c = System.now();
                } else if (oFOrderIdMap.get(oFOrder.Id) == false){
                    oFOrder.LatestUploadUser__c = null;
                    oFOrder.LatestUploadTime__c = null;
                }
            }
            System.debug('oFOrderList = ' + oFOrderList);
            update oFOrderList;
 
        }                                            
    }
    // DB202304087063 SFDC系统附件上传自动通知功能开发需求 start
    if (Trigger.isInsert && Trigger.isAfter) {
            NotetoPdfHandler handler = new NotetoPdfHandler();
            handler.run();
    }
    // DB202304087063 SFDC系统附件上传自动通知功能开发需求 end
 
 
// 20231103 陈京武 Lightning文件修改 End
    
}