高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
public with sharing class MaintenanceProductDataController {
    public static final String NONE = system.label.StartTrading_None;
    public Maintenance_Product_Data__c mpd {get; set;}
    //搜索相关
    public String category1 { get; set; }//Family
    public String category2 { get; set; }//Category2__c
    public String category3 { get; set; }//Category3__c
    public String category4 { get; set; }//Category4__c
    public String category5 { get; set; }//Category5__c
    public String searchCateName { get; set; }
    public String searchProductCodeExt { get; set; }
    public String searchGuranteeType { get; set; }
 
    public String id { get; set; }
    public String mpddId { get; set; }
    public String RecordType {get;set;}
    public String RecordTypeName {get;set;}
 
    //public static Integer FIELDMAX = 200;
    public List<String> tempImportantRroduct{ get; private set; }
 
    //权限
    public  String GuranteeMainProductService {get;set;}
 
    public String  profileName{ get; set; }
    /*****************画面初始化用********************************/
    /******20160313_add**************/
    public Boolean editAble {get;set;}
    public String statusEdit {get;set;}
    public Boolean editDisabled {get;set;}
    //----------
    // 字段标签
    public List<String> title { get; private set; }
    //字段名
    public List<String> column;
    public List<List<String>> columns { get; private set; }
    public List<String> column_Old;
    public List<List<String>> columns_Old { get; private set; }
    //----
    List<String> dataselectedId = new List<String>();
    Map<String, String> dataselectedId_s = new Map<String, String>();
    
    //分页功能
    //public List<SelectOption> paginationSizeOptions{get;set;}
    public Integer size{get;set;}
    public Integer pageLimit{get;set;}
    public Integer noOfRecords{get; set;}
    public ApexPages.StandardSetController con { get; set; }
    public String soql {get;set;}
    //---
    
     /*****************画面表示Bean******************/
    //private List<MaintenanceProductDataDetailsInfo> mpdDerdetailsRecords = new List<MaintenanceProductDataDetailsInfo>();
    public  List<MaintenanceProductDataDetailsInfo> mpdDetailsRecordsView { get; set; }//页面的明细
  
    public List<MpdDetailsInfo>  mpdDetailsRecords { get; set; }//页面已经选择的明细20200622
 
    public Integer mpdDetailsRecordsCount { 
        get {
            return mpdDetailsRecords == null ? 0 : mpdDetailsRecords.size();
        }
    }
    public Integer mpdDetailsRecordsViewCount { 
        get {
            return mpdDetailsRecordsView == null ? 0 : mpdDetailsRecordsView.size();
        }
    }
    //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210908 Star
    public List<Maintenance_Product_Data_Details__c>  RepairInfo { 
        get {
            return  [select id,Service_Category6__c,Service_Category7__c FROM Maintenance_Product_Data_Details__c  where Default_Fixture_Arrival_Product__c = ''  LIMIT 1 ];
        }
    }
    //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210908 End
    public List<Maintenance_Product_Data_Details__c>  EquipmentInfo { 
        get {
            return  [select id,Default_Fixture_Arrival_Product__c FROM Maintenance_Product_Data_Details__c  where Default_Fixture_Arrival_Product__c = ''  LIMIT 1 ];
        }
    }
    //-----
    //附件
    public List<MaintenanceProductDataDetailsInfo> attachmentRecoeds { get; set; }
    public Boolean editDelCommitBtnDisabled {get; private set;}
    public Boolean saveBtnDisabled { get; private set; }
    //public Boolean sorderBtnDisabled { get; private set; }
    private List<Attachment> attachmentinfo = new List<Attachment>();
    //-----------
 
    private void initStandardController(){
        con = new ApexPages.StandardSetController(Database.getQueryLocator( soql ) );
        con.setPageSize(size);
        noOfRecords = con.getResultSize();
    }
 
    public List<Product2> product2s() {
         return (List<Product2>) con.getRecords();
    }
    public PageReference refreshPageSize() {
        con.setPageSize(size);
        getPageInfo();
        return null;
    }
    
    public MaintenanceProductDataController(ApexPages.StandardController controller) {
        mpdDetailsRecordsView = new List<MaintenanceProductDataDetailsInfo>();
        attachmentRecoeds = new List<MaintenanceProductDataDetailsInfo>();
        mpdDetailsRecords = new List<MpdDetailsInfo>();
        id = ApexPages.currentPage().getParameters().get('id');
        statusEdit = ApexPages.currentPage().getParameters().get('KeyWords');
        category1 = ApexPages.currentPage().getParameters().get('category1');
        category2 = ApexPages.currentPage().getParameters().get('category2');
        category3 = ApexPages.currentPage().getParameters().get('category3');
        category4 = ApexPages.currentPage().getParameters().get('category4');
        category5 = ApexPages.currentPage().getParameters().get('category5');
        searchCateName = ApexPages.currentPage().getParameters().get('searchCateName');
        searchProductCodeExt = ApexPages.currentPage().getParameters().get('searchProductCodeExt');
        searchGuranteeType = ApexPages.currentPage().getParameters().get('searchGuranteeType');
        RecordType = ApexPages.currentPage().getParameters().get('RecordType');
        editAble = false;//不可编辑
        editDisabled = true;
        RecordTypeName = ApexPages.currentPage().getParameters().get('RecordTypeName');//20200509 add
        size = Integer.valueOf(System.Label.mpddetLimitsize);//系统标签
        pageLimit = Integer.valueOf(System.Label.mpddetPageLimitsize);//系统标签
        //Integer.valueOf(System.Label.orderdetPageLimitsize)
    }
    private List<MaintenanceProductDataDetailsInfo> getPageInfo(){
        Map<String, String> selectedIdMap = new Map<String, String>();
        //取出选择的产品
        for (MpdDetailsInfo ass : mpdDetailsRecords)  {
            if(ass.check == true){
                selectedIdMap.put(ass.mpded.id,ass.mpded.id);
            }
        }        
        Integer addNo = 0;
        for(Integer i=0; i<mpdDetailsRecordsView.size();i++){
            if(selectedIdMap.containsKey(mpdDetailsRecordsView[i].mpdrdd.Id)){
                addNo++;
            }
            if(addNo >= pageLimit){
                break;
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,  '页面所选数据不能超过200条!'));  
            }
            //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,  '===mpdDetailList_status==='+mpdDetailList_status[0])); 
            //if(mpdDetailsRecordsView[i].check != true)
            if(dataselectedId_s.containsKey(mpdDetailsRecordsView[i].Prod.Id)){
               mpdDetailsRecordsView[i].canSelect = false; 
               mpdDetailsRecordsView[i].check = false; 
            }
            //else{
                if(RecordTypeName.equals('ImportantProduct')){
                    mpdDetailsRecordsView[i].mpdrdd.ENG_DeaerProFlag__c = mpdDetailsRecordsView[i].Prod.ENG_DeaerProFlag__c;
                    mpdDetailsRecordsView[i].mpdrdd.Dealer_special_Object__c = mpdDetailsRecordsView[i].Prod.Dealer_special_Object__c;
                    mpdDetailsRecordsView[i].mpdrdd.Key_product_147P__c = mpdDetailsRecordsView[i].Prod.Key_product_147P__c;
                    mpdDetailsRecordsView[i].mpdrdd.Important_Endopartner_product__c = mpdDetailsRecordsView[i].Prod.Important_Endopartner_product__c;
                    //mpdDetailsRecordsView[i].mpdrdd.Tier1_Category__c = mpdDetailsRecordsView[i].Prod.Tier1_Category__c;
                }
                if(RecordTypeName.equals('GuranteeMain')){
                    Integer Entend_gurantee_period_all =  mpdDetailsRecordsView[i].Prod.Entend_gurantee_period_all__c.intValue();
                    if(Entend_gurantee_period_all==2){mpdDetailsRecordsView[i].mpdrdd.Entend_gurantee_period__c='两年';}
                    if(Entend_gurantee_period_all==3){mpdDetailsRecordsView[i].mpdrdd.Entend_gurantee_period__c='三年';}
                    if(Entend_gurantee_period_all==4){mpdDetailsRecordsView[i].mpdrdd.Entend_gurantee_period__c='四年';}
                    if(Entend_gurantee_period_all==5){mpdDetailsRecordsView[i].mpdrdd.Entend_gurantee_period__c='五年';}
                    mpdDetailsRecordsView[i].mpdrdd.Gurantee_Start_Date__c = mpdDetailsRecordsView[i].Prod.Gurantee_Start_Date_F__c;
                    mpdDetailsRecordsView[i].mpdrdd.Gurantee_End_Date__c = mpdDetailsRecordsView[i].Prod.Gurantee_End_Date_F__c;
                    mpdDetailsRecordsView[i].mpdrdd.GuranteeType__c = mpdDetailsRecordsView[i].Prod.GuranteeType__c;
                    mpdDetailsRecordsView[i].mpdrdd.CanNotCancelledGurantee__c = mpdDetailsRecordsView[i].Prod.CanNotCancelledGurantee__c;//20201012 ljh  CHAN-BTR67N add
                    /*
                    mpdDetailsRecordsView[i].mpdrdd.Maintenance_Price_Year__c = mpdDetailsRecordsView[i].Prod.Maintenance_Price_Year__c;
                    //另两个价格
                    mpdDetailsRecordsView[i].mpdrdd.Intra_Trade_Service_RMB__c = mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB__c;
                    mpdDetailsRecordsView[i].mpdrdd.Intra_Trade_Gurantee_RMB__c = mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB__c;
                    */
                    mpdDetailsRecordsView[i].mpdrdd.Maintenance_Price_Year__c = null;
                    //另两个价格
                    mpdDetailsRecordsView[i].mpdrdd.Intra_Trade_Service_RMB__c = null;
                    //2021/01/19 liying start
                    mpdDetailsRecordsView[i].mpdrdd.Virtual_Contract__c = null;
                     //2021/01/19 liying end
                    mpdDetailsRecordsView[i].mpdrdd.Intra_Trade_Gurantee_RMB__c = null;
                    //日期
                    //Intra_Trade_Service_RMB_DateFrom__c
                    if( 
                        ( 
                        (mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date1__c <= Date.today() &&mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date2__c <= Date.today()&&mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date1__c >= mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date2__c)
                        ||(mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date1__c <= Date.today()&&(mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date2__c > Date.today()||mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date2__c==null)) 
                        )&&Date.today() <= mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_EndDate1__c
                    ){
                        //取时间1    
                        mpdDetailsRecordsView[i].mpdrdd.Intra_Trade_Service_RMB_DateFrom__c =   mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date1__c;             
                    }
                    if( 
                        ( 
                        (mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date2__c <= Date.today() &&mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date1__c <= Date.today()&&mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date2__c >= mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date1__c)
                        ||(mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date2__c <= Date.today()&&(mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date1__c > Date.today()||mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date1__c==null)) 
                        )&&Date.today() <= mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_EndDate2__c
                    ){
                        //取时间2    
                        mpdDetailsRecordsView[i].mpdrdd.Intra_Trade_Service_RMB_DateFrom__c =   mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date2__c;             
                    }
                    //Intra_Trade_Gurantee_RMB_DateFrom__c
                    if( 
                        ( 
                        (mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date1__c <= Date.today() &&mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date2__c <= Date.today()&&mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date1__c >= mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date2__c)
                        ||(mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date1__c <= Date.today()&&(mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date2__c > Date.today()||mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date2__c==null)) 
                        )&&Date.today() <= mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_End_Date1__c
                    ){
                        //取时间1    
                        mpdDetailsRecordsView[i].mpdrdd.Intra_Trade_Gurantee_RMB_DateFrom__c =   mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date1__c;             
                    }
                    if( 
                        ( 
                        (mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date2__c <= Date.today() &&mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date1__c <= Date.today()&&mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date2__c >= mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date1__c)
                        ||(mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date2__c <= Date.today()&&(mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date1__c > Date.today()||mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date1__c==null)) 
                        )&&Date.today() <= mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_End_Date2__c
                    ){
                        //取时间2    
                        mpdDetailsRecordsView[i].mpdrdd.Intra_Trade_Gurantee_RMB_DateFrom__c =   mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date2__c;             
                    }
                                        
                }
                if(RecordTypeName.equals('GuranteePrice')){
                    mpdDetailsRecordsView[i].mpdrdd.Maintenance_Price_Year__c = mpdDetailsRecordsView[i].Prod.Maintenance_Price_Year__c;
                    //另两个价格
                    mpdDetailsRecordsView[i].mpdrdd.Intra_Trade_Service_RMB__c = mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB__c;
                    //2021/01/19 liying start
                    mpdDetailsRecordsView[i].mpdrdd.Virtual_Contract__c = mpdDetailsRecordsView[i].Prod.Virtual_Contract__c;
                    //2021/01/19 liying end
                    mpdDetailsRecordsView[i].mpdrdd.Intra_Trade_Gurantee_RMB__c = mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB__c;
                    //日期
                    //Intra_Trade_Service_RMB_DateFrom__c
                    if( 
                        ( 
                        (mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date1__c <= Date.today() &&mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date2__c <= Date.today()&&mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date1__c >= mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date2__c)
                        ||(mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date1__c <= Date.today()&&(mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date2__c > Date.today()||mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date2__c==null)) 
                        )&&Date.today() <= mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_EndDate1__c
                    ){
                        //取时间1    
                        mpdDetailsRecordsView[i].mpdrdd.Intra_Trade_Service_RMB_DateFrom__c =   mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date1__c;             
                    }
                    if( 
                        ( 
                        (mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date2__c <= Date.today() &&mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date1__c <= Date.today()&&mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date2__c >= mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date1__c)
                        ||(mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date2__c <= Date.today()&&(mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date1__c > Date.today()||mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date1__c==null)) 
                        )&&Date.today() <= mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_EndDate2__c
                    ){
                        //取时间2    
                        mpdDetailsRecordsView[i].mpdrdd.Intra_Trade_Service_RMB_DateFrom__c =   mpdDetailsRecordsView[i].Prod.Intra_Trade_Service_RMB_Date2__c;             
                    }
                    //Intra_Trade_Gurantee_RMB_DateFrom__c
                    if( 
                        ( 
                        (mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date1__c <= Date.today() &&mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date2__c <= Date.today()&&mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date1__c >= mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date2__c)
                        ||(mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date1__c <= Date.today()&&(mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date2__c > Date.today()||mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date2__c==null)) 
                        )&&Date.today() <= mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_End_Date1__c
                    ){
                        //取时间1    
                        mpdDetailsRecordsView[i].mpdrdd.Intra_Trade_Gurantee_RMB_DateFrom__c =   mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date1__c;             
                    }
                    if( 
                        ( 
                        (mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date2__c <= Date.today() &&mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date1__c <= Date.today()&&mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date2__c >= mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date1__c)
                        ||(mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date2__c <= Date.today()&&(mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date1__c > Date.today()||mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date1__c==null)) 
                        )&&Date.today() <= mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_End_Date2__c
                    ){
                        //取时间2    
                        mpdDetailsRecordsView[i].mpdrdd.Intra_Trade_Gurantee_RMB_DateFrom__c =   mpdDetailsRecordsView[i].Prod.Intra_Trade_Gurantee_RMB_Date2__c;             
                    }
                }
                if(RecordTypeName.equals('RepairInfo')){
                    mpdDetailsRecordsView[i].mpdrdd.RepairListPriceLevelA__c = mpdDetailsRecordsView[i].Prod.RepairListPriceLevelA__c;
                    mpdDetailsRecordsView[i].mpdrdd.RepairListPriceLevelB__c = mpdDetailsRecordsView[i].Prod.RepairListPriceLevelB__c;
                    mpdDetailsRecordsView[i].mpdrdd.RepairListPriceLevelC__c = mpdDetailsRecordsView[i].Prod.RepairListPriceLevelC__c;
                    mpdDetailsRecordsView[i].mpdrdd.CanRepairAccsessary__c = mpdDetailsRecordsView[i].Prod.CanRepairAccsessary__c;
                    mpdDetailsRecordsView[i].mpdrdd.Repair_Product_Code__c = mpdDetailsRecordsView[i].Prod.Repair_Product_Code__c;
                    mpdDetailsRecordsView[i].mpdrdd.PartSupplyFinishDate__c = mpdDetailsRecordsView[i].Prod.PartSupplyFinishDate__c;
                    mpdDetailsRecordsView[i].mpdrdd.Maintenance_Price_Year__c = mpdDetailsRecordsView[i].Prod.Maintenance_Price_Year__c;
                    //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210831 Star
                    mpdDetailsRecordsView[i].mpdrdd.Can_Repair__c = mpdDetailsRecordsView[i].Prod.Can_Repair__c;
                    mpdDetailsRecordsView[i].mpdrdd.Service_Category1__c = mpdDetailsRecordsView[i].Prod.Service_Category1__c;
                    mpdDetailsRecordsView[i].mpdrdd.Service_Category2__c = mpdDetailsRecordsView[i].Prod.Service_Category2__c;
                    mpdDetailsRecordsView[i].mpdrdd.Service_Category3__c = mpdDetailsRecordsView[i].Prod.Service_Category3__c;
                    mpdDetailsRecordsView[i].mpdrdd.Service_Category4__c = mpdDetailsRecordsView[i].Prod.Service_Category4__c;
                    mpdDetailsRecordsView[i].mpdrdd.Service_Category5__c = mpdDetailsRecordsView[i].Prod.Service_Category5__c;
                    mpdDetailsRecordsView[i].mpdrdd.Service_Category6__c = mpdDetailsRecordsView[i].Prod.Service_Category6__c;
                    mpdDetailsRecordsView[i].mpdrdd.Service_Category7__c = mpdDetailsRecordsView[i].Prod.Service_Category7__c;
                    //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210831 End
                }
                if(RecordTypeName.equals('EquipmentInfo')){
                    mpdDetailsRecordsView[i].mpdrdd.Asset_loaner_category__c = mpdDetailsRecordsView[i].Prod.Asset_loaner_category__c;
                    mpdDetailsRecordsView[i].mpdrdd.Special_Model__c = mpdDetailsRecordsView[i].Prod.Special_Model__c;
                    mpdDetailsRecordsView[i].mpdrdd.Fixture_Model_No__c = mpdDetailsRecordsView[i].Prod.Fixture_Model_No__c;
                    mpdDetailsRecordsView[i].mpdrdd.Packing_list_Fixture__c = mpdDetailsRecordsView[i].Prod.Packing_list_Fixture__c;
                    mpdDetailsRecordsView[i].mpdrdd.Default_Fixture_Arrival_Process__c = mpdDetailsRecordsView[i].Prod.Default_Fixture_Arrival_Process__c;
                    mpdDetailsRecordsView[i].mpdrdd.Default_Fixture_Arrival_Product__c = mpdDetailsRecordsView[i].Prod.Default_Fixture_Arrival_Product__c; 
                }
            //}  
        }
        return mpdDetailsRecordsView;
    }
    public void init() {
        //权限 start
        profileName = '';
        String  profileId = UserInfo.getProfileId();//18位
        if(profileId.equals(System.Label.ProfileId_EquCenAdminPic)){
            profileName = '2B3';
        }
        if(profileId.equals('00e10000000xno4AAA')){
            profileName = '2F3';//服务
        }
        if(profileId.equals('00e10000000xyViAAI')){
            profileName = '2M5';//产品
        }
        if (profileId.equals('00e10000000xno9AAA')) {
            profileName = '2F4';//技术推进部
        }
        if(String.isBlank(id)&&String.isBlank(RecordTypeName)){
            if(String.isBlank(RecordType)){
                if(profileName.equals('2B3')){
                   RecordTypeName =  'EquipmentInfo';
                } else if (profileName.equals('2F4')) {
                    RecordTypeName =  'RepairInfo';
                }
            }else{
                //RecordTypeId获取对应的RecordTypeName
                List<RecordType> rt_Map = [SELECT Id, Name, DeveloperName, isActive FROM RecordType  WHERE id =:RecordType AND SobjectType = 'Maintenance_Product_Data__c'];
                RecordTypeName = rt_Map[0].DeveloperName;
            } 
        }else if(!String.isBlank(id)){
            mpd =  [SELECT Id,Status__c,Name,RecordTypeId, RecordType.Name,RecordType.DeveloperName, CreatedDate, CreatedById
                        FROM Maintenance_Product_Data__c where Id = :id];
            RecordTypeName = mpd.RecordType.DeveloperName;
        }
        //mpdDerdetailsRecords = new List<MaintenanceProductDataDetailsInfo>();
        mpdDetailsRecordsView = new List<MaintenanceProductDataDetailsInfo>();
        List<Product2> product2Selected = new List<Product2>();
        //查找
        List<Maintenance_Product_Data_Details__c> mpdDetailList = new List<Maintenance_Product_Data_Details__c>();
        Map<String,MaintenanceProductDataDetailsInfo> midMaprecord = new Map<String,MaintenanceProductDataDetailsInfo>();   
        // 获得维护主数据一览
        Map<String, Schema.FieldSet> fsMap = Schema.getGlobalDescribe().get('Maintenance_Product_Data_Details__c').getDescribe().fieldSets.getMap();
        // 20211103 ljh WLIG-BXE443 add start
        String fsTemp = RecordTypeName;
        String fsTempOld = RecordTypeName;
        /*if(RecordTypeName.equals('GuranteeMain')&&profileName.equals('2F3')){
            fsTemp += 'YY';
            fsTempOld += 'YY';
        }*/
        if(RecordTypeName.equals('GuranteeMain')&&profileName.equals('2M5')){
            fsTemp += 'FW';
            fsTempOld += 'FW';
        }
        fsTempOld += '_Old';
        // 20211103 ljh WLIG-BXE443 add end
        // 20211103 ljh WLIG-BXE443 update start
        // Schema.FieldSet fs = fsMap.get(RecordTypeName);
        Schema.FieldSet fs = fsMap.get(fsTemp);
        // 20211103 ljh WLIG-BXE443 update end
        // 获得维护主数据中的所有项目
        List<FieldSetMember> fsmList = fs.getFields();
        // 获得字段标签和字段名
        title = new List<String>();
        List<String> titleService = new List<String>();
        column = new List<String>();
        columns = new List<List<String>>();
        //List<List<String>> columnsService = new List<List<String>>();//20200628 add
        for (FieldSetMember fsm : fsmList) {
            title.add(fsm.getLabel());
            column.add(fsm.getFieldPath());
            columns.add(fsm.getFieldPath().split('\\.'));
        }
        // 20211103 ljh WLIG-BXE443 update start
        // Schema.FieldSet fs_Old = fsMap.get(RecordTypeName+'_Old');
        Schema.FieldSet fs_Old = fsMap.get(fsTempOld);
        // 20211103 ljh WLIG-BXE443 update end
        List<FieldSetMember> fsmList_Old = fs_Old.getFields();
        column_Old = new List<String>();
        columns_Old = new List<List<String>>();
        for (FieldSetMember fsm_Old : fsmList_Old) {
            column_Old.add(fsm_Old.getFieldPath());
            columns_Old.add(fsm_Old.getFieldPath().split('\\.'));
        }
        /*多年保主数据维护不同部门编辑权限 条件真是这些字段不可输入 start*/
        if(RecordTypeName.equals('GuranteeMain')&&profileName.equals('2F3')){
            GuranteeMainProductService = 'GuranteeType__c,Entend_gurantee_period__c,Gurantee_Start_Date__c,Gurantee_End_Date__c,CanNotCancelledGurantee__c';
        }else if(RecordTypeName.equals('GuranteeMain')&&profileName.equals('2M5')){
            GuranteeMainProductService = 'Virtual_Contract__c,Intra_Trade_Gurantee_RMB__c,Intra_Trade_Gurantee_RMB_DateFrom__c,Maintenance_Price_Year__c,Intra_Trade_Service_RMB_DateFrom__c,Intra_Trade_Service_RMB__c';
        }else{
            GuranteeMainProductService = ' ';//必须有值空有可以  所有可输入
        }
        saveBtnDisabled = false; 
        if(id!=null&&id!=''&&statusEdit==''&&statusEdit==null){
            editAble = false;
        }else if((id == null||id=='')&&(statusEdit==''||statusEdit==null)){
            editAble = true;
        }else if(id != null&&id!=''&&statusEdit!=''&&statusEdit!=null){
            editAble = true;
        }
        List<Maintenance_Product_Data_Details__c> mpdDetailList_status = new List<Maintenance_Product_Data_Details__c>();
            mpdDetailList_status= [select Id ,ProductsID__c,Name from Maintenance_Product_Data_Details__c  where Maintenance_Product_Data__r.Status__c !='完成'];
        for(Maintenance_Product_Data_Details__c ss : mpdDetailList_status ){
            dataselectedId_s.put(ss.ProductsID__c,ss.ProductsID__c);
        }
        if(String.isBlank(id)){
            EditDelCommitBtnDisabled = true;
        }else{
            List<Maintenance_Product_Data_Details__c> mpdDetailsSelected = new List<Maintenance_Product_Data_Details__c>();
            String mpdsoql = 'select Id ,Name,GuranteeTypeP__c,ProductCode_Ext__c ,ProductsID__c,Maintenance_Product_Data__c,ProductsID__r.Name  ';
            for (Integer i=0;i<column.size();i++) {
                mpdsoql += ',' + column[i];
            }
            for (Integer i=0;i<column_Old.size();i++) {
                mpdsoql += ',' + column_Old[i];
            }
            mpdsoql += ' from Maintenance_Product_Data_Details__c  where Maintenance_Product_Data__c = \''+id+'\'';
            // 選択済みの明细を取得
            mpdDetailsSelected = Database.query(mpdsoql);      
            for (Maintenance_Product_Data_Details__c mpdd1 : mpdDetailsSelected) {
                dataselectedId.add(mpdd1.ProductsID__c);
            }
            Map<String, MpdDetailsInfo> MidMap = new Map<String, MpdDetailsInfo>();
            for (Integer i = 0; i < mpdDetailsSelected.size(); i++) {
 
                MidMap.put(mpdDetailsSelected[i].Id, new MpdDetailsInfo(mpdDetailsSelected[i]));
            }
            for (MpdDetailsInfo mpded : MidMap.values()) {
 
                mpdDetailsRecords.add(mpded);
            }
            if(String.isNotBlank(searchCateName)||String.isNotBlank(searchProductCodeExt)
                ||String.isNotBlank(searchGuranteeType)
                ||String.isNotBlank(category1)||String.isNotBlank(category2)||String.isNotBlank(category3)||String.isNotBlank(category4)||String.isNotBlank(category5)
                ){
                searchMaintenanceProductDataDetails();
            }
            List<Maintenance_Product_Data__c> qs = New List<Maintenance_Product_Data__c>();
            qs = [SELECT Id,Name,RecordTypeId, RecordType.Name,RecordType.DeveloperName,isAttachment__c,
                     RepairListPriceLevelA_Sum__c,RepairListPriceLevelB_Sum__c,RepairListPriceLevelC_Sum__c,Intra_Trade_Service_RMB_Sum__c,
                     Status__c,ApplyPersion__c,applyDate__c,GuranteeType_Sum__c,Intra_Trade_Gurantee_RMB_Sum__c,BuchangApprovalRecommend__c,
                     ServiceWritePersion__c,Maintenance_Price_Year_Sum__c,CreatedDate, CreatedById
                    FROM Maintenance_Product_Data__c where Id = :id];
            if (qs.size()>0){
                mpd = qs[0];
            }
            if(mpd.Status__c.equals('已提交') || mpd.Status__c.equals('完成')){
                saveBtnDisabled = true;
                editDelCommitBtnDisabled = false;
            }
            //服务本部填写
            //UserInfo.getProfileId().equals(System.Label.ProfileId_SystemAdmin)
            if(profileName.equals('2F3')&&mpd.Status__c.equals('服务填写')){
            //if(profileId.equals(System.Label.ProfileId_SystemAdmin) ||profileName.equals('2F3')&&mpd.Status__c == '服务填写'){
                editDisabled = false;
            }
            //状态多年保主数据【审批中】价格相关字段显示0.00
            if((mpd.Status__c.equals('审批中')||mpd.Status__c.equals('草案中'))&&RecordTypeName.equals('GuranteeMain')){
                for(MpdDetailsInfo mpdPrice : mpdDetailsRecords){ 
                    mpdPrice.mpded.Maintenance_Price_Year__c = null;
                    //2021/01/19 liying start
                    mpdPrice.mpded.Virtual_Contract__c=null;
                    //2021/01/19 liying end
                    mpdPrice.mpded.Intra_Trade_Gurantee_RMB__c = null;
                    mpdPrice.mpded.Intra_Trade_Service_RMB__c = null;
                    mpdPrice.mpded.Maintenance_Price_Year_Old__c = null;
                    mpdPrice.mpded.Intra_Trade_Gurantee_RMB_Old__c = null ;
                    mpdPrice.mpded.Intra_Trade_Service_RMB_Old__c = null;
                }
            }
            //附件
            attachmentinfo =[SELECT Id, Name,OwnerId FROM Attachment WHERE  parentid =:id  ];
            if(attachmentinfo.size()>0){
                for (Integer i = 0; i < attachmentinfo.size(); i++) {
                    attachmentRecoeds.add(new MaintenanceProductDataDetailsInfo(attachmentinfo[i]));
                }
            } 
            //是否上传附件
            if(AttachmentRecoeds.size()>0&&mpd.isAttachment__c != true){
                mpd.isAttachment__c = true;
                update mpd;
            }
            if(!editAble&&((mpd.Status__c.equals('服务填写')&&RecordTypeName.equals('GuranteeMain'))||!RecordTypeName.equals('GuranteeMain'))){
               Integer tempRepairInfo = 0,tempGuranteeMainPrice = 0;
                tempRepairInfo  = mpd.RepairListPriceLevelA_Sum__c.intValue()+mpd.RepairListPriceLevelB_Sum__c.intValue()+mpd.RepairListPriceLevelC_Sum__c.intValue()+mpd.Maintenance_Price_Year_Sum__c.intValue();
                tempGuranteeMainPrice =  mpd.Intra_Trade_Service_RMB_Sum__c.intValue()+mpd.Intra_Trade_Gurantee_RMB_Sum__c.intValue()+mpd.Maintenance_Price_Year_Sum__c.intValue(); 
                //检查应该有附件的是否有附件
                if(RecordTypeName.equals('GuranteePrice') && tempGuranteeMainPrice > 0&&AttachmentRecoeds.size()==0){
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,  '还没有上传附件,请上传附件!'));
                }else if(RecordTypeName.equals('RepairInfo') && tempRepairInfo > 0 && AttachmentRecoeds.size()==0){
                    //RepairInfo
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,  '还没有上传附件,请上传附件!'));
                }else if(RecordTypeName.equals('GuranteeMain')&& tempGuranteeMainPrice > 0 &&AttachmentRecoeds.size()==0){
                    //GuranteeMain
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,  '还没有上传附件,请上传附件!'));
                } 
            }
            //System.debug('============02:'+mpdDetailsRecords);    
        }    
    }
    // 检索
    public void searchMaintenanceProductDataDetails() {
        Map<String, String> selectedIdMap = new Map<String, String>();
        Map<String,MaintenanceProductDataDetailsInfo> midMaprecord = new Map<String,MaintenanceProductDataDetailsInfo>();
        mpdDetailsRecordsView = new List<MaintenanceProductDataDetailsInfo>();
        //检索所有产品
        List<Product2> product2Selected = new List<Product2>();
        soql = this.makeSoql(category1,category2,category3,category4,category5,searchCateName,searchProductCodeExt,searchGuranteeType);
        //size = 20;//系统自定义标签
        //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,  '=====soql:'+soql));
        size =Integer.valueOf(System.Label.mpddetLimitsize);
        initStandardController();
        product2Selected = Database.query(soql);    
        for (Integer i = 0; i < product2Selected.size(); i++) {
            midMaprecord.put(product2Selected[i].Id, new MaintenanceProductDataDetailsInfo(product2Selected[i]));
        }
        //再把map里的值从新赋给ConsumableorderdetailsRecords
        for(MaintenanceProductDataDetailsInfo bss : midMaprecord.values()){
            if(selectedIdMap.containsKey(bss.Prod.id)){
                continue;
            }else{
                mpdDetailsRecordsView.add(bss);
            }
        }
        //都显示product2信息   已经加入/在进程中的不可选
        getPageInfo();
        // 显示数据条数信息
        makeMessage();
    }
 
    public PageReference save() {
        Integer FLG = 0;
        Integer Count = 0;
        for(MaintenanceProductDataDetailsInfo CheckCount : mpdDetailsRecordsView) {
            FLG = FLG + 1;
            if(CheckCount.check == false){
                Count = Count + 1;
            }
        }
        if(Count == FLG){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '请选择所维护产品')); 
        }
        Maintenance_Product_Data__c P = new Maintenance_Product_Data__c(); 
        List<Maintenance_Product_Data__c> mpd_Name = new List<Maintenance_Product_Data__c>();
        List<Maintenance_Product_Data_Details__c> Ins = New List<Maintenance_Product_Data_Details__c>();
        //新建维护时
        Savepoint sp = Database.setSavepoint();
        try{
            Integer i = 1;
            Integer Roll = 0;
            if(String.isBlank(id)){
                //新增的时候
                p.Status__c = '草案中';
                p.RecordTypeid = Schema.SObjectType.Maintenance_Product_Data__c.getRecordTypeInfosByDeveloperName().get(RecordTypeName).getRecordTypeId();
                try{
                    insert p;
                } catch (Exception e) {
                    ApexPages.addMessages(e);
                }
                mpd_Name = [SELECT id, Name FROM Maintenance_Product_Data__c WHERE id =:p.id];
                id = p.id;
            }else{
                //更新
                mpd_Name = [SELECT id,Name FROM Maintenance_Product_Data__c WHERE id =:id];
            }
            List<Maintenance_Product_Data_Details__c> mpdd_Name = [SELECT id,Name FROM Maintenance_Product_Data_Details__c where Maintenance_Product_Data__c =:id order by Name desc Limit 1 ];
            if(mpdd_Name.size()>0){
                String[] nameI= mpdd_Name[0].Name.split('-');
                i = Integer.valueOf(nameI[1])+1;
            }
            List<String> GuranteeType = new List<String>();//20200618 类型一致不一致不能保存 add
            Boolean CanNotCancelledGurantee = false;//20201015 CHAN-BTR67N add start
            for (MaintenanceProductDataDetailsInfo ass : mpdDetailsRecordsView)  {
                dataselectedId.add(ass.Prod.id);
                //20200618 类型一致不一致不能保存 add start
                if(ass.check == true){
                    switch on RecordTypeName {
                        when 'GuranteeMain'{
                            if(ass.mpdrdd.GuranteeType__c !=null&&!GuranteeType.contains(ass.mpdrdd.GuranteeType__c)){
                                GuranteeType.add(ass.mpdrdd.GuranteeType__c);
                            }
                            if(ass.mpdrdd.CanNotCancelledGurantee__c){
                                CanNotCancelledGurantee = true;
                            }                            
                        }
                    }
                }
                //20200618 类型一致不一致不能保存 add  END
            }
                             
            if(GuranteeType.size() > 1){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '保存失败,所有多年保修类型选择保持一致!'));
                return null;
            }else if(((GuranteeType.size()==1&&!GuranteeType[0].equals('市场'))||GuranteeType.size()==0)&&CanNotCancelledGurantee){
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '多年保类型是市场的时候,才可以勾选不可取消多年保修!'));
                    return null;
            }else{
                
                //20200618 类型一致不一致不能保存 add start
                Boolean isWarning = false;
                String  contentWarning = '';
                for (MaintenanceProductDataDetailsInfo ass : mpdDetailsRecordsView)  {
                    Roll = Roll+1;
                    if(ass.check == true){
                        if(Roll==(FLG-Count)){
                        }
                        Maintenance_Product_Data_Details__c InsAfterDel = new Maintenance_Product_Data_Details__c();
                        String str = string.valueOf(i);
                        if(str.length() == 1){
                        str = '0' + str;
                        }
                        InsAfterDel.Maintenance_Product_Data__c = mpd_Name[0].id;
                        InsAfterDel.Name = mpd_Name[0].Name + '-'+ str;
                        InsAfterDel.ProductsID__c = ass.Prod.id;
                        switch on RecordTypeName {
                            when 'ImportantProduct'{
                                //InsAfterDel.RecordTypeName__c = 'ImportantProduct';
                                InsAfterDel.RecordTypeId = Schema.SObjectType.Maintenance_Product_Data_Details__c.getRecordTypeInfosByDeveloperName().get('ImportantProduct').getRecordTypeId();
                                //InsAfterDel.Tier1_Category__c = ass.mpdrdd.Tier1_Category__c;
                                InsAfterDel.ENG_DeaerProFlag__c = ass.mpdrdd.ENG_DeaerProFlag__c;
                                InsAfterDel.Dealer_special_Object__c = ass.mpdrdd.Dealer_special_Object__c;
                                InsAfterDel.Key_product_147P__c = ass.mpdrdd.Key_product_147P__c;
                                InsAfterDel.Important_Endopartner_product__c = ass.mpdrdd.Important_Endopartner_product__c;
                                //old数据
                                //InsAfterDel.Tier1_Category_Old__c = ass.Prod.Tier1_Category__c;
                                InsAfterDel.ENG_DeaerProFlag_Old__c = ass.Prod.ENG_DeaerProFlag__c;
                                InsAfterDel.Dealer_special_Object_Old__c = ass.Prod.Dealer_special_Object__c;
                                InsAfterDel.Key_product_147P_Old__c = ass.Prod.Key_product_147P__c;
                                InsAfterDel.Important_Endopartner_product_Old__c = ass.Prod.Important_Endopartner_product__c;
                            }
                            when 'GuranteeMain'{
                                //InsAfterDel.RecordTypeName__c = 'GuranteeMain';
                                InsAfterDel.RecordTypeId = Schema.SObjectType.Maintenance_Product_Data_Details__c.getRecordTypeInfosByDeveloperName().get('GuranteeMain').getRecordTypeId();
                                InsAfterDel.Entend_gurantee_period__c=ass.mpdrdd.Entend_gurantee_period__c;
                                InsAfterDel.Gurantee_Start_Date__c=ass.mpdrdd.Gurantee_Start_Date__c;
                                InsAfterDel.GuranteeType__c=ass.mpdrdd.GuranteeType__c;
                                InsAfterDel.Gurantee_End_Date__c=ass.mpdrdd.Gurantee_End_Date__c;
                                InsAfterDel.CanNotCancelledGurantee__c=ass.mpdrdd.CanNotCancelledGurantee__c;//20201012 ljh  CHAN-BTR67N add
                                //多年保修类型[服务/市场]不同时老数据公式字段
                                Integer Entend_gurantee_period_all =  ass.Prod.Entend_gurantee_period_all__c.intValue();
                                InsAfterDel.Entend_gurantee_period_Old__c = null;
                                if(Entend_gurantee_period_all==2){InsAfterDel.Entend_gurantee_period_Old__c='两年';}
                                if(Entend_gurantee_period_all==3){InsAfterDel.Entend_gurantee_period_Old__c='三年';}
                                if(Entend_gurantee_period_all==4){InsAfterDel.Entend_gurantee_period_Old__c='四年';}
                                if(Entend_gurantee_period_all==5){InsAfterDel.Entend_gurantee_period_Old__c='五年';}
                                InsAfterDel.Gurantee_Start_Date_Old__c=ass.Prod.Gurantee_Start_Date_F__c; 
                                InsAfterDel.GuranteeType_Old__c=ass.Prod.GuranteeType__c;
                                InsAfterDel.Gurantee_End_Date_Old__c=ass.Prod.Gurantee_End_Date_F__c ; // 20211103 ljh WLIG-BXE443
                                InsAfterDel.CanNotCancelledGurantee_Old__c=ass.Prod.CanNotCancelledGurantee__c ;//20201012 ljh  CHAN-BTR67N add
                                //多年保类型相关有变化就清空价格
                                InsAfterDel.Maintenance_Price_Year__c = ass.Prod.Maintenance_Price_Year__c;
                                //2021/01/19 liying start
                                 InsAfterDel.Virtual_Contract__c =ass.Prod.Virtual_Contract__c;
                                //2021/01/19 liying end
                                InsAfterDel.Intra_Trade_Gurantee_RMB__c =ass.Prod.Intra_Trade_Gurantee_RMB__c ;
                                InsAfterDel.Intra_Trade_Service_RMB__c=ass.Prod.Intra_Trade_Service_RMB__c;
                                //维修合同报价
                                InsAfterDel.Maintenance_Price_Year_Old__c = ass.Prod.Maintenance_Price_Year__c;
                                //多年保修计提价格与NoDiscount价格
                                //2021/01/19 liying start
                                InsAfterDel.Virtual_Contract_Old__c =ass.Prod.Virtual_Contract__c ;
                                //2021/01/19 liying end
                                InsAfterDel.Intra_Trade_Gurantee_RMB_Old__c =ass.Prod.Intra_Trade_Gurantee_RMB__c ;
                                InsAfterDel.Intra_Trade_Service_RMB_Old__c=ass.Prod.Intra_Trade_Service_RMB__c;
                                //Intra_Trade_Service_RMB_DateFrom_Old__c
                                InsAfterDel.Intra_Trade_Service_RMB_DateFrom_Old__c  =   ass.Prod.Intra_Trade_Service_RMB_Date1__c; 
                                //Intra_Trade_Gurantee_RMB_DateFrom_Old__c
                                InsAfterDel.Intra_Trade_Gurantee_RMB_DateFrom_Old__c =  ass.Prod.Intra_Trade_Gurantee_RMB_Date1__c;  
                            }
                            when 'GuranteePrice'{
                                //InsAfterDel.RecordTypeName__c = 'GuranteePrice';
                                InsAfterDel.RecordTypeId = Schema.SObjectType.Maintenance_Product_Data_Details__c.getRecordTypeInfosByDeveloperName().get('GuranteePrice').getRecordTypeId();
                                if(ass.Prod.GuranteeType__c==null||ass.Prod.GuranteeType__c.length()==0){
                                    if ((ass.mpdrdd.Intra_Trade_Service_RMB__c != null && ass.mpdrdd.Intra_Trade_Service_RMB__c.format() != '0')
                                    || (ass.mpdrdd.Intra_Trade_Gurantee_RMB__c != null && ass.mpdrdd.Intra_Trade_Gurantee_RMB__c.format() != '0')
                                    || (ass.mpdrdd.Maintenance_Price_Year__c != null && ass.mpdrdd.Maintenance_Price_Year__c.format() != '0')
                                        ){
                                        isWarning = true;
                                        contentWarning = '产品编码:【'+ass.Prod.ProductCode_Ext__c+'】多年保类型是【空】时,NoDiscount价格、多年保修计提价格、维修合同报价必须为空!';
                                    }
                                }else{
                                    if(ass.Prod.GuranteeType__c.equals('市场')){
                                        if(ass.mpdrdd.Intra_Trade_Service_RMB__c==null 
                                            ||ass.mpdrdd.Intra_Trade_Gurantee_RMB__c==null
                                            ||ass.mpdrdd.Maintenance_Price_Year__c==null){
                                            isWarning = true;
                                            contentWarning = '产品编码:【'+ass.Prod.ProductCode_Ext__c+'】多年保类型是【市场】时,必须填写NoDiscount价格、多年保修计提价格以及维修合同报价!';
                                        }
                                    }
                                    if(ass.Prod.GuranteeType__c.equals('服务')){
                                        if((ass.mpdrdd.Intra_Trade_Service_RMB__c !=null&& ass.mpdrdd.Intra_Trade_Service_RMB__c.format() !='0')
                                            ||ass.mpdrdd.Intra_Trade_Gurantee_RMB__c==null
                                            ||ass.mpdrdd.Maintenance_Price_Year__c==null){
                                            isWarning = true;
                                            if(ass.mpdrdd.Intra_Trade_Gurantee_RMB__c==null
                                            ||ass.mpdrdd.Maintenance_Price_Year__c==null){
                                                contentWarning = '产品编码:【'+ass.Prod.ProductCode_Ext__c+'】多年保类型是【服务】时,必须填写多年保修计提价格、维修合同报价!';
 
                                            }
                                            if(ass.mpdrdd.Intra_Trade_Service_RMB__c !=null && ass.mpdrdd.Intra_Trade_Service_RMB__c.format() !='0'){
                                                contentWarning = '产品编码:【'+ass.Prod.ProductCode_Ext__c+'】多年保类型是【服务】时,NoDiscount价格必须为空!';    
                                            }   
                                        }
                                    }
                                }
                                if(isWarning){
                                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,  contentWarning));
                                    //return null;
                                }else{
                                    InsAfterDel.Intra_Trade_Service_RMB__c=ass.mpdrdd.Intra_Trade_Service_RMB__c;
                                    //2021/01/19 liying start 
                                    InsAfterDel.Virtual_Contract__c =ass.mpdrdd.Virtual_Contract__c ;
                                    //2021/01/19 liying end
                                    InsAfterDel.Intra_Trade_Gurantee_RMB__c =ass.mpdrdd.Intra_Trade_Gurantee_RMB__c ;
                                    InsAfterDel.Maintenance_Price_Year__c = ass.mpdrdd.Maintenance_Price_Year__c;
                                }
                                
                                //维修合同报价
                                InsAfterDel.Maintenance_Price_Year_Old__c = ass.Prod.Maintenance_Price_Year__c;
                                //多年保修计提价格与NoDiscount价格
                                //2021/01/19 liying start 
                                InsAfterDel.Virtual_Contract_Old__c =ass.Prod.Virtual_Contract__c;
                                //2021/01/19 liying end
                                InsAfterDel.Intra_Trade_Gurantee_RMB_Old__c =ass.Prod.Intra_Trade_Gurantee_RMB__c; 
                                InsAfterDel.Intra_Trade_Service_RMB_Old__c=ass.Prod.Intra_Trade_Service_RMB__c;
                                //Intra_Trade_Service_RMB_DateFrom_Old__c
                                InsAfterDel.Intra_Trade_Service_RMB_DateFrom_Old__c  =   ass.Prod.Intra_Trade_Service_RMB_Date1__c;
                                //Intra_Trade_Gurantee_RMB_DateFrom_Old__c
                                InsAfterDel.Intra_Trade_Gurantee_RMB_DateFrom_Old__c =  ass.Prod.Intra_Trade_Gurantee_RMB_Date1__c;  
                            }
                            when 'RepairInfo'{ 
                                //InsAfterDel.RecordTypeName__c = 'RepairInfo';
                                InsAfterDel.RecordTypeId = Schema.SObjectType.Maintenance_Product_Data_Details__c.getRecordTypeInfosByDeveloperName().get('RepairInfo').getRecordTypeId();
                                InsAfterDel.RepairListPriceLevelA__c=ass.mpdrdd.RepairListPriceLevelA__c;
                                InsAfterDel.RepairListPriceLevelB__c=ass.mpdrdd.RepairListPriceLevelB__c;
                                InsAfterDel.RepairListPriceLevelC__c=ass.mpdrdd.RepairListPriceLevelC__c;
                                InsAfterDel.CanRepairAccsessary__c=ass.mpdrdd.CanRepairAccsessary__c;
                                InsAfterDel.Repair_Product_Code__c=ass.mpdrdd.Repair_Product_Code__c;
                                InsAfterDel.PartSupplyFinishDate__c=ass.mpdrdd.PartSupplyFinishDate__c;
                                InsAfterDel.Maintenance_Price_Year__c=ass.mpdrdd.Maintenance_Price_Year__c;
                                //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210831 Star
                                InsAfterDel.Can_Repair__c=ass.mpdrdd.Can_Repair__c;
                                InsAfterDel.Service_Category1__c=ass.mpdrdd.Service_Category1__c;
                                InsAfterDel.Service_Category2__c=ass.mpdrdd.Service_Category2__c;
                                InsAfterDel.Service_Category3__c=ass.mpdrdd.Service_Category3__c;
                                InsAfterDel.Service_Category4__c=ass.mpdrdd.Service_Category4__c;
                                InsAfterDel.Service_Category5__c=ass.mpdrdd.Service_Category5__c;
                                InsAfterDel.Service_Category6__c=ass.mpdrdd.Service_Category6__c;
                                InsAfterDel.Service_Category7__c=ass.mpdrdd.Service_Category7__c;
                                //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210831 End
 
                                //old数据
                                InsAfterDel.PartSupplyFinishDate_Old__c=ass.Prod.PartSupplyFinishDate__c;
                                InsAfterDel.RepairListPriceLevelA_Old__c=ass.Prod.RepairListPriceLevelA__c;
                                InsAfterDel.RepairListPriceLevelB_Old__c=ass.Prod.RepairListPriceLevelB__c;
                                InsAfterDel.RepairListPriceLevelC_Old__c=ass.Prod.RepairListPriceLevelC__c;
                                InsAfterDel.CanRepairAccsessary_Old__c=ass.Prod.CanRepairAccsessary__c;
                                InsAfterDel.Repair_Product_Code_Old__c=ass.Prod.Repair_Product_Code__c;
                                InsAfterDel.Maintenance_Price_Year_Old__c=ass.Prod.Maintenance_Price_Year__c;
                                //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210831 Star
                                InsAfterDel.Can_Repair_Old__c=ass.Prod.Can_Repair__c;
                                InsAfterDel.Service_Category1_Old__c=ass.Prod.Service_Category1__c;
                                InsAfterDel.Service_Category2_Old__c=ass.Prod.Service_Category2__c;
                                InsAfterDel.Service_Category3_Old__c=ass.Prod.Service_Category3__c;
                                InsAfterDel.Service_Category4_Old__c=ass.Prod.Service_Category4__c;
                                InsAfterDel.Service_Category5_Old__c=ass.Prod.Service_Category5__c;
                                InsAfterDel.Service_Category6_Old__c=ass.Prod.Service_Category6__c;
                                InsAfterDel.Service_Category7_Old__c=ass.Prod.Service_Category7__c;
                                //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210831 End
                            }
                            when 'EquipmentInfo'{
                                //InsAfterDel.RecordTypeName__c = 'EquipmentInfo';
                                InsAfterDel.RecordTypeId = Schema.SObjectType.Maintenance_Product_Data_Details__c.getRecordTypeInfosByDeveloperName().get('EquipmentInfo').getRecordTypeId();
                                InsAfterDel.Asset_loaner_category__c = ass.mpdrdd.Asset_loaner_category__c;
                                InsAfterDel.Special_Model__c = ass.mpdrdd.Special_Model__c;
                                InsAfterDel.Fixture_Model_No__c = ass.mpdrdd.Fixture_Model_No__c;
                                InsAfterDel.Packing_list_Fixture__c = ass.mpdrdd.Packing_list_Fixture__c;
                                InsAfterDel.Default_Fixture_Arrival_Process__c = ass.mpdrdd.Default_Fixture_Arrival_Process__c;
                                InsAfterDel.Default_Fixture_Arrival_Product__c = ass.mpdrdd.Default_Fixture_Arrival_Product__c;
                                //old数据
                                InsAfterDel.Asset_loaner_category_Old__c = ass.Prod.Asset_loaner_category__c;
                                InsAfterDel.Special_Model_Old__c = ass.Prod.Special_Model__c;
                                InsAfterDel.Fixture_Model_No_Old__c = ass.Prod.Fixture_Model_No__c;
                                InsAfterDel.Packing_list_Fixture_Old__c = ass.Prod.Packing_list_Fixture__c;
                                InsAfterDel.Default_Fixture_Arrival_Process_Old__c = ass.Prod.Default_Fixture_Arrival_Process__c;
                                InsAfterDel.Default_Fixture_Arrival_Product_Old__c = ass.Prod.Default_Fixture_Arrival_Product__c;
                            }
                        }
                        i++;
                        InsAfterDel.Id = ass.mpdrdd.Id;
                        Ins.add(InsAfterDel);     
                    }
                }
                if(isWarning){
                    return null;
                }else{
                    if(Ins.size()>0){
                        upsert Ins;
                        return setEditAble();
                    }else{
                        return null;
                    }
                }
            }
        }catch(Exception e){
            ApexPages.addMessages(e);
            Database.rollback(sp);
            return null;
        }
    }
    public PageReference UpdateSave() {
        List<Maintenance_Product_Data_Details__c> Ins = New List<Maintenance_Product_Data_Details__c>();
        try{
            if(mpdDetailsRecords.size()>0){
                List<String> GuranteeTyped = new List<String>();
                Boolean CanNotCancelledGurantee0 = false;
                //System.debug('===:'+mpdDetailsRecords[0]);
                for (MpdDetailsInfo assType : mpdDetailsRecords)  {
                //20200618 类型一致不一致不能保存 add start
                    switch on RecordTypeName {
                        when 'GuranteeMain'{
                            if(assType.mpded.GuranteeType__c !=null&&!GuranteeTyped.contains(assType.mpded.GuranteeType__c)){
                                GuranteeTyped.add(assType.mpded.GuranteeType__c);
                            }
                            if(assType.mpded.CanNotCancelledGurantee__c){
                                CanNotCancelledGurantee0 = true;
                            }  
                        } 
                    }
                //20200618 类型一致不一致不能保存 add  END
                }  
                system.debug('GuranteeTyped:'+GuranteeTyped);            
                if(GuranteeTyped.size()>1){
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '保存失败,所有多年保修类型选择保持一致!'));
                    return null;
                }else if(((GuranteeTyped.size()==1&&!GuranteeTyped[0].equals('市场'))||GuranteeTyped.size()==0)&&CanNotCancelledGurantee0){
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '多年保类型是市场的时候,才可以勾选不可取消多年保修!'));
                    return null;
                }else{
                    Boolean isWarning = false;
                    String contentWarning = '';
                    for (MpdDetailsInfo ass : mpdDetailsRecords){
                        //if(ass.check == true){
                        Maintenance_Product_Data_Details__c upMpd = new Maintenance_Product_Data_Details__c();
                        upMpd.Id = ass.mpded.Id;
                        switch on RecordTypeName {
                            when 'ImportantProduct'{
                                //upMpd.Tier1_Category__c = ass.mpded.Tier1_Category__c;
                                upMpd.ENG_DeaerProFlag__c = ass.mpded.ENG_DeaerProFlag__c;
                                upMpd.Dealer_special_Object__c = ass.mpded.Dealer_special_Object__c;
                                upMpd.Key_product_147P__c = ass.mpded.Key_product_147P__c;
                                upMpd.Important_Endopartner_product__c = ass.mpded.Important_Endopartner_product__c;
                            }
                            when 'GuranteeMain'{
                                upMpd.Entend_gurantee_period__c=ass.mpded.Entend_gurantee_period__c;
                                upMpd.Gurantee_Start_Date__c=ass.mpded.Gurantee_Start_Date__c;
                                upMpd.GuranteeType__c=ass.mpded.GuranteeType__c;
                                upMpd.Gurantee_End_Date__c=ass.mpded.Gurantee_End_Date__c;
                                upMpd.CanNotCancelledGurantee__c=ass.mpded.CanNotCancelledGurantee__c;//20201012 ljh  CHAN-BTR67N add
                                if(mpd.Status__c.equals('服务填写')){
                                    // Boolean isWarning = false;
                                    // String contentWarning = '';
                                    
                                    if (ass.mpded.GuranteeType__c == null || ass.mpded.GuranteeType__c.length() == 0) {
                                        if ((ass.mpded.Intra_Trade_Service_RMB__c != null && ass.mpded.Intra_Trade_Service_RMB__c.format() != '0')
                                        || (ass.mpded.Intra_Trade_Gurantee_RMB__c != null && ass.mpded.Intra_Trade_Gurantee_RMB__c.format() != '0')
                                        || (ass.mpded.Maintenance_Price_Year__c != null && ass.mpded.Maintenance_Price_Year__c.format() != '0')
                                           ) {
                                            isWarning = true;
                                            contentWarning = '多年保类型是【空】时,NoDiscount价格、多年保修计提价格、维修合同报价必须为空!';
                                        }
                                    }else{
                                        if(ass.mpded.GuranteeType__c.equals('市场')){
                                            if(ass.mpded.Intra_Trade_Service_RMB__c==null
                                                ||ass.mpded.Intra_Trade_Gurantee_RMB__c==null
                                                ||ass.mpded.Maintenance_Price_Year__c==null){
                                                isWarning = true;
                                                contentWarning = '多年保类型是【市场】时,必须填写NoDiscount价格、多年保修计提价格以及维修合同报价!';
                                            }
                                        }
                                        if(ass.mpded.GuranteeType__c.equals('服务')){
                                            if((ass.mpded.Intra_Trade_Service_RMB__c !=null && ass.mpded.Intra_Trade_Service_RMB__c.format() !='0')
                                                ||ass.mpded.Intra_Trade_Gurantee_RMB__c==null
                                                ||ass.mpded.Maintenance_Price_Year__c==null){
                                                isWarning = true;
                                                if(ass.mpded.Intra_Trade_Service_RMB__c !=null && ass.mpded.Intra_Trade_Service_RMB__c.format() !='0'){
                                                    contentWarning = '多年保类型是【服务】时,NoDiscount价格必须为空!'; 
                                                }
                                                if(ass.mpded.Intra_Trade_Gurantee_RMB__c==null
                                                ||ass.mpded.Maintenance_Price_Year__c==null){
                                                    contentWarning = '多年保类型是【服务】时,必须填写多年保修计提价格、维修合同报价!';  
                                                }                                        
                                            }
                                        }
                                    }
                                    if(isWarning){
                                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,  contentWarning));
                                        //return null;
                                    }else{
                                        //维修合同报价
                                        upMpd.Maintenance_Price_Year__c = ass.mpded.Maintenance_Price_Year__c;
                                        //多年保修计提价格与NoDiscount价格
                                        //2021/01/19 liying start
                                        upMpd.Virtual_Contract__c =ass.mpded.Virtual_Contract__c;
                                        //2021/01/19 liying end
                                        upMpd.Intra_Trade_Gurantee_RMB__c =ass.mpded.Intra_Trade_Gurantee_RMB__c ;
                                        upMpd.Intra_Trade_Service_RMB__c=ass.mpded.Intra_Trade_Service_RMB__c;
                                    }
                                }
                            }
                            when 'GuranteePrice'{
                                // Boolean isWarning = false;
                                // String contentWarning = '';
                                 if (ass.mpded.GuranteeTypeP__c == null || ass.mpded.GuranteeTypeP__c.length() == 0) {
                                    if ((ass.mpded.Intra_Trade_Service_RMB__c != null && ass.mpded.Intra_Trade_Service_RMB__c.format() != '0')
                                    || (ass.mpded.Intra_Trade_Gurantee_RMB__c != null && ass.mpded.Intra_Trade_Gurantee_RMB__c.format() != '0')
                                    || (ass.mpded.Maintenance_Price_Year__c != null && ass.mpded.Maintenance_Price_Year__c.format() != '0')
                                       ) {
                                        isWarning = true;
                                        contentWarning = '产品编码:【'+ass.mpded.ProductCode_Ext__c+'】多年保类型是【空】时,NoDiscount价格、多年保修计提价格、维修合同报价必须为空!';
                                    }
                                }else{
                                    if(ass.mpded.GuranteeTypeP__c.equals('市场')){
                                        if(ass.mpded.Intra_Trade_Service_RMB__c==null
                                            ||ass.mpded.Intra_Trade_Gurantee_RMB__c==null
                                            ||ass.mpded.Maintenance_Price_Year__c==null){
                                            isWarning = true;
                                            contentWarning = '产品编码:【'+ass.mpded.ProductCode_Ext__c+'】多年保类型是【市场】时,必须填写NoDiscount价格、多年保修计提价格以及维修合同报价!';
                                        }
                                    }
                                    if(ass.mpded.GuranteeTypeP__c.equals('服务')){
                                        if((ass.mpded.Intra_Trade_Service_RMB__c !=null&&ass.mpded.Intra_Trade_Service_RMB__c.format() !='0')
                                            ||ass.mpded.Intra_Trade_Gurantee_RMB__c==null
                                            ||ass.mpded.Maintenance_Price_Year__c==null){
                                            isWarning = true;
                                            if(ass.mpded.Intra_Trade_Service_RMB__c !=null && ass.mpded.Intra_Trade_Service_RMB__c.format() !='0'){
                                                contentWarning = '产品编码:【'+ass.mpded.ProductCode_Ext__c+'】多年保类型是【服务】时,NoDiscount价格必须为空!'; 
                                            }
                                            if(ass.mpded.Intra_Trade_Gurantee_RMB__c==null
                                            ||ass.mpded.Maintenance_Price_Year__c==null){
                                                contentWarning = '产品编码:【'+ass.mpded.ProductCode_Ext__c+'】多年保类型是【服务】时,必须填写多年保修计提价格、维修合同报价!'; 
                                            }
                                        }
                                    }
                                }
                                
                                if(isWarning){
                                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,  contentWarning));
                                }else{
                                    //维修合同报价
                                    upMpd.Maintenance_Price_Year__c = ass.mpded.Maintenance_Price_Year__c;
                                    //多年保修计提价格与NoDiscount价格
                                    //2021/01/19 liying start
                                    upMpd.Virtual_Contract__c =ass.mpded.Virtual_Contract__c ;
                                    //2021/01/19 liying end
                                    upMpd.Intra_Trade_Gurantee_RMB__c =ass.mpded.Intra_Trade_Gurantee_RMB__c ;
                                    upMpd.Intra_Trade_Service_RMB__c=ass.mpded.Intra_Trade_Service_RMB__c;
                                }
                            }
                            when 'RepairInfo'{ 
                                upMpd.RepairListPriceLevelA__c=ass.mpded.RepairListPriceLevelA__c;
                                upMpd.RepairListPriceLevelB__c=ass.mpded.RepairListPriceLevelB__c;
                                upMpd.RepairListPriceLevelC__c=ass.mpded.RepairListPriceLevelC__c;
                                upMpd.CanRepairAccsessary__c=ass.mpded.CanRepairAccsessary__c;
                                upMpd.Repair_Product_Code__c=ass.mpded.Repair_Product_Code__c;
                                upMpd.PartSupplyFinishDate__c=ass.mpded.PartSupplyFinishDate__c;
                                upMpd.Maintenance_Price_Year__c=ass.mpded.Maintenance_Price_Year__c;
                                //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210831 Star
                                upMpd.Can_Repair__c=ass.mpded.Can_Repair__c;
                                upMpd.Service_Category1__c=ass.mpded.Service_Category1__c;
                                upMpd.Service_Category2__c=ass.mpded.Service_Category2__c;
                                upMpd.Service_Category3__c=ass.mpded.Service_Category3__c;
                                upMpd.Service_Category4__c=ass.mpded.Service_Category4__c;
                                upMpd.Service_Category5__c=ass.mpded.Service_Category5__c;
                                upMpd.Service_Category6__c=ass.mpded.Service_Category6__c;
                                upMpd.Service_Category7__c=ass.mpded.Service_Category7__c;
                                //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210831 End
                            }
                            when 'EquipmentInfo'{
                                upMpd.Asset_loaner_category__c = ass.mpded.Asset_loaner_category__c;
                                upMpd.Special_Model__c = ass.mpded.Special_Model__c;
                                upMpd.Fixture_Model_No__c = ass.mpded.Fixture_Model_No__c;
                                upMpd.Packing_list_Fixture__c = ass.mpded.Packing_list_Fixture__c;
                                upMpd.Default_Fixture_Arrival_Process__c = ass.mpded.Default_Fixture_Arrival_Process__c;
                                upMpd.Default_Fixture_Arrival_Product__c = ass.mpded.Default_Fixture_Arrival_Product__c;
                            }
                        }
                        Ins.add(upMpd);    
                    }
                    if(isWarning){
                        return null;
                    }else{
                        if(Ins.size()>0){
                            update Ins;
                            return UnabletoEdit();
                        }
                        else{ 
                            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,  '请先追加数据到产品主数据维护!'));
                            return null;
                        }
                    }
                    
                }   
            }else{
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,  '请先追加数据到产品主数据维护!'));
                return null;
            }    
        }catch(Exception e){
            ApexPages.addMessages(e);
            return null ;
        }
    }
    public PageReference deleteMpdd(){
        List<Maintenance_Product_Data_Details__c> deleteRecords2 = [select Id FROM Maintenance_Product_Data_Details__c WHERE id = :mpddId];
        if(deleteRecords2.size() >0){
            delete deleteRecords2;
        }
        return  setEditAble();
    }
    
    // 返回不可编辑状态
    public PageReference UnabletoEdit(){
        PageReference ref = new Pagereference('/apex/MaintenanceProductData?id='+id);
        ref.setRedirect(true);
        return ref;
    }
    // 编辑按钮
    public PageReference setEditAble(){
        statusEdit = 'Redirect';
        String url = '/apex/MaintenanceProductData?id='+id+'&KeyWords='+statusEdit;
        if(String.isNotBlank(searchCateName)){
            url += '&searchCateName='+searchCateName;
        }
        if(String.isNotBlank(searchProductCodeExt)){
            url += '&searchProductCodeExt='+searchProductCodeExt;
        }
        if(RecordTypeName.equals('GuranteeMain')||RecordTypeName.equals('GuranteePrice')){
            if(String.isNotBlank(searchGuranteeType)){
                url += '&searchGuranteeType='+searchGuranteeType;
            }
        }else{
            if(!String.isBlank(category1)){
                url += '&category1='+category1;
            }
            if(!String.isBlank(category2)){
                url += '&category2='+category2;
            }
            if(!String.isBlank(category3)){
                url += '&category3='+category3;
            }
            if(!String.isBlank(category4)){
                url += '&category4='+category4;
            }
            if(!String.isBlank(category5)){
                url += '&category5='+category5;
            }
        }
        PageReference ref = new Pagereference(url);
        ref.setRedirect(true);
        return ref;
    }
    //上传附件
    public PageReference FilesUpload(){
        PageReference ref = new Pagereference('/p/attach/NoteAttach?pid='+id+'&retURL=%2F' + '/apex/MaintenanceProductData?id=' +id);
        ref.setRedirect(true);
        return ref;
    }
    // 提交按钮
    public PageReference Submitmpd() {
        //List<Maintenance_Product_Data__c> qs = New List<Maintenance_Product_Data__c>();
        Maintenance_Product_Data__c mpdJuge= [SELECT Id,Name,RecordTypeId, RecordType.Name,RecordType.DeveloperName,
                                             RepairListPriceLevelA_Sum__c,RepairListPriceLevelB_Sum__c,RepairListPriceLevelC_Sum__c,Intra_Trade_Service_RMB_Sum__c,
                                             Status__c,ApplyPersion__c,applyDate__c,GuranteeType_Sum__c,Intra_Trade_Gurantee_RMB_Sum__c,BuchangApprovalRecommend__c,
                                             ServiceWritePersion__c,Maintenance_Price_Year_Sum__c,CreatedDate, CreatedById
                        FROM Maintenance_Product_Data__c where Id = :id];
        Integer tempRepairInfo = 0,tempGuranteeMainPrice = 0;
        tempRepairInfo  = mpdJuge.RepairListPriceLevelA_Sum__c.intValue()+mpdJuge.RepairListPriceLevelB_Sum__c.intValue()+mpdJuge.RepairListPriceLevelC_Sum__c.intValue()+mpdJuge.Maintenance_Price_Year_Sum__c.intValue();
        tempGuranteeMainPrice =  mpdJuge.Intra_Trade_Service_RMB_Sum__c.intValue()+  mpdJuge.Intra_Trade_Gurantee_RMB_Sum__c.intValue()+mpdJuge.Maintenance_Price_Year_Sum__c.intValue();            
        //检查应该有附件的是否有附件
        if(RecordTypeName.equals('GuranteePrice') && tempGuranteeMainPrice > 0 &&AttachmentRecoeds.size()==0){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,  '还没有上传附件,请上传附件!'));
            return null;
        }else if(RecordTypeName.equals('RepairInfo') && tempRepairInfo > 0 && AttachmentRecoeds.size()==0){
            //RepairInfo
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,  '还没有上传附件,请上传附件!'));
            return null;
        //服务填写价格故提交时不需要判断
        /*}else if(RecordTypeName.equals('GuranteeMain') && tempGuranteeMainPrice > 0 &&AttachmentRecoeds.size()==0){
            //GuranteeMain
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,  '还没有上传附件,请上传附件!'));
            return null;*/
        }else{
            Savepoint sp = Database.setSavepoint();
            Map<Id,String> prodMap = new Map<Id,String>();
            Maintenance_Product_Data__c P = new Maintenance_Product_Data__c();
            P = new Maintenance_Product_Data__c(); 
            p.Id=id;
            //p.applyDate__c = Date.today();
            p.applyDate__c = Datetime.now();
            p.Status__c = '已提交';
            p.ApplyPersion__c = UserInfo.getUserId();
            User loginUser = [SELECT Id, Name, SalesManager__c, JingliApprovalManager__c, BuchangApprovalManager__c, BuchangApprovalManagerSales__c, ZongjianApprovalManager__c, TongkuoZongjian__c FROM User WHERE Id = :UserInfo.getUserId()];
            //p.BuchangApprovalRecommend__c =loginUser.BuchangApprovalManagerSales__c == null?UserInfo.getUserId():loginUser.BuchangApprovalManagerSales__c; //申请人的部长
            p.BuchangApprovalRecommend__c =loginUser.BuchangApprovalManagerSales__c;
            try{
                if(loginUser.BuchangApprovalManagerSales__c != null){
                    
                    //共享给相应的部长
                    /*Maintenance_Product_Data__Share mpdShare = new Maintenance_Product_Data__Share();
                    mpdShare.UserOrGroupId = ml.Engagement__c;//需要共享给的用户 or 小组
                    mpdShare.ParentId = ml.id;//记录id
                    mpdShare.AccessLevel = 'edit'; //edit:读写/read:只读
                    list_share.add(share);*/
                    Maintenance_Product_Data__Share mpdShare = new Maintenance_Product_Data__Share(UserOrGroupId=p.BuchangApprovalRecommend__c, ParentId=p.Id, AccessLevel='Edit');
                    insert mpdShare;
                    update p;
                }else{
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,  '对不起,您无权提交!'));
                    return null;
                }
            }catch(Exception e){
                ApexPages.addmessages(e);
            }
            try {
                Approval.ProcessSubmitRequest psr = new Approval.ProcessSubmitRequest();
                psr.setObjectId(id);
                psr.setSubmitterId(UserInfo.getUserId());
                if(RecordTypeName.equals('GuranteeMain')){
                    psr.setProcessDefinitionNameOrId('MaintenanceProductDataApprovalProcess1');
                } else if(RecordTypeName !=null){
                    psr.setProcessDefinitionNameOrId('MaintenanceProductDataApprovalProcess');
                }
                Approval.ProcessResult submitResult = Approval.process(psr);
                if(submitResult.isSuccess()){
                    return  returnMpdPage(); 
                }else{
                    //return  returnMpdPage1(); //提醒没有成功进审批流
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,  '未成功提交,请稍后再试!'));
                    return null;
                }
                
            }
            catch (Exception e) {
                //ApexPages.addmessages(e);
                Database.rollback(sp);
                return  null; 
            }
        }        
    }
    public PageReference returnMpdPage(){
        PageReference ref;
        if(String.isBlank(id)){
            ref = new Pagereference(ApexPages.currentPage().getParameters().get('retURL'));
        }else{
            ref = new Pagereference('/'+id);
        }
        ref.setRedirect(true);
        return ref;
    }
    
    //搜索时的sql
    public String makeSoql(String Family,String Category2,String Category3,String Category4,String Category5,String CateName,String ProductCode_Ext,String GuranteeType){
        /*String sqlTail_s = '(\'';
        for(Integer i = 0 ; i< dataselectedId_s.size();i++){
            if(i<dataselectedId_s.size()-1){
                sqlTail_s += dataselectedId_s[i]+'\',\'';
            }else{
                sqlTail_s += dataselectedId_s[i]+'\')';
            }
        }*/
 
        String soql = 'select Id ,ProductCode_Ext__c,Name   ';
        if(!RecordTypeName.equals('GuranteeMain') && !RecordTypeName.equals('GuranteePrice') ){
            for (Integer i=0;i<column.size();i++) {soql += ',' + column[i];
            }
        }
        if(RecordTypeName.equals('GuranteePrice')){
            soql += ' ,GuranteeType__c ';
            soql += ',Maintenance_Price_Year__c,Intra_Trade_Service_RMB__c,Virtual_Contract__c,Intra_Trade_Gurantee_RMB__c ';
            soql += ' ,Intra_Trade_Service_RMB_Date1__c,Intra_Trade_Service_RMB_Date2__c, Intra_Trade_Service_RMB_EndDate1__c, Intra_Trade_Service_RMB_EndDate2__c ';
            soql += ' ,Intra_Trade_Gurantee_RMB_Date1__c,Intra_Trade_Gurantee_RMB_Date2__c, Intra_Trade_Gurantee_RMB_End_Date1__c, Intra_Trade_Gurantee_RMB_End_Date2__c ';
        }
        if(RecordTypeName.equals('GuranteeMain')){
            soql += ' ,Maintenance_Price_Year__c,Entend_gurantee_period_all__c,Gurantee_Start_Date_F__c,GuranteeType__c,CanNotCancelledGurantee__c';
            soql += ' ,Gurantee_End_Date_F__c,Intra_Trade_Service_RMB__c,Virtual_Contract__c,Intra_Trade_Gurantee_RMB__c';
            soql += ' ,Intra_Trade_Service_RMB_Date1__c,Intra_Trade_Service_RMB_Date2__c, Intra_Trade_Service_RMB_EndDate1__c, Intra_Trade_Service_RMB_EndDate2__c ';
            soql += ' ,Intra_Trade_Gurantee_RMB_Date1__c,Intra_Trade_Gurantee_RMB_Date2__c, Intra_Trade_Gurantee_RMB_End_Date1__c, Intra_Trade_Gurantee_RMB_End_Date2__c ';
        }
        soql += '  FROM Product2  WHERE IsActive = true  ';
 
        if(!String.isBlank(searchCateName)){
            soql += ' AND (Name like \'%' + String.escapeSingleQuotes(CateName.replaceAll('%', '\\%')) + '%\' or Asset_Model_No__c like \'%' + String.escapeSingleQuotes(CateName.replaceAll('%', '\\%')) + '%\')';
        }
        if(!String.isBlank(searchProductCodeExt)){
            soql += ' AND (ProductCode_Ext__c like \'%'+ String.escapeSingleQuotes(ProductCode_Ext.replaceAll('%', '\\%')) + '%\')';
        }
        if(!String.isBlank(Category1)){
            soql += ' AND (Family like \'%'+ String.escapeSingleQuotes(Category1.replaceAll('%', '\\%')) + '%\')';
        }
        if(!String.isBlank(Category2)){
            soql += ' AND (Category2__c like \'%'+ String.escapeSingleQuotes(Category2.replaceAll('%', '\\%')) + '%\')';
        }
        if(!String.isBlank(Category3)){
            soql += ' AND (Category3__c like \'%'+ String.escapeSingleQuotes(Category3.replaceAll('%', '\\%')) + '%\')';
        }
        if(!String.isBlank(Category4)){
            soql += ' AND (Category4__c like \'%'+ String.escapeSingleQuotes(Category4.replaceAll('%', '\\%')) + '%\')';
        }
        if(!String.isBlank(Category5)){
            soql += ' AND (Category5__c like \'%'+ String.escapeSingleQuotes(Category5.replaceAll('%', '\\%')) + '%\')';
        }
        if(!String.isBlank(searchGuranteeType)){
            soql += ' AND (GuranteeType__c like \'%'+ String.escapeSingleQuotes(GuranteeType.replaceAll('%', '\\%')) + '%\')';
        }
        soql += ' Limit 400';
        //System.debug('soql +++++++++++  ' + soql );
        return soql;
    }
     // 显示数据条数信息
    private void makeMessage() {
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '共有' + noOfRecords + '件产品'));
    }
    private List<SelectOption> getPlickList(String objApi, String fieldApi) {
        Schema.DescribeFieldResult fieldResult = Schema.getGlobalDescribe().get(objApi).getDescribe().fields.getMap().get(fieldApi).getDescribe();
        List<SelectOption> pickListValuesList= new List<SelectOption>();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        pickListValuesList.add(new SelectOption('', NONE));
        for( Schema.PicklistEntry pickListVal : ple){
            pickListValuesList.add(new SelectOption(pickListVal.getValue(), pickListVal.getLabel()));
        }
        return pickListValuesList;
    }
 
    public List<SelectOption> getKeyProduct147POps() {
        List<SelectOption>  options = getPlickList('Maintenance_Product_Data_Details__c', 'Key_product_147P__c');
        // 显示为空白,值为'x',适用时保持原样
        options.add(0, new SelectOption('x',''));
        return options;
    }
    public List<SelectOption> getGuranteeTypeOps() {
        List<SelectOption>  options = getPlickList('Maintenance_Product_Data_Details__c', 'GuranteeType__c');
        // 显示为空白,值为'x',适用时保持原样
        options.add(0, new SelectOption('x',''));
        return options;
    }
    public List<SelectOption> getEntendGuranteePeriodOps() {
        List<SelectOption>  options = getPlickList('Maintenance_Product_Data_Details__c', 'Entend_gurantee_period__c');
        // 显示为空白,值为'x',适用时保持原样
        options.add(0, new SelectOption('x',''));
        return options;
    }
    public List<SelectOption> getDefaultFixtureArrivalProcessOps() {
        List<SelectOption>  options = getPlickList('Maintenance_Product_Data_Details__c', 'Default_Fixture_Arrival_Process__c');
        // 显示为空白,值为'x',适用时保持原样
        options.add(0, new SelectOption('x',''));
        return options;
    }
    public List<SelectOption> getAssetLoanerCategoryOps() {
        List<SelectOption>  options = getPlickList('Maintenance_Product_Data_Details__c', 'Asset_loaner_category__c');
        // 显示为空白,值为'x',适用时保持原样
        options.add(0, new SelectOption('x',''));
        return options;
    }
    //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210908 Star
    public List<SelectOption> getCanRepairOps() {
        List<SelectOption>  options = getPlickList('Maintenance_Product_Data_Details__c', 'Can_Repair__c');
        // 显示为空白,值为'x',适用时保持原样
        options.add(0, new SelectOption('x',''));
        return options;
    }
    public List<SelectOption> getServiceCategory1() {
        List<SelectOption>  options = getPlickList('Maintenance_Product_Data_Details__c', 'Service_Category1__c');
        // 显示为空白,值为'x',适用时保持原样
        options.add(0, new SelectOption('x',''));
        return options;
    }
    public List<SelectOption> getServiceCategory2() {
        List<SelectOption>  options = getPlickList('Maintenance_Product_Data_Details__c', 'Service_Category2__c');
        // 显示为空白,值为'x',适用时保持原样
        options.add(0, new SelectOption('x',''));
        return options;
    }
    public List<SelectOption> getServiceCategory3() {
        List<SelectOption>  options = getPlickList('Maintenance_Product_Data_Details__c', 'Service_Category3__c');
        // 显示为空白,值为'x',适用时保持原样
        options.add(0, new SelectOption('x',''));
        return options;
    }
    public List<SelectOption> getServiceCategory4() {
        List<SelectOption>  options = getPlickList('Maintenance_Product_Data_Details__c', 'Service_Category4__c');
        // 显示为空白,值为'x',适用时保持原样
        options.add(0, new SelectOption('x',''));
        return options;
    }
    public List<SelectOption> getServiceCategory5() {
        List<SelectOption>  options = getPlickList('Maintenance_Product_Data_Details__c', 'Service_Category5__c');
        // 显示为空白,值为'x',适用时保持原样
        options.add(0, new SelectOption('x',''));
        return options;
    }
    //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210908 End
 
    class MaintenanceProductDataDetailsInfo  {
        public Boolean check { get; set; }
        public Boolean oldCheck { get; set; }
        public Boolean canSelect { get; set; }
        public Product2 Prod { get; set; }
        public Maintenance_Product_Data_Details__c mpdrdd {get; set;}
        public Attachment Concc { get; set; }
 
        public MaintenanceProductDataDetailsInfo(Maintenance_Product_Data_Details__c e) {
            check = true;
            oldCheck = true;
            mpdrdd = e;
            Prod = e.ProductsID__r;
            canSelect = true;
        }
        public MaintenanceProductDataDetailsInfo(Product2 e) {
            check = false;
            oldCheck = false;
            mpdrdd = new Maintenance_Product_Data_Details__c();
            Prod = e;
            canSelect = true;
        }
        //附件
        public MaintenanceProductDataDetailsInfo(Attachment e) {
            Concc = e;
        }
    }
    class MpdDetailsInfo  {
        public Boolean check { get; set; }
        public Boolean oldCheck { get; set; }
        public Boolean canSelect { get; set; }
        public Maintenance_Product_Data_Details__c mpded {get; set;}
 
        public MpdDetailsInfo(Maintenance_Product_Data_Details__c e) {
            check = true;
            oldCheck = true;
            mpded = e;
            canSelect = false;
        }
    }
 
}