高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
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
public with sharing class TransferAgencyReceivedController {
    public List<EsdInfo> esdList { get; set; }
    public String apply_no { get; private set; }
    public Boolean done_flg { get; set; }
    private String taId;
    public Boolean readOnly { get;  set; }
    public FixtureDeliverySlip__c slip { get; set; }
    public List<LabelValue> outputFieldList {get; set;}
    public Boolean forceSave { get; set; }
    public Boolean needforceSave { get; set; }
    public Boolean canReturn { get; set; }
    private final static String DELIMITER = '#';
 
    public TransferAgencyReceivedController() {
        taId = ApexPages.currentPage().getParameters().get('id');
        outputFieldList = setFieldSet('TransferApplyDetail__c', 'TransferAgencyReceived');
        forceSave = false;
        needforceSave = false;
        canReturn = false;
    }
    public PageReference init() {
        esdList = new List<EsdInfo>();
        done_flg = false;
        String jsonStr;
 
        if (taId != null) {
            readOnly = false;
            List<TransferApplyDetail__c> eList = getDetails();
            if (eList.size() > 0) {
                //20210111 ljh add start
                Map<String,Boolean> rsMap = new  Map<String,Boolean>();
                for (TransferApplyDetail__c tadd : eList) {
                    Boolean isonetoone = tadd.OneToOneAccessory__c || tadd.Main_OneToOne__c;
                    if (isonetoone) {
                        //有NG或者主机欠品
                        if(rsMap.containsKey(tadd.TransferApplySummary__c) &&  !rsMap.get(tadd.TransferApplySummary__c)) {
                            //满足条件更新值
                            if((String.isNotBlank(tadd.TransferApplySummary__r.First_TAD__r.ArrivalResult__c) && tadd.TransferApplySummary__r.First_TAD__r.ArrivalResult__c == '欠品' && tadd.TransferApplySummary__r.First_TAD__r.Main_OneToOne__c)
                                ||(String.isNotBlank(tadd.ArrivalResult__c) && tadd.ArrivalResult__c == 'NG' && (tadd.TransferApplySummary__r.First_TAD__r.Main_OneToOne__c || tadd.TransferApplySummary__r.First_TAD__r.OneToOneAccessory__c))){
                                rsMap.put(tadd.TransferApplySummary__c,true);
                            }
                        }else{
                            if(!rsMap.containsKey(tadd.TransferApplySummary__c)){
                                //新加入
                                Boolean tempVal;
                                if((String.isNotBlank(tadd.TransferApplySummary__r.First_TAD__r.ArrivalResult__c)&& tadd.TransferApplySummary__r.First_TAD__r.ArrivalResult__c == '欠品')
                                    ||(String.isNotBlank(tadd.ArrivalResult__c) && tadd.ArrivalResult__c == 'NG')){
                                    tempVal = true;
                                }else{
                                    tempVal = false;
                                }
                                rsMap.put(tadd.TransferApplySummary__c, tempVal);
                            }
                        }
                    }
                    else {
                        rsMap.put(tadd.TransferApplySummary__c,false);
                    }
                }
                //20210111 ljh add end
                Boolean need = true;
                Id rsId;
                for (TransferApplyDetail__c tad : eList) {
                    if (rsId != tad.TransferApplySummary__c) {
                        need = need == false;
                    }
                    if(tad.TransferApply__r.Shipment_Slip_tmp__c != null && jsonStr == null) {
                        jsonStr = tad.TransferApply__r.Shipment_Slip_tmp__c;
                    }
                    if(String.isBlank(apply_no)) {
                        apply_no = tad.TransferApply__r.Name;
                    }
                    if (tad.TransferApply__r.Yi_loaner_arranged__c == 0 || tad.TransferApply__r.ShelfUp_ng_num__c == 0) {
                        readOnly = true;
                    }
                    EsdInfo ei = new EsdInfo(tad);
                    ei.needColor = need;
                    ei.canReturn = rsMap.get(tad.TransferApplySummary__c);//20210111 ljh add
                    esdList.add(ei);
                    rsId = tad.TransferApplySummary__c;
                }
            }
            else {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '没有调拨明细!'));
                return null;
            }
        }
        if (jsonStr != null) {
            try {
                slip = (FixtureDeliverySlip__c)JSON.deserialize(jsonStr, FixtureDeliverySlip__c.class);
            } catch (Exception ex) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, ex.getMessage()));
                if (slip == null) {
                    slip = new FixtureDeliverySlip__c();
                }
            }
        }
        else {
            if (slip == null) {
                slip = new FixtureDeliverySlip__c();
            }
        }
        if (slip.Wh_Staff__c == null) {
            slip.Wh_Staff__c = Userinfo.getUserId();
        }
        slip.IsForTransfer__c = true;
 
        return null;
    }
    private List<LabelValue> setFieldSet(String objApi, String fieldSetApi) {
        List<LabelValue> lvList = new List<LabelValue>();
        Map<String, Schema.FieldSet> fsMap = Schema.getGlobalDescribe().get(objApi).getDescribe().fieldSets.getMap();
        Schema.FieldSet fs = fsMap.get(fieldSetApi);
        List<FieldSetMember> fsmList = fs.getFields();
 
        for (FieldSetMember fsm : fsmList) {
            lvList.add(new LabelValue(fsm.getLabel(), fsm.getFieldPath()));
        }
        return lvList;
    }
    // 保存物流信息按钮
    public PageReference saveSlip() {
        done_flg = false;
        if(readOnly) return null;
        Savepoint sp = Database.setSavepoint();
        Boolean needroll = false;
        try {
            Set<Id> tsIdSet = new Set<Id>();
            Boolean haveError = false;
            List<TransferApplyDetail__c> tadList = new List<TransferApplyDetail__c>();
            List<EsdInfo> infoList = new List<EsdInfo>();
 
            Set<Id> ngSet = new Set<Id>();
            Set<Id> lostSet = new Set<Id>();
            for (EsdInfo main : esdList) {
                Boolean isonetoone = main.rec.OneToOneAccessory__c || main.rec.Main_OneToOne__c;
                if ((isonetoone || (main.rec.OneToOneAccessory__c == false && main.rec.Main_OneToOne__c == false)) && main.rec.ArrivalResult__c == 'NG') {
                    ngSet.add(main.rec.TransferApplySummary__c);
                }
                else if (isonetoone && main.rec.ArrivalResult__c == '欠品' && main.rec.Main_OneToOne__c) {
                    lostSet.add(main.rec.TransferApplySummary__c);
                }
                if (main.checked) {
                    tsIdSet.add(main.rec.TransferApplySummary__c);
                }
            }
 
            for (EsdInfo main : esdList) {
                Boolean isonetoone = main.rec.OneToOneAccessory__c || main.rec.Main_OneToOne__c;
                if (main.checked) {
                    if (ngSet.contains(main.rec.TransferApplySummary__c) == false && lostSet.contains(main.rec.TransferApplySummary__c) == false) {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, main.rec.SerialNumber_F__c + '只有主体欠品或者附属品NG的一览才可以回寄'));
                        haveError = true;
                        continue;
                    }
                    else if (isonetoone == false && main.rec.ArrivalResult__c == 'OK') {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, main.rec.SerialNumber_F__c + '只有主体欠品或者附属品NG的一览才可以回寄'));
                        haveError = true;
                        continue;
                    }
                    else {
                        tadList.add(main.rec);
                        infoList.add(main);
                    }
                }
                else if (isonetoone && tsIdSet.contains(main.rec.TransferApplySummary__c) && (ngSet.contains(main.rec.TransferApplySummary__c) || lostSet.contains(main.rec.TransferApplySummary__c))) {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, main.rec.SerialNumber_F__c + '【需整套回寄】'));
                    haveError = true;
                }
            }
            if (haveError == false) {
                if (tadList.size() == 0) {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '请选择要回寄的明细'));
                    return null;
                }
                if (slip.Id != null) {
                    slip = [
                        SELECT Combine_Pack__c, Name, Id, DeliveryCompany_SlipNo__c,DeliveryType__c,Distributor_method__c,DeliveryCompany__c,Wh_Staff__c, Wh_Staff__r.Name
                          FROM FixtureDeliverySlip__c
                         WHERE Id =:slip.Id
                           FOR UPDATE
                    ];
                }
                Map<Id, TransferApplyDetail__c> tadMap = new Map<Id, TransferApplyDetail__c>([SELECT Id
                                                             , Inspection_result_after_ng__c
                                                             , Lost_item_check_staff__c
                                                          FROM TransferApplyDetail__c
                                                         WHERE Id = :tadList
                                                           AND (Inspection_result_after_ng__c != null
                                                            OR Lost_item_check_staff__c != null)
                                                           AND Return_DeliverySlip__c != null]);
 
                tadList = new List<TransferApplyDetail__c>();
                if (tadMap.isEmpty()) {
                    slip.Shippment_loaner_time__c = Datetime.now();
                    needroll = String.isBlank(slip.Id);
                    FixtureUtil.withoutUpsertObjects(new List<FixtureDeliverySlip__c>{ slip });
                }
                for (EsdInfo main : infoList) {
                    if (tadMap.isEmpty()) {
                        main.rec.Inspection_result_after_ng__c = null;
                        main.rec.Inspection_result_after_NG_Final__c = null;
                        main.rec.After_Inspection_time__c = null;
                        main.rec.After_Inspection_time_Final__c = null;
                        main.rec.Inspection_result_after__c = null;
                        main.rec.Inspection_result_after_Final__c = null;
                        main.rec.Return_DeliverySlip__c = slip.Id;
                        main.rec.Asset_return_time__c = Datetime.now();
                        main.rec.Lost_item_check_staff__c = null;
                        main.rec.Lost_item_check_staff_Final__c = null;
                        main.rec.Lost_item_check_time__c = null;
                        main.rec.Check_lost_Item__c = null;
                        main.rec.Check_lost_Item_Final__c = null;
                        main.rec.CDS_complete__c = false;
                        main.rec.Lost_item_check_time_Final__c = null;
                        main.editable = false;
                        tadList.add(main.rec);
                    }
                    else if (tadMap.containsKey(main.rec.Id)) {
                        main.errorInfo = '明细已经做了收货确认,无法回寄,请刷新画面重试';
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, main.rec.SerialNumber_F__c + '明细已经做了收货确认,无法回寄,请刷新画面重试'));
                    }
                }
                if (tadList.size() > 0) {
                    FixtureUtil.withoutUpdate(tadList);
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM, '已回寄'));
                    return init();
                }
                else {
                    return init();
                }
            }
        }
        catch (Exception ex) {
            String msg = ex.getMessage();
            if (msg != null && msg.contains('DUPLICATE_VALUE') && msg.contains('DeliveryCompany_SlipNo__c')) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '系统已存在相同的运输单。如果继续使用该运输单回寄,请输入运输单号检索,检索后再勾选配套回寄。'));
            } else {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, msg));
            }
            Database.rollback(sp);
            done_flg = false;
            if (needroll) {
                slip.Id = null;
            }
        }
        return null;
    }
    private List<TransferApplyDetail__c> getDetails() {
        List<TransferApplyDetail__c> eList = [
                select SerialNumber_F__c, OneToOneAccessory__c, Main_OneToOne__c, TransferApplySummary__c
                        , Internal_Asset_number_key__c, Fixture_Model_No_F__c, TAESD_Status__c
                        , TransferApply__c, Shippment_loaner_time__c
                        , TransferApply__r.Shipment_Slip_tmp__c
                        , TransferApply__r.Yi_loaner_arranged__c
                        , TransferApply__r.Name
                        , Fixture_Model_No__c
                        , SerialNumber__c
                        , Detail_Finish__c
                        , Inspection_result_after_F__c
                        , Loaner_asset_no__c
                        , ArrivalResult__c
                        , Arrival_wh_Abandoned_Flag_F__c
                        , Internal_asset_location_After__c
                        , Salesdepartment_After__c
                        , Return_DeliverySlip__c
                        , Asset_return_time__c
                        , Arrival_wh_Flag_F__c
                        , FSD_OneToOneAccessory_Cnt_F__c
                        , Cancel_Select__c
                        , TransferApply__r.ShelfUp_ng_num__c
                        , Asset__r.Product2.Name
                        , LastModifiedDate
                        , OneToOneAsset__c
                        , First_You_Xiao_Ku_Cun__c
                        , TransferApplySummary__r.First_TAD__r.Loaner_accsessary__c
                        , TransferApplySummary__r.First_TAD__r.ArrivalResult__c
                        , TransferApplySummary__r.First_TAD__r.Lost_item_giveup__c
                        , TransferApplySummary__r.First_TAD__r.Arrival_in_wh__c
                        , TransferApplySummary__r.First_TAD__r.Main_OneToOne__c
                        , TransferApplySummary__r.First_TAD__r.OneToOneAccessory__c
                        , Approved_F__c, Fixture_Name_F__c, TransferApplySummary__r.Fixture_Set__r.Name
                        , Fixture_QRCode_F__c, TransferApplySummary__r.Name, Pre_inspection_time__c, StockDown__c
                        , StockDown_time__c, Id, Name, Asset__c, Asset__r.Name, Asset__r.SerialNumber, Asset__r.Product_Serial_No__c,
                       Asset__r.Remark__c, Asset__r.ImageAsset__c, Asset__r.ImageSerial__c, Asset__r.ImageAssetUploadedTime__c, Asset__r.ImageSerialUploadedTime__c,
                       Loaner_CDS_Info__c, Inspection_result__c, Check_lost_Item__c, Pre_disinfection__c, Water_leacage_check__c, Inspection_result_after__c, Arrival_in_wh__c,
                       Asset__r.Pre_Reserve_TAES_Detail__c, Asset__r.Pre_Reserve_TAES_Detail__r.After_Inspection_time__c,
                       Inspection_result_after_ng__c, Inspection_result_ng__c, Lost_item_giveup__c, CDS_complete__c, Loaner_accsessary__c,Arrival_NG_Comment__c  //20210423 ljh 1832 add Arrival_NG_Comment__c
                  from TransferApplyDetail__c
                 where TransferApply__c = :taId
                   AND Cancel_Select__c = False
                 //order by TransferApplySummary__c, Is_First_TAD_F__c DESC, TransferApplySummary__r.Name, IndexFromUniqueKey__c
                 order by TransferApplySummary__r.Name, TransferApplySummary__c,Name
        ];
        return eList;
    }
    // // 下架按钮
    // public PageReference stockup() {
 
    //     done_flg = false;
    //     needforceSave = false;
    //     String userid = Userinfo.getUserId();
    //     List<TransferApplyDetail__c> eList = new List<TransferApplyDetail__c>();
    //     List<EsdInfo> otoCheckList_main = new List<EsdInfo>();
    //     List<EsdInfo> otoAccessoryList = new List<EsdInfo>();
    //     Set<Id> assetForLock = new Set<Id>();
    //     Set<String> ukeySet = new Set<String>();
    //     Boolean haveCheck = false;
    //     Boolean haveError = false;
    //     List<EsdInfo> newOneToOne = new List<EsdInfo>();
    //     for (EsdInfo esdInfo : esdList) {
    //         if (esdInfo.checked) {
    //             haveCheck = true;
    //             esdInfo.errorInfo = null;
    //             if (esdInfo.needsetone && String.isBlank(esdInfo.rec.OneToOneAsset__c) && esdInfo.rec.Arrival_in_wh__c) {
    //                 ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, esdInfo.rec.Asset__r.Product2.Name + ':上架需要设置一对一主体'));
    //                 haveError = true;
    //             }
 
    //             if (esdInfo.accessoryFlg) {
    //                 otoAccessoryList.add(esdInfo);
    //             } else {
    //                 if (!esdInfo.editable) continue;
    //                 TransferApplyDetail__c esd = esdInfo.rec;
    //                 eList.add(esd);
    //                 if (esd.Arrival_in_wh__c) {
    //                     if (esd.FSD_OneToOneAccessory_Cnt_F__c > 0 && esd.Main_OneToOne__c && (esd.ArrivalResult__c == 'OK' || (!esd.Arrival_wh_Abandoned_Flag_F__c && esd.Arrival_wh_Flag_F__c))) {
    //                         otoCheckList_main.add(esdInfo);
    //                     } else if ((esd.ArrivalResult__c == 'OK' || (!esd.Arrival_wh_Abandoned_Flag_F__c && esd.Arrival_wh_Flag_F__c)) && esd.FSD_OneToOneAccessory_Cnt_F__c > 0 && esd.Fixture_Set_Detail__r.Is_OneToOne__c) {
    //                         otoAccessoryList.add(esdInfo);
    //                     }
    //                     String ukey =  esd.Fixture_Model_No_F__c // 型号;存放地;本部
    //                             + ';' + esd.Internal_asset_location_After__c
    //                             + ';' + esd.Salesdepartment_After__c;
    //                     ukeySet.add(ukey);
    //                 }
    //             }
    //             if (esdInfo.editable) {
    //                 assetForLock.add(esdInfo.rec.Asset__c);
    //             }
    //         }
    //     }
 
    //     if (haveCheck == false) {
    //         ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '请勾选需要操作的明细'));
    //         return null;
    //     }
    //     else if (haveError) {
    //         return null;
    //     }
 
    //     Map<String, Map<Id, List<Id>>> assProductCountMapAll = new Map<String, Map<Id, List<Id>>>(); // 一览Id->产品Id->保有设备Id列表
    //     for (EsdInfo ei : otoAccessoryList) {
    //         Map<Id, List<Id>> assProductCountMap;
    //         if (assProductCountMapAll.containsKey(ei.accessorySetId)) {
    //             assProductCountMap = assProductCountMapAll.get(ei.accessorySetId);
    //         } else {
    //             assProductCountMap = new Map<Id, List<Id>>();
    //         }
    //         Id recId;
    //         Id product2Id;
    //         if (ei.rec2 != null) {
    //             recId = ei.rec2.Id;
    //             product2Id = ei.rec2.Product2Id;
    //         } else {
    //             recId = ei.rec.Asset__c;
    //             product2Id = ei.rec.Asset__r.Product2Id;
    //         }
    //         List<Id> count;
    //         if (assProductCountMap.containsKey(product2Id)) {
    //             count = assProductCountMap.get(product2Id);
    //         } else {
    //             count = new List<Id>();
    //         }
    //         count.add(recId);
    //         assProductCountMap.put(product2Id, count);
    //         assProductCountMapAll.put(ei.accessorySetId, assProductCountMap);
    //     }
 
    //     Boolean needSaveSet = false;
    //     Boolean needSaveDetail = false;
    //     Boolean needSaveAsset = false;
 
    //     List<Fixture_OneToOne_Link__c> otolinkList = new List<Fixture_OneToOne_Link__c>();
    //     boolean err = false;
    //     Map<String, Integer> otoMap = new Map<String, Integer>();
    //     for (EsdInfo main : otoCheckList_main) {
    //         List<TransferApplyDetail__c> tadList;
    //         //没有cancel的看最新一套明细的fsd数量
    //         //已经cancel的看最早一套明细的一对一分配数量
    //         //理论上出库前上架的数据不会有非cancel的情况,所以直接用Cancel_Select__c判断
    //         system.debug(main.rec);
    //         if (main.rec.Cancel_Select__c == false) {
    //             tadList = [select Asset__r.Product2.Name, Asset__c, Asset__r.Product2Id , OneToOneAccessory__c
    //                         from TransferApplyDetail__c
    //                        where TransferApplySummary__c = :main.rec.TransferApplySummary__c
    //                              and OneToOneAccessory__c = true
    //                              and Cancel_Select__c = false //找最新的一套明细
    //                              //and Canceled_Id__c = null //找最初的一套明细
    //                              and Asset__c <> null //过滤未分配的数据。
    //                              order by OneToOneAccessory__c desc];
    //         } else {
    //             tadList = [select Asset__r.Product2.Name, Asset__c, Asset__r.Product2Id , OneToOneAccessory__c
    //                         from TransferApplyDetail__c
    //                        where TransferApplySummary__c = :main.rec.TransferApplySummary__c
    //                              and OneToOneAccessory__c = true
    //                              //and Canceled_Id__c = null
    //                              and Asset__c <> null
    //                              order by OneToOneAccessory__c desc];
    //         }
    //         system.debug(tadList);
    //         Map<Id, List<Id>> assProductCountMap = new Map<Id, List<Id>>();
    //         if (assProductCountMapAll.containsKey(main.rec.TransferApplySummary__c)) {
    //             assProductCountMap = assProductCountMapAll.get(main.rec.TransferApplySummary__c);
    //         }
 
    //         for (TransferApplyDetail__c tad : tadList) {
    //             if (assProductCountMap.containsKey(tad.Asset__r.Product2Id) && assProductCountMap.get(tad.Asset__r.Product2Id).size() > 0) {
 
    //                 List<Id> count = assProductCountMap.get(tad.Asset__r.Product2Id);
    //                 String assetId = (String)count[0];
    //                 count.remove(0);
    //                 assProductCountMap.put(tad.Asset__r.Product2Id, count);
    //                 assProductCountMapAll.put(main.rec.TransferApplySummary__c, assProductCountMap);
 
    //                 String key = (String)main.rec.Asset__c + ':' + assetId;
    //                 Integer otoCnt;
    //                 if (otoMap.containsKey(key)) {
    //                     otoCnt = otoMap.get(key);
    //                 } else {
    //                     otoCnt = 0;
    //                 }
    //                 otoMap.put(key, otoCnt + 1);
    //             } else {
    //                 if (!forceSave && (main.rec.Cancel_Select__c == false || (main.rec.Cancel_Select__c == true && tad.OneToOneAccessory__c))) {
    //                     main.errorInfo = '未补全附属品:'+tad.Asset__r.Product2.Name;
    //                     ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, main.rec.SerialNumber_F__c + '未补全附属品:'+tad.Asset__r.Product2.Name));
    //                     err = true;
    //                     needforceSave = true;
    //                     break;
    //                 } else {
    //                     continue;
    //                 }
    //             }
    //         }
    //     }
 
    //     forceSave = false;
    //     if (err) return null;
 
    //     for (String oto : otoMap.keySet()) {
    //         List<String> keys = oto.split(':');
    //         Fixture_OneToOne_Link__c otolink = new Fixture_OneToOne_Link__c();
    //         otolink.Main_Asset__c = keys[0];
    //         otolink.Accessory_Asset__c = keys[1];
    //         otolink.Quantity__c = otoMap.get(oto);
    //         otolinkList.add(otolink);
    //     }
 
    //     // TODO 没变化不存
    //     // if (Step_status == '上架') {
 
    //         for (TransferApplyDetail__c esd : eList) {
    //             if (esd.Arrival_in_wh__c) {
    //                 esd.Arrival_wh_time__c = System.now();
    //                 esd.Return_wh_chenk_staff__c = userid;
    //             } else {
    //                 esd.Arrival_wh_time__c = null;
    //                 esd.Return_wh_chenk_staff__c = null;
    //             }
    //         }
    //         needSaveDetail = true;
    //     // }
    //     for (EsdInfo esdInfo : esdList) {
    //         if (esdInfo.accessoryFlg) continue;
    //         TransferApplyDetail__c esd = esdInfo.rec;
    //         if (esd.Arrival_in_wh__c && esd.FSD_OneToOneAccessory_Cnt_F__c > 0 && esd.Main_OneToOne__c && esd.Arrival_wh_Abandoned_Flag_F__c) {
    //             otoCheckList_main.add(esdInfo);
    //         }
    //     }
    //     String sqlStr = 'SELECT Id FROM Asset WHERE Id IN:assetForLock ';
    //     String sqlWhere = '';
    //     if(! ukeySet.isEmpty()) {
    //         for(String ukey: ukeySet) {
    //             // 型号;存放地;本部
    //             List<String> fields = ukey.split(';');
    //             // 型号
    //             if (String.isNotBlank(fields[0]) && fields[0] != 'null') {
    //                 sqlWhere += ' (Fixture_Model_No_F__c =\'' + String.escapeSingleQuotes(fields[0]) + '\'';
    //             } else {
    //                 sqlWhere += ' (Fixture_Model_No_F__c = null';
    //             }
    //             // 备品存放地
    //             if (String.isNotBlank(fields[1]) && fields[1] != 'null') {
    //                 sqlWhere += ' AND Internal_asset_location__c =\'' + String.escapeSingleQuotes(fields[1]) + '\'';
    //             } else {
    //                 sqlWhere += ' AND Internal_asset_location__c = null';
    //             }
    //             // 本部
    //             if (String.isNotBlank(fields[2]) && fields[2] != 'null') {
    //                 sqlWhere += ' AND Salesdepartment__c =\'' + String.escapeSingleQuotes(fields[2]) + '\')';
    //             } else {
    //                 sqlWhere += ' AND Salesdepartment__c = null)';
    //             }
 
    //             sqlWhere += ' OR';
    //         }
    //         sqlWhere = sqlWhere.removeEnd('OR');
    //         sqlWhere = '((' + sqlWhere + ')';
    //         sqlWhere += ' AND Delete_Flag__c = false '
    //                 + ' AND Asset_Owner__c = \'Olympus\''
    //                 + ' AND AssetManageConfirm__c = true'
    //                 + ' AND Quantity > 0'
    //                 + ' AND Freeze_sign_Abandoned_Flag__c = False'
    //                 + ' AND Manage_type__c = \'数量管理\')';
    //         sqlStr += ' OR ' + sqlWhere ;
    //     }
    //     sqlStr += ' FOR UPDATE';
    //     List<Asset> astLock = Database.query(sqlStr);
    //     Savepoint sp = Database.setSavepoint();
    //     try {
    //         // 强制删废弃主体的一对一link
    //         if (otoCheckList_main.size() > 0) {
    //             List<Id> mainIds = new List<Id>();
    //             for (EsdInfo oto : otoCheckList_main) {
    //                 if (String.isNotBlank(oto.rec.Asset__c)) {
    //                     // Asset__c 念のための確認
    //                     mainIds.add(oto.rec.Asset__c);
    //                 }
    //             }
    //             FixtureUtil.delOtOLinkData(mainIds);
    //         }
 
    //         if (otolinkList.size() > 0) {
    //             // TODO 只对现场追加的一对一附属品建link
    //             if (needSaveDetail) {
    //                 FixtureUtil.withoutUpsertObjects(otolinkList);
    //                 needSaveDetail = false;
    //             }
    //         }
    //         // 这里只更新明细,保有设备和link都放到trigger里更新
    //         if (needSaveDetail) FixtureUtil.withoutUpsertObjects(eList);
 
    //         List<EsdInfo> esdL = new List<EsdInfo>();
    //         for (EsdInfo esd : esdList) {
    //             if (!esd.accessoryFlg) {
    //                 esdL.add(esd);
    //             }
    //         }
    //         init();
    //         done_flg = true;
    //     } catch (Exception ex) {
    //         system.debug('=====' + ex.getMessage());
    //         ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, ex.getMessage()+'(回库)'));
    //         Database.rollback(sp);
    //         done_flg = false;
    //     }
 
    //     return null;
    // }
 
    public PageReference cdsSave() {
        Set<Id> rndIdSet = new Set<Id>();
        Boolean haveError = false;
        done_flg = false;
        for (EsdInfo main : esdList) {
            if (main.checked) {
                rndIdSet.add(main.rec.Id);
            }
            //20210426 ljh add 1832 start
            if(String.isBlank(main.rec.ArrivalResult__c) || main.rec.ArrivalResult__c != 'NG'){
                system.debug('111:Arrival_NG_Comment__c'+main.rec.Arrival_NG_Comment__c);
                main.rec.Arrival_NG_Comment__c = '';
            }
            //20210426 ljh add 1832 start
        }
        Map<Id, TransferApplyDetail__c> tadMap = new Map<Id, TransferApplyDetail__c>();
        for (TransferApplyDetail__c tad : getTad(rndIdSet)) {
            tadMap.put(tad.Id, tad);
            if (tad.Arrival_in_wh__c) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, tad.SerialNumber_F__c + ':已经回库的明细不可以做到货确认&CDS操作'));
                haveError = true;
            }
            else if (tad.Lost_item_giveup__c) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, tad.SerialNumber_F__c + ':已经欠品放弃的明细不可以做到货确认&CDS操作'));
                haveError = true;
            }
        }
 
        for (EsdInfo main : esdList) {
            if (main.checked) {
                if (main.rec.LastModifiedDate != tadMap.get(main.rec.Id).LastModifiedDate) {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, main.rec.SerialNumber_F__c + ':已经被修改,请刷新画面重试'));
                    haveError = true;
                }
                if (String.isBlank(main.rec.ArrivalResult__c)) {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, main.rec.SerialNumber_F__c + ':您点击的按钮与当前操作不符'));
                    haveError = true;
                }
                else if (main.rec.Arrival_in_wh__c) {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, main.rec.SerialNumber_F__c + ':您点击的按钮与当前操作不符'));
                    haveError = true;
                }
                //20210426 ljh add 1832 start
                system.debug('Arrival_NG_Comment__c'+main.rec.Arrival_NG_Comment__c);
                if (main.rec.ArrivalResult__c != null && main.rec.ArrivalResult__c == 'NG' && String.isBlank(main.rec.Arrival_NG_Comment__c)) {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, main.rec.SerialNumber_F__c + ':调拨到货确认结果NG,到货NG结果备注必须填写!'));
                    haveError = true;
                }
                //20210426 ljh add 1832 end
            }
        }
        if (haveError == false) {
            return saveRecord();
        }
        else {
            return null;
        }
    }
 
    public PageReference arrival_in_whSave() {
        Set<Id> rndIdSet = new Set<Id>();
        Set<Id> allrndIdSet = new Set<Id>();
        Boolean haveError = false;
        done_flg = false;
        for (EsdInfo main : esdList) {
            if (main.checked) {
                rndIdSet.add(main.rec.Id);
                allrndIdSet.add(main.rec.TransferApplySummary__c);
            }
        }
        Map<Id, TransferApplyDetail__c> tadMap = new Map<Id, TransferApplyDetail__c>();
        for (TransferApplyDetail__c tad : getTad(allrndIdSet)) {
            if (rndIdSet.contains(tad.Id)) {
                tadMap.put(tad.Id, tad);
                if (tad.ArrivalResult__c != 'OK') {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, tad.SerialNumber_F__c + ':到货确认不是OK不能回库'));
                    haveError = true;
                }
                else if (tad.CDS_complete__c == false) {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, tad.SerialNumber_F__c + ':没有做CDS保存不能回库'));
                    haveError = true;
                }
                else if (tad.Lost_item_giveup__c) {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, tad.SerialNumber_F__c + ':已经欠品放弃的明细不能回库'));
                    haveError = true;
                }
                else if (tad.Arrival_in_wh__c) {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, tad.SerialNumber_F__c + ':已经回库的明细不能再回库'));
                    haveError = true;
                }
            }
            else if (tad.ArrivalResult__c != '欠品' && tad.Cancel_Select__c == false
                && (tad.Main_OneToOne__c || tad.OneToOneAccessory__c)
                && !tad.Detail_Finish__c
                && tad.TransferApplySummary__r.First_TAD__r.Arrival_in_wh__c == false
            ) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, tad.SerialNumber_F__c + ':回库时必须整套回库'));
                haveError = true;
            }
        }
 
        for (EsdInfo main : esdList) {
            if (main.checked) {
                if (main.rec.LastModifiedDate != tadMap.get(main.rec.Id).LastModifiedDate) {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, main.rec.SerialNumber_F__c + ':已经被修改,请刷新画面重试'));
                    haveError = true;
                }
                if (main.rec.ArrivalResult__c != 'OK') {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, main.rec.SerialNumber_F__c + ':您点击的按钮与当前操作不符'));
                    haveError = true;
                }
                else if (main.rec.CDS_complete__c == false) {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, main.rec.SerialNumber_F__c + ':您点击的按钮与当前操作不符'));
                    haveError = true;
                }
                else if (main.rec.Arrival_in_wh__c == false) {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, main.rec.SerialNumber_F__c + ':您点击的按钮与当前操作不符'));
                    haveError = true;
                }
            }
        }
 
        if (haveError == false) {
            return saveRecord();
        }
        else {
            return null;
        }
    }
 
    private List<TransferApplyDetail__c> getTad(Set<Id> rndIdSet) {
        return [select SerialNumber_F__c, OneToOneAccessory__c, Main_OneToOne__c, TransferApplySummary__c
                    , Internal_Asset_number_key__c, Fixture_Model_No_F__c, TAESD_Status__c
                    , TransferApply__c, Shippment_loaner_time__c
                    , TransferApply__r.Shipment_Slip_tmp__c
                    , TransferApply__r.Yi_loaner_arranged__c
                    , TransferApply__r.Name
                    , Fixture_Model_No__c
                    , SerialNumber__c
                    , Detail_Finish__c
                    , Inspection_result_after_F__c
                    , Loaner_asset_no__c
                    , ArrivalResult__c
                    , Arrival_wh_Abandoned_Flag_F__c
                    , Internal_asset_location_After__c
                    , Salesdepartment_After__c
                    , Return_DeliverySlip__c
                    , Asset_return_time__c
                    , Arrival_wh_Flag_F__c
                    , FSD_OneToOneAccessory_Cnt_F__c
                    , Cancel_Select__c
                    , TransferApply__r.ShelfUp_ng_num__c
                    , Asset__r.Product2.Name
                    , LastModifiedDate
                    , OneToOneAsset__c
                    , Approved_F__c, Fixture_Name_F__c, TransferApplySummary__r.Fixture_Set__r.Name
                    , Fixture_QRCode_F__c, TransferApplySummary__r.Name, Pre_inspection_time__c, StockDown__c
                    , TransferApplySummary__r.First_TAD__r.Loaner_accsessary__c
                    , First_You_Xiao_Ku_Cun__c
                    , TransferApplySummary__r.First_TAD__r.ArrivalResult__c
                    , TransferApplySummary__r.First_TAD__r.Lost_item_giveup__c
                    , TransferApplySummary__r.First_TAD__r.Arrival_in_wh__c
                    , TransferApplySummary__r.First_TAD__r.Main_OneToOne__c
                    , TransferApplySummary__r.First_TAD__r.OneToOneAccessory__c
                    , StockDown_time__c, Id, Name, Asset__c, Asset__r.Name, Asset__r.SerialNumber, Asset__r.Product_Serial_No__c,
                   Asset__r.Remark__c, Asset__r.ImageAsset__c, Asset__r.ImageSerial__c, Asset__r.ImageAssetUploadedTime__c, Asset__r.ImageSerialUploadedTime__c,
                   Loaner_CDS_Info__c, Inspection_result__c, Check_lost_Item__c, Pre_disinfection__c, Water_leacage_check__c, Inspection_result_after__c, Arrival_in_wh__c,
                   Asset__r.Pre_Reserve_TAES_Detail__c, Asset__r.Pre_Reserve_TAES_Detail__r.After_Inspection_time__c,
                   Inspection_result_after_ng__c, Inspection_result_ng__c, Lost_item_giveup__c, CDS_complete__c, Loaner_accsessary__c,Arrival_NG_Comment__c //20210426 ljh 1832 add Arrival_NG_Comment__c
              from TransferApplyDetail__c
             where Id = :rndIdSet OR TransferApplySummary__c = :rndIdSet FOR UPDATE];
    }
 
 
    // 保存按钮
    public PageReference saveRecord() {
        done_flg = false;
        needforceSave = false;
        String userid = Userinfo.getUserId();
        List<TransferApplyDetail__c> eList = new List<TransferApplyDetail__c>();
        List<EsdInfo> otoCheckList_main = new List<EsdInfo>();
        List<EsdInfo> otoAccessoryList = new List<EsdInfo>();
        Set<Id> assetForLock = new Set<Id>();
        Set<String> ukeySet = new Set<String>();
        Boolean haveError = false;
        Boolean haveCheck = false;
 
        List<EsdInfo> otoList = new List<EsdInfo>();
        Map<Id, List<EsdInfo>> checkMap = new Map<Id, List<EsdInfo>>();
        Set<Id> ngSet = new Set<Id>();
        Set<Id> lostSet = new Set<Id>();
        for (EsdInfo main : esdList) {
            if ((main.rec.OneToOneAccessory__c || main.rec.Main_OneToOne__c)
                && (main.rec.TransferApplySummary__r.First_TAD__r.Arrival_in_wh__c == false
                    && main.rec.TransferApplySummary__r.First_TAD__r.Loaner_accsessary__c == false
                    && main.rec.TransferApplySummary__r.First_TAD__r.Lost_item_giveup__c == false)
            ) {
                if (main.rec.ArrivalResult__c == 'NG') {
                    ngSet.add(main.rec.TransferApplySummary__c);
                }
                else if (main.rec.ArrivalResult__c == '欠品' && main.rec.Main_OneToOne__c) {
                    lostSet.add(main.rec.TransferApplySummary__c);
                }
            }
        }
 
        Id tsid;
 
        EsdInfo info;
        for (EsdInfo esdInfo : esdList) {
            tsid = esdInfo.rec.TransferApplySummary__c;
            if (esdInfo.checked) {
                if (esdInfo.rec.Main_OneToOne__c) {
                    info = esdInfo;
                }
                else if (esdInfo.rec.Main_OneToOne__c == false && esdInfo.rec.OneToOneAccessory__c == false) {
                    info = null;
                }
 
                haveCheck = true;
                esdInfo.errorInfo = null;
                if (esdInfo.needsetone && String.isBlank(esdInfo.rec.OneToOneAsset__c) && esdInfo.rec.Arrival_in_wh__c) {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, esdInfo.rec.SerialNumber_F__c + ':上架需要设置一对一主体'));
                    haveError = true;
                }
                if (esdInfo.rec.OneToOneAccessory__c
                    && esdInfo.rec.TransferApplySummary__r.First_TAD__r.Arrival_in_wh__c
                    && esdInfo.rec.First_You_Xiao_Ku_Cun__c == 0) {
                    esdInfo.rec.SkipLink__c = true;
                    continue;
                }
                else if (esdInfo.accessoryFlg) {
                    otoAccessoryList.add(esdInfo);
                } else {
                    if (!esdInfo.editable) continue;
                    TransferApplyDetail__c esd = esdInfo.rec;
                    eList.add(esd);
                    // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '1'));
                    if (esd.Arrival_in_wh__c) {
                        if (info == null && esd.ArrivalResult__c == 'NG') {
                            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '到货NG的附属品不能上架,请做回寄操作'));
                            haveError = true;
                        }
                        if (ngSet.contains(esdInfo.rec.TransferApplySummary__c)) {
                            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, esd.SerialNumber_F__c + ':有到货确认NG的一览不可以上架'));
                            haveError = true;
                        }
                        if (esd.Main_OneToOne__c && (esd.ArrivalResult__c == 'OK' || (!esd.Arrival_wh_Abandoned_Flag_F__c && esd.Arrival_wh_Flag_F__c))) {
                            otoCheckList_main.add(esdInfo);
                            // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '3'));
                        } else if ((esd.ArrivalResult__c == 'OK' || (!esd.Arrival_wh_Abandoned_Flag_F__c && esd.Arrival_wh_Flag_F__c)) && esd.OneToOneAccessory__c) {
                            otoAccessoryList.add(esdInfo);
                            if (info == null && !forceSave && (esdInfo.rec.TransferApplySummary__r.First_TAD__r.Arrival_in_wh__c && esdInfo.rec.First_You_Xiao_Ku_Cun__c > 0) == false) {
                                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, esd.SerialNumber_F__c + ':未选择主体一对一主体'));
                                // needforceSave = true;
                                haveError = true;
                            }
                        }
                        String ukey =  esd.Fixture_Model_No_F__c // 型号;存放地;本部
                                + ';' + esd.Internal_asset_location_After__c
                                + ';' + esd.Salesdepartment_After__c;
                        ukeySet.add(ukey);
                    }
                }
                if (esdInfo.editable) {
                    assetForLock.add(esdInfo.rec.Asset__c);
                }
                if (String.isNotBlank(esdInfo.rec.OneToOneAsset__c) && esdInfo.needsetone && esdInfo.rec.Arrival_in_wh__c) {
                    if (checkMap.containsKey(esdInfo.rec.OneToOneAsset__c) == false) {
                        checkMap.put(esdInfo.rec.OneToOneAsset__c, new List<EsdInfo>());
                    }
                    checkMap.get(esdInfo.rec.OneToOneAsset__c).add(esdInfo);
                    otoList.add(esdInfo);
                }
            }
            else if (info != null && info.rec.Arrival_in_wh__c && info.rec.TransferApplySummary__c == esdInfo.rec.TransferApplySummary__c && esdInfo.rec.ArrivalResult__c != '欠品') {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, esdInfo.rec.SerialNumber_F__c + ':一对一非欠品的附属品必须统一上架'));
                haveError = true;
            }
        }
 
        if (haveCheck == false) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '请勾选需要操作的明细'));
            return null;
        }
        else if (haveError) {
            return null;
        }
 
        if (checkMap.isEmpty() == false) {
            // 20220211 ljh SFDC-CBH5GH update start
            /*for (Asset ass : [SELECT Id FROM ASSET WHERE Asset_Owner__c = 'Olympus'
                                          AND Asset_loaner_category__c != '耗材'
                                          AND RecordTypeId = '01210000000kOPR'
                                          AND Delete_Flag__c = False
                                          AND AssetManageConfirm__c = true
                                          // AND Account.Management_Code__c =:System.Label.Account_Asset
                                          AND Loaner_accsessary__c = false
                                          AND Freeze_sign_Abandoned_Flag__c = false
                                          AND Last_Reserve_RAES_Detail__c = null
                                          AND You_Xiao_Ku_Cun__c > 0
                                          AND Id = :checkMap.keySet()]) {
                checkMap.remove(ass.Id);
            }*/
            // 调拨至 的办事处
            String locationAfter = esdList[0].rec.Internal_asset_location_After__c;
            Map<Id, Asset> checkAssetMap = new Map<Id, Asset>();
            List<Asset> checkAssetList = [SELECT Id,Internal_asset_location__c FROM ASSET WHERE Asset_Owner__c = 'Olympus'
                                          AND Asset_loaner_category__c != '耗材'
                                          AND RecordTypeId = '01210000000kOPR'
                                          AND Delete_Flag__c = False
                                          AND AssetManageConfirm__c = true
                                          // AND Account.Management_Code__c =:System.Label.Account_Asset
                                          AND Loaner_accsessary__c = false
                                          AND Freeze_sign_Abandoned_Flag__c = false
                                          AND Last_Reserve_RAES_Detail__c = null
                                          AND You_Xiao_Ku_Cun__c > 0
                                          AND Internal_asset_location__c = :locationAfter
                                          AND Id = :checkMap.keySet()];
            for (Asset ass : checkAssetList) {
                checkMap.remove(ass.Id);
            }
            if (checkMap.isEmpty() == false) {
                checkAssetMap.putAll([SELECT Id,Internal_asset_location__c,SerialNumber,Fixture_Model_No_F__c FROM ASSET WHERE  Id = :checkMap.keySet()]);
                for (Id assId : checkMap.keySet()) {
                    for (EsdInfo esdInfo : checkMap.get(assId)) {
                        // esdInfo.errorInfo = '保有设备:'+esdInfo.rec.SerialNumber_F__c + '请选择合适的主体';
                        // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '保有设备:'+esdInfo.rec.SerialNumber_F__c + '请选择合适的主体'));
                        // haveError = true;
                        Asset checkAsset = checkAssetMap.get(assId);
                        if(checkAsset != null && checkAsset.Internal_asset_location__c != locationAfter){
                            // esdInfo.errorInfo = '保有设备:'+esdInfo.rec.SerialNumber_F__c + '和主体备品存放地不一致,请选择合适的主体';
                            // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '保有设备:'+esdInfo.rec.SerialNumber_F__c + '和主体备品存放地不一致,请选择合适的主体'));
                            esdInfo.errorInfo = '附属品 '+esdInfo.rec.Fixture_Model_No__c+'('+esdInfo.rec.SerialNumber_F__c + ')与主体 '+checkAsset.Fixture_Model_No_F__c+'('+checkAsset.SerialNumber+')备品存放地不一致,不能入库';
                            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '附属品 '+esdInfo.rec.Fixture_Model_No__c+'('+esdInfo.rec.SerialNumber_F__c + ')与主体 '+checkAsset.Fixture_Model_No_F__c+'('+checkAsset.SerialNumber+')备品存放地不一致,不能入库'));
                            haveError = true;
                        }else{
                            esdInfo.errorInfo = '保有设备:'+esdInfo.rec.SerialNumber_F__c + '请选择合适的主体';
                            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '保有设备:'+esdInfo.rec.SerialNumber_F__c + '请选择合适的主体'));
                            haveError = true;
                        }
                        // 20220211 ljh SFDC-CBH5GH update end
                    }
                }
            }
            // 20220211 ljh SFDC-CBH5GH update end
        }
 
        if (haveError) {
            return null;
        }
 
        Map<String, Map<Id, List<Id>>> assProductCountMapAll = new Map<String, Map<Id, List<Id>>>(); // 一览Id->产品Id->保有设备Id列表
        for (EsdInfo ei : otoAccessoryList) {
            Map<Id, List<Id>> assProductCountMap;
            if (assProductCountMapAll.containsKey(ei.accessorySetId)) {
                assProductCountMap = assProductCountMapAll.get(ei.accessorySetId);
            } else {
                assProductCountMap = new Map<Id, List<Id>>();
            }
            Id recId;
            Id product2Id;
            if (ei.rec2 != null) {
                recId = ei.rec2.Id;
                product2Id = ei.rec2.Product2Id;
            } else {
                recId = ei.rec.Asset__c;
                product2Id = ei.rec.Asset__r.Product2Id;
            }
            List<Id> count;
            if (assProductCountMap.containsKey(product2Id)) {
                count = assProductCountMap.get(product2Id);
            } else {
                count = new List<Id>();
            }
            count.add(recId);
            assProductCountMap.put(product2Id, count);
            assProductCountMapAll.put(ei.accessorySetId, assProductCountMap);
        }
 
        Boolean needSaveSet = false;
        Boolean needSaveDetail = false;
        Boolean needSaveAsset = false;
 
        List<Fixture_OneToOne_Link__c> otolinkList = new List<Fixture_OneToOne_Link__c>();
        boolean err = false;
        Map<String, Integer> otoMap = new Map<String, Integer>();
 
        List<String> soqlList = new List<String>();
        if (otoCheckList_main.size() > 0) {
            for (EsdInfo main : otoCheckList_main) {
                if (main.rec.Cancel_Select__c == false) {
                    String soql = '(TransferApplySummary__c = \'' + main.rec.TransferApplySummary__c + '\' and Cancel_Select__c = false )';
                    soqlList.add(soql);
                }
                else {
                    String soql = '(TransferApplySummary__c = \'' + main.rec.TransferApplySummary__c + '\')';
                    soqlList.add(soql);
                }
            }
 
            String soql = 'select Asset__r.Product2.Name, Asset__c, Asset__r.Product2Id , OneToOneAccessory__c, TransferApplySummary__c,OneToOneAsset__c '
                        + ' from TransferApplyDetail__c '
                        + ' WHERE OneToOneAccessory__c = true and Asset__c <> null '
                        + ' AND (' + String.join(soqlList, ' OR ') + ') order by TransferApplySummary__c, OneToOneAccessory__c desc';
 
            Map<Id, List<TransferApplyDetail__c>> tadMap = new Map<Id, List<TransferApplyDetail__c>>();
            System.debug(soql);
            for (TransferApplyDetail__c tad : Database.query(soql)) {
                if (tadMap.containsKey(tad.TransferApplySummary__c) == false) {
                    tadMap.put(tad.TransferApplySummary__c, new List<TransferApplyDetail__c>());
                }
                tadMap.get(tad.TransferApplySummary__c).add(tad);
            }
            for (EsdInfo main : otoCheckList_main) {
                List<TransferApplyDetail__c> tadList = new List<TransferApplyDetail__c>();
                if (tadMap.containsKey(main.rec.TransferApplySummary__c)) {
                    tadList = tadMap.get(main.rec.TransferApplySummary__c);
                }
                //没有cancel的看最新一套明细的fsd数量
                //已经cancel的看最早一套明细的一对一分配数量
                //理论上出库前上架的数据不会有非cancel的情况,所以直接用Cancel_Select__c判断
                // system.debug(main.rec);
                // if (main.rec.Cancel_Select__c == false) {
                //     tadList = [select Asset__r.Product2.Name, Asset__c, Asset__r.Product2Id , OneToOneAccessory__c
                //                 from TransferApplyDetail__c
                //                where TransferApplySummary__c = :main.rec.TransferApplySummary__c
                //                      and OneToOneAccessory__c = true
                //                      and Cancel_Select__c = false //找最新的一套明细
                //                      //and Canceled_Id__c = null //找最初的一套明细
                //                      and Asset__c <> null //过滤未分配的数据。
                //                      order by OneToOneAccessory__c desc];
                // } else {
                //     tadList = [select Asset__r.Product2.Name, Asset__c, Asset__r.Product2Id , OneToOneAccessory__c
                //                 from TransferApplyDetail__c
                //                where TransferApplySummary__c = :main.rec.TransferApplySummary__c
                //                      and OneToOneAccessory__c = true
                //                      //and Canceled_Id__c = null
                //                      and Asset__c <> null
                //                      order by OneToOneAccessory__c desc];
                // }
                system.debug(tadList);
                Map<Id, List<Id>> assProductCountMap = new Map<Id, List<Id>>();
                if (assProductCountMapAll.containsKey(main.rec.TransferApplySummary__c)) {
                    assProductCountMap = assProductCountMapAll.get(main.rec.TransferApplySummary__c);
                }
 
                for (TransferApplyDetail__c tad : tadList) {
                    if (assProductCountMap.containsKey(tad.Asset__r.Product2Id) && assProductCountMap.get(tad.Asset__r.Product2Id).size() > 0) {
 
                        List<Id> count = assProductCountMap.get(tad.Asset__r.Product2Id);
                        String assetId = (String)count[0];
                        count.remove(0);
                        assProductCountMap.put(tad.Asset__r.Product2Id, count);
                        assProductCountMapAll.put(main.rec.TransferApplySummary__c, assProductCountMap);
 
                        String key = (String)main.rec.Asset__c + ':' + assetId;
                        Integer otoCnt;
                        if (otoMap.containsKey(key)) {
                            otoCnt = otoMap.get(key);
                        } else {
                            otoCnt = 0;
                        }
                        otoMap.put(key, otoCnt + 1);
                    } else {
                        // if (!forceSave && (main.rec.Cancel_Select__c == false || (main.rec.Cancel_Select__c == true && tad.OneToOneAccessory__c))) {
                        //     main.errorInfo = '未补全附属品(部分上架请勾选复选框):'+tad.Asset__r.Product2.Name;
                        //     ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, main.rec.SerialNumber_F__c + '未补全附属品:'+tad.Asset__r.Product2.Name));
                        //     err = true;
                        //     needforceSave = true;
                        //     break;
                        // } else {
                        //     continue;
                        // }
                    }
                }
            }
        }
        Set<Id> tdIdSet = new Set<Id>();
        forceSave = false;
        if (err) return null;
        // TODO 没变化不存
        // if (Step_status == '上架') {
 
            for (TransferApplyDetail__c esd : eList) {
                if (esd.Arrival_in_wh__c) {
                    esd.Arrival_wh_time__c = System.now();
                    esd.Return_wh_chenk_staff__c = userid;
                } else {
                    esd.Arrival_wh_time__c = null;
                    esd.Return_wh_chenk_staff__c = null;
                }
                tdIdSet.add(esd.Id);
            }
            needSaveDetail = true;
        // }
        for (EsdInfo esdInfo : esdList) {
            if (esdInfo.accessoryFlg) continue;
            TransferApplyDetail__c esd = esdInfo.rec;
            if (esd.Arrival_in_wh__c && esd.FSD_OneToOneAccessory_Cnt_F__c > 0 && esd.Main_OneToOne__c && esd.Arrival_wh_Abandoned_Flag_F__c) {
                otoCheckList_main.add(esdInfo);
            }
        }
        String sqlStr = 'SELECT Id FROM Asset WHERE Id IN:assetForLock ';
        String sqlWhere = '';
        if(! ukeySet.isEmpty()) {
            for(String ukey: ukeySet) {
                // 型号;存放地;本部
                List<String> fields = ukey.split(';');
                // 型号
                if (String.isNotBlank(fields[0]) && fields[0] != 'null') {
                    sqlWhere += ' (Fixture_Model_No_F__c =\'' + String.escapeSingleQuotes(fields[0]) + '\'';
                } else {
                    sqlWhere += ' (Fixture_Model_No_F__c = null';
                }
                // 备品存放地
                if (String.isNotBlank(fields[1]) && fields[1] != 'null') {
                    sqlWhere += ' AND Internal_asset_location__c =\'' + String.escapeSingleQuotes(fields[1]) + '\'';
                } else {
                    sqlWhere += ' AND Internal_asset_location__c = null';
                }
                // 本部
                if (String.isNotBlank(fields[2]) && fields[2] != 'null') {
                    sqlWhere += ' AND Salesdepartment__c =\'' + String.escapeSingleQuotes(fields[2]) + '\')';
                } else {
                    sqlWhere += ' AND Salesdepartment__c = null)';
                }
 
                sqlWhere += ' OR';
            }
            sqlWhere = sqlWhere.removeEnd('OR');
            sqlWhere = '((' + sqlWhere + ')';
            sqlWhere += ' AND Delete_Flag__c = false '
                    + ' AND Asset_Owner__c = \'Olympus\''
                    + ' AND AssetManageConfirm__c = true'
                    + ' AND Quantity > 0'
                    + ' AND Freeze_sign_Abandoned_Flag__c = False'
                    + ' AND Manage_type__c = \'数量管理\')';
            sqlStr += ' OR ' + sqlWhere ;
        }
        sqlStr += ' FOR UPDATE';
        List<Asset> astLock = Database.query(sqlStr);
        Savepoint sp = Database.setSavepoint();
        try {
            // 强制删废弃主体的一对一link
            if (otoCheckList_main.size() > 0) {
                List<Id> mainIds = new List<Id>();
                for (EsdInfo oto : otoCheckList_main) {
                    if (String.isNotBlank(oto.rec.Asset__c)) {
                        // Asset__c 念のための確認
                        mainIds.add(oto.rec.Asset__c);
                    }
                }
                FixtureUtil.delOtOLinkData(mainIds);
            }
 
            // 这里只更新明细,保有设备和link都放到trigger里更新
            if (eList.size() > 0) {
                FixtureUtil.withoutUpsertObjects(eList);
            }
 
            if (otoList.size() > 0) {
                soqlList = new List<String>();
                Map<String, Fixture_OneToOne_Link__c> otoMap1 = new Map<String, Fixture_OneToOne_Link__c>(); // 主Id附Id->link
                Map<String, Integer> otoMap2 = new Map<String, Integer>(); // 主Id、主型号、附型号->数量
                Map<Id, TransferApplyDetail__c> tdMap = new Map<Id, TransferApplyDetail__c>([
                    SELECT Id
                    , OneToOneAsset__c
                    , OneToOneAsset__r.Fixture_Model_No_F__c
                    , OneToOneAsset__r.SerialNumber
                    , Asset__c
                    , Asset_After__c
                    , Asset_After__r.Fixture_Model_No_F__c
                    , Asset_After__r.SerialNumber
                    , Asset__r.Fixture_Model_No_F__c
                    , Asset__r.SerialNumber
                    FROM TransferApplyDetail__c WHERE Id = :tdIdSet]);
                Map<String, Integer> standardLinkCount = getLinkCountFromStandard(tdMap);
                Map<Id, String> assetIdToModel = getAssetIdToModel(tdMap);
                Map<Id, String> assetIdToSerial = getAssetIdToSerial(tdMap);
                System.debug(assetIdToModel);
                for (EsdInfo esdInfo : otoList) {
                    Id afterId = esdInfo.rec.Asset__c;
                    if(tdMap.containsKey(esdInfo.rec.Id) && tdMap.get(esdInfo.rec.Id).Asset_After__c != null) {
                        afterId = tdMap.get(esdInfo.rec.Id).Asset_After__c;
                    }
                    // soqlList.add('(Main_Asset__c = \'' + esdInfo.rec.OneToOneAsset__c + '\' AND Accessory_Asset__c = \''+ afterId + '\')');
                    // 主体下已有的全部link要参与检查
                    soqlList.add('(Main_Asset__c = \'' + esdInfo.rec.OneToOneAsset__c  + '\')');
                }
                String linkSql = 'SELECT Quantity__c, Main_Asset__c, Main_Asset__r.Fixture_Model_No_F__c, Main_Asset__r.SerialNumber, Accessory_Asset__c, Accessory_Asset__r.Fixture_Model_No_F__c FROM Fixture_OneToOne_Link__c WHERE ' + String.join(soqlList, ' OR ');
 
                for (Fixture_OneToOne_Link__c oto : Database.query(linkSql)) {
                    otoMap1.put(String.valueOf(oto.Main_Asset__c) + String.valueOf(oto.Accessory_Asset__c), oto);
                    // 机身号不同的同型号主体,link要分开计数
                    String serialLinkName = String.join(new List<String> {
                        oto.Main_Asset__r.Fixture_Model_No_F__c,
                        oto.Main_Asset__r.SerialNumber,
                        oto.Accessory_Asset__r.Fixture_Model_No_F__c
                    }, DELIMITER);
                    Integer cnt = 0;
                    if(otoMap2.containsKey(serialLinkName)) {
                        cnt = otoMap2.get(serialLinkName);
                    }
                    otoMap2.put(serialLinkName, cnt + Integer.valueOf(oto.Quantity__c));
                }
                System.debug(otoMap2);
 
                Map<String, Fixture_OneToOne_Link__c> otoUpsertMap = new Map<String, Fixture_OneToOne_Link__c>();
                for (EsdInfo esdInfo : otoList) {
                    Id afterId = esdInfo.rec.Asset__c;
                    if(tdMap.containsKey(esdInfo.rec.Id) && tdMap.get(esdInfo.rec.Id).Asset_After__c != null) {
                        afterId = tdMap.get(esdInfo.rec.Id).Asset_After__c;
                    }
                    // 主Id附Id作为key,新建link或修改link数量
                    String key = String.valueOf(esdInfo.rec.OneToOneAsset__c) + afterId;
                    if (otoUpsertMap.containsKey(key) == false) {
                        Fixture_OneToOne_Link__c oto = new Fixture_OneToOne_Link__c();
                        if (otoMap1.containsKey(key)) {
                            oto = otoMap1.get(key);
                        }
                        else {
                            oto.Main_Asset__c = esdInfo.rec.OneToOneAsset__c;
                            oto.Accessory_Asset__c = tdMap.get(esdInfo.rec.Id).Asset_After__c;
                            oto.Quantity__c = 0;
                        }
                        otoUpsertMap.put(key, oto);
                    }
                    otoUpsertMap.get(key).Quantity__c += 1;
                    System.debug(otoUpsertMap);
 
                    // 主型号、附型号作为key,和标准配套做比较
                    String linkName = String.join(new List<String> {
                        assetIdToModel.get(esdInfo.rec.OneToOneAsset__c),
                        assetIdToModel.get(afterId)
                    }, DELIMITER);
                    // 主型号,主机身号,附型号作为key,用于和标准配套明细数量比较
                    // 不同主体下的link要分别计数,新入的附属品link要和既存的link按型号合并后计数
                    String serialLinkName = String.join(new List<String> {
                        assetIdToModel.get(esdInfo.rec.OneToOneAsset__c),
                        assetIdToSerial.get(esdInfo.rec.OneToOneAsset__c),
                        assetIdToModel.get(afterId)
                    }, DELIMITER);
                    // 主型号,主机身号,附型号,附机身号,用于画面提醒
                    String serialLinkNameFull = String.join(new List<String> {
                        assetIdToModel.get(esdInfo.rec.OneToOneAsset__c),
                        assetIdToSerial.get(esdInfo.rec.OneToOneAsset__c),
                        assetIdToModel.get(afterId),
                        esdInfo.rec.SerialNumber_F__c
                    }, DELIMITER);
                    System.debug(linkName);
                    System.debug(serialLinkName);
                    System.debug(serialLinkNameFull);
 
                    Integer cnt = 0;
                    if(otoMap2.containsKey(serialLinkName)) {
                        cnt = otoMap2.get(serialLinkName);
                    }
                    otoMap2.put(serialLinkName, cnt + 1);
                    System.debug(otoMap2);
                    System.debug(standardLinkCount);
 
                    if(!standardLinkCount.containsKey(linkName)) {
                        throw new ControllerUtil.myException(String.format('附属品 {2}({3}):不属于主体 {0}({1})配套中附属品,不能入库', serialLinkNameFull.split(DELIMITER)));
                    }
                    else if(standardLinkCount.get(linkName) < otoMap2.get(serialLinkName)) {
                        throw new ControllerUtil.myException(String.format('附属品 {2}({3}):超过主体 {0}({1})配套中该型号附属品数量,多余数量不能入库', serialLinkNameFull.split(DELIMITER)));
                    }
                }
                if (otoUpsertMap.isEmpty() == false) {
                    FixtureUtil.withoutUpsertObjects(otoUpsertMap.values());
                }
            }
 
            List<EsdInfo> esdL = new List<EsdInfo>();
            for (EsdInfo esd : esdList) {
                if (!esd.accessoryFlg) {
                    esdL.add(esd);
                }
            }
            init();
            done_flg = true;
        } catch (Exception ex) {
            system.debug('=====' + ex.getMessage());
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, ex.getMessage()));
            Database.rollback(sp);
            done_flg = false;
        }
 
        return null;
    }
    /**
     * @return 设备id->型号
     *
     * */
    private Map<Id, String> getAssetIdToModel(Map<Id, TransferApplyDetail__c> tdMap) {
        Map<Id, String> assetIdToModel = new Map<Id, String> ();
        for(TransferApplyDetail__c tad:tdMap.values()) {
            assetIdToModel.put(tad.OneToOneAsset__c, tad.OneToOneAsset__r.Fixture_Model_No_F__c);
            assetIdToModel.put(tad.Asset_After__c, tad.Asset_After__r.Fixture_Model_No_F__c);
            assetIdToModel.put(tad.Asset__c, tad.Asset__r.Fixture_Model_No_F__c);
        }
        return assetIdToModel;
    }
    /**
     * @return 设备id->机身号
     *
     * */
    private Map<Id, String> getAssetIdToSerial(Map<Id, TransferApplyDetail__c> tdMap) {
        Map<Id, String> assetIdToSerial = new Map<Id, String> ();
        for(TransferApplyDetail__c tad:tdMap.values()) {
            assetIdToSerial.put(tad.OneToOneAsset__c, tad.OneToOneAsset__r.SerialNumber);
            assetIdToSerial.put(tad.Asset_After__c, tad.Asset_After__r.SerialNumber);
            assetIdToSerial.put(tad.Asset__c, tad.Asset__r.SerialNumber);
        }
        return assetIdToSerial;
    }
    /**
     * @description 按所选主体的型号找到配套明细及数量
     * @return 主型号;附型号->数量
    */
    private Map<String, Integer> getLinkCountFromStandard(Map<Id, TransferApplyDetail__c> tdMap) {
        Map<String, Integer> mainModels = new Map<String, Integer>();
        Map<String, Integer> linkCounts = new Map<String, Integer>();
        for(TransferApplyDetail__c tad:tdMap.values()) {
            mainModels.put(tad.OneToOneAsset__r.Fixture_Model_No_F__c, 0);
        }
        List<Fixture_Set_Detail__c> fsdList = [SELECT Id
            , Quantity__c
            , Fixture_Model_No_F__c
            , Fixture_Set__r.Fixture_Set_Body_Model_No__c
            , Is_Body__c
            , Is_OneToOne__c
            FROM Fixture_Set_Detail__c
            WHERE Fixture_Set__r.Fixture_Set_Body_Model_No__c IN:mainModels.keySet()
            ORDER BY Fixture_Set__c, SortInt_F__c
        ];
        for(Fixture_Set_Detail__c fsd:fsdList){
            // 主体型号不可有多个配套
            if(mainModels.get(fsd.Fixture_Model_No_F__c) == 1) {
                throw new ControllerUtil.myException(fsd.Fixture_Model_No_F__c + '主体对应多个配套,无法继续');
            }
            if(fsd.Is_Body__c) {
                mainModels.put(fsd.Fixture_Model_No_F__c, 1);
            }
            else if (fsd.Is_OneToOne__c) {
                Integer c = 0;
                String linkName = fsd.Fixture_Set__r.Fixture_Set_Body_Model_No__c + DELIMITER + fsd.Fixture_Model_No_F__c;
                if (linkCounts.containsKey(linkName)) {
                    c = linkCounts.get(linkName);
                }
                linkCounts.put(linkName, c + Integer.valueOf(fsd.Quantity__c));
            }
        }
        return linkCounts;
 
    }
 
 
 
    public PageReference searchSlip() {
        done_flg = false;
        Map<String,boolean> esdIdMap = new Map<String,boolean>();
        // for (EsdInfo esd : esdList) {
        //     if (!esd.hasSended)
        //     esdIdMap.put(esd.rec.Id, esd.isChecked);
        // }
 
        String qryString = 'select Id, Name, DeliveryCompany_SlipNo__c,DeliveryType__c,Distributor_method__c,DeliveryCompany__c,Wh_Staff__c '
                + 'from FixtureDeliverySlip__c '
                + 'where Name =\''+ slip.Name +'\' and DeliveryType__c = \'回寄\'';
        if (String.isNotBlank(slip.Distributor_method__c)) {
            qryString += ' and Distributor_method__c = \''+ slip.Distributor_method__c +'\'';
        }
        if (String.isNotBlank(slip.DeliveryCompany__c)) {
            qryString += ' and DeliveryCompany__c = \''+ slip.DeliveryCompany__c +'\'';
        }
        List<FixtureDeliverySlip__c> slipList = Database.query(qryString);
 
        if (slipList.size() > 0) {
            if (slipList.size() > 1) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '当前条件检索到复数条运输单,请追加检索条件!'));
                return null;
            }
            slip = slipList[0];
        }
        else {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, '没有检索出符合条件的运输单'));
        }
        return null;
    }
 
    public class EsdInfo {
        public TransferApplyDetail__c rec { get; set; }
        public Boolean canReturn { get; set; }//true  有能回寄的数据
        // 回库はSet単位で、明細一つ一つのquickCheck要らない
        public Boolean checked {get;set;}
        public boolean editable { get; set; }
        public String fsName {get;set;}
        public String tasId {get;set;}
        // public String errorInfo { get; set; }
 
        public Asset rec2 { get; set; }
        public String errorInfo { get; set; }
        public boolean accessoryFlg { get; set; }
        public String accessorySetId { get; set; }
        public Boolean discardFlg { get; set; }
        public Boolean needsetone { get; set; }
        public Boolean needColor { get; set; }
        // public boolean editable { get; set; }
        // public String fsName {get;set;}
 
        // public EsdInfo() {
        //     this.discardFlg = false;
        //     this.editable = false;
        // }
 
        public EsdInfo(TransferApplyDetail__c rec) {
            this.needColor = false;
            this.rec = rec;
            this.needsetone = rec.Arrival_in_wh__c == false && (
                                    //20210507 ljh update 1834 start
                                    //(rec.Main_OneToOne__c == false && rec.OneToOneAccessory__c == false)
                                    (rec.Main_OneToOne__c == false && rec.OneToOneAccessory__c == false && rec.Loaner_accsessary__c)
                                    //20210507 ljh update 1834 end
                                        || (rec.OneToOneAccessory__c && rec.TransferApplySummary__r.First_TAD__r.Arrival_in_wh__c && rec.First_You_Xiao_Ku_Cun__c == 0)
                                ) ;
            this.accessoryFlg = false;
            this.discardFlg = false;
            this.checked = false;
            this.editable = rec.Shippment_loaner_time__c != null && rec.Detail_Finish__c == false && rec.Asset_return_time__c == null;
            this.fsName = rec.TransferApplySummary__r.Name + ':' + rec.TransferApplySummary__r.Fixture_Set__r.Name;
            this.accessorySetId = rec.TransferApplySummary__c;
            /*this.canReturn = rec.TransferApplySummary__r.First_TAD__r.Loaner_accsessary__c == true
                         || (rec.TransferApplySummary__r.First_TAD__r.ArrivalResult__c != '欠品'
                                && rec.TransferApplySummary__r.First_TAD__r.ArrivalResult__c != 'NG'
                            );*/
            this.canReturn = false;//20210118 ljh update
        }
 
 
        // public EsdInfo(TransferApplyDetail__c rec) {
        //     this.rec = rec;
        //     this.checked = false;
 
        //     this.fsName = rec.TransferApplySummary__r.Name+':'+rec.TransferApplySummary__r.Fixture_Set__r.Name;
        //     this.tasId = rec.TransferApplySummary__r.Id;
        // }
    }
    public class LabelValue {
        public String label {get; set;}
        public String value {get; set;}
 
        public LabelValue() {
            label = '';
            value = '';
        }
 
        public LabelValue(String l, String v) {
            label = l;
            value = v;
        }
    }
}