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
public without sharing class RepairHandler extends Oly_TriggerHandler {
    private Map<Id, Repair__c> newMap;
    private Map<Id, Repair__c> oldMap;
    private List<Repair__c> newList;
    private List<Repair__c> oldList;
 
    // 已经更新过的RAMap 已经更新过的申请书不需要再次更新
    private static Map<Id, Rental_Apply__c> updatedRa = new Map<Id, Rental_Apply__c>();
    // 需要更新的RAMap
    private static Map<Id, Rental_Apply__c> needSendEmailRa = new Map<Id, Rental_Apply__c>();
 
    private final String GETILABEL = FixtureUtil.managetypeMap.get(FixtureUtil.Managetype.Ge_Ti_Guan_Li);
 
    public RepairHandler() {
        this.newMap = (Map<Id, Repair__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Repair__c>) Trigger.oldMap;
        this.newList = (List<Repair__c>) Trigger.new;
        this.oldList = (List<Repair__c>) Trigger.old;
    }
 
    protected override void beforeInsert() {
        beforeSetValue();
        beforeCheck();
    }
 
    protected override void beforeUpdate() {
        needSendEmailRa = new Map<Id, Rental_Apply__c>();
        beforeSetValue();
        beforeCheck();
    }
 
    protected override void afterUpdate() {
        reDetailRepair();
        //明細の修理削除
        cleanDetailRepair();
        // OLY_OCM-520 更新申请 使用目的2为一般用户、保修用户并且关联修理Repair__c的状态变为0.删除、0.取消,给备品中心的邮箱地址发邮件
        setNeedSendEmailRa();
        if (!needSendEmailRa.isEmpty()) {
            update needSendEmailRa.values();
        }
 
        //SWAG-BE532K  QIS逻辑修改 update by vivek 2019-7-22 start
        if(!StaticParameter.rentalApplyIsRunning){
            RepairAfterUpdate();
        }
        //SWAG-BE532K  QIS逻辑修改 update by vivek 2019-7-22 end
    }
 
    protected override void afterInsert() {
        reDetailRepair();
 
        //SWAG-BE532K  QIS逻辑修改 update by vivek 2019-7-22 start
        if(!StaticParameter.rentalApplyIsRunning){
            RepairAfterInsert();
        }
        //SWAG-BE532K  QIS逻辑修改 update by vivek 2019-7-22 end
    }
 
    protected override void afterDelete() {
        //明細の修理削除
        cleanDetailRepair();
 
        //SWAG-BE532K  QIS逻辑修改 update by vivek 2019-7-22 start
        if(!StaticParameter.rentalApplyIsRunning){
            RepairAfterDelete();
        }
        //SWAG-BE532K  QIS逻辑修改 update by vivek 2019-7-22 end
    }
 
    private void beforeSetValue() {
        List<String> depId = new List<String> ();
        List<String> omnId = new List<String> ();
        for (Repair__c nObj : newList) {
            nObj.Rental_Apply_Equipment_Set_Detail_Id__c = nObj.Rental_Apply_Equipment_Set_Detail__c;
            nObj.TransferApplyDetail_Id__c = nObj.TransferApplyDetail__c;
 
            // LHJ DOJ1-2 20200707 Start
            if(Trigger.isUpdate && nObj.ifDeadHurt__c != '有' && oldMap.get(nObj.Id).ifDeadHurt__c == '有'  && !StaticParameter.EscapeRepairUpdate ) {
                nObj.ifDeadHurtChange__c = True;
            }
            if(Trigger.isUpdate && String.isBlank(nObj.OSHRAConfirmUser__c) && String.isNotBlank(nObj.if_Repair_Allowed__c)) {
                nObj.OSHRAConfirmUser__c = UserInfo.getUserId();
                nObj.OSHRAConfirmDate__c = System.now();
            }
            // LHJ DOJ1-2 20200707 End
            // 备品检测标识 20200702 LD Start
            if ((Trigger.isInsert && nObj.SAP_Transfer_time__c != null
                    || Trigger.isUpdate && nObj.SAP_Transfer_time__c != null && oldMap.get(nObj.Id).SAP_Transfer_time__c == null)
                    && nObj.CheckFixtureModel_Pro__c == True
                ) {
                    // 保存战略科室分类id
                    depId.add(nObj.Department_Class__c);
                    // 保存产品型号
                    omnId.add(nObj.OwnershipMachine_No__c);
            }
            // 备品检测标识 20200702 LD End
            
            // 包含报修子单的修理 修理单状态编号变更发送AWS Start
            if (Trigger.isUpdate) {
                Repair__c oldrpr = oldMap.get(nObj.Id);
                // 2022/04/20 zhangyuheng  update start
                if ((nObj.OTSRepairOrder__c == oldrpr.OTSRepairOrder__c) && // OTS订单号发生改变,即时发送AWS
                    (nObj.DeliveryLogisticsNo__c == oldrpr.DeliveryLogisticsNo__c) && // 送修物流单号发生改变,即时发送AWS
                    (nObj.Returns_Product_waySAP__c == oldrpr.Returns_Product_waySAP__c) && // 送修方式发生改变且寄送日有值,即时发送AWS
                    (nObj.FSE_ApplyForRepair_time__c == oldrpr.FSE_ApplyForRepair_time__c) &&                   // FSE申请修理日发生改变,即时发送AWS
                    (nObj.Repair_Ordered_DateTime__c == oldrpr.Repair_Ordered_DateTime__c) &&                   // 4.修理品RC受理日(小程序)发生改变,即时发送AWS
                    // (nObj.Repair_Firstestimated_Date__c == oldrpr.Repair_Firstestimated_Date__c) &&             // 初次报价日(不用)发生改变,即时发送AWS     2022-05-20   zyh    注释
                    (nObj.Repair_Estimated_Date__c == oldrpr.Repair_Estimated_Date__c) &&             // 报价日(不用)--用于初次报价日发生改变,即时发送AWS   2022-05-20   zyh   add
                    (nObj.Repair_Shipped_DateTime__c == oldrpr.Repair_Shipped_DateTime__c) &&                   // 11.RC修理品返送日(小程序)发生改变,即时发送AWS
                    (nObj.Repair_Final_Inspection_DateTime__c == oldrpr.Repair_Final_Inspection_DateTime__c) && // 10.最终检测日(小程序)发生改变,即时发送AWS
                    (nObj.Agreed_DateTime__c == oldrpr.Agreed_DateTime__c) &&                                   //7.用户同意日(小程序)发生改变,即时发送AWS
                    (nObj.Agreed_Date__c == oldrpr.Agreed_Date__c) &&                                           // 7.用户同意日发生改变,即时发送AWS
                    (nObj.Repair_Quotation_Id__c == oldrpr.Repair_Quotation_Id__c) &&                           // 同期中的修理报价发生改变,即时发送AWS   2022-05-20  zyh   add
                    (nObj.engineerSendDate__c == oldrpr.engineerSendDate__c)
                    &&(nObj.SAP_not_accept_repair_result__c == oldrpr.SAP_not_accept_repair_result__c)
                    // 2022-12-22   zyh   add   start
                    && (nObj.DescriptionName__c == oldrpr.DescriptionName__c)
                    && (nObj.InspectionResultFlag__c == oldrpr.InspectionResultFlag__c) 
                    && (nObj.RepairReasonTrigger__c == oldrpr.RepairReasonTrigger__c)
                    // 2022-12-22   zyh   add   end
                    // 20230921 zyh 返品地址 start
                    && (nObj.Incharge_Staff__c == oldrpr.Incharge_Staff__c)         //修理委托者(FSE)
                    && (nObj.Returns_Product_way__c == oldrpr.Returns_Product_way__c)         //返送方式
                    && (nObj.Detailed_Address__c == oldrpr.Detailed_Address__c)         //详细地址
                    // 20230921 zyh 返品地址 end
                    ) {
                    // 2022/04/20 zhangyuheng  update end
                    if (String.isNotBlank(nObj.RepairSubOrder__c) && nObj.RepairOrderStatusCode__c != oldrpr.RepairOrderStatusCode__c) {
                        nObj.AwaitToSendAWS__c = true;
                    } 
                    // 2022/04/20 zhangyuheng  update start
                }
                // 2022/04/20 zhangyuheng  update end
                
            }
            // 包含报修子单的修理 修理单状态编号变更发送AWS End
            
        }
        StaticParameter.EscapeRepairUpdate = True;
        // 备品检测标识 20200702 LD Start
        Map<String,Integer> depMap = new Map<String,Integer>();
 
        // 20210622 SQL优化 Start
        //List<Asset> assList = [SELECT Department_Class__c,OwnershipMachine_No__c  FROM Asset WHERE Department_Class__c in :depId  AND OwnershipMachine_No__c in :omnId];
        List<Asset> assList = new List<Asset>();
        if (depId.size() > 0 || omnId.size() > 0) {
            assList = [SELECT Department_Class__c,OwnershipMachine_No__c  FROM Asset WHERE Department_Class__c in :depId  AND OwnershipMachine_No__c in :omnId];
        }
        // 20210622 SQL优化 End
        
        for (Asset ass : assList) {
            if (depMap.get(ass.Department_Class__c+'-'+ass.OwnershipMachine_No__c) == null){
                depMap.put(ass.Department_Class__c+'-'+ass.OwnershipMachine_No__c,1);
            }else{
                Integer val = depMap.get(ass.Department_Class__c+'-'+ass.OwnershipMachine_No__c);
                depMap.put(ass.Department_Class__c+'-'+ass.OwnershipMachine_No__c,val+1);
            }
        }
 
        for (Repair__c nObj : newList) {
            if ((Trigger.isInsert && nObj.SAP_Transfer_time__c != null
                    || Trigger.isUpdate && nObj.SAP_Transfer_time__c != null && oldMap.get(nObj.Id).SAP_Transfer_time__c == null)
                ) {
 
                if (depMap.get(nObj.Department_Class__c+'-'+nObj.OwnershipMachine_No__c) == 1
                        && nObj.CheckFixtureModel_Pro__c == True
                        && (!(nObj.NewProductGuaranteeObject__c == '1: 新品保修'
                            || nObj.NewProductGuaranteeObject__c == '2: 服务多年保修'
                            || nObj.NewProductGuaranteeObject__c == '8: 市场多年保修'
                            || nObj.Contract_status__c == '契約'))) {
 
                    nObj.IfCheckFixture__c = true;
                } else {
                    nObj.IfCheckFixture__c = false;
                }
 
            }
        }
        // 备品检测标识 20200702 LD End
    }
 
    private void reDetailRepair() {
        // {Asset.Id: [Repair.Id]}
        Map<Id, List<Id>> assetsMap = new Map<Id, List<Id>>();
        List<RepairSubOrder__c> updateRepairSubOrderList = new List<RepairSubOrder__c>();
        //先获取子单nobj 的id 放在list 查询
        List<Id> repairSub = new List<Id>();
        Map<Id, String> repStatus = new Map<Id,String>();
        for (Repair__c nObj :newList ) {
            repairSub.add(nObj.RepairSubOrder__c);
         }
        for (RepairSubOrder__c rep : [ SELECT Id,Status__c FROM RepairSubOrder__c
                                       WHERE Id IN:repairSub]) {
            repStatus.put(rep.Id,rep.Status__c);
        }
 
        for (Repair__c nObj : newList) {
            Repair__c oObj;
            if (Trigger.isUpdate) {
                oObj = oldMap.get(nObj.Id);
            }
            if (Trigger.isInsert || (oObj.Delivered_Product__c != nObj.Delivered_Product__c
                        && oObj.Delivered_Product__c != null)) {
                if (!assetsMap.containsKey(nObj.Delivered_Product__c)) {
                    assetsMap.put(nObj.Delivered_Product__c, new List<Id>());
                }
                assetsMap.get(nObj.Delivered_Product__c).add(nObj.Id);
            }
 
            //判断状态 nobj.addError
            if (Trigger.isInsert) {
                if (String.isNotBlank(nObj.RepairSubOrder__c)) {
                    if(repStatus.get(nObj.RepairSubOrder__c)!='待处理' ){
                        nObj.addError('状态不是待处理,无法申请修理');
                        return;
                    }
                    RepairSubOrder__c repairSubOrder = new RepairSubOrder__c();
                    repairSubOrder.Id = nObj.RepairSubOrder__c;
                    repairSubOrder.Status__c = '已转修理';
                    updateRepairSubOrderList.add(repairSubOrder);
                }
            }
        }
        if (updateRepairSubOrderList.size() > 0) {
            update updateRepairSubOrderList;
        }
        if (assetsMap.isEmpty()) {
            return;
        }
 
        // 检索出个体管理并且已借出并修理的Asset
        List<Asset> haveLastAsset = [SELECT Id, Name
                                       FROM Asset
                                      WHERE Id = :assetsMap.keySet()
                                        AND ((Last_Reserve_RAES_Detail__c != null AND Last_Reserve_RAES_Detail__r.Repair__c != null)
                                            OR (Last_Reserve_TAES_Detail__c != null AND Last_Reserve_TAES_Detail__r.Repair__c != null))
                                        AND Manage_type__c = :GETILABEL
                                        order by Id];
 
        if (haveLastAsset.size() > 0) {
            for (Asset ass : haveLastAsset) {
                newMap.get(assetsMap.get(ass.Id)[0]).addError(String.format('{0}保有设备 {1} 已经在修理中,无法再次创建修理', new String[] {GETILABEL, ass.Name}));
            }
        }
 
        List<Rental_Apply_Equipment_Set_Detail__c> raesds = [Select Id, Asset__c
                From Rental_Apply_Equipment_Set_Detail__c
                Where Asset__c = :assetsMap.keySet()
                AND Arrival_in_wh__c = false
                AND Repair__c = null
                AND (Inspection_result_after_NG_F__c = '维修'
                        OR Inspection_result_NG__c = '维修')
                order by Asset__c, After_Inspection_time_F__c nulls last, Pre_inspection_time__c];
 
        List<Rental_Apply_Equipment_Set_Detail__c> raesdss = new List<Rental_Apply_Equipment_Set_Detail__c>();
        List<Repair__c> rs = new List<Repair__c>();
 
        for (Rental_Apply_Equipment_Set_Detail__c  raesd : raesds) {
            List<Id> rIdSet = assetsMap.get(raesd.Asset__c);
            if (rIdSet.isEmpty()) {
                continue;
            }
            rs.add(new Repair__c(Id = rIdSet[0], Rental_Apply_Equipment_Set_Detail__c = raesd.Id));
            raesd.Repair__c = rIdSet[0];
            rIdSet.remove(0);
            raesdss.add(raesd);
        }
        if(!raesdss.isEmpty()) {
            update raesdss;
        }
        Set<Id> transferAssetIdSet = new Set<Id>();
        for(Id assId: assetsMap.keySet()) {
            if(assetsMap.get(assId).size() > 0) {
                transferAssetIdSet.add(assId);
            }
        }
        if(!transferAssetIdSet.isEmpty()) {
            List<TransferApplyDetail__c> tadss = new List<TransferApplyDetail__c>();
            List<TransferApplyDetail__c> tads = [Select Id, Asset__c
                    From TransferApplyDetail__c
                    Where Asset__c = :transferAssetIdSet
                    AND Arrival_in_wh__c = false
                    AND Repair__c = null
                    AND (Inspection_result_after_NG_F__c = '维修'
                            OR Inspection_result_NG__c = '维修')
                    order by Asset__c, After_Inspection_time_F__c nulls last, Pre_inspection_time__c];
            for (TransferApplyDetail__c  tad : tads) {
                List<Id> rIdSet = assetsMap.get(tad.Asset__c);
                if (rIdSet.isEmpty()) {
                    continue;
                }
                rs.add(new Repair__c(Id = rIdSet[0], TransferApplyDetail__c = tad.Id));
                tad.Repair__c = rIdSet[0];
                rIdSet.remove(0);
                tadss.add(tad);
            }
            if(!tadss.isEmpty()) {
                update tadss;
            }
        }
 
        if (!rs.isEmpty()) {
            update rs;
        }
    }
 
    //新規の場合何もしません
    private void cleanDetailRepair() {
        List<Repair__c> repList;
        if (trigger.isDelete) {
            repList = oldList;
        } else {
            repList = newList;
        }
 
        List<Rental_Apply_Equipment_Set_Detail__c> raesdList = new List<Rental_Apply_Equipment_Set_Detail__c>();
 
        for (Repair__c rep : repList) {
            if (Trigger.isDelete && String.isNotBlank(rep.Rental_Apply_Equipment_Set_Detail__c)) {
                raesdList.add(new Rental_Apply_Equipment_Set_Detail__c(Id = rep.Rental_Apply_Equipment_Set_Detail__c,
                            Repair__c = null));
            } else {
                /* OLY_OCM-424 Start 修理状态为取消/删除时,对修理单来讲,属于完成状态。和修理完成做相同处理 */
                // Repair__c oldObj = oldMap.get(rep.Id);
                // if ((rep.Status__c == '0.取消' || rep.Status__c == '0.删除')
                //     && (oldObj.Status__c != '0.取消' && rep.Status__c != '0.删除')
                //     && String.isNotBlank(rep.Rental_Apply_Equipment_Set_Detail__c)) {
                //     raesdList.add(new Rental_Apply_Equipment_Set_Detail__c(Id = rep.Rental_Apply_Equipment_Set_Detail__c,
                //                 Repair__c = null));
                // }
                /* OLY_OCM-424 End */
            }
        }
 
        if (!raesdList.isEmpty()) {
            update raesdList;
        }
    }
 
    // OLY_OCM-520 更新申请 使用目的2为一般用户、保修用户并且关联修理Repair__c的状态变为0.删除、0.取消,给备品中心的邮箱地址发邮件
    private void setNeedSendEmailRa() {
        Set<Id> reIdSet = new Set<Id>();
        for (Repair__c nObj : newList) {
            Repair__c oldObj = oldMap.get(nObj.Id);
            if ((nObj.Status1__c == '0.取消' || nObj.Status1__c == '0.删除')
                && (oldObj.Status1__c != '0.取消' && oldObj.Status1__c != '0.删除')) {
                reIdSet.add(nObj.Id);
            }
        }
        if (reIdSet.size() > 0) {
            List<Rental_Apply__c> ras = [SELECT Id,
                                                SendRepairCancelDeleteEmail__c,
                                                RA_Status__c
                                           FROM Rental_Apply__c
                                          WHERE (demo_purpose2__c = '一般用户'
                                                OR demo_purpose2__c = '保修用户'
                                                OR demo_purpose2__c = '市场多年保修'
                                                OR demo_purpose2__c = '再修理'
                                                OR demo_purpose2__c = '故障排查'
                                                ) // 使用目的2是一般用户或者保修用户
                                            AND RA_Status__c != :FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Qu_Xiao.ordinal()) // 已经取消的申请书不发送邮件
                                            AND SendRepairCancelDeleteEmail__c = false // 已经发送过的申请书不发送邮件
                                            AND Id != :updatedRa.keySet() // 已经更新过的申请书不需要再次更新
                                            AND Repair__c = :reIdSet];
            if (ras.size() > 0) {
                for (Rental_Apply__c ra : ras) {
                    ra.SendRepairCancelDeleteEmail__c = true;
                    needSendEmailRa.put(ra.Id, ra);
                    updatedRa.put(ra.Id, ra);
                }
            }
        }
    }
 
    //SWAG-BE532K  update QIS修理逻辑 by vivek 2019-7-22 start
    public void RepairAfterDelete(){
        List<Repair__c> repList = oldList;
 
        List<Repair__c> repairList = new List<Repair__c>();
 
        Map<Id,Repair__c> qisRepMap = new Map<Id,Repair__c>();
 
        List<QIS_Report__c> qisList = new List<QIS_Report__c>();
 
        Set<Id> qisIdSet  = new Set<Id>();
 
        Map<Id,List<Repair__c>> qisRepListMap = new Map<Id,List<Repair__c>>();
 
        for(Repair__c rep : repList){
            if(rep.QIS_ID__c != null){
                if(rep.Status1__c != '0.删除' && rep.Status1__c != '0.取消' && rep.Return_Without_Repair__c == false ){
                    repairList.add(rep);
                    qisIdSet.add(rep.QIS_ID__c);
                }
            }
        }
 
        if( repairList != null && !repairList.isEmpty()){
            List<Repair__c> allRepairList = [select id,QIS_ID__c,CreatedDate from Repair__c
                    where Status1__c != '0.删除'
                            and Status1__c != '0.取消'
                            and Return_Without_Repair__c = false
                            and QIS_ID__c =: qisIdSet
                            order by CreatedDate desc];
            List<Rental_Apply__c> rentalList = [select Id,QIS_number__c,QISRepair__c from Rental_Apply__c where QIS_number__c =: QISidSet and Status__c != '取消' and demo_purpose2__c = '索赔QIS'];
            // 为减少sql,通过rep.QIS_ID__c来对符合条件的修理进行分组,获得一个键为QIS的id值为修理的list的map
            for(Id str : qisIdSet){
                List<Repair__c> repairForMap = new List<Repair__c>();
                for(Repair__c rep : allRepairList){
                    if(rep.QIS_ID__c == str){
                        repairForMap.add(rep);
                    }
                }
                if(!repairForMap.isEmpty()){
                    qisRepListMap.put(str, repairForMap);
                }
                // qisRepListMap.put(str, repairForMap);
            }
 
            for(Repair__c rep : repairList){
                if(qisRepListMap.get(rep.QIS_ID__c) != null){
                    qisRepMap.put(rep.QIS_ID__c, qisRepListMap.get(rep.QIS_ID__c)[0]);
                }else{
                    qisRepMap.put(rep.QIS_ID__c, null);
                }
            }
 
            for(Id idd : qisRepMap.keySet()){
                QIS_Report__c qis = new QIS_Report__c();
                qis.Id = idd;
                if(qisRepMap.get(idd) != null){
                    qis.Repair__c = qisRepMap.get(idd).Id;
                }else{
                    qis.Repair__c = null;
                }
                // qis.Repair__c = qisRepMap.get(idd).Id;
                qisList.add(qis);
            }
 
            for(Rental_Apply__c rent : rentalList){
                // rent.QISRepair__c = qisRepMap.get(rent.QIS_number__c).Id;
                if(qisRepMap.get(rent.QIS_number__c) != null){
                    rent.QISRepair__c = qisRepMap.get(rent.QIS_number__c).Id;
                }else{
                    rent.QISRepair__c = null;
                }
            }
            update qisList;
            if(rentalList.size() > 0 ){
                update rentalList;
            }
        }
 
    }
 
    public void RepairAfterInsert(){
 
        List<Repair__c> repairList = newList;
        List<Repair__c> repairUseList = new List<Repair__c>();
        Map<String,Repair__c> qismap = new Map<String,Repair__c>();
        List<QIS_Report__c> qisList = new List<QIS_Report__c>();
 
        for(Repair__c rep : repairList){
            if(rep.Status1__c != '0.删除' && rep.Status1__c != '0.取消' && rep.Return_Without_Repair__c == false && rep.QIS_ID__c != null ){
                // qismap.put(rep.QIS_ID__c, rep);
                if(qismap.get(rep.QIS_ID__c) != null){
                    if(qismap.get(rep.QIS_ID__c).CreatedDate < rep.CreatedDate){
                        qismap.put(rep.QIS_ID__c, rep);
                    }
                }else{
                    qismap.put(rep.QIS_ID__c, rep);
                }
            }
        }
        if(!qismap.isEmpty() ){
            for(String str : qismap.keySet()){
                repairUseList.add(qismap.get(str));
            }
        }
        if(repairUseList != null && !repairUseList.isEmpty()){
            for(Repair__c repair : repairUseList){
                QIS_Report__c qis = new QIS_Report__c();
                qis.Id = repair.QIS_ID__c;
                qis.Repair__c = repair.Id;
                qisList.add(qis);
            }
            update qisList;
            Set<String> QISidSet = qismap.keySet();
            List<Rental_Apply__c> rentalList = [select Id,QIS_number__c,QISRepair__c from Rental_Apply__c where QIS_number__c =: QISidSet  and Status__c != '取消' and demo_purpose2__c = '索赔QIS'];
            if(rentalList != null && !rentalList.isEmpty() ){
                for(Rental_Apply__c rent : rentalList){
                    rent.QISRepair__c = qismap.get(rent.QIS_number__c).Id;
                }
                update rentalList;
            }
        }
    }
 
    private void beforeCheck(){
        for (Repair__c nObj : newList) {
             
            // 通过报修子单来源 数据验证
            if (String.isNotBlank(nObj.RepairSubOrder__c)) {
                if (Trigger.isInsert) {//新增验证
 
                    if (String.isBlank(nObj.DeliveryLogisticsMode__c)) {
                        nObj.addError('送修物流方式 不能为空,请确认');
                    } else if ('快递'.equals(nObj.DeliveryLogisticsMode__c)) {
                        if (nobj.engineerSendDate__c == null) {
                            //20231027  ymh添加注释  修改提示内容
                            // nObj.addError('送修物流方式 是快递,工程师修理品寄送日 不能为空,请确认'); 
                            //20231227 lwt DB202312554975 取消该字段限制 start
                           // nObj.addError('送修物流方式 是快递,修理品寄送日 不能为空,请确认'); 
                            //20231227 lwt DB202312554975 取消该字段限制 end
                        }
                        // 20220104 取消 送修物流单号 Start
                        // if (String.isBlank(nObj.DeliveryLogisticsNo__c)) {
                        //    nObj.addError('送修物流方式 是快递,送修物流单号 不能为空,请确认');  
                        // }
                        // 20220104 取消 送修物流单号 END
                    }
 
                    if (('RC修理'.equals(nobj.On_site_repair__c) ||
                         '直送SORC修理'.equals(nobj.On_site_repair__c) || 
                         '直送OGZ修理'.equals(nobj.On_site_repair__c) ) && nobj.Send_To_RC_Date__c == null ) {
                        nObj.addError('RC修理,直送SORC修理和直送OGZ修理的话,必须要输入3.修理品RC寄送日。');
                    }
                } else {//变更
                    Repair__c oldObj = oldMap.get(nObj.Id);
                    if (nObj.SAP_Transfer_time__c != null) {
                        if (oldObj.DeliveryLogisticsMode__c != nObj.DeliveryLogisticsMode__c) {
                            nObj.addError('已发送给新服务系统 送修物流方式 不能修改,请了解'); 
                        }
 
                        if (oldObj.engineerSendDate__c != nObj.engineerSendDate__c) {
                            //20231027  ymh添加注释  修改提示内容
                            // nObj.addError('已发送给新服务系统 工程师修理品寄送日 不能修改,请了解'); 
                            nObj.addError('已发送给新服务系统 修理品寄送日 不能修改,请了解'); 
                        }
 
                        // if (oldObj.DeliveryLogisticsNo__c != nObj.DeliveryLogisticsNo__c) {   2023-06-29   zyh   小程序一期Plus物流判定需要
                        if (String.isNotBlank(oldObj.DeliveryLogisticsNo__c) && oldObj.DeliveryLogisticsNo__c != nObj.DeliveryLogisticsNo__c) {
                            nObj.addError('已发送给新服务系统 送修物流单号 不能修改,请了解'); 
                        }
                    }
                }
            }
 
        }
    }
 
    public void RepairAfterUpdate(){
        // 插入的repair list
        List<Repair__c> repairList = newList;
        List<QIS_Report__c> qisList = new List<QIS_Report__c>();
        // 有效到有效的list
        List<Repair__c> repairUToUList = new List<Repair__c>();
        // 无效到有效的list
        List<Repair__c> repairNToUList = new List<Repair__c>();
        // 有效到无效的list
        List<Repair__c> repairUToNList = new List<Repair__c>();
        // 无效到无效的list
        List<Repair__c> repairNtoNList = new List<Repair__c>();
        // List<Repair__c> repairUseList = new List<Repair__c>();
        // // 不符合条件的repair list
        // List<Repair__c> repairNoUseList = new List<Repair__c>();
        // 一直找到最新的修理
        Map<Id,Repair__c> qisRepMap = new Map<Id,Repair__c>();
        Integer i = 0;
 
        Set<Id> qisIdSet  = new Set<Id>();
        // 记录SQL记录
        Map<Id,List<Repair__c>> qisRepListMap = new Map<Id,List<Repair__c>>();
 
        for(Repair__c rep : repairList){
            if(rep.QIS_ID__c != null){
                // 判断有效到有效
                if(rep.Status1__c != '0.删除' && rep.Status1__c != '0.取消' && rep.Return_Without_Repair__c == false && oldMap.get(rep.Id).Status1__c != '0.删除' && oldMap.get(rep.Id).Status1__c != '0.取消' && oldMap.get(rep.Id).Return_Without_Repair__c == false){
                    if(rep.QISLatestIsEmpty__c){
                        i++;
                        qisIdSet.add(rep.QIS_ID__c);
                    }
                    repairUToUList.add(rep);
                }
                // 判断无效到有效
                if(rep.Status1__c != '0.删除' && rep.Status1__c != '0.取消' && rep.Return_Without_Repair__c == false && (oldMap.get(rep.Id).Status1__c == '0.删除' || oldMap.get(rep.Id).Status1__c == '0.取消' || oldMap.get(rep.Id).Return_Without_Repair__c == true) ){
                    repairNToUList.add(rep);
                    qisIdSet.add(rep.QIS_ID__c);
                }
                // 判断有效到无效
                if( (rep.Status1__c == '0.删除' || rep.Status1__c == '0.取消' || rep.Return_Without_Repair__c == true ) && (oldMap.get(rep.Id).Status1__c != '0.删除' && oldMap.get(rep.Id).Status1__c != '0.取消' && oldMap.get(rep.Id).Return_Without_Repair__c == false) ){
                    repairUToNList.add(rep);
                    qisIdSet.add(rep.QIS_ID__c);
                }
                // 判断无效到无效
                // if( (rep.Status1__c == '0.删除' || rep.Status1__c == '0.取消' || rep.Return_Without_Repair__c == true ) && ( oldMap.get(rep.Id).Status1__c == '0.删除' || oldMap.get(rep.Id).Status1__c == '0.取消' || oldMap.get(rep.Id).Return_Without_Repair__c == true) ){
                //     repairNToNList.add(rep);
                // }
            }
        }
        // SQL执行条件(空更新的时候,从有效到无效,从无效到有效)
        if( i>0 || repairNToUList.size() > 0 || repairUToNList.size() > 0){
            List<Repair__c> allRepairList = [select id,QIS_ID__c,CreatedDate from Repair__c
                    where Status1__c != '0.删除'
                            and Status1__c != '0.取消'
                            and Return_Without_Repair__c = false
                            and QIS_ID__c =: qisIdSet
                            order by CreatedDate desc];
            List<Rental_Apply__c> rentalList = [select Id,QIS_number__c,QISRepair__c from Rental_Apply__c where QIS_number__c =: QISidSet  and Status__c != '取消' and demo_purpose2__c = '索赔QIS'];
            // 为减少sql,通过rep.QIS_ID__c来对符合条件的修理进行分组,获得一个键为QIS的id值为修理的list的map
            for(Id str : qisIdSet){
                List<Repair__c> repairForMap = new List<Repair__c>();
                for(Repair__c rep : allRepairList){
                    if(rep.QIS_ID__c == str){
                        repairForMap.add(rep);
                    }
                }
                if(!repairForMap.isEmpty()){
                    qisRepListMap.put(str, repairForMap);
                }
            }
 
            // 空更新(只有有效到有效,无效到无效不存在更新)
            if( i > 0){
                for(Repair__c rep : repairUToUList){
                    if(rep.QISLatestIsEmpty__c){
                        qisRepMap.put(rep.QIS_ID__c, qisRepListMap.get(rep.QIS_ID__c)[0]);
                    }
                }
            }
            // 无效到有效或者有效到无效
            if(repairNToUList.size() > 0 || repairUToNList.size() > 0){
                for(Repair__c rep : repairNToUList){
                    qisRepMap.put(rep.QIS_ID__c, qisRepListMap.get(rep.QIS_ID__c)[0]);
                }
                // 有效变为无效的可能存在置空
                for(Repair__c rep : repairUToNList){
                    if(qisRepListMap.get(rep.QIS_ID__c) != null){
                    // if(qisRepListMap != null){
                        qisRepMap.put(rep.QIS_ID__c, qisRepListMap.get(rep.QIS_ID__c)[0]);
                    }else{
                        qisRepMap.put(rep.QIS_ID__c, null);
                    }
                }
            }
            // List<QIS_Report__c> qisList = new List<QIS_Report__c>();
            for(Id idd : qisRepMap.keySet()){
                QIS_Report__c qis = new QIS_Report__c();
                qis.Id = idd;
                if(qisRepMap.get(idd) != null){
                    qis.Repair__c = qisRepMap.get(idd).Id;
                }else{
                    qis.Repair__c = null;
                }
                qisList.add(qis);
            }
 
            for(Rental_Apply__c rent : rentalList){
                if(qisRepMap.get(rent.QIS_number__c) != null){
                    rent.QISRepair__c = qisRepMap.get(rent.QIS_number__c).Id;
                }else{
                    rent.QISRepair__c = null;
                }
            }
 
            update qisList;
            if(rentalList.size() > 0 ){
                update rentalList;
            }
        }
    }
    //SWAG-BE532K  update QIS修理逻辑 by vivek 2019-7-22 end
 
    @TestVisible private static void test() {
        if (false == Test.isRunningTest()) return;
        Integer i = 0;
       
   }
}