buli
2023-05-23 07390e2fcb4adf27c928335bf27ae7939c5a80ad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
public without sharing class ConsumApplyEquipmentSetDetailHandler extends Oly_TriggerHandler {
    private Map<Id, Consum_Apply_Equipment_Set_Detail__c> newMap;
    private Map<Id, Consum_Apply_Equipment_Set_Detail__c> oldMap;
    private List<Consum_Apply_Equipment_Set_Detail__c> newList;
    private List<Consum_Apply_Equipment_Set_Detail__c> oldList;
    //Consum_Apply_Equipment_Set_Detail__c 更新する時にAssetも更新する可能性がありますので,ここでstaticMapを作成しsiAssetを結集します
    private static Map<Id, Asset> assMap                = new Map<Id, Asset>(); // new Only
    private static Map<Id, Asset> assUpdMap             = new Map<Id, Asset>(); // new と old
    private static Set<Id> updatedSet             = new Set<Id>(); // new と old
    private static Set<Id> executeed_refreshQueueNumber = new Set<Id>();        // 已经处理过排队顺 Consum_Apply_Equipment_Set_Detail__c.Id
 
    private static Map<Date, OlympusCalendar__c> calendarMap = new Map<Date, OlympusCalendar__c>();        // 已经处理过排队顺 Consum_Apply_Equipment_Set_Detail__c.Id
 
    public ConsumApplyEquipmentSetDetailHandler() {
        if (Trigger.isUpdate || Trigger.isUndelete || Trigger.isDelete) {
            this.newMap = new Map<Id, Consum_Apply_Equipment_Set_Detail__c>();
            this.newList = new List<Consum_Apply_Equipment_Set_Detail__c>();
            this.oldMap = (Map<Id, Consum_Apply_Equipment_Set_Detail__c>) Trigger.oldMap;
            this.oldList = (List<Consum_Apply_Equipment_Set_Detail__c>) Trigger.old;
            // 大前提 Fixture_Set__c ですが下記の属性が設定されています
            // 参照関係に含まれる参照レコードは削除できません。
            if (Trigger.isUpdate || Trigger.isUndelete) {
                for (SObject nSObj : Trigger.new) {
                    Consum_Apply_Equipment_Set_Detail__c nObj = (Consum_Apply_Equipment_Set_Detail__c) nSObj;
                    Consum_Apply_Equipment_Set_Detail__c oObj = null;
                    if (Trigger.isUpdate) {
                        oObj = oldMap.get(nObj.Id);
                    }
                    if (Trigger.isUpdate && String.isNotBlank(nObj.DeliverySlip__c)
                            && String.isNotBlank(oObj.DeliverySlip__c) && oObj.DeliverySlip__c != nObj.DeliverySlip__c) {
                        nObj.addError('不能修改借出备品配套明细的运输单');
                    }
                    else if (Trigger.isUpdate && String.isNotBlank(nObj.Return_DeliverySlip__c )
                            && String.isNotBlank(oObj.Return_DeliverySlip__c ) && oObj.Return_DeliverySlip__c  != nObj.Return_DeliverySlip__c ) {
                        nObj.addError('不能修改借出备品配套明细的回寄运输单');
                    }
                    else {
                        this.newList.add(nObj);
                        this.newMap.put(nObj.Id, nObj);
                    }
                }
            }
        }
        else {
            // insert
            this.newMap = (Map<Id, Consum_Apply_Equipment_Set_Detail__c>) Trigger.newMap;
            this.oldMap = (Map<Id, Consum_Apply_Equipment_Set_Detail__c>) Trigger.oldMap;
            this.newList = (List<Consum_Apply_Equipment_Set_Detail__c>) Trigger.new;
            this.oldList = (List<Consum_Apply_Equipment_Set_Detail__c>) Trigger.old;
        }
    }
    protected override void beforeInsert() {
        beforeSetValue();
    }
    protected override void beforeUpdate() {
        setcalendarMap();
        beforeSetValue();
    }
 
    private void setcalendarMap() {
        Set<Date> dSet = new Set<Date>();
        for (Consum_Apply_Equipment_Set_Detail__c nObj : newList) {
            Consum_Apply_Equipment_Set_Detail__c oObj = oldMap.get(nObj.Id);
            if (oObj.Consum_Start_Date__c != nObj.Consum_Start_Date__c
                && nObj.Consum_Start_Date__c != null
                && calendarMap.containsKey(nObj.Consum_Start_Date__c) == false) {
                dSet.add(nObj.Consum_Start_Date__c);
            }
        }
        if (dSet.size() > 0) {
            List<OlympusCalendar__c> cList = [SELECT Id
                , Date__c
                , IsWorkDay__c
                , After_1_WorkDay__c
                , After_2_WorkDay__c
                , After_3_WorkDay__c
                , After_4_WorkDay__c
                , After_5_WorkDay__c
                , After_6_WorkDay__c
                , After_7_WorkDay__c
                , After_8_WorkDay__c
                , After_9_WorkDay__c
                , After_10_WorkDay__c
                , After_11_WorkDay__c
                , After_12_WorkDay__c
                , After_13_WorkDay__c
                , After_14_WorkDay__c
                , After_15_WorkDay__c
                , After_16_WorkDay__c
                , After_17_WorkDay__c
                , After_18_WorkDay__c
                , After_19_WorkDay__c
                , After_20_WorkDay__c
                , After_21_WorkDay__c
                , After_22_WorkDay__c
                , After_23_WorkDay__c
                , After_24_WorkDay__c
                , After_25_WorkDay__c
                , After_26_WorkDay__c
                , After_27_WorkDay__c
                , After_28_WorkDay__c
                , After_29_WorkDay__c
                , After_30_WorkDay__c
                , Before_1_WorkDay__c
                , Before_2_WorkDay__c
                , Before_3_WorkDay__c
                , Before_4_WorkDay__c
                , Before_5_WorkDay__c
                , Before_6_WorkDay__c
                , Before_7_WorkDay__c
                , Before_8_WorkDay__c
                , Before_9_WorkDay__c
                , Before_10_WorkDay__c
                , Before_11_WorkDay__c
                , Before_12_WorkDay__c
                , Before_13_WorkDay__c
                , Before_14_WorkDay__c
                , Before_15_WorkDay__c
                , Before_16_WorkDay__c
                , Before_17_WorkDay__c
                , Before_18_WorkDay__c
                , Before_19_WorkDay__c
                , Before_20_WorkDay__c
                , Before_21_WorkDay__c
                , Before_22_WorkDay__c
                , Before_23_WorkDay__c
                , Before_24_WorkDay__c
                , Before_25_WorkDay__c
                , Before_26_WorkDay__c
                , Before_27_WorkDay__c
                , Before_28_WorkDay__c
                , Before_29_WorkDay__c
                , Before_30_WorkDay__c
             FROM OlympusCalendar__c
            WHERE Date__c = :dSet
            ];
            System.debug(dSet);
            System.debug(cList);
            for (OlympusCalendar__c calender: cList) {
                calendarMap.put(calender.Date__c, calender);
            }
        }
    }
 
    protected override void afterInsert() {
        setassMap();
        formulaToTextCheck();
        //入力規則 新建数据不能分配がありますので,ここではLastとOut_of_wh__cの設定必要ありません
 
        Set<Id> raesdAssignSet = new Set<Id>();
        for (Consum_Apply_Equipment_Set_Detail__c nObj : newList) {
            if (nObj.Select_Time__c != null) {
                if (String.isNotBlank(nObj.Consum_Apply__c)) {
                    raesdAssignSet.add(nObj.Consum_Apply__c);
                }
            }
        }
        if (raesdAssignSet.size() > 0) {
            // 设置分配人
            setAssginPerson(raesdAssignSet);
        }
        changeAssetCount();
        if (!assUpdMap.isEmpty()) {
            System.debug(assUpdMap);
            update assUpdMap.values();
        }
    }
 
    private void setassMap() {
        Set<Id> assIds = new Set<Id>();
        List<Consum_Apply_Equipment_Set_Detail__c> caesdList = new List<Consum_Apply_Equipment_Set_Detail__c>();
        if (Trigger.isDelete) {
            caesdList = oldList;
        }
        else {
            caesdList = newList;
        }
        for (Consum_Apply_Equipment_Set_Detail__c raesd : caesdList) {
            if (String.isNotBlank(raesd.Asset__c)) {
                assIds.add(raesd.Asset__c);
            }
        }
        if (!assIds.isEmpty()) {
            assMap = new Map<Id, Asset>([
                    Select Id, Quantity, Status, Manage_type__c,
                           Out_of_wh__c, Rental_Count__c, Abandoned_Inventory__c, Abandoned_RealThing__c,
                           Confirm_Lost_Count__c, Consumed_Count__c
                    From Asset
                    Where Id =: assIds FOR UPDATE]);
        }
    }
 
    protected override void afterDelete() {
        setassMap();
        deleteReFirst();
        //排队顺有变化的时候重新设置排队顺
        reQueueNumber();
        changeAssetCount();
        if (!assUpdMap.isEmpty()) {
            update assUpdMap.values();
        }
 
    }
    protected override void afterUpdate() {
        setassMap();
        // 备品数量的加减
        changeAsset();
        // 排队再分配 借出分配数量的减 等, clear Last_Reserve_RAES_Detail__c, 設定First_RAESD__c
        // clearLastReserveRAESD();
        //Asset变更的时候 更新Asset的 Last_Reserve_RAES_Detail__c
        // changeAssetLast();
        // Asset.Out_of_wh__c+ -
        changeAssetCount();
        // change Consumable_Guaranteen_end__c OLY_OCM-601
        changeAssetConsumable_Guaranteen_end();
        // 同じblock の 排队レコード Queue_Number__c をrefresh
        // refreshQueueNumberByBlock();
        if (!assUpdMap.isEmpty()) {
            System.debug(assUpdMap);
            update assUpdMap.values();
        }
        // 4-XX 的 一对一对应
        // oneToOne4XX();
        // キャンセルコピー, 一览单位取消后的拷贝在一览的Handler里面做
        // cancelCopy();
        // before では数式項目がnullの場合があります
        formulaToTextCheck();
        //排队顺有变化的时候重新设置排队顺
        // reQueueNumber();
        //申请者收回NG后明细回寄时的邮件回寄单内容设置
        receivedConfirmNGSetReturnDeliverySlipText();
        Set<Id> raesdSet = new Set<Id>();
        Set<Id> raesdAssignSet = new Set<Id>();
        for (Consum_Apply_Equipment_Set_Detail__c nObj : newList) {
            Consum_Apply_Equipment_Set_Detail__c oObj = oldMap.get(nObj.Id);
            if (String.isBlank(oObj.Return_DeliverySlip__c) && String.isNotBlank(nObj.Return_DeliverySlip__c)) {
                raesdSet.add(nObj.Id);
            }
            if (nObj.Select_Time__c != null && oObj.Select_Time__c == null) {
                raesdAssignSet.add(nObj.Consum_Apply__c);
            }
        }
        System.debug(raesdSet);
        if (raesdSet.size() > 0) {
            // 设置回寄通知内容
            ConsumApplyEquipmentSetDetailHandler.setRequestAsset_return_Text(raesdSet);
        }
 
        if (raesdAssignSet.size() > 0) {
            // 设置分配人
            setAssginPerson(raesdAssignSet);
        }
    }
 
    private void beforeSetValue() {
        Set<Id> raesdSet = new Set<Id>();
        for (Consum_Apply_Equipment_Set_Detail__c nObj : newList) {
            raesdSet.add(nObj.Consum_Apply_Equipment_Set__c);
        }
        Map<Id, Consum_Apply_Equipment_Set__c> raesdMap = new Map<Id, Consum_Apply_Equipment_Set__c>([Select Id, Model_No__c , Name ,
                IndexFromUniqueKey__c
                From Consum_Apply_Equipment_Set__c
                Where Id =: raesdSet]);
        for (Consum_Apply_Equipment_Set_Detail__c nObj : newList) {
            //因为是Insert时候设值  情况1,申请新建会设值  情况2,NG拷贝出来的时候会设值
            //因为UniqueKey__c判断了DataMigration_Flag__c所以这里也需要判断  检索的时候因为要假Insert所以不需要设置
            // TODO bp3 DetailHandler里, 对FSD设值的地方 需要在Consum_Apply__c的Handler里, 变成批准時, 也要设值。
            if (Trigger.isInsert && nObj.DataMigration_Flag__c == false) {
                //nObj.Is_Body__c = nObj.Is_Body_F__c;
                if (String.isNotBlank(nObj.Fixture_Set_Detail__c)) {
                    nObj.FSD_Id__c = nObj.Fixture_Set_Detail__c;
                }
                if (nObj.Consum_Start_Date__c == null) {
                    nObj.Consum_Start_Date__c = nObj.Consum_Start_Date_F__c;
                }
 
                if (nObj.Consum_Can_Request_approval_Date__c == null) {
                    nObj.Consum_Can_Request_approval_Date__c = nObj.Consum_Can_Request_approval_Date_F__c;
                }
 
                if (nObj.Consum_Start_Date_After_15_Day__c == null) {
                    nObj.Consum_Start_Date_After_15_Day__c = nObj.Consum_Start_Date_After_15_Day_F__c;
                }
                // 272チケット
                // Create_State__c为空并且申请书的标准批准时间不为空的时候设置为1
                // 1 --- 追加附属品
                // 0 --- Cancel拷贝
                // null --- 普通新规
                if (nObj.Create_State__c == null
                        && nObj.Consum_Apply_Request_approval_time_F__c != null
                        //&& nObj.Substitute_Select_Again__c == false) {     // OLY_OCM-404 分配代替品
                ) {
                    nObj.Create_State__c = 1;
                }
                //nObj.FSD_Is_Optional__c = nObj.FSD_Is_Optional_F__c;
                //nObj.FSD_Is_OneToOne__c = nObj.FSD_Is_OneToOne_F__c;
                //nObj.FSD_OneToOneAccessory_Cnt__c = nObj.FSD_OneToOneAccessory_Cnt_F__c;
                nObj.FSD_Fixture_Model_No__c = nObj.Fixture_Model_No_F__c;
                if (nObj.DataMigration_Flag__c == false && (!Trigger.isUpdate || String.isNotBlank(nObj.Asset__c))) {
                    nObj.Fixture_Model_No_text__c = nObj.Fixture_Model_No_F__c;
                    nObj.FSD_Name_CHN__c    = nObj.Fixture_Name_F__c;
                }
                nObj.CreatedBy_ProfileId__c = UserInfo.getProfileId();
//              nObj.ApplyPersonAppended__c = nObj.ApplyPersonAppended_F__c;
            }
            if (nObj.DeliverySlip__c != null
                    && nObj.DataMigration_Flag__c == false && (!Trigger.isUpdate || String.isNotBlank(nObj.Asset__c))) {
                if (nObj.Shippment_loaner_time__c == null) {
                    nObj.Shippment_loaner_time__c = nObj.Shippment_loaner_time2__c;
                }
            }
            nObj.DeliverySlip_Text__c = nObj.DeliverySlip__c;
            nObj.Return_DeliverySlip_Text__c = nObj.Return_DeliverySlip__c;
            nObj.Received_Confirm_Text__c = nObj.Received_Confirm__c;
            Consum_Apply_Equipment_Set_Detail__c oObj = null;
            if (Trigger.isUpdate) {
                oObj = oldMap.get(nObj.Id);
                if(oObj.Received_Confirm__c != nObj.Received_Confirm__c){
                    if(String.isNotBlank(nObj.Received_Confirm__c)){
                        nObj.Loaner_received_time__c = System.Now();
                    }
                    else{
                        nObj.Loaner_received_time__c = null;
                    }
                }
                if (oObj.Asset_Center_Confirm__c != nObj.Asset_Center_Confirm__c
                    && String.isNotBlank(nObj.Asset_Center_Confirm__c)) {
                    nObj.Asset_Center_Confirm_Time__c = System.Now();
                    nObj.Asset_Center_Confirm_Staff__c = UserInfo.getUserId();
                }
                if (oObj.Consum_Start_Date__c != nObj.Consum_Start_Date__c) {
                    if (nObj.Consum_Start_Date__c == null) {
                        nObj.Consum_Can_Request_approval_Date__c = null;
                        nObj.Consum_Start_Date_After_15_Day__c = null;
                    }
                    else {
                        Date d;
                        Date d1;
                        System.debug(calendarMap);
                        System.debug(nObj.Consum_Start_Date__c);
                        if (calendarMap.get(nObj.Consum_Start_Date__c).Before_22_WorkDay__c != null) {
                            d = calendarMap.get(nObj.Consum_Start_Date__c).Before_22_WorkDay__c;
                        }
                        else {
                            d = Consum_ApplyUtil.getWD_addday(nObj.Consum_Start_Date__c, -22);
                        }
 
                        if (calendarMap.get(nObj.Consum_Start_Date__c).After_15_WorkDay__c != null) {
                            d1 = calendarMap.get(nObj.Consum_Start_Date__c).After_15_WorkDay__c;
                        }
                        else {
                            d1 = Consum_ApplyUtil.getWD_addday(nObj.Consum_Start_Date__c, 15);
                        }
 
                        nObj.Consum_Start_Date_After_15_Day__c = d1;
                        nObj.Consum_Can_Request_approval_Date__c = d;
                    }
                }
 
                //出库前检查NGの場合キャンセルします
                //if(oObj.Inspection_result__c != 'NG' && nObj.Inspection_result__c == 'NG') {
                //    nObj.Cancel_Select__c = true;
                //    nObj.Cancel_Reason__c = '重新分配';
                //    nObj.Loaner_cancel_Remarks__c = '出库前检查NG';
                //}
                // OLY_OCM-435対応 start
                String userid = UserInfo.getUserId();
                DateTime now = System.now();
                if (oObj.Inspection_result__c != nObj.Inspection_result__c && String.isNotBlank(nObj.Inspection_result__c)) {
                    nObj.Pre_inspection_time__c = now;
                    nObj.Inspection_staff__c = userid;
                }
 
                if (oObj.Check_lost_Item_Final__c != nObj.Check_lost_Item_Final__c && String.isNotBlank(nObj.Check_lost_Item_Final__c)) {
                    nObj.Lost_item_check_time_Final__c = now;
                    nObj.Lost_item_check_staff_Final__c = userid;
                }
 
                if (oObj.Check_lost_Item__c != nObj.Check_lost_Item__c && String.isNotBlank(nObj.Check_lost_Item__c)) {
                    nObj.Lost_item_check_time__c = now;
                    nObj.Lost_item_check_staff__c = userid;
                }
 
                //if (oObj.CDS_complete__c != nObj.CDS_complete__c && nObj.CDS_complete__c) {
                //    nObj.CDS_complete_time__c = now;
                //    nObj.CDS_staff__c = userid;
                //}
 
                if (oObj.Inspection_result_after_Final__c != nObj.Inspection_result_after_Final__c && String.isNotBlank(nObj.Inspection_result_after_Final__c)) {
                    nObj.After_Inspection_time_Final__c = now;
                    nObj.Inspection_staff_After_Final__c = userid;
                }
 
                if (oObj.Inspection_result_after__c != nObj.Inspection_result_after__c && String.isNotBlank(nObj.Inspection_result_after__c)) {
                    nObj.After_Inspection_time__c = now;
                    nObj.Inspection_staff_After__c = userid;
                }
 
                if (String.isNotBlank(nObj.Picture1__c) || String.isNotBlank(nObj.Picture2__c)) {
                    nObj.Has_Picture__c = true;
                }
                else{
                    nObj.Has_Picture__c = false;
                }
                // OLY_OCM-435対応 end
            }
            Consum_Apply_Equipment_Set__c raes = raesdMap.get(nObj.Consum_Apply_Equipment_Set__c);
            //画面显示用clone出来的数据不需要设置Name
            //IndexFromUniqueKey__c.format()会报错 要判不是Null
            if (nObj.DataMigration_Flag__c == false
                    && raes != null
                    //&& raes.IndexFromUniqueKey__c != null
                    && nObj.IndexFromUniqueKey__c != null
                    //&& (!Trigger.isUpdate || String.isNotBlank(nObj.Fixture_Set_Id__c))) 
                ) {
                String split_ApplyNum = nObj.RequestNoJoinStr2__c;
                if (nObj.Consum_Apply_r_Name__c.contains('_')) {
                    // XXXX-YYYY_1 (ZZZZ_1)
                    List<String> split_ApplyNumList = nObj.Consum_Apply_r_Name__c.split('_');
                    String raName = split_ApplyNumList[split_ApplyNumList.size() - 2];      // XXXX-YYYY (ZZZZ)
                    split_ApplyNum = split_ApplyNumList[split_ApplyNumList.size() - 1];     // 1
                    split_ApplyNumList = raName.split('-');     // [XXXX, YYYY]  ([ZZZZ])
                    if (split_ApplyNumList.size() > 1) {
                        // YYYY_1
                        split_ApplyNum = split_ApplyNumList[split_ApplyNumList.size() - 1] + '_' + split_ApplyNum;
                    } else {
                        // ZZZZ_1
                        split_ApplyNum = nObj.Consum_Apply_r_Name__c;
                    }
                }
                // ToDo 确认以下命名规则是否可以
                nObj.Name = split_ApplyNum + ':' + raes.Model_No__c + ':'
                        + nObj.IndexFromUniqueKey__c.format().leftpad(3, '0');
 
                // 如果是分配代替品名字加:Sub
                //if (nObj.Substitute_Select_Again__c) {
                //    nObj.Name += ':Sub';
                //}
 
                //一览时申请者收货NG的话名字添加NG
                if (nObj.Received_Confirm__c == 'NG') {
                    nObj.Name += ':NG';
                }
                //取消的时候名字加Canceled
                if (nObj.Cancel_Select__c) {
                    nObj.Name += ':Canceled';
                }
            }
            nObj.Loaner_accsessary__c = nObj.Loaner_accsessary_F__c;
            nObj.Loaner_centre_mail_address__c = nObj.Loaner_centre_mail_address_F__c;
            // 暂定分配或保有设备更新时, 设值
            if (Trigger.isInsert || (oObj.Asset__c != nObj.Asset__c )
                || (oObj.Select_Time__c != nObj.Select_Time__c)) {
                // 一对一保管主体(借出时)
                if (nObj.DataMigration_Flag__c == false && (!Trigger.isUpdate)) {
                    // 备品配套明细型号(借出时)
                    if (String.isBlank(nObj.Fixture_Model_No_F__c)) {
                        nObj.addError('分配的Asset:'+ nObj.Asset__c + ', 没有设定 备品配套明细型号');
                    } else {
                        nObj.Fixture_Model_No_text__c = nObj.Fixture_Model_No_F__c;
                    }
                }
                if (nObj.Select_Time__c == null
                        && (Trigger.isInsert || (oObj.Asset__c != nObj.Asset__c ))
                ) {
                    if (nObj.Asset__c != null) {
                        nObj.Provisional_Select_Time__c = Datetime.now();
                    }
                    else {
                        nObj.Provisional_Select_Time__c = null;
                    }
                }
 
                // 机身编号(耗材时)
                nObj.SerialNumber_text__c = nObj.SerialNumber__c;
                // 所在地区(省) 耗材时 #OLY_OCM-654:因为是赋值所以不修改
                nObj.SalesProvince_before__c = nObj.SalesProvince__c;
                // 所在地区(本部) 耗材时 #OLY_OCM-654:因为是赋值所以不修改
                nObj.Salesdepartment_before__c = nObj.Salesdepartment__c;
                // 产品分类(GI/SP)(耗材时) #OLY_OCM-654:因为是赋值所以不修改
                nObj.Product_category_text__c = nObj.Product_category_F__c;
                // 备品分类(耗材时) #OLY_OCM-654:因为是赋值所以不修改
                nObj.Equipment_Type_text__c = nObj.Equipment_Type_F__c;
                // 备品成本(耗材时)
                nObj.Asset_cost_del_before__c = nObj.Asset_cost_del__c;
                // 备品存放地(耗材时)
                nObj.Internal_asset_location_before__c = nObj.Internal_asset_location__c;
                //备品名称(耗材时)
                nObj.Fixture_Name_text__c = nObj.Fixture_Name_F__c;
                // OLY_OCM-243 追加字段对应 备品管理编码(耗材时)
                nObj.EquipmentSet_Managment_Code_text__c = nObj.EquipmentSet_Managment_Code__c;
                //// OLY_OCM-452 追加字段对应 刀头(借出时)
                //if (String.isNotBlank(nObj.Fixture_Set_Detail__c)) {
                //    nObj.Is_Special_Product_Text__c = nObj.Is_Special_Product__c;
                //}
            }
            // 排队时, 要注意 设值 移动到排队btn里
            //else if (Trigger.isUpdate && String.isBlank(nObj.Asset__c) && oObj.Cancel_Select__c == false
            //        // && oObj.Fixture_Model_No_text__c != nObj.Fixture_Model_No_text__c
            //        && (oObj.Queue_Number__c == 0 || oObj.Queue_Number__c == null) && nObj.Queue_Number__c > 0
            //) {
                // 排队时重新赋值Fixture_Model_No_text__c 为了排的不是旧Model_No
                //nObj.FSD_Fixture_Model_No__c = nObj.Fixture_Model_No_F__c;
                // 备品配套明细型号(借出时)
                // nObj.Fixture_Model_No_text__c = nObj.Fixture_Model_No_F__c;
                // 所在地区(省) 借出时
                //nObj.SalesProvince_before__c = nObj.SalesProvince__c;
                // // 所在地区(本部) 借出时
                // nObj.Salesdepartment_before__c = nObj.Salesdepartment__c;
                // // 产品分类(GI/SP)(借出时)
                // nObj.Product_category_text__c = nObj.Product_category_F__c;
                // // 备品分类(借出时)
                // nObj.Equipment_Type_text__c = nObj.Equipment_Type__c;
                // // 备品存放地(借出时)
                // nObj.Internal_asset_location_before__c = nObj.Internal_asset_location__c;
            //}
            // 其他时候(申请时), null设值 和
            // 一对一已分配的附属品,主体重新排队时,一对一附属品需要重新按照申请的逻辑重新赋值
            else if ((Trigger.isInsert && nObj.Cancel_Select__c == false)
                    || (Trigger.isUpdate && String.isBlank(nObj.Asset__c) && oObj.Cancel_Select__c == false)) {
                // 备品配套明细型号(耗材时)
                if (String.isBlank(nObj.Fixture_Model_No_text__c) 
                        && nObj.DataMigration_Flag__c == false && (!Trigger.isUpdate || String.isNotBlank(nObj.Asset__c))) {
                    nObj.Fixture_Model_No_text__c = nObj.Fixture_Model_No_F__c;
                }
                // 所在地区(省) 耗材时 #OLY_OCM-654:因为是赋值所以不修改
                if (String.isBlank(nObj.SalesProvince_before__c)) {
                    nObj.SalesProvince_before__c = nObj.SalesProvince__c;
                }
                // 所在地区(本部) 耗材时 #OLY_OCM-654:因为是赋值所以不修改
                if (String.isBlank(nObj.Salesdepartment_before__c)) {
                    nObj.Salesdepartment_before__c = nObj.Salesdepartment__c;
                }
                // 产品分类(GI/SP)(耗材时) #OLY_OCM-654:因为是赋值所以不修改
                if (String.isBlank(nObj.Product_category_text__c)) {
                    nObj.Product_category_text__c = nObj.Product_category_F__c;
                }
                // 备品分类(耗材时) #OLY_OCM-654:因为是赋值所以不修改
                if (String.isBlank(nObj.Equipment_Type_text__c)) {
                    nObj.Equipment_Type_text__c = nObj.Equipment_Type__c;
                }
                // 备品存放地(耗材时)
                if (String.isBlank(nObj.Internal_asset_location_before__c)) {
                    nObj.Internal_asset_location_before__c = nObj.Internal_asset_location__c;
                }
                // 备品名称(耗材时)
                if (String.isBlank(nObj.Fixture_Name_text__c)) {
                    nObj.Fixture_Name_text__c = nObj.Fixture_Name_F__c;
                }
                // OLY_OCM-243 追加字段对应 备品管理编码(耗材时)
                if (String.isBlank(nObj.EquipmentSet_Managment_Code_text__c)) {
                    nObj.EquipmentSet_Managment_Code_text__c = nObj.EquipmentSet_Managment_Code__c;
                }
                //// OLY_OCM-452 追加字段对应 刀头(借出时)
                //if (String.isBlank(nObj.Is_Special_Product_Text__c)) {
                //    nObj.Is_Special_Product_Text__c = nObj.Is_Special_Product__c;
                //}
            } else {
                // 取消的情况在下面有设值
            }
            if (nObj.DataMigration_Flag__c == false && nObj.Cancel_Select__c == false
                    && (!Trigger.isUpdate || String.isNotBlank(nObj.Asset__c))) {
                // if (String.isBlank(nObj.Fixture_Model_No_text__c)) {
                //     nObj.addError(nObj.Id + ':备品配套明细型号不能为空, 备品配套明细=' + nObj.Fixture_Set_Detail__c);
                // }
 
                // #OLY_OCM-654 数量管理的话,所在地区(本部)&产品分类&备品分类的判断不需要 Start
                // if ('数量管理' == nObj.Manage_type_F__c ) {  // || false == nObj.Loaner_accsessary__c TODO: 需确认
                    if (String.isBlank(nObj.Internal_asset_location_before__c)) {
                        nObj.addError('备品存放地不能为空');
                    // }
                // }
                // else {
                // #OLY_OCM-654 数量管理的话,所在地区(本部)&产品分类&备品分类的判断不需要 end
                    // if (String.isBlank(nObj.Salesdepartment_before__c)) {
                    //     nObj.addError('所在地区(本部)不能为空');
                    // }
                    // if (String.isBlank(nObj.Internal_asset_location_before__c)) {
                    //     nObj.addError('备品存放地不能为空');
                    // }
                    // if (String.isBlank(nObj.Product_category_text__c) && nObj.Is_Body__c) {
                    //     nObj.addError('产品分类(GI/SP)不能为空');
                    // }
                    // if (String.isBlank(nObj.Equipment_Type_text__c) && nObj.Is_Body__c && nObj.Demo_purpose1__c != '其他') {
                    //     nObj.addError('备品分类不能为空');
                    // }
                    // if (String.isBlank(nObj.SalesProvince_before__c) && nObj.Is_Body__c) {
                    //     nObj.addError('所在地区(省)不能为空');
                    // }
                }
            }
            if (nObj.DataMigration_Flag__c == false) {
                //出库指示更新明细的key
                nObj.UniqueKey__c = nObj.RequestNoJoinStr2__c + ':'+ nObj.Consum_Apply_Equipment_Set__c
                        + ':' + nObj.Fixture_Model_No__c + ':' + nObj.Degree_Of_Importance__c;
                System.debug(nObj.Degree_Of_Importance__c);
                if (nObj.Cancel_Select__c) {
                    nObj.UniqueKey__c += nObj.Id;
                    // 下架后的取消加;
                    if (nObj.StockDown__c) {
                       nObj.UniqueKey__c += ';' + nObj.Id;
                    }
                    // nObj.Queue_Number__c = null;
                    if (nObj.StockDown__c && //nObj.Inspection_result__c != 'NG' && 
                        String.isBlank(nObj.DeliverySlip__c)) {
                        nObj.OnStock_By_Cancel__c = true;
//                      nObj.Inspection_result_after_Flag_Text__c = 'true';
                    }
                }
            }
            System.debug(nObj.Degree_Of_Importance__c + 'key is' + nObj.UniqueKey__c);
            // 取消分配
            //Action 18 によってキャンセルの場合
            //借出备品Set一览明细.保有设备(Link)
            //借出备品Set一览明细.分配时间
            //借出备品Set一览明细.已做出库指示
            //借出备品Set一览明细.出库指示时间
            if (Trigger.isInsert
                    || oObj.Cancel_Select__c != nObj.Cancel_Select__c
                    || (oObj.Asset__c != nObj.Asset__c && String.isBlank(nObj.Asset__c))) {
                if (nObj.Cancel_Select__c || String.isBlank(nObj.Asset__c)) {
                    if(nObj.Cancel_Select__c && nObj.NG_Select_Again__C == false){
                        nObj.Cancel_Date__c = System.today();
                        nObj.Cancel_Time__c = MainFixtureSelectController.getCurrentTime();
                        nObj.Cancel_Mem__c = UserInfo.getUserId();
                    }
                    if (nObj.StockDown__c == false) {
                       nObj.Asset__c = null;
                       nObj.Select_Time__c = null;
                       nObj.Shipment_request_time2__c = null;
                       nObj.Shipment_request__c = false;
                       // 根据OLY_OCM-243记载,取消也需要清除接借出时相关的字段
                       if (nObj.DataMigration_Flag__c == false && (!Trigger.isUpdate || String.isNotBlank(nObj.Asset__c))) {
                           // 备品配套明细型号(借出时)
                           nObj.Fixture_Model_No_text__c = nObj.FSD_Fixture_Model_No__c;
                       }
                       // 机身编号(借出时)
                       nObj.SerialNumber_text__c = null;
                       // 所在地区(省) 借出时 #OLY_OCM-654:因为是赋值所以不修改
                       nObj.SalesProvince_before__c = null;
                       // 所在地区(本部) 借出时 #OLY_OCM-654:因为是赋值所以不修改
                       nObj.Salesdepartment_before__c = null;
                       // 产品分类(GI/SP)(借出时) #OLY_OCM-654:因为是赋值所以不修改
                       nObj.Product_category_text__c = null;
                       // 备品分类(借出时) #OLY_OCM-654:因为是赋值所以不修改
                       nObj.Equipment_Type_text__c = null;
                       // 备品成本(借出时)
                       nObj.Asset_cost_del_before__c = null;
                       // 备品存放地(借出时)
                       nObj.Internal_asset_location_before__c = null;
                       //备品名称(借出时)
                       nObj.Fixture_Name_text__c = nObj.FSD_Name_CHN__c;
                       // 备品管理编码(借出时)
                       nObj.EquipmentSet_Managment_Code_text__c = null;
                    }
                } else {
                    nObj.Cancel_Date__c = null;
                    nObj.Cancel_Time__c = null;
                    nObj.Cancel_Mem__c = null;
                    //nObj.Cancel_Reason__c = null;
                }
            }
            if (oObj != null
                && oObj.Select_Time__c != nObj.Select_Time__c) {
                if (nObj.Select_Time__c != null) {
                    nObj.Consum_Assign_Person__c = UserInfo.getUserId();
                }
                else {
                    nObj.Consum_Assign_Person__c = null;
                }
            }
            // 归还后CDS
            //if (Trigger.isInsert || oObj.CDS_complete__c != nObj.CDS_complete__c) {
            //    if (nObj.CDS_complete__c) {
            //        nObj.CDS_complete_time__c = System.now();
            //        nObj.CDS_staff__c = UserInfo.getUserId();
            //    } else {
            //        nObj.CDS_complete_time__c = null;
            //        nObj.CDS_staff__c = null;
            //    }
            //}
            System.debug(nObj.Return_Status_F__c);
            // 必ず最後で置く
            //効かないの場合がありますのでAfterでやります
            // nObj.Repair_Status_Text__c = nObj.Repair_Status_F__c;
            // nObj.Return_Status_Text__c = nObj.Return_Status_F__c;
            // nObj.Shipment_Status_Text__c = nObj.Shipment_Status_F__c;
            nObj.Canceled_Id__c = nObj.Canceled__c;
            //if (Trigger.isUpdate
            //    && ((oObj.Inspection_result_after_Final__c != 'NG'
            //            && nObj.Inspection_result_after_Final__c == 'NG'
            //            && nObj.Inspection_result_after_NG_Final__c == '维修')
            //        || (oObj.Inspection_result_after__c != 'NG'
            //            && nObj.Inspection_result_after__c == 'NG'
            //            && nObj.Inspection_result_after_NG__c == '维修')
            //    )
            //) {
            //    nObj.Repair__c = null;
            //}
 
            // OLY_OCM-531 Start 搬工作流规则
            // if (nObj.Check_lost_Item_F__c == '欠品'
            //        && nObj.Loaner_Giveup_Time__c == null
            //        && nObj.Lost_item_giveup__c == true) {
            //    nObj.Loaner_Giveup_Time__c = Datetime.now();
            // }
 
            //if (nObj.RAESD_Status__c == '排队中'
            //        && nObj.Queue_Day_Text__c == null) {
                //nObj.Queue_Day_Text__c = nObj.Queue_Day__c;
            //    nObj.Queue_Time_Text__c = nObj.Queue_Time__c;
            //}
 
            // OLY_OCM-531 End
            if (Trigger.isUpdate
                   &&oObj.Confirm_Lost_Date__c != null
                   && oObj.Check_lost_Item_F__c != nObj.Check_lost_Item_F__c
                   && oObj.Check_lost_Item_F__c == '欠品') {
               nObj.Confirm_Lost_Date__c = null;
            }
 
            if (Trigger.isUpdate
                    && (oObj.Received_NG_Content__c != nObj.Received_NG_Content__c
                        || oObj.Received_Confirm__c != nObj.Received_Confirm__c
                        || oObj.Picture1__c != nObj.Picture1__c
                        || oObj.Picture2__c != nObj.Picture2__c)
                    ) {
                nObj.Asset_Center_Confirm__c = null;
            }
 
        }
    }
    /*
    * Asset のQuantity更新
    * changeAssetCount()方法でOut_of_wh__c、Consum_Count__c 减1
    */
    private void changeAsset() {
        if (Trigger.isUpdate && Trigger.isAfter) {
            if (assMap.isEmpty()) {
                return;
            }
            for (Consum_Apply_Equipment_Set_Detail__c nObj : newList) {
                if (!assMap.containsKey(nObj.Asset__c)) {
                    continue;
                }
                Consum_Apply_Equipment_Set_Detail__c oObj = null;
                if (Trigger.isUpdate) {
                    oObj = oldMap.get(nObj.Id);
                }
                Asset ass = assMap.get(nObj.Asset__c);
                // Abandoned_Inventory__c  null -> 0
                if (ass.Abandoned_Inventory__c == null) {
                    ass.Abandoned_Inventory__c = 0;
                }
                // Abandoned_RealThing__c  null -> 0
                if (ass.Abandoned_RealThing__c == null) {
                    ass.Abandoned_RealThing__c = 0;
                }
 
                if (ass.Confirm_Lost_Count__c == null) {
                    ass.Confirm_Lost_Count__c = 0;
                }
 
                if (ass.Consumed_Count__c == null) {
                    ass.Consumed_Count__c = 0;
                }
 
                if (String.isBlank(oObj.Check_lost_Item_F__c) && nObj.Check_lost_Item_F__c == 'OK') {
                   //未入力から欠品に変更時何もしません
                } else if (String.isBlank(oObj.Check_lost_Item_F__c) && nObj.Check_lost_Item_F__c == '欠品'
                       && nObj.Lost_item_giveup__c == false) {
                   //未入力から欠品に変更時何もしません
                } else if (String.isBlank(oObj.Check_lost_Item_F__c) && nObj.Check_lost_Item_F__c == '欠品'
                   && nObj.Lost_item_giveup__c == true) {
                   //未入力から欠品放棄に変更時 放弃欠品回收(丢失)のとき 待废弃数(丢失/盘亏)加1
                   ass.Abandoned_Inventory__c += 1;
                   if (oObj.Confirm_Lost_Date__c != null) {
                       ass.Confirm_Lost_Count__c -= 1;
                   }
                   assUpdMap.put(ass.Id, ass);
                } else 
                if (String.isBlank(oObj.Check_lost_Item_Final__c) && nObj.Check_lost_Item_Final__c == '消耗') {
                   //未入力から消耗に変更時Quantityを-1
                   if(ass.Quantity != null && ass.Quantity > 0) {
                       ass.Quantity -= 1;
                   }
                   if(ass.Consumed_Count__c == null){
                       ass.Consumed_Count__c = 0;
                   }
                   ass.Consumed_Count__c += 1;
                   ass.ChangeQuantityReason__c = '消耗';
                   assUpdMap.put(ass.Id, ass);
                } 
                else if (oObj.Check_lost_Item_F__c == 'OK' && String.isBlank(nObj.Check_lost_Item_F__c)) {
                   //OKから空白に変更時何もしません
                } else if (oObj.Check_lost_Item_F__c == 'OK' && nObj.Check_lost_Item_F__c == '欠品'
                       && nObj.Lost_item_giveup__c == false) {
                   //OKから欠品に変更時何もしません
                } else if (oObj.Check_lost_Item_F__c == 'OK' && nObj.Check_lost_Item_F__c == '欠品'
                       && nObj.Lost_item_giveup__c == true) {
                   //OKから欠品放棄に変更時 放弃欠品回收(丢失)のとき 待废弃数(丢失/盘亏)加1
                   ass.Abandoned_Inventory__c += 1;
                   if (oObj.Confirm_Lost_Date__c != null) {
                       ass.Confirm_Lost_Count__c -= 1;
                   }
                   assUpdMap.put(ass.Id, ass);
                } else if (oObj.Check_lost_Item_F__c == 'OK' && nObj.Check_lost_Item_F__c == '消耗') {
                   //OKから消耗に変更時Quantityを-1
                   ass.Quantity -= 1;
                   ass.ChangeQuantityReason__c = '消耗';
                   assUpdMap.put(ass.Id, ass);
                } else if (oObj.Check_lost_Item_F__c == '欠品' && String.isBlank(nObj.Check_lost_Item_F__c)
                       && oObj.Lost_item_giveup__c == false) {
                   if (oObj.Confirm_Lost_Date__c != null) {
                       ass.Confirm_Lost_Count__c -= 1;
                   }
                   assUpdMap.put(ass.Id, ass);
                } else if (oObj.Check_lost_Item_F__c == '欠品' && nObj.Check_lost_Item_F__c == 'OK' 
                       && oObj.Lost_item_giveup__c == false) {
                   if (oObj.Confirm_Lost_Date__c != null) {
                       ass.Confirm_Lost_Count__c -= 1;
                   }
                   assUpdMap.put(ass.Id, ass);
                } else if (oObj.Check_lost_Item_F__c == '欠品' && nObj.Check_lost_Item_F__c == '欠品'
                       && oObj.Lost_item_giveup__c == false && nObj.Lost_item_giveup__c == true) {
                   //欠品から欠品放棄に変更時 放弃欠品回收(丢失)のとき 待废弃数(丢失/盘亏)加1
                   ass.Abandoned_Inventory__c += 1;
                   if (oObj.Confirm_Lost_Date__c != null) {
                       ass.Confirm_Lost_Count__c -= 1;
                   }
                   assUpdMap.put(ass.Id, ass);
                } else if (oObj.Check_lost_Item_F__c == '欠品' && nObj.Check_lost_Item_F__c == '消耗'
                       && oObj.Lost_item_giveup__c == false) {
                   //欠品から消耗に変更時Quantityを-1
                   ass.Quantity -= 1;
                   ass.ChangeQuantityReason__c = '消耗';
                   if (oObj.Confirm_Lost_Date__c != null) {
                       ass.Confirm_Lost_Count__c -= 1;
                   }
                   assUpdMap.put(ass.Id, ass);
                } else if (oObj.Check_lost_Item_F__c == '欠品' && String.isBlank(nObj.Check_lost_Item_F__c) && oObj.Lost_item_giveup__c == true) {
                   //欠品放棄から空白に変更時 待废弃数(丢失/盘亏)减1
                   ass.Abandoned_Inventory__c -= 1;
                   ass.Out_of_wh__c += 1;
                   ass.Rental_Count__c += 1;
                   ass.ChangeQuantityReason__c = '欠品' + '->' + '';
                   assUpdMap.put(ass.Id, ass);
                } else if (oObj.Check_lost_Item_F__c == '欠品' && nObj.Check_lost_Item_F__c == 'OK' && oObj.Lost_item_giveup__c == true) {
                   //欠品放棄からOKに変更時 待废弃数(丢失/盘亏)减1
                   ass.Abandoned_Inventory__c -= 1;
                   ass.Out_of_wh__c += 1;
                   ass.Rental_Count__c += 1;
                   ass.ChangeQuantityReason__c = '欠品' + '->' + 'OK';
                   assUpdMap.put(ass.Id, ass);
                } else if (oObj.Check_lost_Item_F__c == '欠品' && nObj.Check_lost_Item_F__c == '欠品'
                       && oObj.Lost_item_giveup__c == true && nObj.Lost_item_giveup__c == false) {
                   //欠品放棄から欠品に変更時 待废弃数(丢失/盘亏)减1
                   ass.Abandoned_Inventory__c -= 1;
                   ass.Out_of_wh__c += 1;
                   ass.Rental_Count__c += 1;
                   ass.ChangeQuantityReason__c = '欠品放弃 -> ' + '欠品';
                   assUpdMap.put(ass.Id, ass);
                } else if (oObj.Check_lost_Item_F__c == '欠品' && nObj.Check_lost_Item_F__c == '消耗' && oObj.Lost_item_giveup__c == true) {
                   //欠品放棄から消耗に変更時 待废弃数(丢失/盘亏)减1 Quantityを-1
                   ass.Abandoned_Inventory__c -= 1;
                   ass.Quantity -= 1;
                } else if (oObj.Check_lost_Item_F__c == '消耗' && String.isBlank(nObj.Check_lost_Item_F__c)) {
                   //消耗から空白に変更時Quantityを+1
                   ass.Quantity += 1;
                   ass.Out_of_wh__c += 1;
                   ass.Rental_Count__c += 1;
                   ass.ChangeQuantityReason__c = '消耗 -> ' + '';
                   assUpdMap.put(ass.Id, ass);
                } else if (oObj.Check_lost_Item_F__c == '消耗' && nObj.Check_lost_Item_F__c == 'OK') {
                   //消耗から空白に変更時Quantityを+1
                   ass.Quantity += 1;
                   ass.Out_of_wh__c += 1;
                   ass.Rental_Count__c += 1;
                   ass.ChangeQuantityReason__c = '消耗 -> ' + 'OK';
                   assUpdMap.put(ass.Id, ass);
                } else if (oObj.Check_lost_Item_F__c == '消耗' && nObj.Check_lost_Item_F__c == '欠品'
                       && nObj.Lost_item_giveup__c == false) {
                   //消耗から欠品に変更時Quantityを+1
                   ass.Quantity += 1;
                   ass.Out_of_wh__c += 1;
                   ass.Rental_Count__c += 1;
                   ass.ChangeQuantityReason__c = '消耗 -> 欠品';
                   assUpdMap.put(ass.Id, ass);
                } else if (oObj.Check_lost_Item_F__c == '消耗' && nObj.Check_lost_Item_F__c == '欠品'
                       &&  nObj.Lost_item_giveup__c == true) {
                   //消耗から欠品放棄に変更時 待废弃数(丢失/盘亏)+1 Quantityを+1
                   ass.Abandoned_Inventory__c += 1;
                   ass.Quantity += 1;
                }
 
                //废弃
                if (oObj.Arrival_in_wh__c == false && nObj.Arrival_in_wh__c == true
                       && (nObj.Inspection_result_after_NG_F__c == '废弃'
                           //出库前检测废弃的时候也需要判断
                           || nObj.Inspection_result_NG__c == '废弃')) {
                   ass.Abandoned_RealThing__c += 1;
                   ass.ChangeQuantityReason__c = FixtureUtil.raesdStatusMap.get(FixtureUtil.HistoryStatus.Dai_Fei_Qi.ordinal());
                   assUpdMap.put(ass.Id, ass);
                } else if (oObj.Arrival_in_wh__c == true && nObj.Arrival_in_wh__c == false
                       && (oObj.Inspection_result_after_NG_F__c == '废弃'
                           //出库前检测废弃的时候也需要判断
                           || oObj.Inspection_result_NG__c == '废弃')) {
                   ass.Abandoned_RealThing__c -= 1;
                   ass.Out_of_wh__c += 1;
                   ass.Rental_Count__c += 1;
                   // ass.Freeze_sign__c = true; // OLY_OCM-689 删除冻结字段的更新
                   String rea = nObj.Inspection_result_after_NG_F__c == '废弃' ? '待移至报废区' : nObj.Inspection_result_after_NG_F__c;
                   ass.ChangeQuantityReason__c = FixtureUtil.raesdStatusMap.get(FixtureUtil.HistoryStatus.Dai_Fei_Qi.ordinal()) + ' ->' + rea;
                   assUpdMap.put(ass.Id, ass);
                }
                //待废弃
                if (nObj.RAESD_Status__c == FixtureUtil.raesdStatusMap.get(FixtureUtil.HistoryStatus.Dai_Fei_Qi.ordinal())
                        && oObj.RAESD_Status__c != nObj.RAESD_Status__c
                        && ass.Manage_type__c == '个体管理') {
                    ass.Status = FixtureUtil.assetStatusMap.get(FixtureUtil.AssetStatus.Dai_Fei_Qi.ordinal());
                    // ass.Freeze_sign__c = true;
                    assUpdMap.put(ass.Id, ass);
                }
                // //已消耗
                // else if (nObj.Check_lost_Item_Final__c == '消耗'
                //         && oObj.Check_lost_Item_Final__c != nObj.Check_lost_Item_Final__c
                //         && ass.Manage_type__c == '个体管理') {
                //     ass.Status = FixtureUtil.assetFixtureStatusMap.get(FixtureUtil.AssetFixtureStatus.Yi_Xiao_Hao.ordinal());
                //     assUpdMap.put(ass.Id, ass);
                // }
                //丢失
                // else if (nObj.Lost_item_giveup__c == true
                //         && oObj.Lost_item_giveup__c != nObj.Lost_item_giveup__c
                //         && ass.Manage_type__c == '个体管理') {
                    // ass.Status = FixtureUtil.assetFixtureStatusMap.get(FixtureUtil.AssetFixtureStatus.Diu_Shi.ordinal());
                    // assUpdMap.put(ass.Id, ass);
                // }
                // else if (ass.Manage_type__c == '数量管理' && assMap.containsKey(ass.Id)) {
                //     if (ass.Quantity != null && ass.Quantity > 0) {
                //         if (ass.Status != FixtureUtil.assetFixtureStatusMap.get(FixtureUtil.AssetFixtureStatus.Ke_Yi_Fen_Pei.ordinal())) {
                //             ass.Status = FixtureUtil.assetFixtureStatusMap.get(FixtureUtil.AssetFixtureStatus.Ke_Yi_Fen_Pei.ordinal());
                //             assUpdMap.put(ass.Id, ass);
                //         }
                //     }
                //     else {
                //         if (ass.Status != FixtureUtil.assetFixtureStatusMap.get(FixtureUtil.AssetFixtureStatus.Mei_You_Ku_Cun.ordinal())) {
                //             ass.Status = FixtureUtil.assetFixtureStatusMap.get(FixtureUtil.AssetFixtureStatus.Mei_You_Ku_Cun.ordinal());
                //             assUpdMap.put(ass.Id, ass);
                //         }
                //     }
                // }
            }
        }
    }
    /*
     * 回库确认 OFF -> ON:
     * 取消分配 OFF -> ON:
     * 放弃欠品 OFF -> ON
     * 已消耗
     *
     *  清空Asset 最新备品申请借出明细 Last_Reserve_RAES_Detail__c
     *  (nObj.Asset__c 変更しないため、changeAssetLast()でクリアできないため、ここでクリア)
     *
     * クラスの assUpdMap を更新するだけ
     */
    // private void clearLastReserveRAESD() {
    //     for (Consum_Apply_Equipment_Set_Detail__c nObj : newList) {
    //         Consum_Apply_Equipment_Set_Detail__c oObj = oldMap.get(nObj.Id);
    //         // 主体和附属品
    //         if (finishOrChangeAsset(Trigger.isDelete, oObj, nObj)) {
    //             Asset ass = assMap.get(nObj.Asset__c);
    //             if (assUpdMap.containsKey(nObj.Asset__c)) {
    //                 ass = assUpdMap.get(nObj.Asset__c);
    //             }
    //             // 这里 会出现 assUpdMap 有 但是 assMap 没有的情况
    //             if (ass == null
    //                     //分配备品有变化 (下架前)
    //                     && (oObj.Asset__c != null && oObj.Asset__c != nObj.Asset__c
    //                             && nObj.StockDown__c == false)) {
    //                 ass = new Asset(Id = oObj.Asset__c);
    //             }
    //             if (ass == null) {
    //                 continue;
    //             }
    //             // 暂时放在这里
    //             if (oObj.Manage_type_F__c == '个体管理') {
    //                 ass.Last_Reserve_RAES_Detail__c = null;
    //                 assUpdMap.put(ass.Id, ass);
    //             }
    //         }
    //     }
    // }
    //nObj.Asset__cがある場合、変更した場合
    // private void changeAssetLast() {
    //     for (Consum_Apply_Equipment_Set_Detail__c nObj : newList) {
    //         Consum_Apply_Equipment_Set_Detail__c oObj = oldMap.get(nObj.Id);
    //         Asset nass = assMap.get(nObj.Asset__c);
    //         if (assUpdMap.containsKey(nObj.Asset__c)) {
    //             nass = assUpdMap.get(nObj.Asset__c);
    //         } else if (String.isNotBlank(nObj.Asset__c)) {
    //             nass = new Asset(Id = nObj.Asset__c);
    //         }
    //         Asset oass = assMap.get(oObj.Asset__c);
    //         if (assUpdMap.containsKey(oObj.Asset__c)) {
    //             oass = assUpdMap.get(oObj.Asset__c);
    //         } else if (String.isNotBlank(oObj.Asset__c)) {
    //             oass = new Asset(Id = oObj.Asset__c);
    //         }
    //         //nObj.Asset__cがある場合、変更した場合
    //         if (nObj.Asset__c != null && oObj.Asset__c != nObj.Asset__c
    //                 && (oObj.Manage_type_F__c == '个体管理' || nObj.Manage_type_F__c == '个体管理')) {
    //             if (nObj.Manage_type_F__c == '个体管理') {
    //                 nass.Last_Reserve_RAES_Detail__c = nObj.Id;
    //                 assUpdMap.put(nass.Id, nass);
    //             }
    //             if (oObj.Manage_type_F__c == '个体管理') {
    //                 oass.Last_Reserve_RAES_Detail__c = null;
    //                 assUpdMap.put(oass.Id, oass);
    //             }
    //         }
    //     }
    // }
    // 取消分配 的时候, 需要在 beforeSetValue() 里把 Asset__c 清除
    //明細終了とAsset変更の時Out_of_wh__cを更新
    //afterinsert, afterupdate, afterdelete から実行
    private void changeAssetCount() {
        List<Consum_Apply_Equipment_Set_Detail__c> nList = newList;
        if (Trigger.isDelete) {
            nList = oldList;
        }
        for (Consum_Apply_Equipment_Set_Detail__c nObj : nList) {
            Consum_Apply_Equipment_Set_Detail__c oObj;
            Asset nass = assMap.get(nObj.Asset__c);
            if (assUpdMap.containsKey(nObj.Asset__c)) {
                nass = assUpdMap.get(nObj.Asset__c);
            } else if (String.isNotBlank(nObj.Asset__c)) {
                nass = new Asset(Id = nObj.Asset__c);
            }
            if (nass != null && (nass.Out_of_wh__c == null || nass.Rental_Count__c == null || nass.Confirm_Lost_Count__c == null)) {
                nass.Out_of_wh__c = nObj.Out_of_wh__c;
                nass.Rental_Count__c = nObj.Consum_Count__c;
                nass.Confirm_Lost_Count__c = nObj.Confirm_Lost_Count__c;
            }
            if (Trigger.isInsert == false) {
                oObj = oldMap.get(nObj.Id);
                Asset oass = assMap.get(oObj.Asset__c);
                if (assUpdMap.containsKey(oObj.Asset__c)) {
                    oass = assUpdMap.get(oObj.Asset__c);
                } else if (String.isNotBlank(oObj.Asset__c)) {
                    oass = new Asset(Id = oObj.Asset__c);
                }
                //MapにAssetを新規push時値を設定
                if (oass != null && (oass.Out_of_wh__c == null || oass.Rental_Count__c == null || oass.Confirm_Lost_Count__c == null)) {
                    oass.Out_of_wh__c = oObj.Out_of_wh__c;
                    oass.Rental_Count__c = oObj.Consum_Count__c;
                    oass.Confirm_Lost_Count__c = oObj.Confirm_Lost_Count__c;
                }
                System.debug('111111111111111');
                System.debug(Trigger.isDelete);
                // 最終状態及とAsset変更の場合
                if (finishOrChangeAsset(Trigger.isDelete, oObj, nObj)) {
                    System.debug('oass is ' + oass);
                    if (oass != null) {
                        if (oass.Out_of_wh__c != null && oass.Out_of_wh__c > 0) {
                            oass.Out_of_wh__c = oass.Out_of_wh__c - 1;
                            // assUpdMap.put(oass.Id, oass);
                            System.debug('oass.Out_of_wh__c -1' + oass.Out_of_wh__c + ' Id = ' + nObj.Id);
                        }
                        if (nObj.DeliverySlip__c != null) {
                            if (oass.Rental_Count__c != null && oass.Rental_Count__c > 0) {
                                oass.Rental_Count__c = oass.Rental_Count__c - 1;
                            }
                        }
                            assUpdMap.put(oass.Id, oass);
                    }
                } else if (oass != null && oObj.DeliverySlip__c != null && nObj.DeliverySlip__c == null) {
                    //发货运输单 删除的时候已借出数 -1 发货Asset__c原则不会变所以就直接用上面的oass
                    // if (nObj.DeliverySlip__c != null) {
                    if (oass.Rental_Count__c != null && oass.Rental_Count__c > 0) {
                        oass.Rental_Count__c = oass.Rental_Count__c - 1;
                        assUpdMap.put(oass.Id, oass);
                    }
                    // }
                }
                if (oObj.Confirm_Lost_Date__c == null
                        && nObj.Confirm_Lost_Date__c != null) {
                    nass.Confirm_Lost_Count__c = nass.Confirm_Lost_Count__c + 1;
                    assUpdMap.put(nass.Id, nass);
                }
                else if (nObj.Confirm_Lost_Date__c == null
                        && oObj.Confirm_Lost_Date__c != null
                        && nass.Confirm_Lost_Count__c > 0) {
                    nass.Confirm_Lost_Count__c = nass.Confirm_Lost_Count__c - 1;
                    assUpdMap.put(nass.Id, nass);
                }
                //发货运输单设置的时候已解除数+1
                if (oObj.DeliverySlip__c == null && nObj.DeliverySlip__c != null) {
                    nass.Rental_Count__c = nass.Rental_Count__c + 1;
                    assUpdMap.put(nass.Id, nass);
                }
            }
 
            //nObj.Asset__cがある場合、変更した場合
            if (nObj.Asset__c != null
                && ((Trigger.isInsert && nObj.DataMigration_Flag__c == false)
                        || (oObj != null && oObj.Asset__c != nObj.Asset__c)
                        )
            ) {
                if (Trigger.isInsert == false) {
                    System.debug(oObj.Asset__c);
                }
                System.debug(nObj.Asset__c);
                System.debug(Trigger.isInsert);
                System.debug(Trigger.isBefore);
                
                if (nass.Out_of_wh__c == null) {
                     nass.Out_of_wh__c  = 0;
                }
                nass.Out_of_wh__c = nass.Out_of_wh__c + 1;
                System.debug('nass.Out_of_wh__c +1' + nass.Out_of_wh__c  + ' Id = ' + nObj.Id);
                assUpdMap.put(nass.Id, nass);
            }
        }
    }
    // 同じblock の 排队レコード Queue_Number__c をrefresh
    // private void refreshQueueNumberByBlock() {
    //     for (Consum_Apply_Equipment_Set_Detail__c nObj : newList) {
    //         Consum_Apply_Equipment_Set_Detail__c oObj = oldMap.get(nObj.Id);
    //         // 同じblock に 排队変更がある場合
    //         if (oObj.Cancel_Select__c == false
    //                 && String.isBlank(oObj.Fixture_Model_No_text__c) == false
    //                 && String.isBlank(oObj.Salesdepartment_before__c) == false
    //                 && String.isBlank(oObj.Product_category_text__c) == false
    //                 && String.isBlank(oObj.Equipment_Type_text__c) == false
    //                 && oObj.Queue_Number__c > 0
    //                 // 変化を確認
    //                 && (oObj.Cancel_Select__c != nObj.Cancel_Select__c
    //                     || oObj.Fixture_Model_No_text__c != nObj.Fixture_Model_No_text__c
    //                     || oObj.Salesdepartment_before__c != nObj.Salesdepartment_before__c
    //                     || oObj.Product_category_text__c != nObj.Product_category_text__c
    //                     || oObj.Equipment_Type_text__c != nObj.Equipment_Type_text__c
    //                     || (nObj.Queue_Number__c == 0 || nObj.Queue_Number__c == null))
    //         ) {
    //             if (executeed_refreshQueueNumber.contains(nObj.Id) && executeed_refreshQueueNumber.contains(oObj.Id)) {
    //                 continue;
    //             }
    //             else if (executeed_refreshQueueNumber.contains(nObj.Id) == false) {
    //                 executeed_refreshQueueNumber.add(nObj.Id);
    //             } else {   //  executeed_refreshQueueNumber.contains(oObj.Id) == false
    //                 executeed_refreshQueueNumber.add(oObj.Id);
    //             }
    //             // TODO 想定 画面からの1 record しかない
    //             List<Consum_Apply_Equipment_Set_Detail__c> updateList = new List<Consum_Apply_Equipment_Set_Detail__c>();
    //             List<Consum_Apply_Equipment_Set_Detail__c> refreshTargetList = [
    //                     Select Id, Queue_Number__c
    //                       from Consum_Apply_Equipment_Set_Detail__c
    //                      where Cancel_Select__c = false
    //                        and Fixture_Model_No_text__c = :oObj.Fixture_Model_No_text__c
    //                        and Salesdepartment_before__c = :oObj.Salesdepartment_before__c
    //                        and Product_category_text__c = :oObj.Product_category_text__c
    //                        and Equipment_Type_text__c = :oObj.Equipment_Type_text__c
    //                        and Queue_Number__c > 0
    //                      Order by Queue_Number__c];
    //             for (Integer i = 0; i < refreshTargetList.size(); i++) {
    //                 Consum_Apply_Equipment_Set_Detail__c refreshTarget = refreshTargetList[i];
    //                 if (refreshTarget.Queue_Number__c != (i + 1)) {
    //                     refreshTarget.Queue_Number__c = i + 1;
    //                     executeed_refreshQueueNumber.add(refreshTarget.Id);
    //                     updateList.add(refreshTarget);
    //                 }
    //             }
    //             if (!updateList.isEmpty()) { update updateList; }
    //         }
    //     }
    // }
 
    // 4-XX 下架后, 出库前检测的Action一对一分配的话, 和主体一起 Cancel
    private void oneToOne4XX() {
        //主体CnacelSet
        Set<Id> raesdBodyCnacelSet = new Set<Id>();
        Set<Id> raesdBodyNGSet = new Set<Id>();
        //暂时只考虑主体NG或者取消的时候
        Map<Id, String> raesdCancel_Remarks = new Map<Id, String>();
        //检测NG或者本来就Cancel的不需要再更新
        Set<Id> raesdIdSet = new Set<Id>();
        //for (Consum_Apply_Equipment_Set_Detail__c nObj : newList) {
        //    Consum_Apply_Equipment_Set_Detail__c oObj = oldMap.get(nObj.Id);
            //Set没有cancel的话都需要拷贝
            //主体出库前检测NG 维修
            //if (nObj.Is_Body__c == true
            //        && nObj.Inspection_result__c == 'NG'
            //        && oObj.Inspection_result__c != nObj.Inspection_result__c
            //        && nObj.Inspection_result_NG__c == '维修') {
            //    //需要重新分配一对一附属品的一览
            //    raesdBodyNGSet.add(nObj.Consum_Apply_Equipment_Set__c);
            //    raesdCancel_Remarks.put(nObj.Consum_Apply_Equipment_Set__c, nObj.Loaner_cancel_Remarks__c);
            //    raesdIdSet.add(nObj.Id);
            //}
            ////主体出库前检测NG 废弃
            //else if (nObj.Is_Body__c == true
            //        && nObj.Inspection_result__c == 'NG'
            //        && oObj.Inspection_result__c != nObj.Inspection_result__c
            //        && nObj.Inspection_result_NG__c == '废弃') {
            //    //需要重新分配一对一附属品的一览
            //    raesdBodyNGSet.add(nObj.Consum_Apply_Equipment_Set__c);
            //    raesdCancel_Remarks.put(nObj.Consum_Apply_Equipment_Set__c, nObj.Loaner_cancel_Remarks__c);
            //    raesdIdSet.add(nObj.Id);
            //}
            ////附属品出库前检测NG 维修 并且是一对一分配
            //else if (nObj.Is_Body__c == false
            //        && nObj.Inspection_result__c == 'NG'
            //        && oObj.Inspection_result__c != nObj.Inspection_result__c
            //        && nObj.Inspection_result_NG__c == '维修'
            //        //&& nObj.OneToOne_Flag__c == true
            //) {
            //    raesdIdSet.add(nObj.Id);
            //}
            ////附属品出库前检测NG 废弃 并且是一对一分配
            //else if (nObj.Is_Body__c == false
            //        && nObj.Inspection_result__c == 'NG'
            //        && oObj.Inspection_result__c != nObj.Inspection_result__c
            //        && nObj.Inspection_result_NG__c == '废弃'
            //        //&& nObj.OneToOne_Flag__c == true
            //) {
            //    raesdIdSet.add(nObj.Id);
            //}
            ////OLY_OCM-152 下架后主体取消的话一对一附属品也要一起取消
            //else if (String.isBlank(nObj.DeliverySlip__c)
            //        && nObj.Is_Body__c == true
            //        //&& nObj.StockDown__c == true
            //        && oObj.Cancel_Select__c == false
            //        && nObj.Cancel_Select__c == true) {
            //    raesdIdSet.add(nObj.Id);
            //    raesdBodyCnacelSet.add(nObj.Consum_Apply_Equipment_Set__c);
            //    raesdCancel_Remarks.put(nObj.Consum_Apply_Equipment_Set__c, nObj.Loaner_cancel_Remarks__c);
            //}
        //}
        if (raesdBodyNGSet.isEmpty() && raesdBodyCnacelSet.isEmpty()) {
            return;
        }
        List<Consum_Apply_Equipment_Set_Detail__c> raesds = [
                Select Id
                     , UniqueKey__c
                     , Consum_Apply_Equipment_Set__c
                     , Consum_Apply__c
                     , Fixture_Set_Detail__c
                From Consum_Apply_Equipment_Set_Detail__c
                //主体出库前检测NG一览的所有一对一分配的明细
                Where (Consum_Apply_Equipment_Set__c = :raesdBodyNGSet
                    //下架后主体取消一对一附属品也要一起取消
                       OR Consum_Apply_Equipment_Set__c = :raesdBodyCnacelSet)
                    //AND OneToOne_Flag__c = true
                    //和主体一起检测NG的不再更新
                    AND Id !=: raesdIdSet];
        List<Consum_Apply_Equipment_Set_Detail__c> raesdList = new List<Consum_Apply_Equipment_Set_Detail__c>();
        for (Consum_Apply_Equipment_Set_Detail__c raesd : raesds) {
            raesd.OnStock_By_Cancel__c = true;
            raesd.Cancel_Select__c = true;
            // OLY_OCM-163#comment-20120592 通过取消日判断 -》 更新的时候before设置
            //raesd.Cancel_Date__c = Date.today();
            raesd.Cancel_Reason__c = '重新分配';
            //主体出库前检查NG的话取消备注回自动设置为出库前检查NG
            raesd.Loaner_cancel_Remarks__c = raesdCancel_Remarks.get(raesd.Consum_Apply_Equipment_Set__c);
            // }
            raesdList.add(raesd);
        }
        System.debug('raesdList size' + raesdList.size());
        if (!raesdList.isEmpty()) {
            update raesdList;
        }
    }
 
    //一览单位取消后的拷贝在一览的Handler里面做
    // 因为cancel是主体的话First应开始拷贝出来的主体所以First的更新逻辑移动到cancelcopy
    private void cancelCopy() {
        List<Consum_Apply_Equipment_Set_Detail__c> raesdList = new List<Consum_Apply_Equipment_Set_Detail__c>();
        List<Consum_Apply_Equipment_Set_Detail__c> raesdListup = new List<Consum_Apply_Equipment_Set_Detail__c>();
        Map<Id, Consum_Apply_Equipment_Set__c> raesMap = new Map<Id, Consum_Apply_Equipment_Set__c>();
        for (Consum_Apply_Equipment_Set_Detail__c nObj : newList) {
            Consum_Apply_Equipment_Set_Detail__c oObj = oldMap.get(nObj.Id);
            //Set没有cancel的话都需要拷贝
            //Cancel_Select__c はfalseからtrueに変更なのでなのでtrigger二回はしても実行するのは1回だけ
            if (nObj.Set_Cancel_Select__c == false
                    && (oObj.Cancel_Select__c == false 
                    && nObj.Cancel_Select__c == true)
                    && nObj.StockDown__c == true 
                    && nObj.Cancel_Reason__c == '重新分配') {
                Consum_Apply_Equipment_Set_Detail__c craesd = new Consum_Apply_Equipment_Set_Detail__c();
                craesd.Consum_Apply_Equipment_Set__c = nObj.Consum_Apply_Equipment_Set__c;
                craesd.Consum_Apply__c               = nObj.Consum_Apply__c;
                craesd.Fixture_Set_Detail__c         = nObj.Fixture_Set_Detail__c;
                craesd.Fixture_Model_No_text__c      = nObj.FSD_Fixture_Model_No__c;
                craesd.Fixture_Name_text__c          = nObj.FSD_Name_CHN__c;
                craesd.IndexFromUniqueKey_Text__c    = nObj.IndexFromUniqueKey_Text__c;
                craesd.Canceled__c                   = nObj.Id;
                craesd.FSD_Id__c                     = nObj.FSD_Id__c;
                //craesd.FSD_Is_Optional__c            = nObj.FSD_Is_Optional_F__c;
                //craesd.FSD_Is_OneToOne__c            = nObj.FSD_Is_OneToOne_F__c;
                craesd.FSD_Name_CHN__c               = nObj.FSD_Name_CHN__c;
                //craesd.FSD_OneToOneAccessory_Cnt__c  = nObj.FSD_OneToOneAccessory_Cnt_F__c;
                craesd.FSD_Fixture_Model_No__c       = nObj.FSD_Fixture_Model_No__c;
                if (nObj.DataMigration_Flag__c == false && (!Trigger.isUpdate || String.isNotBlank(nObj.Asset__c))) {
                    craesd.Consum_Num__c  = nObj.Consum_Num__c;
                    craesd.IndexFromUniqueKey__c = nObj.IndexFromUniqueKey__c;
                    craesd.Degree_Of_Importance__c = nObj.Degree_Of_Importance__c;
                }
                //cancel重新分配的不算追加附属品 272チケット
                craesd.Create_State__c = 0;
                raesdList.add(craesd);
            }
            // 如果是主体 cancel的话First应该是拷贝出来的主体
            // if ((oObj.Cancel_Select__c == false && nObj.Cancel_Select__c == true)
            //         && nObj.Is_First_RAESD_F__c == true && nObj.Set_Cancel_Select__c == false 
            //         && nObj.DataMigration_Flag__c == false && (!Trigger.isUpdate || String.isNotBlank(nObj.Fixture_Set_Id__c))) {
            //     Consum_Apply_Equipment_Set__c raes = new Consum_Apply_Equipment_Set__c(
            //             Id = nObj.Consum_Apply_Equipment_Set__c,
            //             First_RAESD__c = null);
            //     raesMap.put(nObj.Consum_Apply_Equipment_Set__c, raes);
            // }
        }
        //没下架的字段清空在before里面做
        if (!raesdList.isEmpty()) {
            insert raesdList;
        }
        if (!raesMap.isEmpty()) {
            List<Consum_Apply_Equipment_Set_Detail__c> raess = [Select Id, Consum_Apply_Equipment_Set__c
                From Consum_Apply_Equipment_Set_Detail__c
                Where Consum_Apply_Equipment_Set__c = :raesMap.keySet()
                  and Cancel_Select__c = False
                order by Consum_Apply_Equipment_Set__c
                //, Fixture_Set_Detail__r.SortInt__c
                ASC nulls last];
            System.debug(raess);
            Id raesId = null;
            // 一件目をFirst_RAESD__cに設定
            for (Consum_Apply_Equipment_Set_Detail__c raes : raess) {
                if (raesId != raes.Consum_Apply_Equipment_Set__c) {
                    raesMap.get(raes.Consum_Apply_Equipment_Set__c).First_RAESD__c = raes.Id;
                    raesId = raes.Consum_Apply_Equipment_Set__c;
                }
            }
        }
        //一览First更新
        if (!raesMap.isEmpty()) {
            update raesMap.values();
        }
    }
 
    // afterdeleteのみ
    private void deleteReFirst() {
        Map<Id, Consum_Apply_Equipment_Set__c> raesMap = new Map<Id, Consum_Apply_Equipment_Set__c>();
        for (Consum_Apply_Equipment_Set_Detail__c oObj : oldList) {
            if (oObj.Is_First_RAESD_F__c == true) {
                    Consum_Apply_Equipment_Set__c raes = new Consum_Apply_Equipment_Set__c(
                            Id = oObj.Consum_Apply_Equipment_Set__c,
                            First_RAESD__c = null);
                    raesMap.put(oObj.Consum_Apply_Equipment_Set__c, raes);
                }
        }
        //修改一览的FirstMap
        if (raesMap.isEmpty()) {
            return;
        }
        if (!raesMap.isEmpty()) {
            List<Consum_Apply_Equipment_Set_Detail__c> raess = [Select Id, Consum_Apply_Equipment_Set__c
                From Consum_Apply_Equipment_Set_Detail__c
                Where Consum_Apply_Equipment_Set__c = :raesMap.keySet()
                  and Cancel_Select__c = False
                  //一覧も削除したら更新しません
                  and IsDeleted = false
                // RAESD_SortInt_F__c -> IndexFromUniqueKey_Text__c(备品配套明细.SortInt__c)
                // 1应该是主体
                order by Consum_Apply_Equipment_Set__c, IndexFromUniqueKey_Text__c ASC nulls last];
            Id raesId = null;
            // 一件目をFirst_RAESD__cに設定
            for (Consum_Apply_Equipment_Set_Detail__c raes : raess) {
                if (raesId != raes.Consum_Apply_Equipment_Set__c) {
                    raesMap.get(raes.Consum_Apply_Equipment_Set__c).First_RAESD__c = raes.Id;
                    raesId = raes.Consum_Apply_Equipment_Set__c;
                }
            }
        }
        //一览First更新
        if (!raesMap.isEmpty()) {
            update raesMap.values();
        }
    }
    // 最終状態及びAsset変更
    private Boolean finishOrChangeAsset(Boolean isDelete, Consum_Apply_Equipment_Set_Detail__c oObj, Consum_Apply_Equipment_Set_Detail__c nObj) {
        Boolean rtn = false;
        if (isDelete && updatedSet.contains(oObj.Id) == false) {
            updatedSet.add(oObj.Id);
            // oObj を確認する、oldで もともと finishのもの、falseを返す、
            rtn = !(
                //回库确认
                   (oObj.Arrival_in_wh__c)
                //取消分配, (下架后, 原则需要上架, 所以不能单纯的断开)
                || (oObj.Cancel_Select__c == true 
                    && oObj.StockDown__c == false
                    )
                //下架前, 分配备品有变化 (变成别的配套, or 变 null)
                || (oObj.Asset__c == null 
                    && oObj.StockDown__c == false
                    )
                //取消分配, (下架后, 原则需要上架, 所以不能单纯的断开)
                || (oObj.Cancel_Select__c == true 
                    && nObj.StockDown__c == false
                    )
                //放弃欠品
                //|| (oObj.Lost_item_giveup__c == true)
                //已消耗
                //|| (oObj.Check_lost_Item_F__c == '消耗')
            );
        } else {
            rtn = (
                //回库确认
                   (!oObj.Arrival_in_wh__c && nObj.Arrival_in_wh__c)
                //取消分配, (下架后, 原则需要上架, 所以不能单纯的断开)
                || (oObj.Cancel_Select__c == false && nObj.Cancel_Select__c == true
                    && nObj.StockDown__c == false
                    )
                //下架前, 分配备品有变化 (变成别的配套, or 变 null)
                || (oObj.Asset__c != null && oObj.Asset__c != nObj.Asset__c
                    && nObj.StockDown__c == false
                    )
                //取消分配, (下架后, 原则需要上架, 所以不能单纯的断开)
                || (oObj.Cancel_Select__c == false && nObj.Cancel_Select__c == true
                    && nObj.StockDown__c == false
                    )
                //放弃欠品
                || (oObj.Lost_item_giveup__c == false && nObj.Lost_item_giveup__c == true
                    && oObj.Check_lost_Item_F__c != '消耗')
                //已消耗
                || (oObj.Check_lost_Item_F__c != '消耗' && nObj.Check_lost_Item_F__c == '消耗'
                    && oObj.Lost_item_giveup__c == false)
            );
        }
        return rtn;
    }
    //before 数式の値がnullになる可能性がありますのでここでも一回チェックします
    private void formulaToTextCheck() {
        List<Consum_Apply_Equipment_Set_Detail__c> raesds = new List<Consum_Apply_Equipment_Set_Detail__c>();
        for (Consum_Apply_Equipment_Set_Detail__c nObj : newList) {
            //明细状态没有修理完毕所以修理完毕的时候不拷贝状态到Text字段
            if (//(nObj.Repair_Status_Text__c != nObj.Repair_Status_F__c
                //        && nObj.Repair_Status_F__c != '修理完毕') ||
                nObj.Return_Status_Text__c != nObj.Return_Status_F__c
                    || nObj.Shipment_Status_Text__c != nObj.Shipment_Status_F__c
                    //|| (nObj.ApplyPersonAppended__c != nObj.ApplyPersonAppended_F__c && Trigger.isInsert)
                    ) {
                Consum_Apply_Equipment_Set_Detail__c raesd = new Consum_Apply_Equipment_Set_Detail__c(Id = nObj.Id);
                //明细状态没有修理完毕所以修理完毕的时候不拷贝状态到Text字段
                //if (nObj.Repair_Status_F__c != '修理完毕') {
                //    raesd.Repair_Status_Text__c = nObj.Repair_Status_F__c;
                //}
                raesd.Return_Status_Text__c = nObj.Return_Status_F__c;
                raesd.Shipment_Status_Text__c = nObj.Shipment_Status_F__c;
                //if (Trigger.isInsert) {
                //    raesd.ApplyPersonAppended__c = nObj.ApplyPersonAppended_F__c;
                //}
                raesds.add(raesd);
            }
        }
        if (!raesds.isEmpty()) {
            update raesds;
        }
    }
    private void reQueueNumber() {
        Map<String, List<Consum_Apply_Equipment_Set_Detail__c>> queueRaesdMap = new Map<String, List<Consum_Apply_Equipment_Set_Detail__c>>();
        String wher = '';
        for (Consum_Apply_Equipment_Set_Detail__c oObj : oldList) {
            Consum_Apply_Equipment_Set_Detail__c nObj;
            if (Trigger.isUpdate) {
                nObj = newMap.get(oObj.Id);
            }
            //if ((Trigger.isUpdate && (oObj.Queue_Number__c != 0  && nObj.Queue_Number__c == 0
            //            || (!oObj.Cancel_Select__c && nObj.Cancel_Select__c )
            //            || (String.isBlank(oObj.Fixture_Model_No_text__c) == false
            //                // #OLY_OCM-654 因为是排队的逻辑。所以不需要删除。因为只有主体才能排队 Start
            //                && String.isBlank(oObj.Salesdepartment_before__c) == false
            //                && String.isBlank(oObj.Product_category_text__c) == false
            //                && String.isBlank(oObj.Equipment_Type_text__c) == false
            //                // #OLY_OCM-654 因为是排队的逻辑。所以不需要删除。因为只有主体才能排队 End
            //                && oObj.Queue_Number__c > 0
            //                // 変化を確認
            //                && (oObj.Fixture_Model_No_text__c != nObj.Fixture_Model_No_text__c
            //                    // #OLY_OCM-654 因为是排队的逻辑。所以不需要删除。因为只有主体才能排队 Start
            //                    || oObj.Salesdepartment_before__c != nObj.Salesdepartment_before__c
            //                    || oObj.Product_category_text__c != nObj.Product_category_text__c
            //                    || oObj.Equipment_Type_text__c != nObj.Equipment_Type_text__c))))
            //                    // #OLY_OCM-654 因为是排队的逻辑。所以不需要删除。因为只有主体才能排队 End
            //        || (Trigger.isDelete && oObj.Queue_Number__c != 0 && oObj.Queue_Number__c != null)) {
            //    if (String.isBlank(oObj.Fixture_Model_No_text__c)) {
            //        if (Trigger.isUpdate) {
            //            //更新时用nObj
            //            if (String.isNotBlank(nObj.Asset__c)) nObj.addError('备品配套型号不能为空');
            //        } else {
            //            //因为上面的If文已经判断了只有更新和删除所以这里不再判断删除
            //            //删除时用oObj
            //            oObj.addError(oObj.Id + '(排队):备品配套明细型号不能为空');
            //        }
            //        // 不continue的话下面使用的时候会报NullOoint
            //        continue;
            //    }
            //    String key = oObj.Fixture_Model_No_text__c;
            //    // 式の左と右 ともに Fixture_Model_No_text__c なので、問題ないです。
            //    wher += ' ( Fixture_Model_No_text__c = \'' + String.escapeSingleQuotes(oObj.Fixture_Model_No_text__c) + '\'';
            //    // if (String.isNotBlank(oObj.Internal_asset_location_before__c)) {
            //    //     wher += ' and Internal_asset_location_before__c =\'' + String.escapeSingleQuotes(oObj.Internal_asset_location_before__c) + '\'';
            //    // }
 
            //    // #OLY_OCM-654 这里是排队的逻辑。只有主体才能排队。所以是不删除的 Start
            //    if (String.isNotBlank(oObj.Salesdepartment_before__c)) {
            //        wher += ' and Salesdepartment_before__c =\'' + String.escapeSingleQuotes(oObj.Salesdepartment_before__c) + '\'';
            //    }
            //    if (String.isNotBlank(oObj.Product_category_text__c)) {
            //        wher += ' and Product_category_text__c =\'' + String.escapeSingleQuotes(oObj.Product_category_text__c) + '\'';
            //    }
            //    if (String.isNotBlank(oObj.Equipment_Type_text__c)) {
            //        wher += ' and Equipment_Type_text__c =\'' + String.escapeSingleQuotes(oObj.Equipment_Type_text__c) + '\'';
            //    }
            //    // #OLY_OCM-654 这里是排队的逻辑。只有主体才能排队。所以是不删除的 End
 
            //    wher += ') OR';
            //}
        }
        if (String.isBlank(wher)) {
            return;
        }
        wher = wher.removeEnd('OR');
        wher = '(' + wher + ')';
        //String soql = 'Select Id, Queue_Number__c, Fixture_Model_No_text__c, Internal_asset_location_before__c, Product_category_text__c,'
        //             + ' Equipment_Type_text__c, Salesdepartment_before__c'
        //             + ' From Consum_Apply_Equipment_Set_Detail__c '
        //             + ' Where Queue_Number__c > 0 AND Cancel_Select__c = false'
        //             + ' AND Queue_Number__c != null AND '
        //             + wher
        //             + ' order by Queue_Number__c ASC';
        //System.debug(soql);
        //List<Consum_Apply_Equipment_Set_Detail__c> queueRaesds = Database.query(soql);
        //System.debug(queueRaesds.size());
        //for (Consum_Apply_Equipment_Set_Detail__c raesd : queueRaesds) {
        //    String key = raesd.Fixture_Model_No_text__c;
        //    // if (String.isNotBlank(raesd.Internal_asset_location_before__c)) {
        //    //     key += '_' + raesd.Internal_asset_location_before__c;
        //    // }
 
        //    // #OLY_OCM-654 这里是排队的逻辑。只有主体才能排队。所以是不删除的 Start
        //    if (String.isNotBlank(raesd.Salesdepartment_before__c)) {
        //        key += '_' + raesd.Salesdepartment_before__c;
        //    }
        //    if (String.isNotBlank(raesd.Product_category_text__c)) {
        //        key += '_' + raesd.Product_category_text__c;
        //    }
        //    if (String.isNotBlank(raesd.Equipment_Type_text__c)) {
        //        key += '_' + raesd.Equipment_Type_text__c;
        //    }
        //    // #OLY_OCM-654 这里是排队的逻辑。只有主体才能排队。所以是不删除的 End
 
        //    if (!queueRaesdMap.containsKey(key)) {
        //        queueRaesdMap.put(key, new List<Consum_Apply_Equipment_Set_Detail__c>());
        //    }
        //    queueRaesdMap.get(key).add(raesd);
        //}
        List<Consum_Apply_Equipment_Set_Detail__c> raesdList = new List<Consum_Apply_Equipment_Set_Detail__c>();
        for (String key : queueRaesdMap.keySet()) {
            List<Consum_Apply_Equipment_Set_Detail__c> raesds = queueRaesdMap.get(key);
            for (Integer i = 1; i <= raesds.size(); i++) {
                Consum_Apply_Equipment_Set_Detail__c raesd = raesds[i-1];
                //if (raesd.Queue_Number__c != i) {
                //    raesd.Queue_Number__c = i;
                //    raesdList.add(raesd);
                //}
            }
        }
        if (!raesdList.isEmpty()) {
            update raesdList;
        }
    }
    //申请者收回NG后明细回寄时的邮件回寄单内容设置
    //只在Afterupdate里面调用
    private void receivedConfirmNGSetReturnDeliverySlipText() {
        //一览Id和对应的回寄发货单ID
        Map<Id, Id> raesRDSMap = new Map<Id, Id>();
        Map<Id, String> raesDateMap = new Map<Id, String>();
        for (Consum_Apply_Equipment_Set_Detail__c nObj : newList) {
            Consum_Apply_Equipment_Set_Detail__c oObj = oldMap.get(nObj.Id);
            //申请者收货NG的明细
            if (nObj.Received_Confirm__c == 'NG') {
                //回寄运输单赋值
                if (String.isBlank(oObj.Return_DeliverySlip__c) && String.isNotBlank(nObj.Return_DeliverySlip__c)) {
                    //因为是以为一览为单位,所以只有一条回寄单
                    //申请者收货NG的明细拆成两个回寄单回寄的情况暂时不考虑(应该没有这种情况)
                    if (!raesRDSMap.containsKey(nObj.Consum_Apply_Equipment_Set__c)) {
                        raesRDSMap.put(nObj.Consum_Apply_Equipment_Set__c, nObj.Return_DeliverySlip__c);
                        raesDateMap.put(nObj.Consum_Apply_Equipment_Set__c, nObj.Asset_return_time__c.format());
                    }
                }
            }
        }
        if (raesRDSMap.isEmpty()) {
            return;
        }
        //NG_Return_DeliverySlip__c为空的一览才做邮件发送
        Map<Id, Consum_Apply_Equipment_Set__c> raessMap = new Map<Id, Consum_Apply_Equipment_Set__c>([Select Id From Consum_Apply_Equipment_Set__c
                WHERE Id =: raesRDSMap.keySet()
                AND NG_Return_DeliverySlip__c = null]);
        Map<Id, FixtureDeliverySlip__c> fdss = new Map<Id, FixtureDeliverySlip__c>([Select Id, Name, Combine_Pack__c, Wh_Staff__c, DeliveryCompany__c, Shippment_loaner_time__c,
                Distributor_method__c, DeliveryCompany_SlipNo__c, DeliveryType__c
                From FixtureDeliverySlip__c
                Where Id =: raesRDSMap.values()]);
        System.debug('fdss is ' + fdss);
        List<Consum_Apply_Equipment_Set__c> raess = new List<Consum_Apply_Equipment_Set__c>();
        for (Id key: raesRDSMap.keySet()) {
            //当NG_Return_DeliverySlip__c不为空时不发邮件
            if (!raessMap.containsKey(key)) {
                continue;
            }
            FixtureDeliverySlip__c fds = fdss.get(raesRDSMap.get(key));
            String message = '发货-发货运输单号:'
                            + fds.Name + '<BR>'
                            + '发货-物流公司:'
                            + fds.DeliveryCompany__c + '<BR>'
                            + '发货-运输方式:'
                            + fds.Distributor_method__c + '<BR>'
                            + '物流提货时间:'
                            + raesDateMap.get(key) + '<BR>';
            raess.add(new Consum_Apply_Equipment_Set__c(Id = key,
                    NG_Return_DeliverySlip_Information__c = message,
                    NG_Return_DeliverySlip__c = fds.Id));
        }
        if (!raess.isEmpty()) {
            update raess;
        }
    }
 
    //设置申请书的回寄通知文本
    @future
    private static void setRequestAsset_return_Text(Set<Id> raesdIdSet) {
        List<Consum_Apply_Equipment_Set_Detail__c> raesdList = [SELECT Id,
                                                                       Consum_Apply__c,
                                                                       Return_DeliverySlip__c,
                                                                       Consum_Apply_Equipment_Set__c,
                                                                       Fixture_Model_No_text__c,
                                                                       //Lost_Item_return__c,
                                                                       NoItemReturn__c,
                                                                       Consum_Apply__r.Name,
                                                                       Consum_Apply__r.Id,
                                                                       Return_DeliverySlip__r.Id,
                                                                       Return_DeliverySlip__r.Name,
                                                                       Return_DeliverySlip__r.DeliveryCompany__c,
                                                                       Return_DeliverySlip__r.Distributor_method__c,
                                                                       Return_DeliverySlip__r.Shippment_loaner_time__c
                                                                  FROM Consum_Apply_Equipment_Set_Detail__c
                                                                 WHERE Id = :raesdIdSet
                                                                   // 回寄通知只发送有链接回寄单的明细信息
                                                                   AND Return_DeliverySlip__c != null
                                                                 ORDER BY Consum_Apply__c, Consum_Apply_Equipment_Set__c, Name];
        // 收集回寄明细 {raesId -> raesdList}
        Map<Id, List<Consum_Apply_Equipment_Set_Detail__c>> raesdListMap = new Map<Id, List<Consum_Apply_Equipment_Set_Detail__c>>();
        Map<Id, Consum_Apply__c> return_DeliverySlipIdMap = new Map<Id, Consum_Apply__c>();
        Map<Id, FixtureDeliverySlip__c> return_DeliverySlipMap = new Map<Id, FixtureDeliverySlip__c>();
        for (Consum_Apply_Equipment_Set_Detail__c raesd : raesdList) {
            if (!raesdListMap.containsKey(raesd.Consum_Apply_Equipment_Set__c)) {
                raesdListMap.put(raesd.Consum_Apply_Equipment_Set__c, new List<Consum_Apply_Equipment_Set_Detail__c>());
            }
            return_DeliverySlipIdMap.put(raesd.Return_DeliverySlip__c, raesd.Consum_Apply__r);
            raesdListMap.get(raesd.Consum_Apply_Equipment_Set__c).add(raesd);
            /* --------- 设置运输单Map Start --------- */
            if (return_DeliverySlipMap.containsKey(raesd.Consum_Apply__c) == false) {
                FixtureDeliverySlip__c fds = raesd.Return_DeliverySlip__r;
                // 无实物回寄的回寄单不显示物流单信息
                if (fds.Name == raesd.Consum_Apply__r.Name + '_Dummy') {
                    continue;
                }
                return_DeliverySlipMap.put(raesd.Consum_Apply__c, fds);
            }
            /* --------- 设置运输单Map End --------- */
        }
        if (raesdListMap.isEmpty()) {
            return;
        }
        // 检索回寄的一览 {raId -> raesList}
        Map<Id, List<Consum_Apply_Equipment_Set__c>> raesListMap = new Map<Id, List<Consum_Apply_Equipment_Set__c>>();
        List<Consum_Apply_Equipment_Set__c> raesList = [SELECT Id,
                                                               //Loaner_code_F__c,
                                                               Consum_Apply__c,
                                                               //Asset_return_Status__c,
                                                               Received_Confirm__c
                                                          FROM Consum_Apply_Equipment_Set__c
                                                         WHERE Id =: raesdListMap.keySet()];
        for (Consum_Apply_Equipment_Set__c raes : raesList) {
            if (!raesListMap.containsKey(raes.Consum_Apply__c)) {
                raesListMap.put(raes.Consum_Apply__c, new List<Consum_Apply_Equipment_Set__c>());
            }
            raes.recalculateFormulas();
            raesListMap.get(raes.Consum_Apply__c).add(raes);
        }
        List<Consum_Apply__c> raList = new List<Consum_Apply__c>();
        List<Consum_Apply__c> raList2 = new List<Consum_Apply__c>();
        String message = '';
        Integer i = 0;
        // 申请书单位都是欠品回寄的明细
        Boolean allLostItemReturnFlag = true;
        // 查看所有的申请书
        for (Id raId : raesListMap.keySet()) {
            allLostItemReturnFlag = true;
            i = 0;
            // 查看申请书下面的所有一览
            for (Consum_Apply_Equipment_Set__c raes : raesListMap.get(raId)) {
                i++;
                //message += '配套' + i + ':' + raes.Loaner_code_F__c;
                String message2 = '<BR>';
                Boolean haveLost_Item_return = false;
                 // 查看一览下的所有明细
                for (Consum_Apply_Equipment_Set_Detail__c nObj : raesdListMap.get(raes.Id)) {
                    message2 += '&nbsp;&nbsp;&nbsp;&nbsp;' + nObj.Fixture_Model_No_text__c;
                    //if (nObj.Lost_Item_return__c) {
                    //    message2 += ' (欠品归还)';
                    //    haveLost_Item_return = true;
                    //}
                    //else {
                    //    // 如果有一条不是欠品回寄那么就不显示欠品回寄
                    //    allLostItemReturnFlag = false;
                    //}
                    if (nObj.NoItemReturn__c == true) {
                        message2 += ' (无实物归还)';
                    }
                    // if (nObj.Received_Confirm__c == 'NG') {
                    //     message += '(NG归还)';
                    // }
                    message2 += '<BR>';
                }
                if (haveLost_Item_return == false && raes.Received_Confirm__c == 'NG') {
                     message += ' (NG归还)';
                }
                message += message2;
            }
 
            message += '<BR>';
            FixtureDeliverySlip__c fds = new FixtureDeliverySlip__c();
            if (return_DeliverySlipMap.containsKey(raId)) {
                 fds = return_DeliverySlipMap.get(raId);
            }
            message += '回寄-发货运输单号:' + (String.isBlank(fds.Name)? '' : fds.Name) + '<BR>';
            message += '回寄-物流公司:' + (String.isBlank(fds.DeliveryCompany__c)? '' : fds.DeliveryCompany__c) + '<BR>';
            message += '回寄-运输方式:' + (String.isBlank(fds.Distributor_method__c)? '' : fds.Distributor_method__c) + '<BR>';
            String sdate = fds.Shippment_loaner_time__c == null ? '' : fds.Shippment_loaner_time__c.format();
            message += '物流提货时间:' + sdate + '<BR>';
            if (String.isNotBlank(message)) {
                raList.add(new Consum_Apply__c(Id = raId,
                                               Asset_return_Text__c = message,
                                               Lost_item_Return_Flag__c = allLostItemReturnFlag));
                raList2.add(new Consum_Apply__c(Id = raId,
                                               Asset_return_Text__c = null));
                message = '';
            }
        }
        if (raList.size() > 0) {
            FixtureUtil.withoutUpdate(raList2);
            FixtureUtil.withoutUpdate(raList);
        }
    }
 
    private void setAssginPerson(Set<Id> raId) {
        List<Consum_Apply__c> raList = [select Id, Assign_Person__c from Consum_Apply__c where Id in :raId];
        List<Consum_Apply__c> updateList = new List<Consum_Apply__c>();
        for (Consum_Apply__c ra : raList) {
            if (ra.Assign_Person__c == null) {
                ra.Assign_Person__c = UserInfo.getUserId();
                updateList.add(ra);
            }
        }
        if (updateList.size() > 0) update updateList;
    }
 
    private void changeAssetConsumable_Guaranteen_end() {
        for (Consum_Apply_Equipment_Set_Detail__c nObj : newList) {
            Consum_Apply_Equipment_Set_Detail__c oObj;
            if (trigger.isUpdate) {
                oObj = oldMap.get(nObj.Id);
            }
 
            if (oObj.Shippment_loaner_time__c == null
                    && nObj.Shippment_loaner_time__c != null
                    && nObj.NeedSet_Consumable_Guaranteen_end_F__c == true) {
                            Asset nass = assMap.get(nObj.Asset__c);
                if (assUpdMap.containsKey(nObj.Asset__c)) {
                    nass = assUpdMap.get(nObj.Asset__c);
                } else if (String.isNotBlank(nObj.Asset__c)) {
                    nass = new Asset(Id = nObj.Asset__c);
                }
                Datetime dt = nObj.Shippment_loaner_time__c.addYears(1);
                nass.Consumable_Guaranteen_end__c = Date.newinstance(dT.year(), dT.month(), dT.day());
                nass.HaveSet_Consumable_Guaranteen_end__c = true;
            }
        }
    }
    @TestVisible private static void test() {
        Integer i = 0;
       
    }
}