高章伟
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
/*
 * 借出备品配套明细选择画面
 * 只显示Fixture_Set_Detail__c有值的明细
 */
public with sharing class RentalFixtureSetDetilSelectController extends CreateRelationListPagingCtrlBase {
    private List<LineExtraInfo> extraInfoList;  // viewList と同じ順番、同じ件数
    public static Integer FIELDMAX = 200;//20200908 ljh add
    public override Integer getSearchNumMax() {
        //各ページに制御あれば、最大件数を指定する
        searchNumMax = Integer.valueOf(Label.Product_Select_Limit);
        //TODO change
        searchNumMax = 100;
        return searchNumMax;
    }
 
    public override Boolean getIsNeedRunSearch() {
        return true;
        //isNeedRunSearch = true;
        //return isNeedRunSearch;
    }
    /* 選択されたデータ取得用Soql Fromから*/
    public override String getSelectedDataSql() {
        // オブジェクトAPI名
        selectedDataSql = ' From Rental_Apply_Equipment_Set_Detail__c';
        selectedDataSql += ' where Rental_Apply_Equipment_Set__r.UniqueKey__c = \'' + String.escapeSingleQuotes(parentId) + '\'';
        //Fixture_Set_Detail__c没有值不能检索出数据
        selectedDataSql += '   and Fixture_Set_Detail__c != null';
        // OLY_OCM-286 まず全部selectして group by Fixture_Set_Detail__c のlogicにてcountしたいですが
        // Fixture_Set_Detail__c を検索時、全部キャンセルした明細の行が表示しなくなるため、
        // and Cancel_Select__c = False を必ず有効にしてください。
        selectedDataSql += '   and Cancel_Select__c = False';
        selectedDataSql += ' order by Fixture_Set_Detail__r.SortInt__c, UniqueKey__c';
        if(parentObj.Rental_Apply__r.demo_purpose2__c != '试用(有询价)' && parentObj.Rental_Apply__r.demo_purpose2__c != '试用(无询价)') {
            String label = Rental_Apply_Equipment_Set_Detail__c.Product_Status_Flag_F__c.getDescribe().getLabel();
            Integer i = myComponentController.titleRight.indexOf(label);
            if(i > -1) {
                myComponentController.titleRight.remove(i);
                myComponentController.columnRightCss.remove(i);
                myComponentController.columnsRightApi.remove(i);
                myComponentController.columnRightRW.remove('Product_Status_Flag_F__c');
            }
        }
        return selectedDataSql;
    }
 
    // 検索元対象オブジェクトAPI名
    public override String getOriginObjName() {
        // オブジェクトAPI名
        originObjName = 'Fixture_Set_Detail__c';
        return originObjName;
    }
    public override String getOriginObjColumns() {
        // 項目セット
        originObjColumns =  'Id, Quantity__c, Is_Body__c';
        return originObjColumns;
    }
 
    public override String getObjName() {
        // オブジェクトAPI名
        objName = 'Rental_Apply_Equipment_Set_Detail__c';
        return objName;
    }
    public override String getColumnLeftFieldSetName() {
        // 左の項目セット
        columnLeftFieldSetName = 'RentalFixtureSetDetailSelect_left';
        return columnLeftFieldSetName;
    }
    public override String getColumnRightFieldSetName() {
        // 右の項目セット
        columnRightFieldSetName = 'RentalFixtureSetDetailSelect_right';
        return columnRightFieldSetName;
    }
 
    public override List<String> getColumnFieldList() {
        // strColumus 里加 field
        return new List<String>{'Id', 'UniqueKey__c', 'Rental_Apply_Equipment_Set__c', 'Rental_Num__c', 'Cancel_Select__c', 'IndexFromUniqueKey__c', 'Draft_Appended__c', 'Is_Body__c',
                 'Fixture_Set_Detail__r.SortInt__c', 'Rental_Apply__r.Status__c', 'Rental_Apply__r.Add_Approval_Status__c', 'Fixture_Set_Detail__r.Is_Optional__c', 'FSD_Is_Optional__c'
                 ,'Fixture_Set_Detail__r.Product_Status_Flag__c','Rental_Apply__r.demo_purpose2__c','Product_Status_Flag_F__c','Rental_Apply_Equipment_Set__r.Substitute_flag__c'};
    }
 
    // 画面里直接可以输入的項目 List
    public override List<String> getWritableColumnFieldList() {
        return new List<String>{'Rental_Num__c'};
    }
    // getObjName 连 getOriginObjName 的 FK
    public override String getFKColumnField() {
        return 'Fixture_Set_Detail__c';
    }
 
    public override String getRecordTypeId() {
        //ページレイアウトを収得するのレコードタイプ
        recordTypeId = '';
        return recordTypeId;
    }
 
    // ページコントローラに検索処理は、WhereSoql作成のみ、パラメータとして、コンポーネントに渡される
    public override String getSqlWhereStr() {
        sqlWhereStr = '';
 
        sqlWhereStr = this.makeSoql();
 
        return sqlWhereStr;
    }
    public override String getOrderbyStr() {
        return 'order by SortInt__c';
    }
 
    /*****************ソート時再検索条件(画面からの入力条件を無視するため)******************/
    private List<String> sidList;
    public String saveType { get; set; }
    public Rental_Apply_Equipment_Set__c parentObj { get; set; }
    public List<Rental_Apply_Equipment_Set_Detail__c> selectedGroupData { get; set; }
    private String deleteAdd_ApprovalErrorMessage = '第{0}行 付属品{1}件(草案中的追加付属品 {2}件) 更新失败 减少的数量大于草案中的追加付属品的数量';
    private String deleteAdd_ApprovalMessage = '第{0}行 付属品{1}件(草案中的追加付属品 {2}件) 成功更新成 付属品{3}件(草案中的追加付属品 {4}件)';
 
    public RentalFixtureSetDetilSelectController() {
        isNeedRunSearch = true;
        String sids = ApexPages.currentPage().getParameters().get('sids');
 
        sidList = sids.split(',');
        if(sidList.size()>0) {
            // 借出备品配套
            //02517%3Aa2P0k000000PEkjEAG%3A1
            parentId = sidList.get(0);
            //备品配套下的所有明细
            if (!String.isBlank(this.parentId)) {
                //画面上ヘタ表示の項目はここで追加
                List<Rental_Apply_Equipment_Set__c> parentObjs =
                    [select Id, Name,
                            Substitute_Select_Again__c,
                            Fixture_Set_Idx__c,
                            Rental_Apply__r.RequestNoJoinStr2__c,
                            Rental_Apply__r.Request_shipping_day__c,
                            Rental_Apply__r.Request_return_day__c,
                            Rental_Num__c,
                            Same_Accessory_flag__c,
                            Loaner_name__c,
                            Loaner_name_F__c,
                            Loaner_code__c,
                            Fixture_Set__c,
                            Loaner_code_F__c,
                            Rental_Apply__r.Add_Approval_Status__c,
                            Rental_Apply__r.Status__c,
                            Fixture_Set__r.Name,
                            Rental_Apply__c,UniqueKey__c,
                            Substitute_flag__c,
                            Yi_Assigned_Cnt__c,
                            Rental_Apply__r.demo_purpose2__c
                    from Rental_Apply_Equipment_Set__c
                    where UniqueKey__c = :parentId
                      AND Cancel_Select__c = false
                      AND Fixture_Set__c != null];
 
                if (parentObjs.size() > 0) {
                    parentObj = parentObjs.get(0);
                    if (String.isBlank( parentObj.Fixture_Set__c )) {
                        throw new ControllerUtil.myException('借出备品配套一览,没有指定备品配套,不能进行操作!');
                    }
                } else {
                    throw new ControllerUtil.myException('没有找到借出备品一览,请进行正确的操作!');
                }
                if ([SELECT Id
                       FROM Rental_Apply_Equipment_Set__c
                      WHERE Rental_Apply__c = :parentObj.Rental_Apply__c
                        AND Fixture_Set__c = :parentObj.Fixture_Set__c
                        AND Same_Accessory_flag__c = true
                        AND Cancel_Select__c = false
                        AND Id IN (
                                SELECT Rental_Apply_Equipment_Set__c
                                  FROM Rental_Apply_Equipment_Set_Detail__c
                                 WHERE Rental_Apply_Equipment_Set__r.Rental_Apply__c = :parentObj.Rental_Apply__c
                                   AND Rental_Apply_Equipment_Set__r.Fixture_Set__c = :parentObj.Fixture_Set__c
                                   AND Rental_Apply_Equipment_Set__r.Same_Accessory_flag__c = true
                                   AND Rental_Apply_Equipment_Set__r.Cancel_Select__c = false
                                   AND Is_Body__c = false
                                   AND Cancel_Select__c = true
                                   AND Cancel_Reason__c != '重新分配' // OLY_OCM-163#comment-20120592 通过取消理由判断
                            )
                        ].size() > 0) {
 
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,
                            System.Label.SameAccessory_CannotSync));
                }
            }
        }
    }
 
    public void init() {
        isNeedSearchFirst = true;
        viewList = new List<WrapperInfo>();
        extraInfoList = new List<LineExtraInfo>();  // viewList と同じ順番、同じ件数
        getSqlWhereStr();
    }
 
    public PageReference searchOpp() {
 
        // 選択済みの製品を取得
        myComponentController.getSelectedDataInfo();
 
        getSqlWhereStr();
        // コンポーネントにSoqlを発行して、ページングする
        myComponentController.searchAndPaging();
        return null;
    }
 
    public PageReference save() {
 
        //20170906 upsert by UniqueKey__c
        List<Rental_Apply_Equipment_Set_Detail__c> mfUpsert = new List<Rental_Apply_Equipment_Set_Detail__c>();
        Savepoint sp = Database.setSavepoint();
        Set<String> clearUniqueKeySet = new Set<String>();
        // 有删除未审批的追加附属品
        Boolean haveAdd_ApprovalDelete = false;
 
        try {
            // 同一附属品 一括設定する
            if (parentObj.Same_Accessory_flag__c) {
                if ([SELECT Id
                       FROM Rental_Apply_Equipment_Set__c
                      WHERE Rental_Apply__c = :parentObj.Rental_Apply__c
                        AND Fixture_Set__c = :parentObj.Fixture_Set__c
                        AND Same_Accessory_flag__c = true
                        AND Cancel_Select__c = false
                        AND Id IN (
                                SELECT Rental_Apply_Equipment_Set__c
                                  FROM Rental_Apply_Equipment_Set_Detail__c
                                 WHERE Rental_Apply_Equipment_Set__r.Rental_Apply__c = :parentObj.Rental_Apply__c
                                   AND Rental_Apply_Equipment_Set__r.Fixture_Set__c = :parentObj.Fixture_Set__c
                                   AND Rental_Apply_Equipment_Set__r.Same_Accessory_flag__c = true
                                   AND Rental_Apply_Equipment_Set__r.Cancel_Select__c = false
                                   AND Is_Body__c = false
                                   AND Cancel_Select__c = true
                                   AND Cancel_Reason__c != '重新分配' // OLY_OCM-163#comment-20120592 通过取消理由判断
                            )
                        ].size() > 0) {
 
                    // 申請から進んだ明細がある場合
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,
                            System.Label.SameAccessory_CannotSync));
                    return null;
                }
            }
 
            // 画面中チェックされた対象、確定ボタンを押下すると、一つずつ順番に明細を選択のため。
            List<Rental_Apply_Equipment_Set__c> sameRAES_objs = null;
 
            // 申請書ロック(Add_Approval_Status__c 更新のため)
            List<Rental_Apply__c> raObjs = [
                SELECT Id, Name,
                       Status__c,
                       Add_Approval_Status__c,
                       RequestNoJoinStr2__c
                  FROM Rental_Apply__c
                 WHERE Id = :parentObj.Rental_Apply__c
                   FOR UPDATE];
            if (raObjs.size() == 0) {
                throw new ControllerUtil.myException('没有检索出正确的借出备品申请,不能进行操作');
            }
            Rental_Apply__c raObj = raObjs[0];
            if(raObj.Add_Approval_Status__c == '申请中') {
                throw new ControllerUtil.myException('申请中不可以更新明细');
            }
            // parentObj のみ ロック
            sameRAES_objs = [
                SELECT Id, Name,
                       Fixture_Set_Idx__c,
                       Rental_Apply__r.RequestNoJoinStr2__c,
                       Rental_Num__c, Same_Accessory_flag__c,
                       Loaner_name__c,
                       Loaner_name_F__c,
                       Loaner_code__c,
                       Fixture_Set__c,
                       Rental_Apply__c,
                       First_RAESD__c,
                       Loaner_code_F__c,
                       Rental_Apply__r.Strategic_dept__c,//20200825 ljh SWAG-BRY6PF  add
                       IsOPD_Account__c,//20201010 ljh SFDC-BU947L add
                       Canceled__r.First_RAESD__r.Queue_Day_Text__c, //20210903 you SFDC-C6E3WQ
                       Canceled__r.First_RAESD__r.Queue_Time_Text__c, //20210903 you SFDC-C6E3WQ
                       UniqueKey__c
                  FROM Rental_Apply_Equipment_Set__c
                 WHERE Id = :parentObj.Id
                   FOR UPDATE];
            if (sameRAES_objs.size() == 0) {
                throw new ControllerUtil.myException('没有检索出正确的借出备品配套一览,不能进行操作!');
            }
            if (sameRAES_objs[0].Rental_Num__c > 1 && sameRAES_objs[0].Same_Accessory_flag__c) {
                // 同一付属品の一覧もロック
                sameRAES_objs = [
                    SELECT Id, Name,
                           Fixture_Set_Idx__c,
                           Rental_Apply__r.RequestNoJoinStr2__c,
                           Rental_Num__c,Same_Accessory_flag__c,
                           Loaner_name__c,
                           Loaner_name_F__c,
                           Loaner_code__c,
                           Fixture_Set__c,
                           Rental_Apply__c,
                           First_RAESD__c,
                           Loaner_code_F__c,
                           Rental_Apply__r.Strategic_dept__c,//20200825 ljh SWAG-BRY6PF  add
                           IsOPD_Account__c,//20201010 ljh SFDC-BU947L add
                           Canceled__r.First_RAESD__r.Queue_Day_Text__c, //20210903 you SFDC-C6E3WQ
                           Canceled__r.First_RAESD__r.Queue_Time_Text__c, //20210903 you SFDC-C6E3WQ
                           UniqueKey__c
                      FROM Rental_Apply_Equipment_Set__c
                     WHERE Rental_Apply__c = :parentObj.Rental_Apply__c
                       AND Fixture_Set__c = :parentObj.Fixture_Set__c
                       AND Same_Accessory_flag__c = true
                       AND Cancel_Select__c = false
                       FOR UPDATE];
            }
            // SUM出没有还没有批准的追加附属品和已经批准或者不是追加附属品的数量
            // 因为 #getSelectedDataSql() 用 Fixture_Set_Detail__c
            // 所以这里也用 AND Fixture_Set_Detail__c != null
            List<AggregateResult> appendedGroupBy = [
                    SELECT Fixture_Set_Detail__c, Count(Id) cnt, SUM(Draft_Appended__c) Draft_Appended__c, SUM(Not_Draft_Appended__c) Not_Draft_Appended__c
                      FROM Rental_Apply_Equipment_Set_Detail__c
                     WHERE Rental_Apply_Equipment_Set__c = :parentObj.Id
                       AND Cancel_Select__c = false
                       AND Fixture_Set_Detail__c != null
                     GROUP BY Fixture_Set_Detail__c];
            /* Key Fixture_Set_Detail__c value↓
             *                           Draft_Appended -> 未批准的追加附属品数量
             *                           Not_Draft_Appended -> 不是未批准的追加附属品数量
             *                           Accsessarys -> 所有的附属品数量
             */
            Map<String, Map<String, Integer>> add_ApprovalMaps = new Map<String, Map<String, Integer>>();
            for (AggregateResult a : appendedGroupBy) {
                Integer Draft_Appended = a.get('Draft_Appended__c') == null ? 0 : Integer.valueOf(a.get('Draft_Appended__c'));
                Integer Not_Draft_Appended = a.get('Not_Draft_Appended__c') == null ? 0 : Integer.valueOf(a.get('Not_Draft_Appended__c'));
                add_ApprovalMaps.put(String.valueOf(a.get('Fixture_Set_Detail__c')),
                    new Map<String, Integer>{
                        'Draft_Appended' => Draft_Appended,
                        'Not_Draft_Appended' => Not_Draft_Appended,
                        'Accsessarys' => a.get('cnt') == null ? 0 : Integer.valueOf(a.get('cnt'))
                    });
            }
            List<WrapperInfo> targetViewList = new List<WrapperInfo>();
            // 删除未审批追加附属品ERROR信息
            String deleteAdd_ApprovalErrorMessages = '';
            // 删除未审批追加附属品信息
            // 因为有Error的时候不现实成功信息所以需要分开保存最后判断显示什么信息
            String deleteAdd_ApprovalMessages = '';
            Boolean haveError = false;
            for (WrapperInfo wprInfo : viewList) {
                Rental_Apply_Equipment_Set_Detail__c robj = (Rental_Apply_Equipment_Set_Detail__c) wprInfo.sobj;
                if ((robj.FSD_Is_Optional__c == false || robj.Fixture_Set_Detail__r.Is_Optional__c == false)
                        && robj.Rental_Num__c < 1) {
// From 王硕 20180104 没有强制要求必须申请标配的备品。 非选配的也可以不申请。
//                    robj.Rental_Num__c.addError('非选配明细数量不能为0');
//                    return null;
                }
                // 当数量大于0或者数量为0但是是草案中的追加附属品的话可以修改数量
                if (robj.Rental_Num__c > 0 || robj.Draft_Appended__c == 1) {
                    if (add_ApprovalMaps.containsKey(robj.Fixture_Set_Detail__c)) {
                        Map<String, Integer> add_ApprovalMap = add_ApprovalMaps.get(robj.Fixture_Set_Detail__c);
                        //有删除操作。并且未审批追加附属品大于0
                        if (robj.Rental_Num__c < add_ApprovalMap.get('Accsessarys') && add_ApprovalMap.get('Draft_Appended') > 0) {
                            haveAdd_ApprovalDelete = true;
                            // 明细数量小于已批准的追加附属品加上非追加附属品
                            if (robj.Rental_Num__c < add_ApprovalMap.get('Not_Draft_Appended')
                                    && raObj.Status__c != FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Cao_An_Zhong.ordinal())
                                    && raObj.Add_Approval_Status__c == FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Cao_An_Zhong.ordinal())) {
                                deleteAdd_ApprovalErrorMessages += String.format(deleteAdd_ApprovalErrorMessage,
                                                                new String[]{String.valueOf(wprInfo.lineNo + 1),String.valueOf(add_ApprovalMap.get('Accsessarys')),String.valueOf(add_ApprovalMap.get('Draft_Appended'))});
                                deleteAdd_ApprovalErrorMessages += '\n';
                                haveError = true;
                            }
                            // 有删除操作并且明细数量大于等于已批准的追加附属品加上非追加附属品
                            else if (robj.Rental_Num__c >= add_ApprovalMap.get('Not_Draft_Appended')
                                    && raObj.Status__c != FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Cao_An_Zhong.ordinal())
                                    && raObj.Add_Approval_Status__c == FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Cao_An_Zhong.ordinal())) {
                                deleteAdd_ApprovalMessages += String.format(deleteAdd_ApprovalMessage,
                                                                new String[]{String.valueOf(wprInfo.lineNo + 1),String.valueOf(add_ApprovalMap.get('Accsessarys')),String.valueOf(add_ApprovalMap.get('Draft_Appended')),String.valueOf(robj.Rental_Num__c),String.valueOf(add_ApprovalMap.get('Draft_Appended') - (add_ApprovalMap.get('Accsessarys') - robj.Rental_Num__c))});
                                deleteAdd_ApprovalMessages += '\n';
                            }
                        }
                    }
                    targetViewList.add(wprInfo);
                } else if (String.isNotBlank(robj.Id)) {
                    if (raObj.Status__c != FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Cao_An_Zhong.ordinal())) {
                        if (!parentObj.Substitute_flag__c) {
                            robj.Rental_Num__c.addError('只有在草案中的状态下可以减少数量');
                            return null;
                        } else if (parentObj.Yi_Assigned_Cnt__c > 0) {
                            // 分配代替品后,选择配套明细--'已分配件数 Yi_Assigned_Cnt__c'>0且状态为'草案中'时减少数量,报error
                            robj.Rental_Num__c.addError('有明细已分配,不能减少数量');
                            return null;
                        }
                    }
                }
            }
 
            if (haveError) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, deleteAdd_ApprovalErrorMessages));
                return null;
            }
 
            //OLY_OCM-171 与可以选择存放地的简档一致
            Boolean is2B = System.Label.ProfileId_EquCenAdmin.contains(Userinfo.getProfileId())
                                   || Userinfo.getProfileId() == System.Label.ProfileId_SystemAdmin;
                                // || System.Label.ProfileId_EquCenCheckAndDepot.contains(Userinfo.getProfileId())
                                // || Userinfo.getProfileId() == System.Label.ProfileId_EquipmentCenter;
 
            Map<Id, Rental_Apply_Equipment_Set_Detail__c> setIdFirstDetailMap = new Map<Id, Rental_Apply_Equipment_Set_Detail__c>();
            for (WrapperInfo wprInfo : targetViewList) {
                Rental_Apply_Equipment_Set_Detail__c robj = (Rental_Apply_Equipment_Set_Detail__c) wprInfo.sobj;
                LineExtraInfo extraInfo = extraInfoList.get(wprInfo.lineNo);
                Integer maxIndexInLoop = null;
                for (Integer i = 0; i < robj.Rental_Num__c; i++) {
                    // sameRAES_objs には自分の明細か、申請 直後の明細しかないか の条件があります。
                    // なので、直接cloneしてOKです。
                    for (Rental_Apply_Equipment_Set__c sameRAES_obj : sameRAES_objs) {
                        Rental_Apply_Equipment_Set_Detail__c rupsobj = robj.clone(false);
                        // 借出申请RequestNoJoinStr2__c:借出申请配套一览Id:配套明细Id
                        String uniqueKeyStr = raObj.RequestNoJoinStr2__c + ':' + sameRAES_obj.Id
                                + ':'+ robj.Fixture_Set_Detail__c + ':';
                        // default値は最初に設定した
                        rupsobj.IndexFromUniqueKey__c = i+1;
                        if (i < extraInfo.indexFromUniqueKeyList.size()) {
                            rupsobj.IndexFromUniqueKey__c = extraInfo.indexFromUniqueKeyList[i];
                            uniqueKeyStr += rupsobj.IndexFromUniqueKey__c;
                        } else if (extraInfo.maxIndexFromUniqueKey != null) {
                            if (maxIndexInLoop == null) { maxIndexInLoop = extraInfo.maxIndexFromUniqueKey; }
                            maxIndexInLoop++;
                            uniqueKeyStr = raObj.RequestNoJoinStr2__c + ':' + sameRAES_obj.Id
                                    + ':'+ robj.Fixture_Set_Detail__c + ':'+ maxIndexInLoop;
                            rupsobj.IndexFromUniqueKey__c = maxIndexInLoop;
                        } else {
                            // uniqueKeyStr に default の IndexFromUniqueKey__c を使う
                            uniqueKeyStr += rupsobj.IndexFromUniqueKey__c;
                        }
 
                        System.debug(rupsobj.IndexFromUniqueKey__c + '测试uniqueKeyStr:' + uniqueKeyStr);
                        rupsobj.UniqueKey__c          = uniqueKeyStr;
                        // 配套(or 申请)·から
                        rupsobj.Rental_Apply_Equipment_Set__c = sameRAES_obj.Id;
                        rupsobj.Rental_Apply__c               = sameRAES_obj.Rental_Apply__c;
                        
                        mfUpsert.add(rupsobj);
                        if (setIdFirstDetailMap.containsKey(sameRAES_obj.Id) == false || rupsobj.Is_Body__c == true) {
                            setIdFirstDetailMap.put(sameRAES_obj.Id, rupsobj);
                        }
                        if (String.isBlank(rupsobj.Id)
                                && !String.isBlank(uniqueKeyStr)) {
                            // IdあるのレコードをIdクリアSetに追加
                            clearUniqueKeySet.add(uniqueKeyStr);
                        }
                    }
                }
            }
            if (mfUpsert.isEmpty()) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,
                        '请输入明细的数量。'));
                return null;
            }
            else {
                //upsert mfUpsert UniqueKey__c;
                // Upsert
                FixtureUtil.withoutUpsertRaesd(mfUpsert);
                // 削除
                FixtureUtil.delRAESD_excludedUpserted(sameRAES_objs, mfUpsert);
                System.debug('备品配套借出一览明细保存成功');
                List<String> StrategicDeptId = new List<String>();//备品借出申请的战略科室
                List<String> FixtureSetId = new List<String>();//备品配套
                List<String> RAESId = new List<String>();//借出备品配套一览
                Map<String, String> RAES_SMap = new Map<String, String>();//借出备品配套一览+备品借出申请的战略科室
                Map<String, String> RAES_FMap = new Map<String, String>();//借出备品配套一览+备品配套
                Map<String, List<String>> sdept_targetMap = new Map<String, List<String>>();//备品借出申请的战略科室+目标
                Map<String, List<String>> RAES_targetMap = new Map<String, List<String>>();//借出备品配套一览+目标
                List<Rental_Apply_Equipment_Set_Detail__c> rupsobjList =new List<Rental_Apply_Equipment_Set_Detail__c>();//20210903 you SFDC-C6E3WQ
                for (Rental_Apply_Equipment_Set__c sameRAES_obj: sameRAES_objs) {
                    Rental_Apply_Equipment_Set_Detail__c rupsobj = setIdFirstDetailMap.get(sameRAES_obj.Id);
                    sameRAES_obj.First_RAESD__c = rupsobj.Id;  //第一条一览明细
                    system.debug(rupsobj.Id+'==Is_Body__c==='+rupsobj.Is_Body__c+'==='+rupsobj);
                    if(rupsobj.Is_Body__c==true){//必须得是主体才会去更新
                        rupsobj.Queue_Time_Text__c = sameRAES_obj.Canceled__r.First_RAESD__r.Queue_Time_Text__c;//20210903 you SFDC-C6E3WQ
                        rupsobj.Queue_Day_Text__c = sameRAES_obj.Canceled__r.First_RAESD__r.Queue_Day_Text__c;//20210903 you SFDC-C6E3WQ
                        rupsobjList.add(rupsobj);//20210903 you SFDC-C6E3WQ 
                    }
                    //20200825 ljh SWAG-BRY6PF  add start
                    if(sameRAES_obj.Rental_Apply__r.Strategic_dept__c !=null){
                        RAESId.add(sameRAES_obj.Id);
                        StrategicDeptId.add(sameRAES_obj.Rental_Apply__r.Strategic_dept__c);
                        FixtureSetId.add(sameRAES_obj.Fixture_Set__c);
                        RAES_SMap.put(sameRAES_obj.Id,sameRAES_obj.Rental_Apply__r.Strategic_dept__c);
                        RAES_FMap.put(sameRAES_obj.Id,sameRAES_obj.Fixture_Set__c);
                    }
                    //20200825 ljh SWAG-BRY6PF  add end
                }
                //20200825 ljh SWAG-BRY6PF  add start
                if(StrategicDeptId != null&&StrategicDeptId.size()>0){
                    //客户这条线
                    // 目标客户重点产品和目标客户产品借出次数
                    List<String> column = new List<String>();
                    List<String> columnNum = new List<String>();
                    SS_Batch_Column_Mapping__c mpdMapping = SS_Batch_Column_Mapping__c.getValues('targetKeyProduct');
                    Map<String,List<String>> tempImportantProduct = new Map<String,List<String>>();
                    Map<String,String> tempImportantProductNum = new Map<String,String>();
                    for (Integer i = 1; i <= FIELDMAX; i++) {
                        String lpadI = ('00' + i).right(3);
                        String fromColumn = 'From_Column_' + lpadI + '__c';
                        String apiStr = String.valueOf(mpdMapping.get(fromColumn));
                        if (String.isBlank(apiStr) == false) {
                            String ssColumn = 'SS_Column_' + lpadI + '__c';
                            String ssApiStr = String.valueOf(mpdMapping.get(ssColumn));
                            if(i<101){
                                column.add(apiStr);
                                List<String> ssApiStrList = new List<String>(ssApiStr.Split(','));
                                tempImportantProduct.put(apiStr,ssApiStrList);
                            }//else{ 20201026 you SWAG-BSC5WP  在101以后添加新的字段 所以限制一下
                                if(i>=101 && i<116){
                                columnNum.add(apiStr);
                                tempImportantProductNum.put(apiStr,ssApiStr);
                            }
                        }
                    }
                    //所在期数
                    Date dateNow = Date.today();
                    Integer year = dateNow.year();
                    Integer month = dateNow.month();
                    if (month < 4) {
                        year -= 1;
                    }
                    String currentPeriod = String.valueOf(year - 1867 + 'P');
                    //战略科室
                    String StrategicDeptId_s = '(\'';
                    for(Integer i = 0 ; i< StrategicDeptId.size();i++){
                        if(i<StrategicDeptId.size()-1){
                            StrategicDeptId_s += StrategicDeptId[i]+'\',\'';
                        }else{
                            StrategicDeptId_s += StrategicDeptId[i]+'\')';
                        }
                }
                    String soql = 'select Id ,Name,Account__c ';
                    for (Integer i=0;i<column.size();i++) {
                        soql += ',' + column[i];
                    }
                    for (Integer num=0;num<columnNum.size();num++) {
                        soql += ',' + columnNum[num];
                    }
                    soql += '  FROM Account_Number_of_target__c  WHERE  OCM_Period__c = \''+currentPeriod+'\'  and Account__c in '+StrategicDeptId_s;
                    List<Account_Number_of_target__c>  antargetList = Database.query(soql);
                    Map<String,Account_Number_of_target__c> sdept_LendNumMap = new Map<String,Account_Number_of_target__c>();
                    Map<String,Account_Number_of_target__c> RAES_LendNumMap = new Map<String,Account_Number_of_target__c>();
                    for(Account_Number_of_target__c ant:antargetList){
                        List<String> titleAccout = new List<String>();
                        for(Integer i=0;i<column.size();i++){
                            if(1==ant.get(column[i])){
                                titleAccout.addAll(tempImportantProduct.get(column[i]));
                            }
                        }
                        sdept_targetMap.put(ant.Account__c,titleAccout);
                        sdept_LendNumMap.put(ant.Account__c,ant);
                    }
                    //两个Map合并 借出备品配套一览+备品借出申请的战略科室 与 备品借出申请的战略科室+目标=借出备品配套一览+目标
                    Set<String> keySet0 = RAES_SMap.keySet();
                    for(String ks0:keySet0){
                        RAES_targetMap.put(ks0,sdept_targetMap.get(RAES_SMap.get(ks0)));
                        RAES_LendNumMap.put(ks0,sdept_LendNumMap.get(RAES_SMap.get(ks0)));
                    }
                    //备品主体产品这条线
                    List<Fixture_Set_Detail__c> fsDetail = [select  Id,Fixture_Set__c, Product2__c from Fixture_Set_Detail__c where Is_Body__c=true and Fixture_Set__c in:FixtureSetId];
                    List<String> Product2Id = new List<String>();
                    Map<String, String> map1 = new Map<String, String>();
                    Map<String, String> map2 = new Map<String, String>();
                    Map<String, String> map1_2 = new Map<String, String>();
                    Map<String, String> RAES_ImpMap = new Map<String, String>();
                    //RAES_FMap<借出备品配套一览,备品配套> map1<备品配套,重点产品ID>
                    for(Fixture_Set_Detail__c fs1:fsDetail){
                        map1.put(fs1.Fixture_Set__c,fs1.Product2__c);
                        Product2Id.add(fs1.Product2__c);
                    }
                    //map2<重点产品ID,重点产品>
                    List<Product2> product2 = [select id,Key_product_147P__c from Product2 where Id in:Product2Id];
                    for(Product2 p:product2){
                        map2.put(p.Id,p.Key_product_147P__c);
                    }
                    //两个Map1与Map2合并 在与RAES_FMap合并得到 Map<借出备品配套一览,重点产品>
                    Set<String> keySet = map1.keySet();
                    for(String ks:keySet){
                        map1_2.put(ks,Map2.get(map1.get(ks)));
                    }
                    Set<String> keySet1 = RAES_FMap.keySet();
                    for(String ks1:keySet1){
                        RAES_ImpMap.put(ks1,map1_2.get(RAES_FMap.get(ks1)));
                    }
                    //对比是否是重点考察产品
                    Map<String, Integer> mapIdIs = new Map<String, Integer>();
                    Map<String, Integer> LendNumMap = new Map<String, Integer>();
                    Set<String> keySet2 = RAES_ImpMap.keySet();
                    for(String ks2:keySet2){
                        Integer tempIs = 0;
                        Integer LendNum = null;
                        if(RAES_targetMap.get(ks2) != null &&RAES_targetMap.get(ks2).size()>0){
                            if(String.isNotBlank(RAES_ImpMap.get(ks2))&&RAES_targetMap.get(ks2).contains(RAES_ImpMap.get(ks2).substring(3))){
                                tempIs = 1;
                            }
                        }
                        //主体产品借出的次数
                        if(String.isNotBlank(RAES_ImpMap.get(ks2))){
                            //主体产品借出的次数
                            String temp = null ;
                            Set<String> keySet4 = tempImportantProductNum.keySet();
                            for (String ks4:keySet4) {
                                if(tempImportantProductNum.get(ks4).equals(RAES_ImpMap.get(ks2).substring(3))){
                                    temp = ks4;
                                    break;
                                }
                            }
                            if(temp !=null){
                              Account_Number_of_target__c tempANOT= RAES_LendNumMap.get(ks2);
                              if(tempANOT!=null &&tempANOT.get(temp) !=null){
                                LendNum = Integer.valueOf(tempANOT.get(temp));
                              }
                            }
                        }
                        mapIdIs.put(ks2,tempIs);
                        LendNumMap.put(ks2,LendNum);
                    }
                    Set<String> keySet3 = mapIdIs.keySet();
                    for (Rental_Apply_Equipment_Set__c rAES_obj1: sameRAES_objs) {
                        if(keySet3.contains(rAES_obj1.Id)){
                            rAES_obj1.IsOPD_Account__c = mapIdIs.get(rAES_obj1.Id);
                            rAES_obj1.Product2__c = RAES_ImpMap.get(rAES_obj1.Id);
                            rAES_obj1.LendNum__c = LendNumMap.get(rAES_obj1.Id);
                        }
                    }
                }
                //20200825 ljh SWAG-BRY6PF  add end
                //update sameRAES_objs;
                FixtureUtil.withoutUpdate(sameRAES_objs); 
                // Upsert
                FixtureUtil.withoutUpsertRaesd(rupsobjList);//20210903 you SFDC-C6E3WQ
            }
 
            //申请书的追加附属品状态需
            //因为是申请的状态所以需要要看是不是申请书下所有的追加附属品(不只是这一个一览下的)
            List<AggregateResult> add_Approvals = [Select Count(Id) cnt, SUM(Draft_Appended__c) Draft_Appended__c
                    From Rental_Apply_Equipment_Set_Detail__c
                   Where Rental_Apply__c = :parentObj.Rental_Apply__c
                     AND Cancel_Select__c = false
                     AND Fixture_Set_Detail__c != null
                     AND ApplyPersonAppended_F__c = true
                   Group by Rental_Apply__c];
            Integer Draft_Appended = 0;
            Integer add_ApprovalCount = 0;
            if (!add_Approvals.isEmpty()) {
                Draft_Appended = add_Approvals[0].get('Draft_Appended__c') == null ? 0 : Integer.valueOf(add_Approvals[0].get('Draft_Appended__c'));
                add_ApprovalCount = add_Approvals[0].get('cnt') == null ? 0 : Integer.valueOf(add_Approvals[0].get('cnt'));
            }
 
            List<Rental_Apply__c> raList = new List<Rental_Apply__c>();
            // 有草案中的追加附属品并且申请书的状态不是草案中,&& 不是分配代替品 的需要改变状态为草案中
            if (Draft_Appended > 0
                    && raObj.Add_Approval_Status__c != FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Cao_An_Zhong.ordinal())
                    && (raObj.Status__c == FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Yi_Pi_Zhun.ordinal())
                        || raObj.Status__c == FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Yi_Chu_Ku_Zhi_Shi.ordinal()))
                    && parentObj.Substitute_Select_Again__c == false) {
                raObj.Add_Approval_Status__c = FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Cao_An_Zhong.ordinal());
                raList.add(raObj);
            }
            // 申请书单位已经没有草案中的附属品
            else if (Draft_Appended == 0) {
                //没有追加附属品状态为空
                if (add_ApprovalCount == 0 && String.isNotBlank(raObj.Add_Approval_Status__c)) {
                    raObj.Add_Approval_Status__c = '';
                }
                // 有追加附属品状态为草案中
                else if (add_ApprovalCount > 0  && raObj.Add_Approval_Status__c != FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Yi_Pi_Zhun.ordinal())) {
                    raObj.Add_Approval_Status__c = FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Yi_Pi_Zhun.ordinal());
                }
                raList.add(raObj);
            }
            if (!raList.isEmpty()) {
                //20201010 ljh SFDC-BU947L add start
                Boolean IsOPDAccountFlag = false;
                //system.debug('112233:'+sameRAES_objs);
                List<Rental_Apply_Equipment_Set__c>  sameRAES_objsIs = [SELECT Id, Name,
                           Fixture_Set_Idx__c,
                           Rental_Apply__r.RequestNoJoinStr2__c,
                           Rental_Num__c,Same_Accessory_flag__c,
                           Loaner_name__c,
                           Loaner_name_F__c,
                           Loaner_code__c,
                           Fixture_Set__c,
                           Rental_Apply__c,
                           First_RAESD__c,
                           Loaner_code_F__c,
                           Rental_Apply__r.Strategic_dept__c,//20200825 ljh SWAG-BRY6PF  add
                           IsOPD_Account__c,//20201010 ljh SFDC-BU947L add
                           UniqueKey__c
                      FROM Rental_Apply_Equipment_Set__c
                     WHERE Rental_Apply__c = :sameRAES_objs[0].Rental_Apply__c
                       AND Cancel_Select__c = false
                       FOR UPDATE ];
                for (Rental_Apply_Equipment_Set__c sameRAES_obj : sameRAES_objsIs) {
                    if(sameRAES_obj.IsOPD_Account__c !=null && sameRAES_obj.IsOPD_Account__c.intValue()==1){
                        IsOPDAccountFlag = true;break;
                    }
                }
                if(IsOPDAccountFlag){
                    raList[0].IsOPD_Account__c = 1;
                }else{
                    raList[0].IsOPD_Account__c = 0;
                }
                //20201010 ljh SFDC-BU947L  add end
                FixtureUtil.withoutUpdate(raList);
            }
            // 更新成功才添加成功信息
            if (haveAdd_ApprovalDelete == true) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, deleteAdd_ApprovalMessages));
 
            }
 
        } catch (Exception ex) {
            System.debug(ex.getStackTraceString());
            ApexPages.addMessages(ex);
            Database.rollback(sp);
            // Id をクリア
            for (Rental_Apply_Equipment_Set_Detail__c robj : mfUpsert) {
                if (clearUniqueKeySet.contains(robj.UniqueKey__c)) {
                    robj.Id = null;
                }
            }
            return null;
        }
 
        // 因为不能更新sidList所以需要在更新sidList前return
        if (haveAdd_ApprovalDelete == true) {
            return null;
        }
 
        if (saveType == '1') {
            searchOpp();
            saveType = '';
            return null;
        } else if (saveType == '2') {
        // ソート時の変更ある
            myComponentController.getSelectedDataInfo();
            getSqlWhereStr();
            myComponentController.sortTable();
            saveType = '';
            return null;
        } else {
            //借出备品配套明细选择画面
            List<String> uniqStrs = parentId.split(':');
            //画面参数为多个配套明细的UniqueKey__c,同一附属品情况下,一括设定该备品配套(group)下的所有借出备品配套明细
            //非同一附属品情况下,依次设定该备品配套所存在的借出备品配套明细的设定
            //判断是否同一附属品
            if(parentObj.Same_Accessory_flag__c){
                sidList.remove(0);
            }else{
                system.debug('●●●●● uniqStrs  ' + uniqStrs );
 
                String tempSize = uniqStrs.get(uniqStrs.size()-1);
                // 分配代替品画面
                if (parentObj.Substitute_Select_Again__c) {
                    tempSize = tempSize.substring(0, 1);
                }
 
                if(Integer.valueOf(tempSize) < parentObj.Rental_Num__c){
                    List<String> uniqStrsTemp = new List<String> ();
                    uniqStrsTemp.addall(uniqStrs);
                    uniqStrsTemp.remove(uniqStrsTemp.size()-1);
                    Integer currObjBan = Integer.valueOf(tempSize);
                    uniqStrsTemp.add( (currObjBan + 1) + '');
                    String uniqStrsNew = String.join(uniqStrsTemp, ':');
                    sidList.set(0, uniqStrsNew);
                }else{
                    sidList.remove(0);
                }
 
            }
 
            if (sidList.size() > 0) {
                String chkIdsStr = String.join(sidList, ',');
                PageReference pg = new PageReference('/apex/RentalFixtureSetDetilSelect');
                pg.getParameters().put('sids',chkIdsStr);
                pg.setRedirect(true);
                return pg;
            } else {
                // 借出申请页面
                PageReference pg = new PageReference('/'+ parentObj.Rental_Apply__c);
                pg.setRedirect(true);
                return pg;
            }
        }
 
 
 
    }
 
 
    public PageReference cancel() {
        PageReference ret = null;
        if (!String.isBlank(parentObj.Rental_Apply__c)) {
            ret = new PageReference('/' + parentObj.Rental_Apply__c);
        }
        return ret;
    }
 
    public PageReference goBack() {
        PageReference ret = null;
        if (!String.isBlank(parentObj.Rental_Apply__c)) {
            ret = new PageReference('/apex/RentalFixtureSetSelect?pt_recid=' + parentObj.Rental_Apply__c);
        }
        return ret;
    }
 
    private String makeSoql() {
 
        String soql ='';
        soql += 'where Id != null';
 
        //备品配套下的所有明细
        if (!String.isBlank(this.parentId)) {
system.debug('●●●●● parentObj ' + parentObj );
            soql += ' and Fixture_Set__c = \'' + parentObj.Fixture_Set__c + '\'';
        }
        // soql += ' order by SortInt__c';
        system.debug(soql);
        return soql;
    }
 
    public override void setViewList(List<sObject> queryList) {
        viewList = new List<WrapperInfo>();                             // selectedGroupData と同じ順番、同じ件数
        extraInfoList = new List<LineExtraInfo>();  // viewList と同じ順番、同じ件数
        Set<Id> selectedFSDSet = new Set<Id>();
        // 選択済みの明细
        if (selectedData.size() > 0) {
            selectedGroupData = new List<Rental_Apply_Equipment_Set_Detail__c>();
            Map<Integer, Rental_Apply_Equipment_Set_Detail__c> sltFixtureMap = new Map<Integer, Rental_Apply_Equipment_Set_Detail__c>();
            Map<Integer, Integer> sltFixtureCnt = new Map<Integer, Integer>();
            Map<Integer, LineExtraInfo> sltExtraInfoMap = new Map<Integer, LineExtraInfo>();
            // グループリスト取得
            // group by Fixture_Set_Detail__c
            // order by Fixture_Set_Detail__r.SortInt__c
            for (SObject sobj : selectedData) {
                Rental_Apply_Equipment_Set_Detail__c raesdobj = (Rental_Apply_Equipment_Set_Detail__c) sobj;
                if (!sltFixtureMap.containsKey(raesdobj.Fixture_Set_Detail__r.SortInt__c.intValue())) {
                    if (!raesdobj.Cancel_Select__c) {
                        sltFixtureMap.put(raesdobj.Fixture_Set_Detail__r.SortInt__c.intValue(), raesdobj);
                        selectedGroupData.add(raesdobj);
                        sltFixtureCnt.put(raesdobj.Fixture_Set_Detail__r.SortInt__c.intValue(), 0);
                        LineExtraInfo extraInfo = new LineExtraInfo(raesdobj);
                        extraInfoList.add(extraInfo);
                        sltExtraInfoMap.put(raesdobj.Fixture_Set_Detail__r.SortInt__c.intValue(), extraInfo);
                    }
                } else {
                    LineExtraInfo extraInfo = sltExtraInfoMap.get(raesdobj.Fixture_Set_Detail__r.SortInt__c.intValue());
                    extraInfo.updateExtraInfo(raesdobj);
                }
                if (raesdobj.Cancel_Select__c) {
                    // 念のため追加した
                    continue;
                }
 
                Rental_Apply_Equipment_Set_Detail__c nobj = sltFixtureMap.get(raesdobj.Fixture_Set_Detail__r.SortInt__c.intValue());
                Integer nCnt = sltFixtureCnt.get(raesdobj.Fixture_Set_Detail__r.SortInt__c.intValue());
                nCnt++;
                sltFixtureCnt.put(raesdobj.Fixture_Set_Detail__r.SortInt__c.intValue(), nCnt);
                nobj.Rental_Num__c = nCnt;
                nobj.VF_Rental_Num__c = nCnt;
                selectedFSDSet.add(raesdobj.Fixture_Set_Detail__c);
            }
            for (Integer i = 0; i < selectedGroupData.size(); i++) {
                // 501を超えた場合前500のみを出す
                if (i >= getSearchNumMax()) { break; }
                Rental_Apply_Equipment_Set_Detail__c raesdobj = selectedGroupData[i];
                viewList.add(new WrapperInfo(raesdobj, myComponentController));
                viewList[viewList.size() - 1].lineNo = viewList.size() - 1;
                if((raesdobj.Rental_Apply__r.demo_purpose2__c == '试用(有询价)' || raesdobj.Rental_Apply__r.demo_purpose2__c == '试用(无询价)')
                  && !raesdobj.Rental_Apply_Equipment_Set__r.Substitute_flag__c){
                    viewList[viewList.size() - 1].canEdit = raesdobj.Fixture_Set_Detail__r.Product_Status_Flag__c;
                }
                raesdobj.IndexFromUniqueKey_Text__c = viewList.size().format().leftpad(2, '0');
            }
        }
        if (queryList.size() == 0) {
            return;
        }
 
        Savepoint sp = Database.setSavepoint();
 
        // 数式項目値を取れるのために、一回Insertする
        List<Rental_Apply_Equipment_Set_Detail__c> tempList = new List<Rental_Apply_Equipment_Set_Detail__c>();
        for (Integer i = 0; i < queryList.size(); i++) {
            // 501を超えた場合前500のみを出す
            if (i == getSearchNumMax()) { break; }
            Rental_Apply_Equipment_Set_Detail__c mf = new Rental_Apply_Equipment_Set_Detail__c();
            mf.Rental_Apply_Equipment_Set__c        = parentObj.Id;
            mf.Rental_Apply__c                      = parentObj.Rental_Apply__c;
            mf.Fixture_Set_Detail__c                = queryList[i].Id;
            Fixture_Set_Detail__c fsdobj            = (Fixture_Set_Detail__c) queryList[i];
            mf.Is_Body__c                           = fsdobj.Is_Body__c;
            if (selectedData.size() > 0) {
                fsdobj.Quantity__c = 0;
            } else {
                fsdobj.Quantity__c = fsdobj.Quantity__c == null ? 0 : fsdobj.Quantity__c;
            }
            mf.Rental_Num__c                        = fsdobj.Quantity__c;
            mf.VF_Rental_Num__c                     = fsdobj.Quantity__c;
            mf.DataMigration_Flag__c                = true;
            tempList.add(mf);
        }
 
        Database.SaveResult[] results = FixtureUtil.withoutInsert(tempList, false);
 
        final String soqlStr = 'Select {0} {1} ';
        String whereStr = ' FROM Rental_Apply_Equipment_Set_Detail__c WHERE ID in: tempList';
        String soql = String.format(soqlStr, new String[] {myComponentController.strColumus , whereStr});
 
        tempList = Database.query(soql);
        if (queryList.size() != tempList.size()) {
            //error message 検索処理正しくありません、システム管理者に連絡してください。
            for (Integer i = 0; i < results.size(); i ++) {
                Database.SaveResult dmlResult = results[i];
                if (!dmlResult.isSuccess()) {
                    System.debug(System.LoggingLevel.ERROR, '第[' + (i + 1) + ']条 insert error:' + dmlResult);
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,
                            '第[' + (i + 1) + ']条 insert error:' + dmlResult));
                    // 1件目だけlogに出す
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,
                            System.Label.CreateRelationListSearchError));
                    Database.rollback(sp);
                    return;
                }
            }
        }
        // 強制ロールバック
        Database.rollback(sp);
        Map<String, SObject> fsDMap = new Map<String,SObject>();
        for(Rental_Apply_Equipment_Set_Detail__c fsd : tempList){
            Rental_Apply_Equipment_Set_Detail__c robj = fsd.clone(false);
            robj.DataMigration_Flag__c = false;
            robj.FSD_Product_Status_Flag__c = true;
            fsDMap.put(fsd.Fixture_Set_Detail__c, robj);
        }
 
        Integer addedNum = 0;
        Map<Integer, Rental_Apply_Equipment_Set_Detail__c> sltFixtureMap = new Map<Integer, Rental_Apply_Equipment_Set_Detail__c>();
        for (Integer i = 0; i < queryList.size(); i++) {
 
            addedNum ++;
            // 501を超えた場合前500のみを出す
            if (addedNum == getSearchNumMax()) { break; }
 
            if (selectedFSDSet.contains(queryList[i].Id)) {
                continue;
            }
 
            SObject mf = fsDMap.get(queryList[i].Id);
            Rental_Apply_Equipment_Set_Detail__c raesdobj = (Rental_Apply_Equipment_Set_Detail__c) mf;
            if (!sltFixtureMap.containsKey(raesdobj.Fixture_Set_Detail__r.SortInt__c.intValue())) {
                sltFixtureMap.put(raesdobj.Fixture_Set_Detail__r.SortInt__c.intValue(), raesdobj);
                // 2回目以降、設定してない行は0にする
                // if (!selectedData.isEmpty()) {
                // OLY_OCM-404: 选择配套明细里,无论主体还是附属品,都设置默认数量为0。
                raesdobj.Rental_Num__c    = 0;
                raesdobj.VF_Rental_Num__c = 0;
                // }
                viewList.add(new WrapperInfo(raesdobj, myComponentController));
                extraInfoList.add(new LineExtraInfo(null));
                viewList[viewList.size() - 1].lineNo = viewList.size() - 1;
                if((raesdobj.Rental_Apply__r.demo_purpose2__c == '试用(有询价)' || raesdobj.Rental_Apply__r.demo_purpose2__c == '试用(无询价)')
                  && !raesdobj.Rental_Apply_Equipment_Set__r.Substitute_flag__c){
                    viewList[viewList.size() - 1].canEdit = raesdobj.Fixture_Set_Detail__r.Product_Status_Flag__c;
                }
                raesdobj.IndexFromUniqueKey_Text__c = viewList.size().format().leftpad(2, '0');
            }
        }
 
