liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
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
// xudan 20160106 Arrival_wh_time__c -> Arrival_wh_time2__c(回库はSet単位なので、積み上げ項目使わず)
// RentalFixtureManage11Test
// RentalFixtureManage12Test
public without sharing class EquipmentSetShippmentReceived6Controller {
    public Rental_Apply_Equipment_Set__c es { get; set; }
    public List<EsdInfo> esdList { get; set; }
    public String crossRegion {get;set;}
    private Map<String, String> roleRegionMap {get;set;}
 
    public Boolean saveBtnDisabled { get; private set; }
    public boolean ReturnRefuse {get;private set;}
    public String ErrorMessage {get; set;}
    public String CDSFinished {get;set;}
    public boolean LostFlg {get;set;}
    public boolean done_flg {get;set;}
    public String SR_status { get; private set; }
    public String Step_status { get; private set; }
    public Boolean needCDS { get; private set; }
    public String open_type { get; private set; }
    public Boolean forceSave { get; set; }
    public String Raid {get;set;}
    public String AccessoryId {get;set;}
    public String AccessorySetId {get;set;}
    public String UnCheckedId {get;set;}
    private String Id;
 
    public Integer getEsdListSize() {
        return esdList.size();
    }
 
    public EquipmentSetShippmentReceived6Controller() {
//        Apexpages.currentPage().getHeaders().put('X-UA-Compatible', 'IE=8');
        Id = ApexPages.currentPage().getParameters().get('id');
        if (SR_status == null) {
            SR_status = ApexPages.currentPage().getParameters().get('type');
        }
        if (Step_status == null) {
            Step_status = ApexPages.currentPage().getParameters().get('step');
        }
        if (open_type == null) {
            open_type = ApexPages.currentPage().getParameters().get('open');
        }
        roleRegionMap = new Map<String, String> {
            '备品中心北方管理成员' => '北京 备品中心',
            '备品中心华东管理成员' => '上海 备品中心',
            '备品中心南方管理成员' => '广州 备品中心'
        };
    }
 
    // 画面初始化
    public PageReference init() {
        forceSave = false;
        done_flg = false;
        es = new Rental_Apply_Equipment_Set__c();
        esdList = new List<EsdInfo>();
        Set<Id> esIds = new Set<Id>();
        saveBtnDisabled = false;
        ReturnRefuse = true;
        ErrorMessage = '';
        crossRegion = '';
        if (Id != null) {
            List<String> ids = Id.split(',');
            // 当前User
            String userid = Userinfo.getUserId();
            User user = [select Id,Name,UserRole.Name from User where Id = :userid];
            // 备品set
            List<Rental_Apply_Equipment_Set__c> esList = [select Rental_Apply__c, Id, RAES_Status__c, Name, Shippment_loaner_time2__c
                                                         , Rental_Apply__r.Cross_Region_Assign__c
                                            from Rental_Apply_Equipment_Set__c
                                           where (Rental_Apply__r.Name in :ids or Id in :ids)
                                             ];
 
            //List<Fixture_Set__c> esList = [select Id, Name, CDS_staff__c, Shippment_loaner_time__c, Received_loaner_time__c, 
            //                                       delivery_company__c, Return_to_wh_staff__c, Return_to_wh_staff__r.Name, Fedex_number__c, Distributor_method__c,
            //                                       Return_delivery_company__c,Return_wh_chenk_staff__c, Received_confirmation_staff__c, Received_confirmation_staff__r.Name, Return_Fedex_number__c, Return_Distributor_method__c,
            //                                       Arrival_wh_time2__c, Asset_return_time__c,
            //                                       CDS_complete__c, Arrival_in_wh__c, StockDown__c,
            //                                       Lost_item_check_time__c,Last_Reserve_Rental_Apply_Fixture_Set__c,
            //                                       StockDown_time__c
            //                                  from Fixture_Set__c where Name = :Id];
            if (esList.size() > 0) {
                //es = esList[0];
                for (Rental_Apply_Equipment_Set__c raes : esList) {
                    esIds.add(raes.Id);
                    Raid = raes.Rental_Apply__c;
                    if(String.isNotBlank(raes.Rental_Apply__r.Cross_Region_Assign__c)){
                        crossRegion = raes.Rental_Apply__r.Cross_Region_Assign__c;
                        if(roleRegionMap.containsKey(user.UserRole.Name)
                            && roleRegionMap.get(user.UserRole.Name) == raes.Rental_Apply__r.Cross_Region_Assign__c) {
                                crossRegion = ''; 
                        }
                    }
                }
            } else {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '备品不存在'));
                saveBtnDisabled = true;
                return null;
            }
            //检查验证规则修理最终检测日
            /*   todo
            List<Rental_Apply_Equipment_Set__c> CheckRAES = [select id,Name,RAES_Status__c,Rental_Apply__c,Rental_Apply__r.Repair__c,Cancel_Date__c,Fixture_Set__c 
                    from Rental_Apply_Equipment_Set__c where id=:es.Last_Reserve_Rental_Apply_Equipment_Set__c];
            Rental_Apply_Equipment_Set__c Ra_c = new Rental_Apply_Equipment_Set__c();
            Rental_Apply__c Rac = new Rental_Apply__c();
            if(CheckRAES.size()>0){
                Ra_c = CheckRAES[0];
                Raid = Ra_c.Rental_Apply__c;
            }
            system.debug(Ra_c.Rental_Apply__r.Repair__c );
            List<Rental_Apply__c> CheckError = new List<Rental_Apply__c>();
            CheckError = [Select id,name,repair__c,repair__r.Return_Without_Repair_Date__c,repair__r.Repair_Final_Inspection_Date__c,repair__r.Repair_Shipped_Date__c,
                                Campaign__c,Campaign__r.Status
                                            from Rental_Apply__c where id=:Ra_c.Rental_Apply__c
                                            and RA_Status__c !='取消'
                                            and RA_Status__c !='删除'];
            if(CheckError.size()>0){
                Rac = CheckError[0];
            }
            */
 
            //system.debug('All::::'+Rac.repair__r.Repair_Final_Inspection_Date__c);
            // system.debug('All::::'+Rac.repair__c);
            //  system.debug('All::::'+Ra_c.RAES_Status__c);
            // 本番暂不上线该功能,启用后不能入库操作感觉不对
 
        }
 
        // 备品set明细
        //154p 1414 20211014 you 后面加查询
        List<Rental_Apply_Equipment_Set_Detail__c> eList = [
                select Substitute_flag__c, Cancel_Select__c, Rental_Apply_Equipment_Set__c, AssetName__c, Asset__r.Product2Id, Canceled_Id__c, SerialNumber_F__c, Arrival_wh_Flag_F__c, Arrival_wh_Abandoned_Flag_F__c, OnStock_By_Cancel__c, Fixture_Set_Detail__r.Is_OneToOne__c, Is_Body__c, 
                       Rental_Apply_Equipment_Set__r.Fixture_Set__r.Name, Fixture_Set_Detail__c, FSD_OneToOneAccessory_Cnt_F__c, Asset__r.WH_location__c, OneToOne_Flag__c,Fixture_QRCode_F__c, 
                       Rental_Apply_Equipment_Set__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_F__c, Arrival_in_wh__c,
                       Asset__r.Pre_Reserve_RAES_Detail__c, Asset__r.Pre_Reserve_RAES_Detail__r.After_Inspection_time__c,
                       Inspection_result_after_ng_F__c, Inspection_result_ng__c, Lost_item_giveup__c, CDS_complete__c, Loaner_accsessary__c,
                       BoxInconsistent__c, Fixture_Model_No_text__c,Equipment_Type_text__c,Rental_Apply__r.RecordTypeId, 
                        // 【FY24大及巨大课题】北京新法规项目对应 DB202307428742 20230801 by lc Start
                        Asset__r.Storage_Conditions__c, Asset__r.Temperature_And_Humidity_Zone__c, Asset__r.Storage_Conditions_Met_Status__c, Asset__r.Product2.ProductCode, Asset__r.Product2.SFDA_Approbation_No__c
                        // 【FY24大及巨大课题】北京新法规项目对应 DB202307428742 20230801 by lc End
                  from Rental_Apply_Equipment_Set_Detail__c
                 where Rental_Apply_Equipment_Set__c in :esIds and ((Shipment_request_time2__c <> null and Cancel_Select__c = False ) or Arrival_wh_Flag_F__c = true or Arrival_wh_Abandoned_Flag_F__c = true)
                 order by Rental_Apply_Equipment_Set__r.Name, Rental_Apply_Equipment_Set__c, Name
        ];
        //String assid = '';
        Map<Id,List<Rental_Apply_Equipment_Set_Detail__c>> raesMap = new Map<Id,List<Rental_Apply_Equipment_Set_Detail__c>>();
        for (Rental_Apply_Equipment_Set_Detail__c esd : eList) {
            List<Rental_Apply_Equipment_Set_Detail__c> raesdList;
            if (raesMap.containsKey(esd.Rental_Apply_Equipment_Set__c)) {
                raesdList = raesMap.get(esd.Rental_Apply_Equipment_Set__c);
            } else {
                raesdList = new List<Rental_Apply_Equipment_Set_Detail__c>();
            }
            raesdList.add(esd);
            raesMap.put(esd.Rental_Apply_Equipment_Set__c, raesdList);
        }
        Set<Id> otomainSet = new Set<Id>();
        for (Id raesId : raesMap.keySet()) {
            //Set<Id> assetIds = new Set<Id>();
            for (Rental_Apply_Equipment_Set_Detail__c esd : raesMap.get(raesId)) {
                EsdInfo ei = new EsdInfo(esd);
                if (esd.Arrival_in_wh__c == false 
                    && (esd.Arrival_wh_Flag_F__c || esd.Arrival_wh_Abandoned_Flag_F__c)) {
                    ei.editable = true;
                    if (esd.Arrival_wh_Abandoned_Flag_F__c) {
                        ei.discardFlg = true;
                    } else if (esd.FSD_OneToOneAccessory_Cnt_F__c > 0) {
                        if (esd.Is_Body__c) {
                            //ei.locChange = true;
                            //otomainSet.add(esd.Rental_Apply_Equipment_Set__c);
                        } else if (!esd.Fixture_Set_Detail__r.Is_OneToOne__c) {
                    //      if (!assetIds.contains(esd.Asset__c)) {
                    //          ei.locChange = true;
                                //assetIds.add(esd.Asset__c);
                    //      }
                        }
                    } else {
                   //     if (!assetIds.contains(esd.Asset__c)) {
                //          ei.locChange = true;
                            //assetIds.add(esd.Asset__c);
                //      }
                    }
                }
                //SFDC-C63DFK 上架时镜箱/周转箱不符的标记   thh 2021-09-07 start
                if(String.isNotBlank(esd.Fixture_Model_No_text__c) && esd.Fixture_Model_No_text__c == 'WA05990A' || esd.Fixture_Model_No_text__c.indexOf('镜箱') != -1 ) {
                    ei.isShow = true;
                }
                //SFDC-C63DFK 上架时镜箱/周转箱不符的标记   thh 2021-09-07 end
                ei.accessorySetId = esd.Rental_Apply_Equipment_Set__c;
 
                // 【FY24大及巨大课题】北京新法规项目对应 DB202307428742 20230804 by lc Start
                if (esd.Asset__c != null) {
                    if (String.isBlank(esd.Asset__r.Temperature_And_Humidity_Zone__c)) {
                        // 初始默认为A区
                        esd.Asset__r.Temperature_And_Humidity_Zone__c = 'A区';
                    }
                }
                // 【FY24大及巨大课题】北京新法规项目对应 DB202307428742 20230804 by lc End
                esdList.add(ei);
            }
        }
        //for (EsdInfo esd : esdList) {
        //  if (esd.rec.FSD_OneToOneAccessory_Cnt__c > 0 && esd.rec.FSD_Is_OneToOne__c && esd.rec.Inspection_result_after_F__c == 'OK') {
        //      esd.otoWithoutMain = !otomainSet.contains(esd.rec.Rental_Apply_Equipment_Set__c);
        //  }
        //}
 
 
        //for (Rental_Apply_Equipment_Set_Detail__c esd : eList) {
        //    EsdInfo ei = new EsdInfo(esd);
        //    if (esd.Arrival_in_wh__c == false
        //      && (esd.Inspection_result_after_F__c == 'OK'
        //          || (esd.Inspection_result_after_F__c == 'NG' && esd.Inspection_result_after_ng_F__c == '废弃') || esd.OnStock_By_Cancel__c == true)) {  
        //      ei.editable = true;
        //      if (esd.Inspection_result_after_ng_F__c == '废弃' || (esd.Inspection_result_NG__c == '废弃' && esd.OnStock_By_Cancel__c)) {
           //         ei.discardFlg = true;
           //     } else if (esd.FSD_OneToOneAccessory_Cnt__c > 0) {
           //         if (esd.Is_Body__c) {
           //             if (assid != (String)esd.Asset__c) {
           //                 ei.locChange = true;
           //                 assid = esd.Asset__c;
           //             }
           //         } else if (!esd.Fixture_Set_Detail__r.Is_OneToOne__c) {
           //           if (assid != (String)esd.Asset__c) {
           //               ei.locChange = true;
           //               assid = esd.Asset__c;
           //             }
           //         }
           //     } else {
           //         if (assid != (String)esd.Asset__c) {
           //             ei.locChange = true;
           //             assid = esd.Asset__c;
           //         }
           //     }
        //    }
 
        //    esdList.add(ei);
        //}
 
        if (esdList.size() <= 0) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '没有备品set明细'));
            saveBtnDisabled = true;
            return null;
        }
        Step_status = '上架';
        SR_status = 'Shippment';
        system.debug('=====' + Step_status);
        system.debug('=====' + open_type);
 
        return null;
    }
 
    // Step 切り替えボタン、发货前-检测
    public PageReference Shippment1() {
        Step_status = '发货前';
        return new PageReference('/apex/EquipmentSetShippmentReceived2?id=' + this.Id + '&type=' + this.SR_status + '&step=' + this.Step_status + '&open=' + this.open_type);
    }
 
    // Step 切り替えボタン、发货-发货运输单号 等
    public PageReference Shippment2() {
        Step_status = '发货';
        return new PageReference('/apex/EquipmentSetShippmentReceived6?id=' + this.Id);
    }
 
    // Step 切り替えボタン、回寄-发货运输单号 等
    public PageReference Received1() {
        Step_status = '回寄';
        return new PageReference('/apex/EquipmentSetShippmentReceived2?id=' + this.Id + '&type=' + this.SR_status + '&step=' + this.Step_status + '&open=' + this.open_type);
    }
 
    // Step 切り替えボタン、CDS 等
    public PageReference Received2() {
        Step_status = 'CDS';
        return new PageReference('/apex/EquipmentSetShippmentReceived2?id=' + this.Id + '&type=' + this.SR_status + '&step=' + this.Step_status + '&open=' + this.open_type);
    }
 
    // Step 切り替えボタン、回库检查 等
    public PageReference Received3() {
        Step_status = '回库检查';
        return new PageReference('/apex/EquipmentSetShippmentReceived2?id=' + this.Id + '&type=' + this.SR_status + '&step=' + this.Step_status + '&open=' + this.open_type);
    }
 
    // Step 切り替えボタン、回库 等
    public PageReference Received4() {
        Step_status = '回库';
        return new PageReference('/apex/EquipmentSetShippmentReceived2?id=' + this.Id + '&type=' + this.SR_status + '&step=' + this.Step_status + '&open=' + this.open_type);
    }
 
    public PageReference deleteDetail() {
        if (UnCheckedId != null) {
            integer index = 0;
            for (EsdInfo esd : esdList) {
                if (esd.accessoryFlg && UnCheckedId == esd.rec2.Id) {
                    break;
                }
                index++;
            }
            esdList.remove(index);
        }
        UnCheckedId = null;
        return null;
    }
 
    public PageReference addAccessory() {
        system.debug(AccessorySetId);
        if (AccessoryId != null) {
            List<Asset> assList = [select Id, Name, SerialNumber, Product2Id,WH_location__c from Asset
                    where Fixture_QRCode__c = :AccessoryId
                      and Ji_Zhong_Guan_Li_Ku_Cun__c > 0
                      and Asset_loaner_category__c != '耗材'
                      and RecordTypeId =:System.label.access_1];
            system.debug(assList);
            if (assList.size() > 0) {
                //Rental_apply_equipment_Set_Detail__c raesd = new Rental_apply_equipment_Set_Detail__c();
                EsdInfo accInfo = new EsdInfo();
                accInfo.accessoryFlg = true;
                accInfo.rec2=asslist[0];
                accInfo.accessorySetId = AccessorySetId;
 
                Integer index = 1;
                Integer insert_index = 0;
                for (EsdInfo esd : esdList) {
                    if (esd.rec.Rental_Apply_Equipment_Set__c == AccessorySetId) insert_index = index;
                    index++;
                }
                if (esdList.size() == insert_index) {
                    esdList.add(accInfo);
                } else {
                    esdList.add(insert_index, accInfo);
                }
            }
        }
        AccessoryId = null;
        return null;
    }
    // 保存按钮
    public PageReference save() {
        done_flg = false;
        String userid = Userinfo.getUserId();
        List<Rental_Apply_Equipment_Set_Detail__c> eList = new List<Rental_Apply_Equipment_Set_Detail__c>();
        List<EsdInfo> otoCheckList_main = new List<EsdInfo>();
        //List<EsdInfo> otoCheckList = new List<EsdInfo>();
        List<EsdInfo> otoAccessoryList = new List<EsdInfo>();
        Map<Id, String> assetloc = new Map<Id, String>();
        Set<Id> assetlocAbandoned = new Set<Id>();
        Set<Id> assetForLock = new Set<Id>();
        Map<Id, Asset> assUptMap = new Map<Id, Asset>();
        //Map<Id, Integer> accessoryMap = new Map<Id, Integer>();
        //Set<Id> otomainSet = new Set<Id>();
        ////一对一主体
        //for (EsdInfo esdInfo : esdList) {
        //  if (!esdInfo.accessoryFlg && esdInfo.editable && esdInfo.rec.FSD_OneToOneAccessory_Cnt__c > 0) {
        //      if (esdInfo.rec.Arrival_in_wh__c && esdInfo.rec.Is_Body__c) {
        //          otomainSet.add(esdInfo.rec.Rental_Apply_Equipment_Set__c);
        //      }
        //  }
        //}
 
        //for (EsdInfo esdInfo : esdList) {
        //  if (!esdInfo.accessoryFlg && esdInfo.editable && esdInfo.rec.FSD_OneToOneAccessory_Cnt__c > 0) {
        //      if (esdInfo.rec.FSD_Is_OneToOne__c && esdInfo.rec.Inspection_result_after_F__c == 'OK' && !esdInfo.otoWithoutMain) {
        //          esdInfo.rec.Arrival_in_wh__c = otomainSet.contains(esdInfo.rec.Rental_Apply_Equipment_Set__c);
        //      }
        //  }
        //}
 
        Set<Id> mainSet = new Set<Id>();
        for (EsdInfo esdInfo : esdList) {
            esdInfo.errorInfo = null;
            if (esdInfo.accessoryFlg) {
                otoAccessoryList.add(esdInfo);
                //accessoryMap.put(esdInfo.rec2.Product2Id, 0);
            } else {
                if (!esdInfo.editable) continue;
                Rental_Apply_Equipment_Set_Detail__c esd = esdInfo.rec;
                eList.add(esd);
                //if (esd.Inspection_result_after_F__c == 'OK' && esd.FSD_OneToOneAccessory_Cnt__c > 0 && esd.FSD_Is_OneToOne__c) esd.Arrival_in_wh__c = true;
                if (esd.Arrival_in_wh__c) {
                    if (esd.Arrival_wh_Abandoned_Flag_F__c) {
                        assetlocAbandoned.add(esd.Asset__c);
                    }
                    if (esd.FSD_OneToOneAccessory_Cnt_F__c > 0 && esd.Is_Body__c && (esd.Inspection_result_after_F__c == 'OK' || (!esd.Arrival_wh_Abandoned_Flag_F__c && esd.Arrival_wh_Flag_F__c))) {
                        otoCheckList_main.add(esdInfo);
                        mainSet.add(esd.Rental_Apply_Equipment_Set__c);
                        assetloc.put(esd.Asset__c, esd.Asset__r.WH_location__c);
                    } else if ((esd.Inspection_result_after_F__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) {
                        //if (String.isNotBlank(esd.Canceled_Id__c)) {
                        //    otoAccessoryList.add(esdInfo);
                        //} else {
                        //    otoCheckList.add(esdInfo);
                        //}
                        otoAccessoryList.add(esdInfo);
                    } else if (!assetloc.containsKey(esd.Asset__c)) {
                        assetloc.put(esd.Asset__c, esd.Asset__r.WH_location__c);
                        //assetIds.add(esd.Asset__c);
                    }
                }
            }
            if (esdInfo.editable) {
                assetForLock.add(esdInfo.rec.Asset__c);
            }
 
            // 【FY24大及巨大课题】北京新法规项目对应 DB202307428742 20230804 by lc Start
            if(esdInfo.rec.Arrival_in_wh__c){
                if (!esdInfo.editable) continue;
 
                String checkFlag = null;
                // 选择的温湿度区分
                String thZone = esdInfo.rec.Asset__r.Temperature_And_Humidity_Zone__c;
                // 储存条件 5~40℃,10~95%
                String storageCon = esdInfo.rec.Asset__r.Storage_Conditions__c;
                if (String.isBlank(thZone)) {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '请选择温湿度分区!'));
                    return null;
                }
                if (esdInfo.rec.Asset__r.Product2.ProductCode.StartsWith('BP')) {
                    checkFlag = 'OK';
                } else if (esdInfo.rec.Asset__r.Product2.SFDA_Approbation_No__c == 'FYL') {
                    checkFlag = 'OK';
                } else {
                    if (String.isBlank(storageCon)) {
                        checkFlag = 'OK';
                    } else {
                        checkFlag = FixtureUtil.compareTemperatureAndHumidity(thZone, storageCon);
                    }
                }
 
                Asset ass = new Asset();
                ass.Id = esdInfo.rec.Asset__c;
                // 不满足则标记上储存条件不符合
                ass.Storage_Conditions_Met_Status__c = checkFlag;
                ass.Temperature_And_Humidity_Zone__c = thZone;
                assUptMap.put(esdInfo.rec.Asset__c, ass);
            }
            // 【FY24大及巨大课题】北京新法规项目对应 DB202307428742 20230804 by lc End
        }
 
        //Map<Id,List<EsdInfo>> esdMap = new Map<Id,List<EsdInfo>>();
        for (EsdInfo esd : otoAccessoryList) {
            //List<EsdInfo> raesdList;
            //if (esdMap.containsKey(esd.rec.Rental_Apply_Equipment_Set__c)) {
            //    raesdList = esdMap.get(esd.rec.Rental_Apply_Equipment_Set__c);
            //} else {
            //    raesdList = new List<EsdInfo>();
            //}
            //raesdList.add(esd);
            //esdMap.put(esd.rec.Rental_Apply_Equipment_Set__c, raesdList);
            if (!esd.accessoryFlg && !mainSet.contains(esd.rec.Rental_Apply_Equipment_Set__c) && !assetloc.containsKey(esd.rec.Asset__c)) {
                assetloc.put(esd.rec.Asset__c, esd.rec.Asset__r.WH_location__c);
            }
        }
 
        Map<String, Map<Id, List<Id>>> assProductCountMapAll = new Map<String, Map<Id, List<Id>>>();
        //Map<Id, List<Id>> assProductCountMap = new Map<Id, List<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>();
        List<Asset> updAsset = null;
        Map<Id, Asset> updAssetMap = new Map<Id, Asset>();
        List<Asset> updAssetAbandoned = null;
        Map<Id, Asset> updAssetAbandonedMap = new Map<Id, Asset>();
        //if (!forceSave) {
        boolean err = false;
        Map<String, Integer> otoMap = new Map<String, Integer>();
        for (EsdInfo main : otoCheckList_main) {
            //Fixture_Set_Detail__c m1 = [select Id, Fixture_Set__c from Fixture_Set_Detail__c where Id = :main.rec.Fixture_Set_Detail__c];
            //List<Fixture_Set_Detail__c> asslist = [select Product2__r.Name, Id,Product2__c,Is_Body__c, Quantity__c from Fixture_Set_Detail__c where Is_Body__c = false and Fixture_Set__c = :m1.Fixture_Set__c and Is_OneToOne__c = true];
 
            //List<EsdInfo> raesdList = esdMap.get(main.rec.Rental_Apply_Equipment_Set__c);
            List<Rental_Apply_Equipment_Set_Detail__c> asslist;
            //没有cancel的看最新一套明细的fsd数量
            //已经cancel的看最早一套明细的一对一分配数量
            //理论上出库前上架的数据不会有非cancel的情况,所以直接用Cancel_Select__c判断
            system.debug(main.rec);
            if (main.rec.Cancel_Select__c == false) {
                asslist = [select Asset__r.Product2.Name, Asset__c, Asset__r.Product2Id , OneToOne_Flag__c
                            from Rental_Apply_Equipment_Set_Detail__c 
                           where Rental_Apply_Equipment_Set__c = :main.rec.Rental_Apply_Equipment_Set__c
                                 and Fixture_Set_Detail__r.Is_OneToOne__c = true
                                 and Cancel_Select__c = false //找最新的一套明细
                                 //and Canceled_Id__c = null //找最初的一套明细
                                 and Asset__c <> null //过滤未分配的数据。
                                 and Substitute_flag__c = false
                                 // 一览NG的情况拷贝新的一览,不影响一对一数量Count
                                 // and Rental_Apply_Equipment_Set__r.Canceled_Id__c = null
                                 order by OneToOne_Flag__c desc];
            } else {
                asslist = [select Asset__r.Product2.Name, Asset__c, Asset__r.Product2Id , OneToOne_Flag__c
                            from Rental_Apply_Equipment_Set_Detail__c
                           where Rental_Apply_Equipment_Set__c = :main.rec.Rental_Apply_Equipment_Set__c
                                 and Fixture_Set_Detail__r.Is_OneToOne__c = true
                                 and Substitute_flag__c = false
                                 and Canceled_Id__c = null
                                 and Asset__c <> null
                                 order by OneToOne_Flag__c desc];
            }
            system.debug(asslist);
            Map<Id, List<Id>> assProductCountMap = new Map<Id, List<Id>>();
            if (assProductCountMapAll.containsKey(main.rec.Rental_Apply_Equipment_Set__c)) {
                assProductCountMap = assProductCountMapAll.get(main.rec.Rental_Apply_Equipment_Set__c);
            }
            //Map<Id, List<EsdInfo>> assCountMap = new Map<Id, List<EsdInfo>>();
            //if (raesdList != null) {
            //    for (EsdInfo ei : raesdList) {
            //        List<EsdInfo> count;
            //        if (assCountMap.containsKey(ei.rec.Asset__c)) {
            //            count = assCountMap.get(ei.rec.Asset__c);
            //        } else {
            //            count = new List<EsdInfo>();
            //        }
            //        count.add(ei);
            //        assCountMap.put(ei.rec.Asset__c, count);
            //    }
            //}
 
            for (Rental_Apply_Equipment_Set_Detail__c ass : assList) {
                //if (assCountMap.containsKey(ass.Asset__c) && assCountMap.get(ass.Asset__c).size() > 0) {
 
                //    List<EsdInfo> count = assCountMap.get(ass.Asset__c);
                //    count.remove(0);
                //    assCountMap.put(ass.Asset__c, count);
 
                //    String key = (String)main.rec.Asset__c + ':' + (String)ass.Asset__c;
                //    Integer otoCnt;
                //    if (otoMap.containsKey(key)) {
                //        otoCnt = otoMap.get(key);
                //    } else {
                //        otoCnt = 0;
                //    }
                //    otoMap.put(key, otoCnt + 1);
                //} else
                if (assProductCountMap.containsKey(ass.Asset__r.Product2Id) && assProductCountMap.get(ass.Asset__r.Product2Id).size() > 0) {
 
                    List<Id> count = assProductCountMap.get(ass.Asset__r.Product2Id);
                    String assetId = (String)count[0];
                    count.remove(0);
                    assProductCountMap.put(ass.Asset__r.Product2Id, count);
                    assProductCountMapAll.put(main.rec.Rental_Apply_Equipment_Set__c, assProductCountMap);
                    //accessoryMap.put(ass.Asset__r.Product2Id, accessoryMap.get(ass.Asset__r.Product2Id)+1);
 
                    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 && ass.OneToOne_Flag__c))) {
                        main.errorInfo = '未补全附属品(部分上架请勾选复选框):'+ass.Asset__r.Product2.Name;
                        err = true;
                        break;
                    } else {
                        continue;
                    }
                    //return null;
                }
            }
            //有多余的附属品
            //for (Id count : assCountMap.keySet()) {
            //  if (assCountMap.get(count).size() > 0) {
            //      err = true;
            //      for (EsdInfo ei : assCountMap.get(count)) {
            //          ei.errorInfo = '一对一附属品无对应主体(拆分上架请勾选复选框)';
            //      }
            //  }
            //}
        }
 
 
        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);
        }
 
        //}
        //附属品和一览走,不需要check
  //      system.debug(accessoryMap);
  //      for (String sKey : accessoryMap.keySet()) {
        //    if (accessoryMap.get(sKey) > 1) {
        //      ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '有追加附属品对应不同主体,不可更新( '+Step_status+' )'));
        //      return null;
        //    }
        //}
 
        //return null;
        // TODO 没变化不存
        if (Step_status == '上架') {
 
            for (Rental_Apply_Equipment_Set_Detail__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;
        }
        if (assetloc.keySet().size() > 0) {
            updAsset = [select Id, WH_location__c from Asset
                    where Id in :assetloc.keySet()
                      and Asset_loaner_category__c != '耗材'
                      and RecordTypeId =:System.label.access_1];
            for (Asset upda : updAsset) {
                upda.WH_location__c = assetloc.get(upda.Id);
                updAssetMap.put(upda.Id, upda);
            }
            needSaveAsset = true;
        }
        if (assetlocAbandoned.size() > 0) {
            updAssetAbandoned = [select Id, WH_location__c from Asset
                    where Id in :assetlocAbandoned
                      and Asset_loaner_category__c != '耗材'
                      and RecordTypeId =:System.Label.access_1
                      and Manage_type__c = '个体管理'];
            for (Asset upda : updAssetAbandoned) {
                upda.WH_location__c = '废弃区';
                updAssetAbandonedMap.put(upda.Id, upda);
            }
            needSaveAsset = true;
        }
        for (EsdInfo esdInfo : esdList) {
            if (esdInfo.accessoryFlg) continue;
            Rental_Apply_Equipment_Set_Detail__c esd = esdInfo.rec;
            if (esd.Arrival_in_wh__c && esd.FSD_OneToOneAccessory_Cnt_F__c > 0 && esd.Is_Body__c && esd.Arrival_wh_Abandoned_Flag_F__c) {
                otoCheckList_main.add(esdInfo);
            }
        }
 
        // 【FY24大及巨大课题】北京新法规项目对应 DB202307428742 20230804 by lc Start
        // 存在保有设备的更新
        List<Asset> finalUpdAssetList = new List<Asset>();
        if (!updAssetMap.isEmpty() || !updAssetAbandonedMap.isEmpty() || !assUptMap.isEmpty()) {
            for (Id ids : assetForLock) {
                Boolean isAddToList = false;
                Asset assTemp = new Asset();
                assTemp.Id = ids;
                if (!updAssetMap.isEmpty() && updAssetMap.containskey(ids)) {
                    assTemp.WH_location__c = updAssetMap.get(ids).WH_location__c;
                    isAddToList = true;
                }
                if (!updAssetAbandonedMap.isEmpty() && updAssetAbandonedMap.containskey(ids)) {
                    assTemp.WH_location__c = '废弃区';
                    isAddToList = true;
                }
                if (!assUptMap.isEmpty() && assUptMap.containskey(ids)) {
                    assTemp.Storage_Conditions_Met_Status__c = assUptMap.get(ids).Storage_Conditions_Met_Status__c;
                    assTemp.Temperature_And_Humidity_Zone__c = assUptMap.get(ids).Temperature_And_Humidity_Zone__c;
                    isAddToList = true;
                }
 
                if (isAddToList) finalUpdAssetList.add(assTemp);
            }
        }
        // 【FY24大及巨大课题】北京新法规项目对应 DB202307428742 20230804 by lc End
 
        List<Asset> astLock = [select Id from Asset
                where Id in :assetForLock
                  for update];
        //return null;
        Savepoint sp = Database.setSavepoint();
        try {
            //if (needSaveSet) ControllerUtil.upRAdEquipmentSet(es);
            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);
                    }
                }
                //ControllerUtil.delOtOLinkData(mainIds);
                FixtureUtil.delOtOLinkData(mainIds);
            }
            if (otolinkList.size() > 0) {
          //    Set<Id> mainIds = new Set<Id>();
                //for (Fixture_OneToOne_Link__c oto : otolinkList) {
                //    if (String.isBlank(oto.Main_Asset__c) == false) {
                //        mainIds.add(oto.Main_Asset__c);
                //    }
                //}
          //    ControllerUtil.delOtOLinkData(mainIds);
                if (needSaveDetail) {
                    //ControllerUtil.updRAEquipmentSetDetail(eList);
                    FixtureUtil.withoutUpsertObjects(eList);
                    needSaveDetail = false;
                }
                //ControllerUtil.updOtOLinkData(otolinkList);
                Oly_TriggerHandler.clearBypass(AssetHandlerCheck.class.getName());
                FixtureUtil.withoutInsert(otolinkList);
                Oly_TriggerHandler.bypass(AssetHandlerCheck.class.getName());
            }
            //if (needSaveDetail) ControllerUtil.updRAEquipmentSetDetail(eList);
            if (needSaveDetail) FixtureUtil.withoutUpsertObjects(eList);
            //if (needSaveAsset) ControllerUtil.updAssetLocation(updAsset);
 
            // 【FY24大及巨大课题】北京新法规项目对应 DB202307428742 20230804 by lc Start
            //if (needSaveAsset && updAsset != null && updAsset.size() > 0) FixtureUtil.withoutUpdate(updAsset);
            //if (needSaveAsset && updAssetAbandoned != null && updAssetAbandoned.size() > 0) FixtureUtil.withoutUpdate(updAssetAbandoned);
            if (!finalUpdAssetList.isEmpty()) FixtureUtil.withoutUpdate(finalUpdAssetList);
            // 【FY24大及巨大课题】北京新法规项目对应 DB202307428742 20230804 by lc End
 
            //PageReference ref = new Pagereference('/apex/EquipmentSetShippmentReceived6?id=' + Id);
            //ref.setRedirect(true);
            //return ref;
            List<EsdInfo> esdL = new List<EsdInfo>();
            for (EsdInfo esd : esdList) {
                if (!esd.accessoryFlg) {
                    esdL.add(esd);
                }
            }
            //esdList = esdL;
            init();
            done_flg = true;
            return null;
        } catch (Exception ex) {
            system.debug('=====' + ex.getMessage());
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, ex.getMessage()+'( '+Step_status+' )'));
            Database.rollback(sp);
            done_flg = false;
            return null;
        }
 
        return null;
    }
 
    public class EsdInfo {
        public Rental_Apply_Equipment_Set_Detail__c rec { get; set; }
        public Asset rec2 { get; set; }
        public String imageAssetUploadedTime { get; set; }
        public String imageSerialUploadedTime { get; set; }
        public String errorInfo { get; set; }
        public boolean accessoryFlg { get; set; }
        public String accessorySetId { get; set; }
        public Boolean discardFlg { get; set; }
        //public Boolean locChange { get; set; }
        public boolean editable { get; set; }
        //public boolean otoWithoutMain { get; set; }
        // 回库はSet単位で、明細一つ一つのquickCheck要らない
        public boolean quickCheck { get; set; }
        //判断镜箱/灭菌盒不符是否显示
        public boolean isShow { get; set; }
 
        public EsdInfo() {
            this.quickCheck = false;
            //this.locChange = false;
            this.discardFlg = false;
            this.editable = false;
            this.isShow = false;
            //this.otoWithoutMain = false;
        }
 
        public EsdInfo(Rental_apply_equipment_Set_Detail__c rec) {
            this.rec = rec;
            this.accessoryFlg = false;
            //this.locChange = false;
            this.discardFlg = false;
            this.editable = false;
            this.isShow = false;
            //this.otoWithoutMain = false;
 
            if (rec.Asset__r.Pre_Reserve_RAES_Detail__c != null && rec.Asset__r.Pre_Reserve_RAES_Detail__r.After_Inspection_time__c != null) {
                this.quickCheck = Datetime.now() < rec.Asset__r.Pre_Reserve_RAES_Detail__r.After_Inspection_time__c + 30 ? true : false;
            } else {
                this.quickCheck = false;
            }
 
            if (rec.Asset__r.ImageAssetUploadedTime__c != null) {
                imageAssetUploadedTime = rec.Asset__r.ImageAssetUploadedTime__c.format('yyyy/MM/dd HH:mm');
            }
            if (rec.Asset__r.ImageSerialUploadedTime__c != null) {
                imageSerialUploadedTime = rec.Asset__r.ImageSerialUploadedTime__c.format('yyyy/MM/dd HH:mm');
            }
        }
    }
}