高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
public without sharing class InspectionReportTriggerHandler extends Oly_TriggerHandler {
    private Map<Id, Inspection_Report__c> newMap;
    private Map<Id, Inspection_Report__c> oldMap;
    private List<Inspection_Report__c> newList;
    private List<Inspection_Report__c> oldList;
    public InspectionReportTriggerHandler() {
        this.newMap = (Map<Id, Inspection_Report__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Inspection_Report__c>) Trigger.oldMap;
        this.newList = (List<Inspection_Report__c>) Trigger.new;
        this.oldList = (List<Inspection_Report__c>) Trigger.old;
    }
 
    protected override void afterInsert() {
        updateAsset();
    }
 
    protected override void afterUpdate() {
        updateAsset();
        //fxk 2021/8/31 点检计划中相应字段的赋值逻辑 Star
        updateCheckPlan();
        // updateCheckPlanSubmit();
        // updateCheckPlanreject();
        //fxk 2021/8/31 点检计划中相应字段的赋值逻辑 end
        //add by rentx 20210823 start 在批准之后更新 使用ET 等信息到维修合同上
        updateVM();
        //add by rentx 20210823 end 在批准之后更新 使用ET 等信息到维修合同上
    }
 
    protected override void beforeDelete() {
        updateAsset_delete();
    }
 
    // 2021-04-16   mzy LJPH-BZPB7W SFDC点检报告更新逻辑修改 start
    //将更新保有设备的代码  移至Batch中  每天凌晨运行
    //Batch : UpdateInspectionReportAssetBatch
    private void updateAsset() {
        // 触发trigger的点检报告书
        List<String> irIds = new List<String>();
        for (Inspection_Report__c new_ir : newList) {
            irIds.add(new_ir.Id);
        }
        if (Trigger.isUpdate) {
            List<Inspection_Report__c> updateList = new List<Inspection_Report__c>();
            //2021-08-23 mzy  LJPH-C5Y5YD 【委托】合同前点检结果的实时带入 start
            //获取批准的报告书Id
            Set<String> irIdSet = new Set<String>();
            //2021-08-23   mzy  LJPH-C5Y5YD 【委托】合同前点检结果的实时带入 end
            for (Inspection_Report__c new_ir : newList) {
 
                //System.debug('新数据 :'+new_ir);
                //获取老数据
                Inspection_Report__c oldReport = oldMap.get(new_ir.Id) == null ? new Inspection_Report__c() : oldMap.get(new_ir.Id);
                //System.debug('老数据 :'+oldReport);
                //1.判断状态是否发生改变--为申请中
                if (oldReport != null
                        && !String.isEmpty(String.valueOf(oldReport.Status__c))
                        && new_ir.Status__c != null
                        && '申请中'.equals(new_ir.Status__c)
                        && oldReport.Status__c != new_ir.Status__c
                   ) {
                    irIdSet.add(new_ir.Id);
                }
 
 
                //2.判断状态是否发生改变--为批准
                if (oldReport != null
                        && !String.isEmpty(String.valueOf(oldReport.Status__c))
                        && new_ir.Status__c != null
                        && '批准'.equals(new_ir.Status__c)
                        && oldReport.Status__c != new_ir.Status__c
                   ) {
 
                    // 2021-08-23  mzy LJPH-C5Y5YD 【委托】合同前点检结果的实时带入 start
                    if (!new_ir.if_UpdateAsset__c) {
                        //需要更新
                        Inspection_Report__c tempReport = new Inspection_Report__c();
                        tempReport.Id = new_ir.id;
                        tempreport.if_UpdateAsset__c = true;
                        updateList.add(tempreport);
                    }
 
                    // 将批准的报告书 Id 保存到set集合中
                    irIdSet.add(new_ir.id);
                    // 2021-08-23 mzy LJPH-C5Y5YD 【委托】合同前点检结果的实时带入 end
                }
            }
 
            if (updateList.size() > 0) {
                System.debug('List集合 :' + updateList);
                update updateList;
            }
 
            //2021-09-01  mzy LJPH-C5Y5YD 【委托】合同前点检结果的实时带入 改为调用Batch来实现更新到保有设备 start
            if (irIdSet.size() > 0) {
                //如果有批准的点检报告书则将点检报告书的Id传入Batch进行更新保有设备
                List<String> ids = new List<String>();
                for (String i : irIdSet) {
                    ids.add(i);
                }
                UpdateInspectionReportAssetBatch.updateAssetFun(ids);
                
            }
            //2021-09-01  mzy LJPH-C5Y5YD 【委托】合同前点检结果的实时带入 改为调用Batch来实现更新到保有设备 end
        }
 
 
        /*
        System.debug('希望下面的代码不要运行.....');
 
        // 点检报告书中所有的保有设备
        List<String> assIds = new List<String>();
        Map<String, String> assMap = new Map<String, String>();
        List<Inspection_Item__c> iis = [select id, AssetId__c from Inspection_Item__c where Inspection_ReportId__c = :irIds];
        for (Inspection_Item__c ii : iis) {
            if (ii.AssetId__c != null && assMap.containsKey(ii.AssetId__c) == false) {
                assIds.add(ii.AssetId__c);
                assMap.put(ii.AssetId__c, ii.AssetId__c);
            }
        }
        // 保有设备对应的所有点检报告明细
        List<Inspection_Item__c> iiList = [select id, AssetId__c, Inspection_ReportId__c, Inspection_Result__c, Inspection_Comment__c, Inspection_ReportId__r.Name, Inspection_ReportId__r.Approved_date__c
                                             from Inspection_Item__c
                                            where AssetId__c = :assIds
                                            order by Inspection_ReportId__r.Approved_date__c desc];
 
        Map<String, Inspection_Item__c> iiMap = new Map<String, Inspection_Item__c>();
        for (Inspection_Item__c ii : iiList) {
            if (iiMap.containsKey(ii.AssetId__c) == false && ii.Inspection_ReportId__r.Approved_date__c != null) {
                iiMap.put(ii.AssetId__c, ii);
            }
        }
 
        List<Asset> updList = new List<Asset>();
        for (String assid : assIds) {
            Inspection_Item__c tmp = iiMap.get(assid) == null ? new Inspection_Item__c() : iiMap.get(assid);
            Asset ass = new Asset(
                Id = assid,
                Inspection_report_number__c = tmp.Inspection_ReportId__r.Name,
                Inspection_Report__c = tmp.Inspection_ReportId__c,
                Inspection_Comment__c = tmp.Inspection_Comment__c,
                Inspection_Result__c = tmp.Inspection_Result__c
            );
            updList.add(ass);
        }
 
        if (updList.size() > 0) update updList;
 
        */
 
    }
    // 2021-04-16   mzy LJPH-BZPB7W SFDC点检报告更新逻辑修改 end
 
    private void updateAsset_delete() {
        // 触发trigger的点检报告书
        List<String> irIds = new List<String>();
        for (Inspection_Report__c old_ir : oldList) {
            irIds.add(old_ir.Id);
        }
        // 点检报告书中所有的保有设备
        List<String> assIds = new List<String>();
        Map<String, String> assMap = new Map<String, String>();
        List<Inspection_Item__c> iis = [select id, AssetId__c from Inspection_Item__c where Inspection_ReportId__c = :irIds];
        for (Inspection_Item__c ii : iis) {
            if (assMap.containsKey(ii.AssetId__c) == false) {
                assIds.add(ii.AssetId__c);
                assMap.put(ii.AssetId__c, ii.AssetId__c);
            }
        }
        // 保有设备对应的所有点检报告明细
        List<Inspection_Item__c> iiList = [select id, AssetId__c, Inspection_ReportId__c, Inspection_Result__c, Inspection_Comment__c, Inspection_ReportId__r.Name, Inspection_ReportId__r.Approved_date__c
                                           from Inspection_Item__c
                                           where AssetId__c = :assIds
                                                   and Inspection_ReportId__c <> :irIds
                                                   order by Inspection_ReportId__r.Approved_date__c desc];
 
        Map<String, Inspection_Item__c> iiMap = new Map<String, Inspection_Item__c>();
        for (Inspection_Item__c ii : iiList) {
            if (iiMap.containsKey(ii.AssetId__c) == false && ii.Inspection_ReportId__r.Approved_date__c != null) {
                iiMap.put(ii.AssetId__c, ii);
            }
        }
 
        List<Asset> updList = new List<Asset>();
        for (String assid : assIds) {
            Inspection_Item__c tmp = iiMap.get(assid) == null ? new Inspection_Item__c() : iiMap.get(assid);
            Asset ass = new Asset(
                Id = assid,
                Inspection_report_number__c = tmp.Inspection_ReportId__r.Name,
                Inspection_Report__c = tmp.Inspection_ReportId__c,
                Inspection_Comment__c = tmp.Inspection_Comment__c,
                Inspection_Result__c = tmp.Inspection_Result__c
            );
            updList.add(ass);
        }
 
        if (updList.size() > 0) update updList;
    }
    //fxk 2021/8/31 点检计划中相应字段的赋值逻辑 star
    private void updateCheckPlan() {
        System.debug('Start');
        List<Inspectup_Plan__c> updateIPInfoList = new List<Inspectup_Plan__c>();
        Set<String> IPIdSet = new Set<String>();
        for (Inspection_Report__c new_IRInfo : newList) {
            if ((new_IRInfo.Status__c != oldMap.get(new_IRInfo.Id).Status__c) && new_IRInfo.Status__c == '批准') {
                if (new_IRInfo.Inspectup_Plan__c != null) {
                    IPIdSet.add(new_IRInfo.Inspectup_Plan__c);
                }
            }
        }
        Map<Id, Map<String, Decimal>> iplanMap = new Map<Id, Map<String, Decimal>>();
        List<String > sumNumsList = new List<String > {'aEQs', 'aEQInPlans', 'failnum', 'abandonmentnum', 'assetNum', 'reports', 'iDate', 'aEQsKeyRepeat'};
        Map<String, Set<Id>> PlanToAssetMap = new Map<String, Set<Id>>();
        List<Inspection_Item__c> ItemInfoList = new List<Inspection_Item__c>();
        if (IPIdSet.size() > 0) {
            ItemInfoList = [select Id, name, AssetId__c, Failourassetnumber__c, Inspection_ReportId__c, Inspection_ReportId__r.Inspectup_Plan__c,
                            Inspection_ReportId__r.Inspection_Date__c, Inspection_ReportId__r.Failourassetnumber__c,Inspection_ReportId__r.Inspectup_Plan__r.overstep_check_interval__c,
                            Inspection_ReportId__r.Inspectup_Plan__r.Planned_Start_Date__c, Inspection_ReportId__r.Inspectup_Plan__r.Planned_End_Date__c,
                            Inspection_ReportId__r.Inspectup_Plan__r.Check_Object_Quantity__c, Inspection_ReportId__r.Start_Date__c,
                            Inspection_ReportId__r.Inspection_asset_number__c, ItemStatus__c,Inspection_ReportId__r.Inspectup_Plan__r.Execution_End_Date__c, 
                            Inspection_ReportId__r.Abandonment_Num__c, Inspection_ReportId__r.Status__c from Inspection_Item__c
                            where Inspection_ReportId__r.Inspectup_Plan__c in: IPIdSet AND Inspection_ReportId__r.Status__c = '批准'];
        }
        System.debug('-------3------' + ItemInfoList);
        Map<String, Date> IDateMap = new Map<String, Date>();
        Map<String, Decimal> SumNumMap = new Map<String, Decimal>();
        // add 如果 超出点检区间=true 则个点检计划数据不计算 fxk 2021/10/20 Star
        Map<String, Boolean> overstepMap = new Map<String, Boolean>();
        // add 如果 超出点检区间=true 则个点检计划数据不计算 fxk 2021/10/20 End
        if (ItemInfoList.size() > 0) {
            for (Inspection_Item__c items : ItemInfoList) {
                Decimal aEQs = 0;//实际执行数(季度):没去重   计划内外
                Decimal aEQInPlans = 0;//计划内实际执行数
                Decimal failnum = 0;//点检计划,设备异常数
                Decimal abandonmentnum = 0;//点检计划 放弃设备树
                // Decimal Reportnum = 0;//本次点检次数(审批过后的点检报告书)
                String planId = items.Inspection_ReportId__r.Inspectup_Plan__c;
 
 
                if (iplanMap.containsKey(planId)) {
                    SumNumMap = iplanMap.get(planId);
                }
                // iplanMap.put(ANY key, ANY value)
                // SumNumMap.put(planId , SumNumMap.get(planId))
                if (items.Inspection_ReportId__r.Inspection_Date__c >= items.Inspection_ReportId__r.Inspectup_Plan__r.Planned_Start_Date__c) {
                    //items.Inspection_ReportId__r.Inspection_asset_number__c;//计划内和外(实际执行数)
                    // System.debug('-------6------'+ a );
                    // 实际执行数(季度):没去重   计划内外
                    String aEQsKeyRepeat = planId + sumNumsList[7];
                    if (SumNumMap.containsKey(aEQsKeyRepeat)) {
                        aEQs = SumNumMap.get(aEQsKeyRepeat);
                    }
                    aEQs++;
                    SumNumMap.put(aEQsKeyRepeat, aEQs);
                    System.debug('-------7------' + SumNumMap );
                    // 实际点检设备数   去重
                    if (items.AssetId__c != null) {
                        String aEQsKey = planId + sumNumsList[0];
                        Set<Id> AssetSet = new Set<Id>();
                        if (PlanToAssetMap.containsKey(aEQsKey)) {
                            AssetSet = PlanToAssetMap.get(aEQsKey);
                        }
                        AssetSet.add(items.AssetId__c);
                        PlanToAssetMap.put(aEQsKey , AssetSet);
                        System.debug('-------8------' + PlanToAssetMap );
                    }
                }
                if (items.Inspection_ReportId__r.Inspection_Date__c <= items.Inspection_ReportId__r.Inspectup_Plan__r.Planned_End_Date__c
                        && items.Inspection_ReportId__r.Inspection_Date__c >= items.Inspection_ReportId__r.Inspectup_Plan__r.Planned_Start_Date__c) {
                    // AEQInPlans++; //items.Inspection_ReportId__r.Inspection_asset_number__c;
                    // 计划期内完成设备数
                    String aEQInPlansKey = planId + sumNumsList[1];
                    if (SumNumMap.containsKey(aEQInPlansKey)) {
                        AEQInPlans = SumNumMap.get(aEQInPlansKey);
                    }
                    AEQInPlans++;
                    SumNumMap.put(aEQInPlansKey, AEQInPlans);
                    System.debug('-------9------' + PlanToAssetMap );
                    // 计划内点检设备数
                    if (items.AssetId__c != null) {
                        String assetNumKey = planId + sumNumsList[4];
                        Set<Id> AssetSet = new Set<Id>();
                        if (PlanToAssetMap.containsKey(assetNumKey)) {
                            AssetSet = PlanToAssetMap.get(assetNumKey);
                        }
                        AssetSet.add(items.AssetId__c);
                        PlanToAssetMap.put(assetNumKey , AssetSet);
                        System.debug('-------8------' + PlanToAssetMap );
                    }
                }
                //点检计划,设备异常数
                if (items.ItemStatus__c == 'NG' && items.AssetId__c != null) {
                    String failnumKey = planId + sumNumsList[2];
                    Set<Id> AssetSet = new Set<Id>();
                    if (PlanToAssetMap.containsKey(failnumKey)) {
                        AssetSet = PlanToAssetMap.get(failnumKey);
                    }
                    AssetSet.add(items.AssetId__c);
                    PlanToAssetMap.put(failnumKey, AssetSet);
                }
                //点检计划 放弃设备数
                if (items.ItemStatus__c == '医院放弃' && items.AssetId__c != null ) {
                    String abandonmentnumKey = planId + sumNumsList[3];
                    Set<Id> AssetSet = new Set<Id>();
                    if (PlanToAssetMap.containsKey(abandonmentnumKey)) {
                        AssetSet = PlanToAssetMap.get(abandonmentnumKey);
                    }
                    AssetSet.add(items.AssetId__c);
                    PlanToAssetMap.put(abandonmentnumKey, AssetSet);
                }
                // 计划点检对象数
                String reportKey = planId + sumNumsList[5];
                SumNumMap.put(reportKey, items.Inspection_ReportId__r.Inspectup_Plan__r.Check_Object_Quantity__c);
                //点检报告书 检测日期
                // String IDateKey = planId + sumNumsList[6];
                IDateMap.put(planId, items.Inspection_ReportId__r.Inspection_Date__c);
                // 报告书 执行日
                IDateMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c + 'Start', items.Inspection_ReportId__r.Start_Date__c);
                // 点检计划 完成日
                IDateMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c + 'End', items.Inspection_ReportId__r.Inspectup_Plan__r.Execution_End_Date__c);
                // add 如果 超出点检区间=true 则个点检计划数据不计算 fxk 2021/10/20 Star
                // 点检计划 超出点检区间
                if(items.Inspection_ReportId__r.Inspectup_Plan__r.overstep_check_interval__c){
                    overstepMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c,items.Inspection_ReportId__r.Inspectup_Plan__r.overstep_check_interval__c);
                }
                // add 如果 超出点检区间=true 则个点检计划数据不计算 fxk 2021/10/20 End
                iplanMap.put(planId, SumNumMap);
            }
        }
        if (IPIdSet.size() > 0) {
            // 点检计划赋值
            for (String iPId : IPIdSet) {
                // add 如果 超出点检区间=true 则个点检计划数据不计算 fxk 2021/10/20 Star
                System.debug('超出'+overstepMap.get(iPId+'overstep'));
                if(!overstepMap.containskey(iPId)){
                // add 如果 超出点检区间=true 则个点检计划数据不计算 fxk 2021/10/20 End
                Inspectup_Plan__c IPlanInfo =  new Inspectup_Plan__c();
                IPlanInfo.Id = iPId;
                SumNumMap = iplanMap.get(iPId);
                System.debug('-------4------' + iplanMap);
                System.debug('-------2------' + SumNumMap);
                // 20210911 gzw bug fix start
                // if (SumNumMap.size() > 0) {
                    if (SumNumMap != null && SumNumMap.size() > 0 ) {
                        // 20210911 gzw bug fix end
                        if (PlanToAssetMap.containsKey(iPId + sumNumsList[0])) {
                            IPlanInfo.Actual_Execution_Quantity__c = PlanToAssetMap.get(iPId + sumNumsList[0]).size();//实际点检设备数   去重
                        }
                        IPlanInfo.Actual_Execution_Quantity_Inplan__c = SumNumMap.get(iPId + sumNumsList[1]);// 计划期内完成设备数
                        System.debug('-------11------' + SumNumMap.get(iPId + sumNumsList[1]));
                        if (PlanToAssetMap.containsKey(iPId + sumNumsList[4])) {
                            System.debug('-------10------' + PlanToAssetMap.get(iPId + sumNumsList[4]).size());
                            IPlanInfo.Planned_check_equipment_Num__c = PlanToAssetMap.get(iPId + sumNumsList[4]).size();// 计划内点检设备数
                        }
                        if (PlanToAssetMap.containsKey(iPId + sumNumsList[2])) {
                            IPlanInfo.Outlier_Quantity__c = PlanToAssetMap.get(iPId + sumNumsList[2]).size();//点检计划,设备异常数
                        }
                        if (PlanToAssetMap.containsKey(iPId + sumNumsList[3])) {
                            IPlanInfo.Abandonment_Number__c = PlanToAssetMap.get(iPId + sumNumsList[3]).size();//点检计划 放弃设备数
                        }
                        System.debug('-------131------' + IPlanInfo.Planned_check_equipment_Num__c);
                        System.debug('-------121------' + SumNumMap.get(iPId + sumNumsList[5]));
                        if (IPlanInfo.Actual_Execution_Quantity__c == SumNumMap.get(iPId + sumNumsList[5])) {
                            System.debug('-------13------' + IPlanInfo.Actual_Execution_Quantity__c);
                            System.debug('-------12------' + SumNumMap.get(iPId + sumNumsList[5]));
                            IPlanInfo.Ben_Actual_Times__c = 1;//本次点检次数(审批过后的点检报告书)
                            if (IDateMap.get(iPId + 'End') == null) {
                                IPlanInfo.Execution_End_Date__c = IDateMap.get(iPId);//点检计划 完成日
                            }
                        }
                        if (IDateMap.get(iPId + 'Start') == null) {
                            System.debug('--------123------' + IDateMap.get(iPId + 'Start'));
                            IPlanInfo.Execution_Start_Date__c = IDateMap.get(iPId);//执行开始日--检测日期
                        }
                        IPlanInfo.Actual_Execute_Num__c = SumNumMap.get(iPId + sumNumsList[7]);
                    }
                    System.debug('=====1===='+IPlanInfo.overstep_check_interval__c);
                    updateIPInfoList.add(IPlanInfo);
                    System.debug('-------1------' + updateIPInfoList);
                }
            }
        }
        if (updateIPInfoList.size() > 0) {
            update updateIPInfoList;
        }
        // // System.debug('IPIdMap.values():' + IPIdMap.values());
        // // 报告书批准后,点检计划
        // List<Inspection_Report__c> IRInfoList = [select Id, Failourassetnumber__c, Inspection_asset_number__c, Inspectup_Plan__c, Inspection_Date__c,
        //                             Submit_date__c, Abandonment_Num__c
        //                             from Inspection_Report__c where Inspectup_Plan__c in:IPIdSet and Status__c = '批准'];
        // // System.debug('IRInfoList:' + IRInfoList);
        // Map<String, List<Inspection_Report__c>> IPToIRsMap = new Map<String, List<Inspection_Report__c>>();
        // List<String> InsReportIdList = new List<String>();
        // for (Inspection_Report__c IRInfo : IRInfoList) {
        //     String IPkey = String.valueOf(IRInfo.Inspectup_Plan__c);
        //     // if (IPToIRsMap.containsKey(IPkey)) {
        //     //     List<Inspection_Report__c> IRValue = IPToIRsMap.get(IPkey);
        //     //     IRValue.add(IRInfo);
        //     //     IPToIRsMap.put(IPkey, IRValue);
        //     // } else {
        //     //     List<Inspection_Report__c> IRValue1 = new List<Inspection_Report__c>();
        //     //     IRValue1.add(IRInfo);
        //     //     IPToIRsMap.put(IPkey, IRValue1);
        //     // }
        //     List<Inspection_Report__c> IRValue = new List<Inspection_Report__c>();
        //     if (IPToIRsMap.containsKey(IPkey)) {
        //         IRValue = IPToIRsMap.get(IPkey);
        //     }
        //     IRValue.add(IRInfo);
        //     IPToIRsMap.put(IPkey, IRValue);
        //     InsReportIdList.add(IRInfo.Id);
        // }
        // List<Inspectup_Plan__c> IPInfoList = [select Id, Check_Object_Quantity__c, Actual_Execution_Quantity__c,
        //                                       Planned_Start_Date__c, Planned_End_Date__c
        //                                       from Inspectup_Plan__c where Id in:IPToIRsMap.keySet()];
        // Map<Id, set<String>> PlanToItemMap = new Map<Id, set<String>>();
        // List<Inspection_Item__c> ItemInfoList = [select Id, name, AssetId__c, Inspection_ReportId__r.Inspectup_Plan__c from Inspection_Item__c
        //                                         where Inspection_ReportId__c in: InsReportIdList AND Inspection_ReportId__r.Status__c = '批准'];
        // for (Inspection_Item__c Item : ItemInfoList) {
        //     set<String> AssetSet = new set<String>();
        //     if (PlanToItemMap.containsKey(Item.Inspection_ReportId__r.Inspectup_Plan__c)) {
        //         AssetSet = PlanToItemMap.get(Item.Inspection_ReportId__r.Inspectup_Plan__c);
        //     }
        //     AssetSet.add(Item.AssetId__c);
        //     PlanToItemMap.put(Item.Inspection_ReportId__r.Inspectup_Plan__c, AssetSet);
        // }
        // if (IPInfoList.size() > 0) {
        //     for (Inspectup_Plan__c IPInfokeys : IPInfoList) {
        //         Inspectup_Plan__c IPInfo = new Inspectup_Plan__c();
        //         IPInfo.Id = IPInfokeys.Id;
        //         Decimal AEQs = 0;//计划内和外(实际执行数)
        //         Decimal AEQInPlans = 0;//计划内实际执行数
        //         Decimal Failnum = 0;//点检计划,设备异常数
        //         Decimal Abandonmentnum = 0;//点检计划 放弃设备树
        //         for (Inspection_Report__c IRFeilds : IPToIRsMap.get(IPInfokeys.Id)) {
        //             if (IRFeilds.Inspection_Date__c >= IPInfokeys.Planned_Start_Date__c) {
        //                 AEQs += IRFeilds.Inspection_asset_number__c;//计划内和外(实际执行数)
        //             }
        //             if (IRFeilds.Inspection_Date__c < IPInfokeys.Planned_End_Date__c
        //                     && IRFeilds.Inspection_Date__c > IPInfokeys.Planned_Start_Date__c) {
        //                 AEQInPlans += IRFeilds.Inspection_asset_number__c;//计划内实际执行数
        //             }
        //             Failnum += IRFeilds.Failourassetnumber__c;//点检计划,设备异常数
        //             Abandonmentnum += IRFeilds.Abandonment_Num__c;//点检计划 放弃设备数
        //             if (IPInfokeys.Check_Object_Quantity__c == PlanToItemMap.get(IPInfokeys.Id).size()) {
        //                 IPInfo.Execution_End_Date__c = IRFeilds.Inspection_Date__c;//点检计划 完成日
        //             }
        //         }
        //         IPInfo.Planned_check_equipment_Num__c = PlanToItemMap.get(IPInfokeys.Id).size();
        //         IPInfo.Actual_Execution_Quantity_Inplan__c = AEQInPlans;//计划内实际执行数
        //         IPInfo.Actual_Execution_Quantity__c = AEQs;//计划内和外(实际执行数)
        //         IPInfo.Outlier_Quantity__c = Failnum;//点检计划,设备异常数
        //         IPInfo.Abandonment_Number__c = Abandonmentnum;//点检计划 放弃设备数
        //         IPInfo.Ben_Actual_Times__c = IPToIRsMap.get(IPInfokeys.Id).size();//本次点检次数(审批过后的点检报告书)
 
        //         updateIPInfoList.add(IPInfo);
        //     }
        //     // update updateIPInfoList;
        // }
 
        // // System.debug('updateIPInfoList:' + updateIPInfoList);
        // if (updateIPInfoList.size() > 0) {
        //     update updateIPInfoList;
        // }
    }
    // private void updateCheckPlanSubmit() {
    //     System.debug('--------123123123123------');
    //     List<Inspectup_Plan__c> updateIPInfoList = new List<Inspectup_Plan__c>();
    //     Set<String> IPIdSet = new Set<String>();
    //     for (Inspection_Report__c new_IRInfo : newList) {
    //         if ((new_IRInfo.Status__c != oldMap.get(new_IRInfo.Id).Status__c) && new_IRInfo.Status__c == '申请中') {
    //             if (new_IRInfo.Inspectup_Plan__c != null) {
    //                 IPIdSet.add(new_IRInfo.Inspectup_Plan__c);
    //                 // Inspectup_Plan__c IPInfo = new Inspectup_Plan__c();
    //                 // IPInfo.Id = new_IRInfo.Inspectup_Plan__c;
    //                 // System.debug('--------123123------' + new_IRInfo.Start_Date__c);
    //                 // if (new_IRInfo.Start_Date__c == null) {
    //                 //     System.debug('--------123------' + new_IRInfo.Start_Date__c);
    //                 //     IPInfo.Execution_Start_Date__c = new_IRInfo.Inspection_Date__c;//执行开始日--申请日
    //                 // }
    //                 // updateIPInfoList.add(IPInfo);
    //             }
    //         }
    //     }
    //     // ItemSql(IPIdSet, '申请中', '批准');
    //     List<Inspection_Item__c> ItemInfoList = new List<Inspection_Item__c>();
    //     if (IPIdSet.size() > 0) {
    //         ItemInfoList = [select Id, name, AssetId__c, Failourassetnumber__c, Inspection_ReportId__c, Inspection_ReportId__r.Inspectup_Plan__c,
    //                         Inspection_ReportId__r.Inspection_Date__c, Inspection_ReportId__r.Failourassetnumber__c,
    //                         Inspection_ReportId__r.Inspectup_Plan__r.Planned_Start_Date__c, Inspection_ReportId__r.Inspectup_Plan__r.Planned_End_Date__c,
    //                         Inspection_ReportId__r.Inspectup_Plan__r.Check_Object_Quantity__c,
    //                         Inspection_ReportId__r.Inspection_asset_number__c, ItemStatus__c, Inspection_ReportId__r.Start_Date__c,
    //                         Inspection_ReportId__r.Abandonment_Num__c, Inspection_ReportId__r.Status__c from Inspection_Item__c
    //                         where Inspection_ReportId__r.Inspectup_Plan__c in: IPIdSet AND (Inspection_ReportId__r.Status__c = '申请中' OR Inspection_ReportId__r.Status__c = '批准')];
    //     }
    //     // 获取日期
    //     Map<String, Date> IDateMap = new Map<String, Date>();
    //     // 去重设备数
    //     Map<String, Set<Id>> NumsMap = new Map<String, Set<Id>>();
    //     // 获取设备数量
    //     Map<String, Decimal> SumNumMap = new Map<String, Decimal>();
    //     if (ItemInfoList.size() > 0) {
    //         for (Inspection_Item__c items : ItemInfoList) {
    //             // 提交或申请设备数
    //             if (items.AssetId__c != null) {
    //                 Set<Id> AssetSet = new Set<Id>();
    //                 if (NumsMap.containsKey(items.Inspection_ReportId__r.Inspectup_Plan__c)) {
    //                     AssetSet = NumsMap.get(items.Inspection_ReportId__r.Inspectup_Plan__c);
    //                 }
    //                 AssetSet.add(items.AssetId__c);
    //                 NumsMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c, AssetSet);
    //             }
    //             // 报告书 检测日期(为申请日和完成日赋值)
    //             IDateMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c, items.Inspection_ReportId__r.Inspection_Date__c);
    //             // 报告书 执行日
    //             IDateMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c + 'Start', items.Inspection_ReportId__r.Start_Date__c);
    //             // 计划点检对象数
    //             SumNumMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c + 'PlanNum', items.Inspection_ReportId__r.Inspectup_Plan__r.Check_Object_Quantity__c);
 
    //         }
    //     }
    //     if (IPIdSet.size() > 0) {
    //         for (String iPId : IPIdSet) {
    //             Inspectup_Plan__c IPlanInfo =  new Inspectup_Plan__c();
    //             IPlanInfo.Id = iPId;
    //             System.debug('--------123321------' + IDateMap.get(iPId + 'Start'));
    //             if (IDateMap.get(iPId + 'Start') == null) {
    //                 System.debug('--------123------' + IDateMap.get(iPId + 'Start'));
    //                 IPlanInfo.Execution_Start_Date__c = IDateMap.get(iPId);//执行开始日--检测日期
    //             }
    //             if (NumsMap.containsKey(iPId)) {
    //                 IPlanInfo.Submit_Approval_Num__c = NumsMap.get(iPId).size();
    //             }
    //             if (IPlanInfo.Submit_Approval_Num__c == SumNumMap.get(iPId + 'PlanNum')) {
    //                 IPlanInfo.Execution_End_Date__c = IDateMap.get(iPId);//完成日--检测日期
    //             }
    //             updateIPInfoList.add(IPlanInfo);
    //         }
    //     }
    //     if (updateIPInfoList.size() > 0) {
    //         update updateIPInfoList;
    //     }
    // }
    // private void updateCheckPlanreject() {
    //     List<Inspectup_Plan__c> updateIPInfoList = new List<Inspectup_Plan__c>();
    //     Set<String> IRIdSet = new Set<String>();
    //     Set<String> IPIdSet = new Set<String>();
    //     for (Inspection_Report__c new_IRInfo : newList) {
    //         if ((new_IRInfo.Status__c != oldMap.get(new_IRInfo.Id).Status__c)
    //                 && new_IRInfo.Status__c == '草案中'
    //                 && oldMap.get(new_IRInfo.Id).Status__c == '申请中') {
    //             if (new_IRInfo.Inspectup_Plan__c != null ) {
    //                 IRIdSet.add(new_IRInfo.Id);
    //                 IPIdSet.add(new_IRInfo.Inspectup_Plan__c);
    //             }
    //         }
    //     }
    //     List<Inspection_Item__c> ItemInfoList = new List<Inspection_Item__c>();
    //     if (IPIdSet.size() > 0 && IRIdSet.size() > 0) {
    //         ItemInfoList = [select Id, name, AssetId__c, Failourassetnumber__c, Inspection_ReportId__c, Inspection_ReportId__r.Inspectup_Plan__c,
    //                         Inspection_ReportId__r.Inspection_Date__c, Inspection_ReportId__r.Failourassetnumber__c,
    //                         Inspection_ReportId__r.Inspectup_Plan__r.Planned_Start_Date__c, Inspection_ReportId__r.Inspectup_Plan__r.Planned_End_Date__c,
    //                         Inspection_ReportId__r.Inspectup_Plan__r.Check_Object_Quantity__c,
    //                         Inspection_ReportId__r.Inspection_asset_number__c, ItemStatus__c, Inspection_ReportId__r.Inspectup_Plan__r.Submit_Approval_Num__c,
    //                         Inspection_ReportId__r.Abandonment_Num__c, Inspection_ReportId__r.Status__c from Inspection_Item__c
    //                         where Inspection_ReportId__r.Inspectup_Plan__c in: IPIdSet and Inspection_ReportId__c in: IRIdSet];
    //     }
    //     // 驳回(去重设备数)
    //     Map<String, Set<Id>> NumsMap = new Map<String, Set<Id>>();
    //     // 获取设备数量
    //     Map<String, Decimal> SumNumMap = new Map<String, Decimal>();
    //     if (ItemInfoList.size() > 0) {
    //         for (Inspection_Item__c items : ItemInfoList) {
    //             // 提交或申请设备数(驳回设备数)
    //             if (items.AssetId__c != null) {
    //                 Set<Id> AssetSet = new Set<Id>();
    //                 if (NumsMap.containsKey(items.Inspection_ReportId__r.Inspectup_Plan__c)) {
    //                     AssetSet = NumsMap.get(items.Inspection_ReportId__r.Inspectup_Plan__c);
    //                 }
    //                 AssetSet.add(items.AssetId__c);
    //                 NumsMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c, AssetSet);
    //             }
    //             // 提交或申请设备数
    //             SumNumMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c, items.Inspection_ReportId__r.Inspectup_Plan__r.Submit_Approval_Num__c);
    //         }
    //     }
    //     if (IPIdSet.size() > 0) {
    //         for (String iPId : IPIdSet) {
    //             Inspectup_Plan__c IPlanInfo =  new Inspectup_Plan__c();
    //             IPlanInfo.Id = iPId;
    //             if (NumsMap.containsKey(iPId)) {
    //                 IPlanInfo.Submit_Approval_Num__c = SumNumMap.get(iPId) - NumsMap.get(iPId).size();
    //             }
    //             IPlanInfo.Execution_End_Date__c = null;
    //             updateIPInfoList.add(IPlanInfo);
    //         }
    //     }
    //     if (updateIPInfoList.size() > 0) {
    //         update updateIPInfoList;
    //     }
    // }
    // private List<Inspection_Item__c> ItemSql(Set<String> IPIdsSet, String Status1, String Status2) {
    //     List<Inspection_Item__c> ItemInfoList = [select Id, name, AssetId__c, Failourassetnumber__c, Inspection_ReportId__c, Inspection_ReportId__r.Inspectup_Plan__c,
    //                              Inspection_ReportId__r.Inspection_Date__c, Inspection_ReportId__r.Failourassetnumber__c,
    //                              Inspection_ReportId__r.Inspectup_Plan__r.Planned_Start_Date__c, Inspection_ReportId__r.Inspectup_Plan__r.Planned_End_Date__c,
    //                              Inspection_ReportId__r.Inspectup_Plan__r.Check_Object_Quantity__c,
    //                              Inspection_ReportId__r.Inspection_asset_number__c, ItemStatus__c,
    //                              Inspection_ReportId__r.Abandonment_Num__c, Inspection_ReportId__r.Status__c from Inspection_Item__c
    //                              where Inspection_ReportId__r.Inspectup_Plan__c in: IPIdsSet AND Inspection_ReportId__r.Status__c = : Status1 AND Inspection_ReportId__r.Status__c = :Status2];
    //     // return ItemInfoList;
    //     Map<Id, Set<Id>> NumsMap = new Map<Id, Set<Id>>();
    //     for (Inspection_Item__c items : ItemInfoList) {
    //         Set<Id> AssetSet = new Set<Id>();
    //         if (NumsMap.containsKey(items.Inspection_ReportId__r.Inspectup_Plan__c)) {
    //             AssetSet = NumsMap.get(items.Inspection_ReportId__r.Inspectup_Plan__c);
    //         }
    //         AssetSet.add(items.AssetId__c);
    //         NumsMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c, AssetSet);
    //     }
    // }
    //fxk 2021/8/31 点检计划中相应字段的赋值逻辑 end
 
    //add by rentx 20210823 start 在批准之后更新 使用ET 等信息到维修合同上
    private void updateVM() {
        // 批准后的点检报告书上传至AWS(关联的维修合同对应的修理来源是报修子单)
        Map<Id,Id>  idMap = new Map<Id,Id>();
 
        List<Maintenance_Contract__c> updatevmList = new List<Maintenance_Contract__c>();
        //1. 当前操作是否变为 批准
        for (Inspection_Report__c rep : newList) {
            if (rep.Status__c == '批准' && oldMap.get(rep.Id).Status__c != '批准' ) {
                //2. 判断当前报告书是否关联维修合同
                if (rep.Contract__c != null) {
                    Boolean flag = false;
                    Maintenance_Contract__c vm = new Maintenance_Contract__c();
                    vm.Id = rep.Contract__c;
                    if (rep.Used_ET__c != null && rep.Used_ET__c != '') {
                        vm.Used_ET__c = rep.Used_ET__c;
                        flag = true;
                    }
                    if (rep.UsedMachine__c != null && rep.UsedMachine__c != '') {
                        vm.UsedMachine__c = rep.UsedMachine__c;
                        flag = true;
                    }
                    if (rep.Disinfectant__c != null && rep.Disinfectant__c != '') {
                        vm.Disinfectant__c = rep.Disinfectant__c;
                        flag = true;
                    }
                    if (rep.CleaningFluid__c != null && rep.CleaningFluid__c != '') {
                        vm.CleaningFluid__c = rep.CleaningFluid__c;
                        flag = true;
                    }
                    if (rep.Remarks__c != null && rep.Remarks__c != '') {
                        vm.Remarks__c = rep.Remarks__c;
                        flag = true;
                    }
                    if (rep.Mode__c != null && rep.Mode__c != '') {
                        vm.Mode__c = rep.Mode__c;
                        flag = true;
                    }
                    if (rep.SterilizationMethod__c != null && rep.SterilizationMethod__c != '') {
                        vm.SterilizationMethod__c = rep.SterilizationMethod__c;
                        flag = true;
                    }
                    if (flag) {
                        updatevmList.add(vm);
                    }
                    // 记录类型是 合同点检 时,发送至ASW 
                    if(rep.RecordTypeId.equals(System.Label.InspectionReportRecordTypeId)){
                        idMap.put(rep.Id, rep.Contract__c);
                    }
                    
                }
            }
        }
 
        if (updatevmList.size() > 0) {
            update updatevmList;
        }
 
        if (idMap.size() > 0) {
            NFM602Controller.sendToAWS(idMap);
        }
 
    }
    //add by rentx 20210823 end 在批准之后更新 使用ET 等信息到维修合同上
}