system.debug('●●●●● setViewList END ' );
    }
 
 
    public class LineExtraInfo {
        public Integer maxIndexFromUniqueKey;           // { get; set; }
        public List<Decimal> indexFromUniqueKeyList;    // { get; set; }
        public Set<String> raesdUniqueKeySet;           // { get; set; }
 
        // Cancel_Select 処理しない
        public LineExtraInfo(Rental_Apply_Equipment_Set_Detail__c raesdobj) {
            // maxIndexFromUniqueKey を null にする
            raesdUniqueKeySet = new Set<String>();
            indexFromUniqueKeyList = new List<Decimal>();
            if (raesdobj == null || String.isBlank(raesdobj.Id)) {
                // 新規行、空のインスタンスを返す
                return;
            }
            if (raesdobj.Cancel_Select__c) {
                throw new ControllerUtil.myException('raesdobj is Canceled.');
            }
            raesdUniqueKeySet.add(raesdobj.UniqueKey__c);
            indexFromUniqueKeyList.add(raesdobj.IndexFromUniqueKey__c);
            maxIndexFromUniqueKey = raesdobj.IndexFromUniqueKey__c.intValue();
        }
 
        // Cancel_Select も処理対象 (maxIndexFromUniqueKeyを更新するために)
        public void updateExtraInfo(Rental_Apply_Equipment_Set_Detail__c raesdobj) {
            if (raesdobj == null || String.isBlank(raesdobj.Id)
                    || maxIndexFromUniqueKey == null) {
                // 新規行、なにも更新しない
                return;
            }
            if (raesdUniqueKeySet.contains(raesdobj.UniqueKey__c)) {
                return;
            }
            if (!raesdobj.Cancel_Select__c) {
                raesdUniqueKeySet.add(raesdobj.UniqueKey__c);
                indexFromUniqueKeyList.add(raesdobj.IndexFromUniqueKey__c);
            }
            Integer indexFromUniqueKey = raesdobj.IndexFromUniqueKey__c.intValue();
            if (indexFromUniqueKey > maxIndexFromUniqueKey) {
                maxIndexFromUniqueKey = raesdobj.IndexFromUniqueKey__c.intValue();
            }
        }
    }
}