高章伟
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
public without sharing class ReceivingNoteDetailHandler extends Oly_TriggerHandler {
    private Map<Id, ReceivingNoteDetail__c> newMap;
    private Map<Id, ReceivingNoteDetail__c> oldMap;
    private List<ReceivingNoteDetail__c> newList;
    private List<ReceivingNoteDetail__c> oldList;
 
    private static Datetime createdDate = Datetime.now();
    private static Date minDate;
    private static Date maxDate;
    private static Map<Date, Integer> dateIntMap = new Map<Date, Integer>();
    private static List<Date> dateList = new List<Date>();
    private static Date minDay;
    private static Date maxDay;
    private static Time ts = Time.newInstance(9, 0, 0, 0);
    private static Time te = Time.newInstance(17, 30, 0, 0);
 
    public ReceivingNoteDetailHandler() {
        this.newMap = (Map<Id, ReceivingNoteDetail__c>) Trigger.newMap;
        this.oldMap = (Map<Id, ReceivingNoteDetail__c>) Trigger.oldMap;
        this.newList = (List<ReceivingNoteDetail__c>) Trigger.new;
        this.oldList = (List<ReceivingNoteDetail__c>) Trigger.old;
    }
 
    protected override void beforeInsert() {
        beforeSetValue();
        setConsumablesFixedValue();
 
        // 新品收货功能LT时长统计
        setCalculateTime();
    }
    protected override void beforeUpdate() {
        beforeSetValue();
 
        // 新品收货功能LT时长统计
        setCalculateTime();
    }
    protected override void afterInsert() {
        setRNSFirst();
        //cleanReceivingStatus();
    }
    protected override void afterUpdate() {
        setRNSFirst();
        sendEmail();
    }
    /**
    @description 耗材在插入时三个字段赋固定值
    */
    public void setConsumablesFixedValue(){
        for (ReceivingNoteDetail__c nObj : newList) {
            if(nObj.Asset_loaner_category_F__c == '耗材'){
                nObj.unknow_serial_NO_product__c = 'FALSE';
                nObj.Loaner_accsessary__c = 'TRUE';
            }
        }
    }
    /**
    @description 变更点=‘新建’的明细插入时,把一览的【清空收货确认时间】和【外包装是否完好】清空
    */
    //private void cleanReceivingStatus(){
    //    Set<Id> rnsIdSet = new Set<Id>();
    //    for (ReceivingNoteDetail__c nObj : newList) {
    //        if(Trigger.isInsert && nObj.Change_Point__c == '新建' && nObj.DataConfirmation__c == false){
    //            rnsIdSet.add(nObj.ReceivingNoteSummary__c);
    //        }
    //    }
    //    List<ReceivingNoteSummary__c> rnsList = [
    //        SELECT Id
    //             , Package_Status_Memo__c
    //             , Package_Status__c
    //             , Received_Confirm_Staff__c
    //             , Received_Confirm_Time__c
    //          FROM ReceivingNoteSummary__c
    //         WHERE Id IN:rnsIdSet
    //          FOR UPDATE
    //        ];
    //    for(ReceivingNoteSummary__c rns:rnsList) {
    //        rns.Received_Confirm_Time__c = null;
    //        rns.Received_Confirm_Staff__c = null;
    //        rns.Package_Status__c = null;
    //        rns.Package_Status_Memo__c = null;
    //    }
    //    if(!rnsList.isEmpty()){
    //        update rnsList;
    //    }
    //}
 
    private void sendEmail() {
        Map<Id, List<ReceivingNoteDetail__c>> needSendEmailMap = new Map<Id, List<ReceivingNoteDetail__c>>();
        for (ReceivingNoteDetail__c nObj : newList) {
            ReceivingNoteDetail__c oObj;
            if (Trigger.isUpdate) {
                oObj = oldMap.get(nObj.Id);
                if (oObj.Arrival_wh_Request_time__c == null
                        && nObj.Arrival_wh_Request_time__c != null) {
                    if (needSendEmailMap.containsKey(nObj.ReceivingNote__c) == false) {
                        needSendEmailMap.put(nObj.ReceivingNote__c, new List<ReceivingNoteDetail__c>());
                    }
                    needSendEmailMap.get(nObj.ReceivingNote__c).add(nObj);
                }
            }
        }
        if (needSendEmailMap.isEmpty() == false) {
            List<ReceivingNote__c> rnList1 = new List<ReceivingNote__c>();
            List<ReceivingNote__c> rnList2 = new List<ReceivingNote__c>();
            for (Id rnId : needSendEmailMap.keySet()) {
                List<ReceivingNoteDetailWrapper> rndWrapperList = new List<ReceivingNoteDetailWrapper>();
                for(ReceivingNoteDetail__c rnd:needSendEmailMap.get(rnId)){
                    rndWrapperList.add(new ReceivingNoteDetailWrapper(rnd));
                }
                rndWrapperList.sort();
 
                String emailBody = '';
                for (ReceivingNoteDetailWrapper rndWrapper : rndWrapperList) {
                    emailBody += '\n' + rndWrapper.rnd.Fixture_Model_No_F__c;
                }
 
                rnList1.add(new ReceivingNote__c(Id = rnId, Please_Arrival_wh_Email__c = null));
                rnList2.add(new ReceivingNote__c(Id = rnId, Please_Arrival_wh_Email__c = emailBody));
            }
            update rnList1;
            update rnList2;
        }
    }
 
    public void setRNSFirst() {
        Map<String, ReceivingNoteSummary__c> rnsMap = new Map<String, ReceivingNoteSummary__c>();
        for (ReceivingNoteDetail__c nObj : newList) {
            if (String.isNotBlank(nObj.RNDAssert__c)) {
                if (rnsMap.containsKey(nObj.ReceivingNoteSummary__c) == false) {
                    rnsMap.put(nObj.ReceivingNoteSummary__c, new ReceivingNoteSummary__c(Id = nObj.ReceivingNoteSummary__c, First_RND__c = nObj.Id));
                }
            }
        }
        if (rnsMap.isEmpty() == false) {
            update rnsMap.values();
        }
    }
    public void beforeSetValue() {
        // 获取明细所在的一览
        List<Id> rnsIdList = new List<Id>();
        for(ReceivingNoteDetail__c rnd:newList){
            rnsIdList.add(rnd.ReceivingNoteSummary__c);
        }
        List<ReceivingNoteSummary__c> rnsList = [
                SELECT Id
                     , Fixture_Set__r.Name
                     , Fixture_Set__c
                     , ReceivingNote__r.Name
                     , ReceivingNote__r.passNo7__c
 
                  FROM ReceivingNoteSummary__c
                 WHERE Id in :rnsIdList
            ];
        Map<Id, ReceivingNoteSummary__c> rnsMap = new Map<Id, ReceivingNoteSummary__c>();
        for(ReceivingNoteSummary__c rns:rnsList){
            rnsMap.put(rns.Id, rns);
        }
        //add by rentx 20210825 start 新品收货_集中备品_建立数据逻辑限制 
        Set<String> mxxhSet = new Set<String>();
        //add by rentx 20210825 end 新品收货_集中备品_建立数据逻辑限制 
        for (ReceivingNoteDetail__c nObj : newList) {
            nObj.FSD_Id__c = nObj.Fixture_Set_Detail__c;
            nObj.ReceivingNote__c = nObj.ReceivingNoteId_F__c;
            nObj.Loaner_centre_mail_address__c = nObj.Loaner_centre_mail_address_F__c;
            // 新建明细的管理种类默认为数量管理
            if(String.isBlank(nObj.Change_Point__c) && String.isBlank(nObj.Manage_type__c) && Trigger.isInsert){
                nObj.Manage_type__c = '数量管理';
            }
            // UniqueKey 赋值
            if (nObj.DataMigration_Flag__c == false
                    &&String.isNotBlank(nObj.FSD_Id__c)) {
                // System.debug('KKKKKis ' + nObj.IndexFromUniqueKey__c);
                nObj.UniqueKey__c = nObj.ReceivingNote__c + ':' + nObj.ReceivingNoteSummary__c + ':'+ nObj.FSD_Id__c
                            + ':' + nObj.passNo3__c;
                System.debug(nObj.passNo3__c + 'key is' + nObj.UniqueKey__c);
            }
            else {
                System.debug('nullId');
                // 因为收货清单明细取消的话精琢创建的明细不会删除;
                nObj.UniqueKey__c = null;
            }
            if (nObj.Delete_Flag__c) {
                nObj.UniqueKey__c += ':' + nObj.Id;
            }
 
            // Name赋值
            // ReceivingNoteSummary__c rns = rnsMap.get(nObj.ReceivingNoteSummary__c);
            nObj.Name = nObj.ReceivingNoteDetailNo__c;
 
        // if (nObj.DataMigration_Flag__c == false
            //         && rns != null
            //         && String.isNotBlank(rns.Fixture_Set__r.Name)
            //         //&& rns.IndexFromUniqueKey__c != null
            //         && nObj.IndexFromUniqueKey__c != null
            //         && (!Trigger.isUpdate || String.isNotBlank(rns.Fixture_Set__c))) {
            //     String split_RnNum = rns.ReceivingNote__r.passNo7__c;
            //     if (rns.ReceivingNote__r.Name.contains('_')) {
            //         // XXXX-YYYY_1 (ZZZZ_1)
            //         List<String> split_RnNumList = rns.ReceivingNote__r.Name.split('_');
            //         String raName = split_RnNumList[split_RnNumList.size() - 2];      // XXXX-YYYY (ZZZZ)
            //         split_RnNum = split_RnNumList[split_RnNumList.size() - 1];     // 1
            //         split_RnNumList = raName.split('-');     // [XXXX, YYYY]  ([ZZZZ])
            //         if (split_RnNumList.size() > 1) {
            //             // YYYY_1
            //             split_RnNum = split_RnNumList[split_RnNumList.size() - 1] + '_' + split_RnNum;
            //         } else {
            //             // ZZZZ_1
            //             split_RnNum = rns.ReceivingNote__r.Name;
            //         }
            //     }
            //     nObj.Name = nObj.ReceivingNoteDetailNo__c;
            //             //+ rns.IndexFromUniqueKey__c.format().leftpad(3, '0');
 
            //     // 如果是分配代替品名字加:Sub
            //     //if (nObj.Substitute_Select_Again__c) {
            //     //    nObj.Name += ':Sub';
            //     //}
 
            //     //一览时申请者收货NG的话名字添加NG
            //     //if (nObj.Received_Confirm_F__c == 'NG') {
            //     //    nObj.Name += ':NG';
            //     //}
            //     nObj.Name += ':' + nObj.IndexFromUniqueKey_Text__c + ':'
            //             + nObj.IndexFromUniqueKey__c.format().leftpad(3, '0');
            //     //取消的时候名字加Canceled
            //     //if (nObj.Cancel_Select__c) {
            //     //    nObj.Name += ':Canceled';
            //     //}
        // }
 
 
            //add by rentx 20210825 start 
            if (nObj.Asset_loaner_category_F__c == '固定资产') {
                nObj.unknow_serial_NO_product__c = 'FALSE';   
                nObj.Manage_type__c = '个体管理';
            }
            mxxhSet.add(nObj.Fixture_Model_No_F__c);
                
            //add by rentx 20210825 end
        }
 
        //根据备品配套明细型号去查备品配套明细//add by rentx 20210825 start 
        Map<String,String> tempMap = new Map<String,String>();
        List<Fixture_Set_Detail__c> fixdetaillist = new List<Fixture_Set_Detail__c>();
        fixdetaillist = [select id,Is_Body__c,Fixture_Model_No_F__c from Fixture_Set_Detail__c where Fixture_Model_No_F__c in :mxxhSet];
        if (fixdetaillist.size() > 0) {
            for (Fixture_Set_Detail__c detail : fixdetaillist ) {
                //是附属品
                if (!detail.Is_Body__c) {
                    tempMap.put(detail.Fixture_Model_No_F__c, 'TRUE');
                }else{
                    tempMap.put(detail.Fixture_Model_No_F__c, 'FALSE');
                }
            }
        }
        for (ReceivingNoteDetail__c nObj : newList) {
            if (tempMap.containsKey(nObj.Fixture_Model_No_F__c)) {
                String tempstr = tempMap.get(nObj.Fixture_Model_No_F__c);
                if (nObj.Loaner_accsessary__c == '' || nObj.Loaner_accsessary__c == null) {
                    nObj.Loaner_accsessary__c = tempStr;   
                }
            }
        }
        //add by rentx 20210825 end
 
    }
 
    public void setCalculateTime() {
        // 日期范围变化Flg
        Boolean minMaxDateIsChanged = false;
        // Map< Id, Map< 字段api名, Map<起始时间, 终了时间>>>
        Map<ReceivingNoteDetail__c, Map<String, Map<Datetime, Datetime>>> calculateMap = new Map<ReceivingNoteDetail__c, Map<String, Map<Datetime, Datetime>>>();
        for (ReceivingNoteDetail__c nObj : newList) {
            Boolean needSet = (null != nObj.Posting_Date_F__c // 发货日
                                                && null != nObj.Received_Confirm_Date_F__c // 收货确认日
                                                && nObj.Posting_Date_F__c <= nObj.Received_Confirm_Date_F__c); // 发货日 <= 收货确认日
            // 新品到货时长
            if ((needSet
                    && (Trigger.isInsert
                        || nObj.Posting_Date_F__c != oldMap.get(nObj.Id).Posting_Date_F__c // 发货日更新时
                        || nObj.Received_Confirm_Date_F__c != oldMap.get(nObj.Id).Received_Confirm_Date_F__c // 收货确认日更新时
                        || nObj.ProductArrivalTime_Day__c == null
                    )
                )
                || (nObj.ProductArrivalTime_Day__c != null
                        && needSet == false
                    )
            ) {
                if (needSet) {
                    minMaxDateIsChanged = editCalculateMap(
                        nObj,
                        Datetime.newInstance(nObj.Posting_Date_F__c, ts),
                        Datetime.newInstance(nObj.Received_Confirm_Date_F__c, te),
                        'ProductArrivalTime',
                        calculateMap,
                        minMaxDateIsChanged
                    );
                }
                else {
                    nObj.ProductArrivalTime_Day__c = null;
                }
            }
            Datetime cd = Trigger.isInsert ? createdDate : nObj.createdDate;
            needSet = (null != nObj.DataConfirmationTime__c // 数据确认时间
                        && cd <= nObj.DataConfirmationTime__c); // 创建日期 <= 数据确认时间
            // 受理数据确认时长
            if (
                (needSet &&
                    (Trigger.isInsert // 数据插入时
                        || nObj.DataConfirmationTime__c != oldMap.get(nObj.Id).DataConfirmationTime__c // 数据确认时间更新时
                        || nObj.AccepDataConfirmTime_Day__c == null
                        || nObj.AccepDataConfirmTime_Hour__c == null))
                || (needSet == false
                    && (nObj.AccepDataConfirmTime_Day__c != null
                        || nObj.AccepDataConfirmTime_Hour__c != null)
                    )
            ) {
                if (needSet) {
                    minMaxDateIsChanged = editCalculateMap(nObj, cd, nObj.DataConfirmationTime__c, 'AccepDataConfirmTime', calculateMap, minMaxDateIsChanged);
                }
                else {
                    nObj.AccepDataConfirmTime_Day__c = null;
                    nObj.AccepDataConfirmTime_Hour__c = null;
                }
            }
 
            needSet = (null != nObj.DataConfirmationTime__c // 数据确认时间
                        && null != nObj.Received_Confirm_Time_F__c // 收货确认时间
                        && nObj.DataConfirmationTime__c <= nObj.Received_Confirm_Time_F__c); // 数据确认时间 <= 收货确认时间
            // 仓库收货时长
            if ((needSet
                    &&(Trigger.isInsert // 数据插入时
                    || nObj.DataConfirmationTime__c != oldMap.get(nObj.Id).DataConfirmationTime__c // 数据确认时间更新时
                    || nObj.Received_Confirm_Time_F__c != oldMap.get(nObj.Id).Received_Confirm_Time_F__c // 收货确认时间更新时
                    || nObj.WarehouseReceiptTime_Day__c == null
                ))
                || (needSet == false && nObj.WarehouseReceiptTime_Day__c != null)
            ) {
                if (needSet) {
                    minMaxDateIsChanged = editCalculateMap(nObj, nObj.DataConfirmationTime__c, nObj.Received_Confirm_Time_F__c, 'WarehouseReceiptTime', calculateMap, minMaxDateIsChanged);
                }
                else {
                    nObj.WarehouseReceiptTime_Day__c = null;
                }
            }
 
            needSet = (null != nObj.Received_Confirm_Time_F__c // 收货确认时间
                        && null != nObj.AcceptanceTime__c // 清点时间
                        && nObj.Received_Confirm_Time_F__c <= nObj.AcceptanceTime__c); // 收货确认时间 <= 清点时间
            // 仓库清点时长
            if ((needSet
                    &&(Trigger.isInsert // 数据插入时
                        || nObj.Received_Confirm_Time_F__c != oldMap.get(nObj.Id).Received_Confirm_Time_F__c // 收货确认时间更新时
                        || nObj.AcceptanceTime__c != oldMap.get(nObj.Id).AcceptanceTime__c // 清点时间更新时
                        || nObj.WarehouseAcceptanceTime_Day__c == null
                        || nObj.WarehouseAcceptanceTime_Hour__c == null
                    )
                )
                || (needSet == false
                        && (nObj.WarehouseAcceptanceTime_Day__c != null
                            || nObj.WarehouseAcceptanceTime_Hour__c != null
                        )
                    )
            ) {
                if (needSet) {
                    minMaxDateIsChanged = editCalculateMap(nObj, nObj.Received_Confirm_Time_F__c, nObj.AcceptanceTime__c, 'WarehouseAcceptanceTime', calculateMap, minMaxDateIsChanged);
                }
                else {
                    nObj.WarehouseAcceptanceTime_Day__c = null;
                    nObj.WarehouseAcceptanceTime_Hour__c = null;
                }
            }
 
            needSet = (null != nObj.AcceptanceTime__c // 清点时间
                        && null != nObj.After_Inspection_time__c // 验收完毕时间
                        && nObj.AcceptanceTime__c <= nObj.After_Inspection_time__c); // 清点时间 <= 验收完毕时间
            // 检测验收时长
            if (
                (needSet
                    &&(Trigger.isInsert // 数据插入时
                        || nObj.AcceptanceTime__c != oldMap.get(nObj.Id).AcceptanceTime__c // 清点时间更新时
                        || nObj.After_Inspection_time__c != oldMap.get(nObj.Id).After_Inspection_time__c // 验收完毕时间更新时
                        || nObj.InspectionAcceptanceTime_Day__c == null
                        || nObj.InspectionAcceptanceTime_Hour__c == null
                    )
                )
                || (needSet == false
                        && (nObj.InspectionAcceptanceTime_Day__c != null
                                || nObj.InspectionAcceptanceTime_Hour__c != null
                            )
                    )
            ) {
                if (needSet) {
                    minMaxDateIsChanged = editCalculateMap(nObj, nObj.AcceptanceTime__c, nObj.After_Inspection_time__c, 'InspectionAcceptanceTime', calculateMap, minMaxDateIsChanged);
                }
                else {
                    nObj.InspectionAcceptanceTime_Day__c = null;
                    nObj.InspectionAcceptanceTime_Hour__c = null;
                }
            }
 
            needSet = (null != nObj.After_Inspection_time__c // 验收完毕时间
                        && null != nObj.DataConfirmationAgainTime__c // 再数据确认时间
                        && nObj.After_Inspection_time__c <= nObj.DataConfirmationAgainTime__c); // 验收完毕时间 <= 再数据确认时间
            // 受理再数据确认时长
            if ((needSet
                    &&(Trigger.isInsert // 数据插入时
                        || nObj.After_Inspection_time__c != oldMap.get(nObj.Id).After_Inspection_time__c // 验收完毕时间更新时
                        || nObj.DataConfirmationAgainTime__c != oldMap.get(nObj.Id).DataConfirmationAgainTime__c // 再数据确认时间更新时
                        || nObj.AcceptDataConfirmAgainTime_Day__c == null
                        || nObj.AcceptDataConfirmAgainTime_Hour__c == null
                    )
                )
                || (needSet == false
                        && (nObj.AcceptDataConfirmAgainTime_Day__c != null
                                || nObj.AcceptDataConfirmAgainTime_Hour__c != null
                            )
                    )
            ) {
                if (needSet) {
                    minMaxDateIsChanged = editCalculateMap(nObj, nObj.After_Inspection_time__c, nObj.DataConfirmationAgainTime__c, 'AcceptDataConfirmAgainTime', calculateMap, minMaxDateIsChanged);
                }
                else {
                    nObj.AcceptDataConfirmAgainTime_Day__c = null;
                    nObj.AcceptDataConfirmAgainTime_Hour__c = null;
                }
            }
 
            needSet = (null != nObj.After_Inspection_time__c // 验收完毕时间
                            && null != nObj.Arrival_wh_Request_time__c // 入库指示时间
                            && nObj.After_Inspection_time__c <= nObj.Arrival_wh_Request_time__c); // 再数据确认时间 <= 入库指示时间
            // 受理再数据入库指示时长
            if ((needSet
                    &&(Trigger.isInsert // 数据插入时
                        || nObj.After_Inspection_time__c != oldMap.get(nObj.Id).After_Inspection_time__c // 再数据确认时间更新时
                        || nObj.Arrival_wh_Request_time__c != oldMap.get(nObj.Id).Arrival_wh_Request_time__c // 入库指示时间更新时
                        || nObj.AcceptDataArrivalRequestAgainTime_Day__c == null
                        || nObj.AcceptDataArrivalRequestAgainTime_Hour__c == null
                    )
                )
                || (needSet == false
                        && (nObj.AcceptDataArrivalRequestAgainTime_Day__c != null
                                && nObj.AcceptDataArrivalRequestAgainTime_Hour__c != null
                            )
                    )
            ) {
                if (needSet) {
                    minMaxDateIsChanged = editCalculateMap(nObj, nObj.After_Inspection_time__c, nObj.Arrival_wh_Request_time__c, 'AcceptDataArrivalRequestAgainTime', calculateMap, minMaxDateIsChanged);
                }
                else {
                    nObj.AcceptDataArrivalRequestAgainTime_Day__c = null;
                    nObj.AcceptDataArrivalRequestAgainTime_Hour__c = null;
                }
            }
 
            needSet = (null != nObj.Arrival_wh_Request_time__c // 入库指示时间
                        && null != nObj.Arrival_wh_time__c // 入库确认完成时间
                        && nObj.Arrival_wh_Request_time__c <= nObj.Arrival_wh_time__c); // 入库指示时间 <= 入库确认完成时间
            // 仓库入库时长
            if ((needSet
                    &&(Trigger.isInsert // 数据插入时
                        || nObj.Arrival_wh_Request_time__c != oldMap.get(nObj.Id).Arrival_wh_Request_time__c // 入库指示时间更新时
                        || nObj.Arrival_wh_time__c != oldMap.get(nObj.Id).Arrival_wh_time__c // 入库确认完成时间更新时
                        || nObj.WarehouseArrivalTime_Day__c == null
                        || nObj.WarehouseArrivalTime_Hour__c == null
                    )
                )
                || (needSet == false
                        && (nObj.WarehouseArrivalTime_Day__c != null
                                || nObj.WarehouseArrivalTime_Hour__c != null
                            )
                    )
            ) {
                if (needSet) {
                    minMaxDateIsChanged = editCalculateMap(nObj, nObj.Arrival_wh_Request_time__c, nObj.Arrival_wh_time__c, 'WarehouseArrivalTime', calculateMap, minMaxDateIsChanged);
                }
                else {
                    nObj.WarehouseArrivalTime_Day__c = null;
                    nObj.WarehouseArrivalTime_Hour__c = null;
                }
            }
 
            needSet = (null != nObj.Received_Confirm_Time_F__c // 收货确认时间
                            && null != nObj.Arrival_wh_time__c // 入库确认完成时间
                            && nObj.Arrival_wh_Request_time__c <= nObj.Arrival_wh_time__c); // 收货确认时间 <= 入库确认完成时间
            // 新品收货操作总时长
            if ((needSet
                    &&(Trigger.isInsert // 数据插入时
                        || nObj.Received_Confirm_Time_F__c != oldMap.get(nObj.Id).Received_Confirm_Time_F__c // 收货确认时间更新时
                        || nObj.Arrival_wh_time__c != oldMap.get(nObj.Id).Arrival_wh_time__c // 入库确认完成时间更新时
                        || nObj.ProductReceiptTotalTime_Day__c == null
                    )
                )
                || (needSet == false
                        && nObj.ProductReceiptTotalTime_Day__c != null
                    )
            ) {
                if (needSet) {
                    minMaxDateIsChanged = editCalculateMap(nObj, nObj.Received_Confirm_Time_F__c, nObj.Arrival_wh_time__c, 'ProductReceiptTotalTime', calculateMap, minMaxDateIsChanged);
                }
                else {
                    nObj.ProductReceiptTotalTime_Day__c = null;
                }
            }
        }
 
        System.debug('####minDate: '+minDate);
        System.debug('####maxDate: '+maxDate);
 
        // 设置日期Map(仅日期范围扩大时需再次设置)
        if (true == dateIntMap.isEmpty() || true == minMaxDateIsChanged) {
            Integer idx = 0;
            if (false == dateIntMap.isEmpty()) {
                dateIntMap.clear();
            }
            dateIntMap.clear(); // Map clear
            minDay = null;
            for (OlympusCalendar__c workday : [
                    Select Date__c
                      From OlympusCalendar__c
                     Where Date__c >= :minDate And Date__c <= :maxDate
                       And IsWorkDay__c = 1
                  ORDER BY Date__c ASC
            ]) {
                dateIntMap.put(workday.Date__c, idx);
                dateList.add(workday.Date__c);
                if (minDay == null) {
                    minDay = workday.Date__c;
                }
                maxDay = workday.Date__c;
                idx ++;
            }
        }
 
        // 计算各项时长
        for (ReceivingNoteDetail__c nObj : calculateMap.keySet()) {
            Map<String, Map<Datetime, Datetime>> apiDateMap = calculateMap.get(nObj);
            for (String apiName : calculateMap.get(nObj).keySet()) {
                // 新品到货时长
                if ('ProductArrivalTime' == apiName) {
                    List<Decimal> calculateResult = doCalculate(apiDateMap, apiname);
                    nObj.ProductArrivalTime_Day__c = calculateResult[0];
                }
 
                // 受理数据确认时长
                if ('AccepDataConfirmTime' == apiName) {
                    List<Decimal> calculateResult = doCalculate(apiDateMap, apiname);
                    nObj.AccepDataConfirmTime_Day__c = calculateResult[0];
                    nObj.AccepDataConfirmTime_Hour__c = calculateResult[1];
                }
 
                // 仓库收货时长
                if ('WarehouseReceiptTime' == apiName) {
                    List<Decimal> calculateResult = doCalculate(apiDateMap, apiname);
                    nObj.WarehouseReceiptTime_Day__c = calculateResult[0];
                }
 
                // 仓库清点时长
                if ('WarehouseAcceptanceTime' == apiName) {
                    List<Decimal> calculateResult = doCalculate(apiDateMap, apiname);
                    nObj.WarehouseAcceptanceTime_Day__c = calculateResult[0];
                    nObj.WarehouseAcceptanceTime_Hour__c = calculateResult[1];
                }
 
                // 检测验收时长
                if ('InspectionAcceptanceTime' == apiName) {
                    List<Decimal> calculateResult = doCalculate(apiDateMap, apiname);
                    nObj.InspectionAcceptanceTime_Day__c = calculateResult[0];
                    nObj.InspectionAcceptanceTime_Hour__c = calculateResult[1];
                }
 
                // 受理再数据确认时长
                if ('AcceptDataConfirmAgainTime' == apiName) {
                    List<Decimal> calculateResult = doCalculate(apiDateMap, apiname);
                    nObj.AcceptDataConfirmAgainTime_Day__c = calculateResult[0];
                    nObj.AcceptDataConfirmAgainTime_Hour__c = calculateResult[1];
                }
 
                // 受理再数据入库指示时长
                if ('AcceptDataArrivalRequestAgainTime' == apiName) {
                    List<Decimal> calculateResult = doCalculate(apiDateMap, apiname);
                    nObj.AcceptDataArrivalRequestAgainTime_Day__c = calculateResult[0];
                    nObj.AcceptDataArrivalRequestAgainTime_Hour__c = calculateResult[1];
                }
 
                // 仓库入库时长
                if ('WarehouseArrivalTime' == apiName) {
                    List<Decimal> calculateResult = doCalculate(apiDateMap, apiname);
                    nObj.WarehouseArrivalTime_Day__c = calculateResult[0];
                    nObj.WarehouseArrivalTime_Hour__c = calculateResult[1];
                }
 
                // 新品收货操作总时长
                if ('ProductReceiptTotalTime' == apiName) {
                    List<Decimal> calculateResult = doCalculate(apiDateMap, apiname);
                    nObj.ProductReceiptTotalTime_Day__c = calculateResult[0];
                }
            }
        }
    }
 
    private Boolean editCalculateMap(ReceivingNoteDetail__c nObj, Datetime startDate, Datetime endDate, String apiName,
        Map<ReceivingNoteDetail__c, Map<String, Map<Datetime, Datetime>>> calculateMap, Boolean minMaxDateIsChanged
    ) {
        if (null == calculateMap.get(nObj)) {
            calculateMap.put(nObj, new Map<String, Map<Datetime, Datetime>>());
        }
        Map<String, Map<Datetime, Datetime>> apiDateMap = calculateMap.get(nObj);
        if (null == apiDateMap.get(apiName)) {
            apiDateMap.put(apiName, new Map<Datetime, Datetime>());
        }
        Map<Datetime, Datetime> dateMap = apiDateMap.get(apiName);
        if (null == dateMap.get(startDate)) {
            dateMap.put(startDate, endDate);
        }
 
        // 最大,最小日期设定
        if (null == minDate || minDate > startDate.date()) {
            minDate = startDate.date();
            minMaxDateIsChanged = true;
        }
        if (null == maxDate || maxDate < endDate.date()) {
            maxDate = endDate.date();
            minMaxDateIsChanged = true;
        }
 
        return minMaxDateIsChanged;
    }
 
    private List<Decimal> doCalculate (Map<String, Map<Datetime, Datetime>> apiDateMap, String apiName) {
        // 开始结束之间不包含工作日
        if (0 == dateIntMap.size()) {
            return new Decimal[] {0.00, 0.00};
        }
        System.debug('###apiName: '+apiName);
        System.debug('###apiDateMap: '+apiDateMap);
        // apiDateMap Key开始时间, Value结束时间
        Datetime startDate = (new List<Datetime> (apiDateMap.get(apiName).keySet()))[0];
        Datetime endDate = apiDateMap.get(apiName).values()[0];
 
        System.debug('###startDate0: '+startDate.format());
        System.debug('###endDate0: '+endDate.format());
        System.debug('###dateIntMap: '+dateIntMap);
 
        Boolean sFlg = dateIntMap.containsKey(startDate.date()) == false;
        Boolean eFlg = dateIntMap.containsKey(endDate.date()) == false;
 
        System.debug('###sFlg: '+sFlg);
        System.debug('###eFlg: '+eFlg);
 
        if (sFlg || eFlg) {
            // 循环次数
            Integer maxStartIdx = sFlg ? (startDate.date().daysBetween(maxDay) + 1) : 0;
            Integer maxEndIdx = eFlg ? (minDay.daysBetween(endDate.date()) + 1) : 0;
            System.debug('###maxStartIdx: '+maxStartIdx);
            System.debug('###maxEndIdx: '+maxEndIdx);
            Datetime sDt = startDate;
            Datetime eDt = endDate;
            for (Integer i = 0; i < Math.max(maxStartIdx, maxEndIdx); i++) {
                if (true == sFlg) {
                    sDt = sDt.addDays(1);
                    // 找到最近开始日期时
                    if (dateIntMap.containsKey(sDt.date())) {
                        startDate = Datetime.newInstance(sDt.year(), sDt.month(), sDt.day(), 9, 0, 0);
                        sFlg = false;
                    }
                }
                if (true == eFlg) {
                    eDt = eDt.addDays(-1);
                    // 找到最近结束日期时
                    if (dateIntMap.containsKey(eDt.date())) {
                        endDate = Datetime.newInstance(eDt.year(), eDt.month(), eDt.day(), 17, 30, 0);
                        eFlg = false;
                    }
                }
                // 开始结束日期都找到时,退出循环
                if (false == sFlg && false == eFlg) {
                    break;
                }
            }
        }
 
        System.debug('###startDate: '+startDate.format());
        System.debug('###endDate: '+endDate.format());
 
        if (startDate.date() <= maxDay && endDate.date() >= minDay && startDate < endDate) {
            // 总天数和
            Decimal totalDays = dateIntMap.get(endDate.date()) - dateIntMap.get(startDate.date()) + 1;
            System.debug('###totalDays: '+totalDays);
 
            // 第一天和最后一天以外的分钟数
            Decimal middleWorkMins = 0;
            if (1 < totalDays) {
                middleWorkMins = (totalDays - 2) * 510;
            }
            // 第一天的分钟数
            Decimal firstWorkMins = 0;
            Decimal firstTotalMins = startDate.hour() * 60 + startDate.minute();
            Decimal lastWorkMins = 0;
            Decimal lastTotalMins = endDate.hour() * 60 + endDate.minute();
            Decimal totalMins = 0;
            if (startDate.date() != endDate.date()) {
                System.debug(firstTotalMins);
                // 第一天开始时间小于9:00(570分钟)
                if (540 > firstTotalMins) {
                    firstWorkMins = 510;
                }
                // 第一天开始时间小于17:30(1050分钟)
                else if (1050 > firstTotalMins) {
                    firstWorkMins = 1050 - firstTotalMins;
                }
                // 最后一天分钟数
                System.debug(lastTotalMins);
                // 最后一天结束时间大与17:30(1050分钟)
                if (1 < totalDays && 1050 < lastTotalMins) {
                    lastWorkMins = 510;
                }
                // 最后一天结束时间大与9:00(540分钟)
                else if (1 < totalDays && 540 < lastTotalMins) {
                    lastWorkMins = lastTotalMins - 540;
                }
                System.debug(startDate);
                System.debug(endDate);
                System.debug(firstWorkMins);
                System.debug(lastWorkMins);
                totalMins = firstWorkMins + middleWorkMins + lastWorkMins;
            }
            else {
                if (540 > firstTotalMins) {
                    firstTotalMins = 540;
                }
                else if (1050 < firstTotalMins) {
                    firstTotalMins = 1050;
                }
                if (540 > lastTotalMins) {
                    lastTotalMins = 540;
                }
                else if (1050 < lastTotalMins) {
                    lastTotalMins = 1050;
                }
                totalMins = lastTotalMins - firstTotalMins;
            }
            // 总分钟数和
            System.debug('###totalMins: '+totalMins + '   ' + apiName);
 
            System.debug('###calculateResult: '+ (totalMins / 51.00).setScale(1, System.RoundingMode.HALF_UP) / 10.00 + ' , ' + (totalMins / 6.00).setScale(1, System.RoundingMode.HALF_UP) / 10.00);
 
            // 天数统计值, 小时数统计值
            return new Decimal[] {(totalMins / 51.00).setScale(1, System.RoundingMode.HALF_UP) / 10.00, (totalMins / 6.00).setScale(1, System.RoundingMode.HALF_UP) / 10.00};
        } else {
            return new Decimal[] {0.00, 0.00};
        }
        
    }
 
    /**
    @description 明细按No排序
    */
    public class ReceivingNoteDetailWrapper implements Comparable {
 
        public ReceivingNoteDetail__c rnd;
 
        // Constructor
        public ReceivingNoteDetailWrapper(ReceivingNoteDetail__c rnd1) {
            rnd = rnd1;
        }
        public Integer compareTo(Object compareTo) {
            ReceivingNoteDetailWrapper compareToRnd = (ReceivingNoteDetailWrapper)compareTo;
            Integer returnValue = 0;
            if (rnd.ReceivingNoteDetailNo__c > compareToRnd.rnd.ReceivingNoteDetailNo__c) {
                returnValue = 1;
            } else if (rnd.ReceivingNoteDetailNo__c < compareToRnd.rnd.ReceivingNoteDetailNo__c) {
                returnValue = -1;
            }
            return returnValue;
        }
    }
}