binxie
2024-01-18 0e0dd1e20e7211f3c3c11d77a41090d998dfd06c
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
trigger RentalApplyTrigger on Rental_Apply__c (before insert, before update,after insert,after update,after delete) {
    Integer i = 0;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;    
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    if ((!Test.isRunningTest()) && !System.Label.ByPassTrigger.contains(UserInfo.getUserId()) && !FixtureUtil.isRepairRenewal) {
        RentalApplyTriggerHandler handler = new RentalApplyTriggerHandler();
        handler.run();
 
        // ----------------------------------------------------------------------
        // ここより、承認プロセス用に、经理、部长、总监を更新する
        // ----------------------------------------------------------------------
        //修理set
        //LD 20200819 因备品出借可视化管理注释以下代码 Start
        //Set<String> repairSet = new Set<String>();
 
 
    //bp2    List<id> idlistLocal = new List<id>();
    //bp2    Map<id,List<Rental_Apply_Equipment_Set__c>> AllDetailsMaps = new Map<id,List<Rental_Apply_Equipment_Set__c>>();
 
        if (Trigger.isBefore) {
            // OLY_OCM-584 Start 移动到Handler
            // 申請中かどうかのチェック
            // List<Id> copyUserIds = new List<Id>();                       // 件数は Trigger.New と同じ
            // List<Rental_Apply__c> newList = new List<Rental_Apply__c>(); // 件数は Trigger.New と同じ
            // OLY_OCM-584 End
            Rental_Apply__c old = null;
            List<String> personInChargeIds = new List<String>();
    //bp2
    //        for(Rental_Apply__c local : Trigger.New){
    //            if(local.id != null){
    //                idlistLocal.add(local.id);
    //            }
    //        }
    //        List<Rental_Apply_Equipment_Set__c> AllDetails = new List<Rental_Apply_Equipment_Set__c>();
    //        AllDetails = [select Name,Rental_Apply__c,Equipment_Set__r.Name,Equipment_Set__r.Loaner_code__c,SerialNumber__c from Rental_Apply_Equipment_Set__c 
    //            where Rental_Apply__c in: idlistLocal
    //            and Inspection_before__c!='NG' 
    //            and RAES_Status__c != '引当済'
    //            and RAES_Status__c != '取消分配'];
    //        for(Rental_Apply_Equipment_Set__c raes : AllDetails){
    //            if(AllDetailsMaps.containsKey(raes.Rental_Apply__c)){
    //                List<Rental_Apply_Equipment_Set__c> raseList = AllDetailsMaps.get(raes.Rental_Apply__c);
    //                raseList.add(raes);
    //            }else{
    //                List<Rental_Apply_Equipment_Set__c> raseList = new List<Rental_Apply_Equipment_Set__c>();
    //                raseList.add(raes);
    //                AllDetailsMaps.put(raes.Rental_Apply__c, raseList);
    //            }
    //        }
            for (Rental_Apply__c local : Trigger.New) {
                if ( Trigger.isUpdate) {
                    old = Trigger.oldMap.get(local.Id);
                }
 
                // 主担当をOwnerIdにします
                // if (local.Person_In_Charge__c != null) local.OwnerId = local.Person_In_Charge__c;
 
                // 黑名单用户不能提交申请  OCSM_BP3-10: 去掉"犯规点数超过Max的时候不能申请"的限制条件。【EquipmentRentalBlackList为999999999的时候, 事实上就是不check 】
                if (trigger.isUpdate
                        && local.Foul_Points__c >= Integer.valueOf(System.Label.EquipmentRentalBlackList)
                        && local.demo_purpose2__c !='索赔QIS' //使犯规也要通过
                        // 申请中 防止change owner
                        && ((local.Status__c == FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Shen_Qing_Zhong.ordinal()) || local.Status__c == '申请中(OPD未通过)')    //20231224 sx add  备品智能化添加状态 优化
                                && old.OwnerId != local.OwnerId
                        )
                        && System.Label.ProfileId_GPI_IF != UserInfo.getProfileId()) {
                    local.addError('已超期借用备品过多,不能提交备品借出申请。备品借出申请:' + local.Name);
                }
                // 去掉"犯规点数超过Max的时候不能申请"的限制条件。【EquipmentRentalBlackList_ChangeOwner_Threshold为999999999的时候, 事实上就是不check 】
                if (trigger.isUpdate
                        && local.Foul_Points__c >= Integer.valueOf(System.Label.EquipmentRentalBlackList_ChangeOwner_Threshold)
                        && local.demo_purpose2__c !='索赔QIS' //使犯规也要通过
                        // OLY_OCM-398
                        && (old.Status__c != FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Tian_Xie_Wan_Bi.ordinal())
                                && local.Status__c == FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Tian_Xie_Wan_Bi.ordinal())
                        )
                        && System.Label.ProfileId_GPI_IF != UserInfo.getProfileId()) {
                    local.addError('已超期借用备品过多,不能提交备品借出申请。备品借出申请:' + local.Name);
                }
                
        //*************************Create 20160615 No.153 趙徳芳 Start*************************//
    //bp2
    //        //更新出库富文本
    //            if((Trigger.isUpdate||Trigger.isInsert)
    //                &&(local.Shipment_requested_cnt__c!=null
    //                    &&local.Pre_inspection_ng_num__c!=null
    //                        &&local.Rental_Apply_Equipment_Set_Cnt__c!=null
    //                            &&local.Rental_Apply_Equipment_Set_Cnt__c!=0)){
    //                local.Notice_of_Delivery_Text__c = '出库通知:<BR> 已出库条数:'
    //                            + (local.Shipment_requested_cnt__c - local.Shippment_ng_num__c) 
    //                            + '<BR>明细共:'
    //                            + local.Rental_Apply_Equipment_Set_Cnt__c
    //                            + '条。<BR>';
    //            }
    //        //更新全部出库邮件通知文本
    //            List<Rental_Apply_Equipment_Set__c> AllDetails = new List<Rental_Apply_Equipment_Set__c>();
    //            AllDetails = [select Name, Fixture_Set__r.Name, Fixture_Set__r.Loaner_code__c, First_RAESD__r.SerialNumber_F__c
    //                from Rental_Apply_Equipment_Set__c 
    //                where Rental_Apply__c =: local.id 
    //                 and Inspection_before__c!='NG' 
    //                 and Cancel_Select__c = False
    //                 and RAES_Status__c != '引当済'
    //                 and RAES_Status__c != '取消分配'];
    //            system.debug('这是所有明细::::::::'+AllDetails);
    //            system.debug('这是所有::::::::'+local.id);
    //            if (AllDetails.size() > 0) {
    //                local.Assigned_Text__c = '<br/>具体明细:<br/>       ';
    //                for(Rental_Apply_Equipment_Set__c raesMail : AllDetails){
    //                    local.Assigned_Text__c = local.Assigned_Text__c
    //                            + raesMail.Fixture_Set__r.Name + '  '
    //                            + ' 型号: ' + raesMail.Fixture_Set__r.Loaner_code__c
    //                            +' 机身编号:'+( raesMail.First_RAESD__r.SerialNumber_F__c!=null? raesMail.First_RAESD__r.SerialNumber_F__c:'')+
    //                            + '<br/>';
    //                } 
    //            }else{
    //                local.Assigned_Text__c = '<br/>无可以出库的备品<br/>       ';
    //            }
        //*************************Create 20160615 No.153 趙徳芳 End***************************//
                // OLY_OCM-584 Start 移动到Handler
                // 申請中になったとき、申請者上司を設定
                // if (Trigger.isInsert
                //         || (Trigger.isUpdate && old.Status__c != local.Status__c && local.Status__c == '申请中')
                //         || (Trigger.isUpdate && old.OwnerId != local.OwnerId)
                // ) {
                //     newList.add(local);
                //     //if (Trigger.isInsert || (Trigger.isUpdate && old.applyUser__c != local.applyUser__c)) {
                //     copyUserIds.add(local.OwnerId);
                //     //} else {
                //     //    copyUserIds.add(UserInfo.getUserId());
                //     //}
                // }
                // OLY_OCM-584 End
            //*************************SWAG-AZNAER 20180627 Start*************************//
                personInChargeIds.add(local.Person_In_Charge__c);
            }
            if (System.Test.isRunningTest() && trigger.isUpdate) {
            } else {
                Map<Id, User> personInChargeMap = new Map<Id, User>([
                    SELECT Id, Name, OCM_man_province_Rental__c, Dept__c FROM User WHERE Id IN :personInChargeIds
                ]);
                // 20220517 ljh SWAG-CDTEFS update start
                // List<OCM_Management_Province__c> mpList = [select id, Name, Admin_assistant__c, Admin_assistant2__c, Admin_assistant3__r.Name, Energy_assistant__c from OCM_Management_Province__c];
                List<OCM_Management_Province__c> mpList = [select id, Name, Admin_assistant__c, Admin_assistant2__c, Admin_assistant3__r.Name, Energy_assistant__c,OBA3_Email_Group__c from OCM_Management_Province__c];
                Map<String, OCM_Management_Province__c> mpMap = new Map<String, OCM_Management_Province__c>();
                for (OCM_Management_Province__c mp : mpList) {
                    mpMap.put(mp.Name, mp);
                }
                for (Rental_Apply__c rac : Trigger.New) {
                    if (personInChargeMap.containsKey(rac.Person_In_Charge__c)) {
                        User tempUser = personInChargeMap.get(rac.Person_In_Charge__c);
                        OCM_Management_Province__c omp = mpMap.get(tempUser.OCM_man_province_Rental__c);
                        if (omp != null) {
                            /*
                            SWAG-B6X8CK  把能量事业本部的判断去掉,都取备品助理 2018/11/29 start
                            */
                            /*if (tempUser.Dept__c == '能量事业本部') {
                                rac.Rental_Assistant__c = omp.Energy_assistant__c;
                                rac.Rental_Assistant2__c = null;
                                rac.Rental_Assistant3__c = null;
                            } else {*/
                                rac.Rental_Assistant__c = omp.Admin_assistant__c;
                                rac.Rental_Assistant2__c = omp.Admin_assistant2__c;
                                rac.Rental_Assistant3__c = omp.Admin_assistant3__r.Name;// SWAG-BZL695 you 20210331
                                rac.OBA3_Email_Group__c = omp.OBA3_Email_Group__c;// 20220517 ljh SWAG-CDTEFS add
                            //}
                            /*
                            SWAG-B6X8CK  把能量事业本部的判断去掉,都取备品助理 2018/11/29 end
                            */
                        }
                    }
                }
            }
            //*************************SWAG-AZNAER 20180627 End*************************//
            // OLY_OCM-584 Start  移动到Handler
            // if (copyUserIds.size() > 0) {
            //     Map<Id, User> copyUserMap = new Map<Id, User>([
            //         SELECT Id, Name, Buzhang_Equipment_Manager__c, JingliEquipmentManager__c, SalesManager__c, BuchangApprovalManagerSales__c, JingliApprovalManager__c, BuchangApprovalManager__c, ZongjianApprovalManager__c, TongkuoZongjian__c FROM User WHERE Id IN :copyUserIds
            //     ]);
            //
            //     for (Integer i = 0; i < copyUserIds.size(); i++) {
            //         Rental_Apply__c local = newList[i];
            //         User loginUser = copyUserMap.get(copyUserIds[i]);
            //         local.SalesManager__c = loginUser.JingliEquipmentManager__c != null ? loginUser.JingliEquipmentManager__c : loginUser.SalesManager__c;
            //         local.BuchangApprovalManagerSales__c = loginUser.Buzhang_Equipment_Manager__c != null ? loginUser.Buzhang_Equipment_Manager__c : loginUser.BuchangApprovalManagerSales__c;
            //         local.JingliApprovalManager__c = loginUser.JingliApprovalManager__c;
            //         local.BuchangApprovalManager__c = loginUser.BuchangApprovalManager__c;
            //         local.ZongjianApprovalManager__c = loginUser.ZongjianApprovalManager__c;
            //         local.TongkuoZongjian__c = loginUser.TongkuoZongjian__c;
            //     }
            // }
            // OLY_OCM-584 End
            /** 暂时先不要编码加_1,_2
            // SWAG-BZL695 you 20210402 start 给再申请得备品编码赋值 原编码_1...
 
            if (Trigger.isInsert) {
                Map<string, string> old_raMap = new Map<string, string>();//原备品申请,新备品申请
 
                for (Rental_Apply__c ra: Trigger.New) {
                if(String.isNotBlank(ra.Zsq_Rental_Apply__c) ){
                    old_raMap.put(ra.Zsq_Rental_Apply__c,ra.id);
                }
 
                }
                
                // 查一下再申请出来得个数 用来拼接编码
                List<AggregateResult> oldRaList = [
                    select Rental_Apply__c
                    from Rental_Apply_Equipment_Set__c where Zsq_Rental_Apply__c In:old_raMap.keySet()
                    group by Rental_Apply__c];
                
                for (Rental_Apply__c ra: Trigger.New) {
                        if(old_raMap.containsKey(ra.Zsq_Rental_Apply__c)){
                            Integer num = oldRaList.size() + 1;
                            ra.Name+='_'+num;
                        }
                        
 
                    
                
                }
                
            }
            // SWAG-BZL695 you 20210402 end
                //ra.Name =ra.Name+'_'+ra.bpjcsqbh__c; 
            **/
        }
        // ----------------------------------------------------------------------
        // ここまで、承認プロセス用に、经理、总监を更新する
        // ----------------------------------------------------------------------
        if (Trigger.isAfter) {
            //*************************Create 20171027 SWAG-ASH66S 趙徳芳 Start***************************//
            if (Trigger.isInsert) {
                for (Rental_Apply__c local : Trigger.New) {
                    if(local.applyUser__c != local.Person_In_Charge__c){
                        try {
                            Rental_Apply__Share ras = new Rental_Apply__Share(
                                RowCause = 'ApplyUserShare__c',
                                ParentId = local.Id,
                                UserOrGroupId = local.applyUser__c,
                                AccessLevel = 'Read'
                            );
                            insert ras;    // FIXME why DML in loop? and why try catch in trigger?
                        } catch (Exception e) {
                            local.addError( e.getMessage());
                        }
                    }
                }
            }
 
            //2021-10-14 yjk 在trigger里触发修改qis是否有备品字段 start
 
 
            if (Trigger.isInsert && Trigger.isAfter) {
                List<Id> qisIdList = new List<Id>();
                for (Rental_Apply__c local : Trigger.New) {
                    if(local.QIS_number__c != null){
                        
                        qisIdList.add(local.QIS_number__c);
                    }
                }
                if(qisIdList != null && qisIdList.size() > 0){
                    List<QIS_Report__c> qisList = [select id,isLendRental__c from QIS_Report__c where id in :qisIdList];
                    for(QIS_Report__c qisReport : qisList){
                        qisReport.isLendRental__c = true;
                    }
                    update qisList;
                }
                
            }
 
 
            if (Trigger.isUpdate && Trigger.isAfter) {
                List<Id> qisIdList = new List<Id>();
                for (Rental_Apply__c local : Trigger.New) {
                    if(local.QIS_number__c != null && local.QIS_number__c != Trigger.oldMap.get(local.Id).QIS_number__c){
                        
                        qisIdList.add(local.QIS_number__c);
                    }
                }
                if(qisIdList != null && qisIdList.size() > 0){
                    List<QIS_Report__c> qisList = [select id,isLendRental__c from QIS_Report__c where id in :qisIdList];
                    for(QIS_Report__c qisReport : qisList){
                        qisReport.isLendRental__c = true;
                    }
                    update qisList;
                }
                
            }
            //2021-10-14 yjk 在trigger里触发修改qis是否有备品字段 end
 
 
        // SWAG-BZL695 you 20210331 start 将原来的 备品借出申请的 一览和一览明细 克隆一份至 新备品借出申请
            if (Trigger.isInsert) {
            Map<string, string> raMap = new Map<string, string>();//原备品申请,新备品申请
 
                for (Rental_Apply__c ra: Trigger.New) {
                if(String.isNotBlank(ra.Zsq_Rental_Apply__c) ){
                    raMap.put(ra.Zsq_Rental_Apply__c,ra.id);
                    //ra.addError(ra.requestNoJoinStr2__c+'===');
 
                }
 
                }
                if (raMap!= null && raMap.size() > 0) {
                    //查询原来备品申请下得一览
                    List<Rental_Apply_Equipment_Set__c> raesList = [SELECT Id, Name,Loaner_centre_mail_address__c,Received_Confirm__c,
                    IsOPD_Account__c,LendNum__c,Received_NG_ReAssign_Text__c,Received_ng_detail__c, Rental_Apply__r.RecordTypeId,Rental_Apply__c,Bollow_Date_Add_10_WD__c,Guzhangpaicha_Final_reply_day_Text__c,
                    Fixture_Set__c,Loaner_code_text__c,Cancel_Select__c,Cancel_Reason__c,Loaner_cancel_Remarks__c,Loaner_cancel_reason__c,Cancel_Mem__c,
                    requestNoJoinStr2__c,Cancel_Date__c,UniqueKey__c,IndexFromUniqueKey__c,DataMigration_Flag__c,Rental_Num__c,First_RAESD__c,zsqybpyl__c
                    FROM Rental_Apply_Equipment_Set__c
                    WHERE Rental_Apply__c In:raMap.keySet() and Substitute_flag__c <> true];
                    //分单的时候 数量(Sys)是原来 总配套的数量,再申请的时候需要重新按照 配套型号,在重新计算 数量(Sys) 
                    Map<String, List<Rental_Apply_Equipment_Set__c>> Fixture_Set_Map = new Map<String, List<Rental_Apply_Equipment_Set__c>>();
                    if(null!=raesList && raesList.size() > 0){
                        for (Rental_Apply_Equipment_Set__c raes:raesList){
                            if (!Fixture_Set_Map.containsKey(raes.Fixture_Set__c)) {
                                Fixture_Set_Map.put(raes.Fixture_Set__c, new List<Rental_Apply_Equipment_Set__c>());
                            }
                            Fixture_Set_Map.get(raes.Fixture_Set__c).add(raes);//原来的配套型号,一览
                        }
                    }
 
                    if (null!=raesList && raesList.size() > 0) {  
                        List<Rental_Apply_Equipment_Set__c> insertCloneList = new List<Rental_Apply_Equipment_Set__c>();
                        for(String fsm : Fixture_Set_Map.keySet()){
                            List<Rental_Apply_Equipment_Set__c> fsmlist = Fixture_Set_Map.get(fsm);
                            Integer i=0;
                            for (Rental_Apply_Equipment_Set__c raes:fsmlist){      
                                Rental_Apply_Equipment_Set__c raesClone = new Rental_Apply_Equipment_Set__c();
                                raesClone = raes.clone();
                                raesClone.Id = null;
                                raesClone.Cancel_Select__c = false;
                                raesClone.Cancel_Reason__c = null; 
                                raesClone.Loaner_cancel_Remarks__c = null; 
                                raesClone.Loaner_cancel_reason__c = null; //20210706 SFDC-C448KZ you
                                raesClone.Cancel_Mem__c = null; 
                                raesClone.Cancel_Date__c = null; 
                                raesClone.Rental_Num__c = fsmlist.size();
                                raesClone.IndexFromUniqueKey__c = i+1;
                                raesClone.Rental_Apply__c = raMap.get(raes.Rental_Apply__c);
                                raesClone.zsqybpyl__c = raes.id;  //旧得一览id ,用来比对原来得一览下面得一览明细
                                raesClone.Zsq_Rental_Apply__c = raes.Rental_Apply__c;//旧得备品
                                system.debug('一览==='+raesClone.UniqueKey__c+'==raesClone.IndexFromUniqueKey__c=='+raesClone.IndexFromUniqueKey__c);
                                insertCloneList.add(raesClone);
                                i++;
                            }
                        }
                        
                        if (insertCloneList.size() > 0) {
                            
                            System.debug('==一览==insertCloneList=='+insertCloneList);
                            //插入一览
                            insert insertCloneList;
                        }
 
                        if(null!=insertCloneList && insertCloneList.size()>0){
                            //旧得一览id,新得一览
                            Map<String, Rental_Apply_Equipment_Set__c> raesMap = new Map<String, Rental_Apply_Equipment_Set__c>();
                            //旧的一览明细id,新的一览
                            Map<String, Rental_Apply_Equipment_Set__c> oldylmxidMap = new Map<String, Rental_Apply_Equipment_Set__c>();
                            
                            for (Rental_Apply_Equipment_Set__c raes:insertCloneList){
                            raesMap.put(raes.zsqybpyl__c, raes);//旧得一览,
                            oldylmxidMap.put(raes.First_RAESD__c, raes);//旧的一览明细id,新的一览
                            }  
                            if (raesMap!= null && raesMap.size() > 0) {
                                //查询之前备品和一览下面得明细
                                List<Rental_Apply_Equipment_Set_Detail__c> RaesdList = [select id,Name,IndexFromUniqueKey_Text__c, Rental_Apply_Equipment_Set__c,Fixture_Name_text__c,Fixture_Set_Detail__c,Is_Body__c,Loaner_accsessary__c,Fixture_Model_No_text__c,
                                Inspection_result__c,Pre_Inspection_Comment__c,Inspection_result_NG__c, Inspection_staff__c,DeliverySlip__c,Shippment_loaner_time__c,Return_DeliverySlip__c,Asset_return_time__c,
                                FSD_Product_Status_Flag__c,Internal_asset_location_before__c,Salesdepartment_before__c,Equipment_Type_text__c,SalesProvince_before__c,Product_category_text__c,
                                DataMigration_Flag__c,UniqueKey__c,IndexFromUniqueKey__c,Rental_Num__c,RequestNoJoinStr2__c,Loaner_centre_mail_address__c,zsqybpylmx__c,FSD_Id__c
                                from Rental_Apply_Equipment_Set_Detail__c
                                where Rental_Apply_Equipment_Set__c in :raesMap.keySet() and Rental_Apply__c in:raMap.keySet() and Cancel_Reason__c <>'重新分配'];
                            
                                if (null!=RaesdList && RaesdList.size() > 0) {
                                    List<Rental_Apply_Equipment_Set_Detail__c> RaesdCloneList = new List<Rental_Apply_Equipment_Set_Detail__c>();
                                    for (Rental_Apply_Equipment_Set_Detail__c raesd:RaesdList){
                                        Rental_Apply_Equipment_Set_Detail__c raesdClone = new Rental_Apply_Equipment_Set_Detail__c();
                                        raesdClone = raesd.clone();
                                        raesdClone.Id = null;
                                        raesdClone.Cancel_Select__c = false;
                                        raesdClone.Cancel_Reason__c = null; 
                                        raesdClone.Loaner_cancel_Remarks__c = null; 
                                        raesdClone.Loaner_cancel_reason__c = null; //20210706 SFDC-C448KZ you
                                        raesdClone.DataMigration_Flag__c = false;
                                        raesdClone.Cancel_Mem__c = null; 
                                        raesdClone.Cancel_Date__c = null; 
                                        raesdClone.Rental_Apply__c = raesMap.get(raesd.Rental_Apply_Equipment_Set__c).Rental_Apply__c;
                                        raesdClone.Rental_Apply_Equipment_Set__c = raesMap.get(raesd.Rental_Apply_Equipment_Set__c).id;
                                        raesdClone.zsqybpylmx__c  = raesd.id;//暂时存放旧的一览明细 raesMap.get(raesd.Rental_Apply_Equipment_Set__c).requestNoJoinStr2__c
                                        
                                        RaesdCloneList.add(raesdClone);
 
                                    }
                                    if (RaesdCloneList.size() > 0) {
                                        //插入旧的一览明细
                                        System.debug('==RaesdCloneList=='+RaesdCloneList);
                                        insert RaesdCloneList;
                                    }
                                    if(null!=RaesdCloneList && RaesdCloneList.size()>0){
                                        //旧得一览明细id,新的一览明细
                                        Map<String, Rental_Apply_Equipment_Set_Detail__c> raesdMap = new Map<String, Rental_Apply_Equipment_Set_Detail__c>();
                                        for (Rental_Apply_Equipment_Set_Detail__c reasd:RaesdCloneList){
                                        raesdMap.put(reasd.zsqybpylmx__c, reasd);
                                        } 
                                        List<Rental_Apply_Equipment_Set__c> raesdList1 = New List<Rental_Apply_Equipment_Set__c>();
                                        for(String  oldylmxid : oldylmxidMap.keySet()){
                                            Rental_Apply_Equipment_Set__c raesobj = new Rental_Apply_Equipment_Set__c();
                                            raesobj = oldylmxidMap.get(oldylmxid);
                                            if(raesdMap.containsKey(oldylmxid)){
                                            raesobj.First_RAESD__c=raesdMap.get(oldylmxid).id; 
                                            }
                                            raesdList1.add(raesobj);
                                        }
                                        if (raesdList1.size() > 0) {
                                            //更新一览上面得   第一条备品配套明细 
                                            update raesdList1;
                                        }
 
 
                                    } 
 
                                }
                                
                            }  
                        }
 
                }
                }
            }    
        
        // SWAG-BZL695 you 20210331 end
            //                     LD 20200819 因备品出借可视化管理注释以下代码块 Start
            //*************************Create 20171027 SWAG-ASH66S 趙徳芳 End***************************//
            //取到插入或者更新备品借出申请的修理
            // if (trigger.isInsert || trigger.isUpdate){
            //     for (Rental_Apply__c local : Trigger.New) {
            //         if (local.Repair__c!=null)
            //         repairSet.add(local.Repair__c);
            //     }
            // }
            // if (trigger.isUpdate){
            //     for (Rental_Apply__c local : Trigger.old) {
            //         if (local.Repair__c!=null)
            //         repairSet.add(local.Repair__c);
            //     }
            // }
            // //取到删除备品借出申请的修理
            // if (trigger.isDelete){
            //     for (Rental_Apply__c local : Trigger.old) {
            //         if (local.Repair__c!=null)
            //         repairSet.add(local.Repair__c);
            //     }
            // }
 
            // if (repairSet.size()>0){
            //     List<Repair__c> uprepair = [SELECT Id FROM Repair__c WHERE  Id in :repairSet AND Status1__c != '0.删除'];
            //     AggregateResult[] results = [SELECT Repair__c,count(Id) cnt 
            //                                  FROM Rental_Apply__c 
            //                                  //WHERE Repair__c in :repairSet
            //                                  WHERE Repair__c in (SELECT Id FROM Repair__c WHERE  Id in :repairSet AND Status1__c != '0.删除')
            //                                  AND Status__c != '草案中' 
            //                                  AND Status__c != '取消'
            //                                  group by Repair__c];
 
            //     List<Repair__c> repairUplist = new List<Repair__c>();
            //     for (Repair__c strRepair:uprepair){
            //         Integer cnt =0;
            //         for(AggregateResult ar: results){
            //              if(String.valueOf(ar.get('Repair__c')) == strRepair.Id){
            //                 cnt = Integer.valueOf(ar.get('cnt'));
            //                 break;
            //              }
            //         }
            //         Repair__c repair = new Repair__c();
            //         repair.Id = strRepair.Id;
            //         repair.Effective_RentalApply_No__c = cnt;
            //         repairUplist.add(repair);
            //     }
                
            //     if (repairUplist.size() > 0){
            //        update repairUplist;
            //     }
            // }
        }
    }
}