高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
public without sharing class MaintenanceUpdateProductHandler extends Oly_TriggerHandler {
    private Map<Id, Maintenance_Product_Data__c> newMap;
    private Map<Id, Maintenance_Product_Data__c> oldMap;
    private List<Maintenance_Product_Data__c> newList;
    private List<Maintenance_Product_Data__c> oldList;
    public String query;
    public static Integer FIELDMAX = 200;
    public MaintenanceUpdateProductHandler() {
        this.newMap = (Map<Id, Maintenance_Product_Data__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Maintenance_Product_Data__c>) Trigger.oldMap;
        this.newList = (List<Maintenance_Product_Data__c>) Trigger.new;
        this.oldList = (List<Maintenance_Product_Data__c>) Trigger.old;
    }
    protected override void beforeUpdate() {
        beforeUpdateProduct(this.newList);
    }
    protected override void afterUpdate() {
        updateProduct(this.newList);
    }
    private void beforeUpdateProduct(List<Maintenance_Product_Data__c> nList){
        List<String> mpdIds = new List<String>();
        for (Maintenance_Product_Data__c newMpd : nList) {
            Maintenance_Product_Data__c old = oldMap.get(newMpd.Id);
            if(newMpd.Status__c=='审批中-服务' && newMpd.Status__c != old.Status__c){
                //newMpd.addError('请填写完整价格相关的信息!');
                mpdIds.add(newMpd.Id);
            }
        }
        if(mpdIds.size()>0){ 
            List<Maintenance_Product_Data_Details__c> mpddList = [select Id, Name ,ProductsID__c,Maintenance_Product_Data__c,GuranteeType__c,Intra_Trade_Service_RMB__c,Virtual_Contract__c,Intra_Trade_Gurantee_RMB__c,Maintenance_Price_Year__c 
             from Maintenance_Product_Data_Details__c where  Maintenance_Product_Data__c in :mpdIds ];
            List<Maintenance_Product_Data__c> mpdList = [select Id, Name ,Intra_Trade_Service_RMB_Sum__c,Intra_Trade_Gurantee_RMB_Sum__c,Maintenance_Price_Year_Sum__c  from Maintenance_Product_Data__c where  id in :mpdIds ];
            Boolean isWarning = false;
            Boolean attachmentSize =false;
            Boolean attachmentChange = false;
            String  contentWarning = '';
            for(Maintenance_Product_Data_Details__c mpdDetails : mpddList){
                //   1.2  多年保修类型为服务时,NoDiscount价格不能填写,多年保修计提价格\维修合同报价必须填写;
                //  1.1  多年保修类型为市场时,NoDiscount价格\多年保修计提价格\维修合同报价必须填写;
                //Intra_Trade_Service_RMB__c Intra_Trade_Gurantee_RMB__c Maintenance_Price_Year__c
                if(mpdDetails.GuranteeType__c==null||mpdDetails.GuranteeType__c.length()==0){
                    if((mpdDetails.Intra_Trade_Service_RMB__c !=null&&mpdDetails.Intra_Trade_Service_RMB__c.format()!='0')
                        ||(mpdDetails.Intra_Trade_Gurantee_RMB__c!=null&&mpdDetails.Intra_Trade_Gurantee_RMB__c.format()!='0')
                        ||(mpdDetails.Maintenance_Price_Year__c !=null&&mpdDetails.Maintenance_Price_Year__c.format() !='0')
                        ){
                        isWarning = true;
                        contentWarning = 'NoDiscount价格、多年保修计提价格、维修合同报价必须为空!';
                    }
                }else{
                    if(mpdDetails.GuranteeType__c.equals('市场')){
                        if(mpdDetails.Intra_Trade_Service_RMB__c==null
                            ||mpdDetails.Intra_Trade_Gurantee_RMB__c==null
                            ||mpdDetails.Maintenance_Price_Year__c==null){
                            isWarning = true;
                            contentWarning = '必须填写NoDiscount价格、多年保修计提价格以及维修合同报价!';
                        }
                    }
                    if(mpdDetails.GuranteeType__c.equals('服务')){
                        if((mpdDetails.Intra_Trade_Service_RMB__c !=null&&mpdDetails.Intra_Trade_Service_RMB__c.format() !='0')
                            ||mpdDetails.Intra_Trade_Gurantee_RMB__c==null
                            ||mpdDetails.Maintenance_Price_Year__c==null){
                            isWarning = true;
                            if(mpdDetails.Intra_Trade_Gurantee_RMB__c==null
                            ||mpdDetails.Maintenance_Price_Year__c==null){
                                contentWarning = '必须填写多年保修计提价格、维修合同报价!';
                            }
                            if(mpdDetails.Intra_Trade_Service_RMB__c !=null&&mpdDetails.Intra_Trade_Service_RMB__c.format() !='0'){
                                contentWarning = 'NoDiscount价格必须为空!';
                            }
                        }
                    }
 
                }   
            }
            for(String id:mpdIds){
                List<Attachment> attachmentinfo = [SELECT Id, Name,OwnerId FROM Attachment WHERE  parentid  =:mpdIds ];
                if(attachmentinfo.size() ==0){
                    attachmentSize = true;
                }
            }
            for(Maintenance_Product_Data__c mpdChange : mpdList){
                if(mpdChange.Intra_Trade_Service_RMB_Sum__c>0 || mpdChange.Intra_Trade_Gurantee_RMB_Sum__c>0 ||mpdChange.Maintenance_Price_Year_Sum__c>0){
                    attachmentChange = true;
                }
            }
            for (Maintenance_Product_Data__c newMpd : nList) {
                Maintenance_Product_Data__c old = oldMap.get(newMpd.Id);
                if(newMpd.Status__c=='审批中-服务'&& newMpd.Status__c != old.Status__c && isWarning){
                    newMpd.addError(contentWarning);
                }
                
                if(newMpd.Status__c=='审批中-服务'&& newMpd.Status__c != old.Status__c && attachmentSize&&attachmentChange){
                    newMpd.addError('还没有上传附件,请上传附件!');
                }
            }
        }
    }
    private void updateProduct(List<Maintenance_Product_Data__c> nList){
        List<String> mpdId = new List<String>();
        List<Maintenance_Product_Data__c> EmailMPD = new List<Maintenance_Product_Data__c>();
        for (Maintenance_Product_Data__c newMpd : nList) {
            Maintenance_Product_Data__c old = oldMap.get(newMpd.Id);
            if(newMpd.Status__c=='完成'&&newMpd.Status__c != old.Status__c){
                mpdId.add(newMpd.Id);
                EmailMPD.add(newMpd);
            }
        }
        if(mpdId.size()>0){ 
            String sqlTail = '(\'';
            for(Integer i = 0 ; i< mpdId.size();i++){
                if(i<mpdId.size()-1){
                    sqlTail += mpdId[i]+'\',\'';
                }else{
                    sqlTail += mpdId[i]+'\')';
                }
            } 
            //Tier1_Category__c,
            query = 'select Id, Name ,ProductsID__c,Maintenance_Product_Data__c,RecordType.DeveloperName  ';
            query += ' ,ENG_DeaerProFlag__c,Dealer_special_Object__c,Key_product_147P__c,Key_product_147P_Old__c,Important_Endopartner_product__c ';
            query += ' ,GuranteeType__c,Entend_gurantee_period__c,Gurantee_Start_Date__c,Gurantee_End_Date__c,CanNotCancelledGurantee__c  ';
            query += ' ,GuranteeType_Old__c,Entend_gurantee_period_Old__c,Gurantee_Start_Date_Old__c,Gurantee_End_Date_Old__c';
            query += ' ,Intra_Trade_Service_RMB__c,Intra_Trade_Service_RMB_DateFrom__c,Virtual_Contract__c,Intra_Trade_Gurantee_RMB__c, Intra_Trade_Gurantee_RMB_DateFrom__c,Maintenance_Price_Year__c ';
            query += ' ,Intra_Trade_Service_RMB_Old__c,Intra_Trade_Gurantee_RMB_Old__c';
            query += ' ,Intra_Trade_Service_RMB_Date1__c,Intra_Trade_Service_RMB_EndDate1__c  ';
            query += ' ,Intra_Trade_Gurantee_RMB_Date1__c,Intra_Trade_Gurantee_RMB_End_Date1__c ';
            query += ' ,RepairListPriceLevelA__c ,RepairListPriceLevelB__c ,RepairListPriceLevelC__c ,CanRepairAccsessary__c ,Repair_Product_Code__c ,PartSupplyFinishDate__c ';
            query += ' ,Asset_loaner_category__c , Special_Model__c ,Fixture_Model_No__c,Packing_list_Fixture__c,Default_Fixture_Arrival_Process__c, Default_Fixture_Arrival_Product__c   ';
            //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210831 Star
            query += ' ,Can_Repair__c , Service_Category1__c ,Service_Category2__c,Service_Category3__c,Service_Category4__c, Service_Category5__c,Service_Category6__c,Service_Category7__c ';
            //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210831 End
            query += ' from Maintenance_Product_Data_Details__c where Maintenance_Product_Data__r.Status__c =\'完成\' ';
            query += ' and Maintenance_Product_Data__c in '+sqlTail;
            List<Maintenance_Product_Data_Details__c> mpdList = Database.query(query);
            List<Product2> product2Update = new List<Product2>();
            //Map<String,String> mpdId = new Map<String,String>();
            List<Maintenance_Product_Data_Details__c> mpdDetailsStatus = new List<Maintenance_Product_Data_Details__c>();
            for(Maintenance_Product_Data_Details__c mpdDetails : mpdList){
                Maintenance_Product_Data_Details__c pdd = new Maintenance_Product_Data_Details__c();
                Product2 product2 = new Product2();
                //mpdId.put(mpdDetails.Maintenance_Product_Data__c,mpdDetails.Maintenance_Product_Data__c);
                pdd.Id = mpdDetails.Id;
                if(mpdDetails.RecordType.DeveloperName.equals('GuranteeMain')||mpdDetails.RecordType.DeveloperName.equals('GuranteeMain')){
                    pdd.Intra_Trade_Service_RMB_DateFrom__c = Date.toDay();
                    pdd.Intra_Trade_Gurantee_RMB_DateFrom__c = Date.toDay();
                }
                pdd.Status__c = '已更新';
                mpdDetailsStatus.add(pdd);
                //五个记录类型
                if(mpdDetails.RecordType.DeveloperName.equals('ImportantProduct')){
                    product2.Id = mpdDetails.ProductsID__c;
                    //product2.Tier1_Category__c = mpdDetails.Tier1_Category__c;
                    product2.ENG_DeaerProFlag__c = mpdDetails.ENG_DeaerProFlag__c;
                    product2.Dealer_special_Object__c = mpdDetails.Dealer_special_Object__c;
                    product2.Important_Endopartner_product__c = mpdDetails.Important_Endopartner_product__c;
                    //需要做处理
                    //MaintenanceProductDataDetails
                    SS_Batch_Column_Mapping__c mpdMapping = SS_Batch_Column_Mapping__c.getValues('MaintenanceProductDataDetails');
                    Map<String,String> tempImportantRroduct = new Map<String,String>();
                    for (Integer i = 1; i <= FIELDMAX; i++) {
                        String lpadI = ('00' + i).right(3);
                        String fromColumn = 'From_Column_' + lpadI + '__c';
                        String apiStr = String.valueOf(mpdMapping.get(fromColumn));
                        if (String.isBlank(apiStr) == false) {
                            String ssColumn = 'SS_Column_' + lpadI + '__c';
                            String ssApiStr = String.valueOf(mpdMapping.get(ssColumn));
                            tempImportantRroduct.put(ssApiStr,apiStr); 
                        }
                    }
                    String keyOld;
                    String old_BF;
                    String old_ENT;
                    String old_ET;
                    String old_GI;
                    String old_GS;
                    String old_GYN;
                    String old_OTH;
                    String old_URO;
 
                    String keyNew;
                    String new_BF;
                    String new_ENT;
                    String new_ET;
                    String new_GI;
                    String new_GS;
                    String new_GYN;
                    String new_OTH;
                    String new_URO;
                    if(String.isNotBlank(mpdDetails.Key_product_147P_Old__c)){
                        keyOld = tempImportantRroduct.get(mpdDetails.Key_product_147P_Old__c);
                        if(String.isNotBlank(keyOld)){
                            old_BF = 'Important_Rroduct_'+keyOld+'BF__c';
                            old_ENT = 'Important_Rroduct_'+keyOld+'ENT__c';
                            old_ET = 'Important_Rroduct_'+keyOld+'ET__c';
                            old_GI = 'Important_Rroduct_'+keyOld+'GI__c';
                            old_GS = 'Important_Rroduct_'+keyOld+'GS__c';
                            old_GYN = 'Important_Rroduct_'+keyOld+'GYN__c';
                            old_OTH = 'Important_Rroduct_'+keyOld+'OTH__c';
                            old_URO = 'Important_Rroduct_'+keyOld+'URO__c';
                        }
                    }
                    if(String.isNotBlank(mpdDetails.Key_product_147P__c)){
                        keyNew = tempImportantRroduct.get(mpdDetails.Key_product_147P__c);
                        System.debug('====keyNew:'+keyNew);
                        new_BF = 'Important_Rroduct_'+keyNew+'BF__c';
                        new_ENT = 'Important_Rroduct_'+keyNew+'ENT__c';
                        new_ET = 'Important_Rroduct_'+keyNew+'ET__c';
                        new_GI = 'Important_Rroduct_'+keyNew+'GI__c';
                        new_GS = 'Important_Rroduct_'+keyNew+'GS__c';
                        new_GYN = 'Important_Rroduct_'+keyNew+'GYN__c';
                        new_OTH = 'Important_Rroduct_'+keyNew+'OTH__c';
                        new_URO = 'Important_Rroduct_'+keyNew+'URO__c';
                    }
                    if(String.isNotBlank(mpdDetails.Key_product_147P__c)){
                        if(mpdDetails.Key_product_147P_Old__c !=null&&!mpdDetails.Key_product_147P__c.equals(mpdDetails.Key_product_147P_Old__c)){
                            //old的false 新的true
                            if(String.isNotBlank(keyNew)){
                                product2.put(new_BF,true);product2.put(new_ENT,true);
                                product2.put(new_ET,true);product2.put(new_GI,true);
                                product2.put(new_GS,true);product2.put(new_GYN,true);
                                product2.put(new_OTH,true);product2.put(new_URO,true);
                            }
                            if(String.isNotBlank(keyOld)){
                                product2.put(old_BF,false);product2.put(old_ENT,false);
                                product2.put(old_ET,false);product2.put(old_GI,false);
                                product2.put(old_GS,false);product2.put(old_GYN,false);
                                product2.put(old_OTH,false);product2.put(old_URO,false);
                            }  
                        }else if(mpdDetails.Key_product_147P_Old__c == null){
                            //old的空只需要新的true
                            System.debug('====keyNew:'+keyNew);
                            if(String.isNotBlank(keyNew)){
                                product2.put(new_BF,true);product2.put(new_ENT,true);
                                product2.put(new_ET,true);product2.put(new_GI,true);
                                product2.put(new_GS,true);product2.put(new_GYN,true);
                                product2.put(new_OTH,true);product2.put(new_URO,true);
                            }
                        }   
                    }
                    else if(String.isNotBlank(mpdDetails.Key_product_147P_Old__c)){
                        //old的false 新的true
                        if(String.isNotBlank(keyOld)){
                            product2.put(old_BF,false);product2.put(old_ENT,false);
                            product2.put(old_ET,false);product2.put(old_GI,false);
                            product2.put(old_GS,false);product2.put(old_GYN,false);
                            product2.put(old_OTH,false);product2.put(old_URO,false);
                        }
                    }
 
                }
                if(mpdDetails.RecordType.DeveloperName.equals('GuranteeMain')){
                    product2.Id = mpdDetails.ProductsID__c;
                    if(mpdDetails.GuranteeType_Old__c !=null&&String.isNotBlank(mpdDetails.GuranteeType_Old__c)){
                        if(mpdDetails.GuranteeType__c !=null&&!mpdDetails.GuranteeType__c.equals(mpdDetails.GuranteeType_Old__c)){
                            if(mpdDetails.GuranteeType__c==null||mpdDetails.GuranteeType__c.equals('')){
                                product2.Extend_new_product_gurantee__c = false;//服务
                                product2.Extend_new_product_gurantee_MD__c = false;
 
                                product2.Entend_gurantee_period_MD__c = null;
                                product2.Extend_Gurantee_Start_MD__c = null;
                                product2.Extend_Gurantee_End_MD__c = null;
 
                                product2.Entend_gurantee_period__c = null;
                                product2.Extend_Gurantee_Start__c = null;
                                product2.Extend_Gurantee_End__c = null;
                            }else{
                                if(mpdDetails.GuranteeType__c.equals('市场')){
                                    product2.Extend_new_product_gurantee__c = false;//服务
                                    product2.Extend_new_product_gurantee_MD__c = true;
                                    product2.Entend_gurantee_period_MD__c = mpdDetails.Entend_gurantee_period__c;
                                    product2.Extend_Gurantee_Start_MD__c = mpdDetails.Gurantee_Start_Date__c;
                                    product2.Extend_Gurantee_End_MD__c = mpdDetails.Gurantee_End_Date__c;    
                                }
                                if(mpdDetails.GuranteeType__c.equals('服务')){
                                    product2.Extend_new_product_gurantee_MD__c = false;//市场
                                    product2.Extend_new_product_gurantee__c = true;
                                    product2.Entend_gurantee_period__c = mpdDetails.Entend_gurantee_period__c;
                                    product2.Extend_Gurantee_Start__c = mpdDetails.Gurantee_Start_Date__c;
                                    product2.Extend_Gurantee_End__c = mpdDetails.Gurantee_End_Date__c;
                                }
                            }   
                        }else{
                            if(mpdDetails.GuranteeType__c==null){
                                product2.Extend_new_product_gurantee__c = false;//服务
                                product2.Extend_new_product_gurantee_MD__c = false;
 
                                product2.Entend_gurantee_period_MD__c = null;
                                product2.Extend_Gurantee_Start_MD__c = null;
                                product2.Extend_Gurantee_End_MD__c = null;
 
                                product2.Entend_gurantee_period__c = null;
                                product2.Extend_Gurantee_Start__c = null;
                                product2.Extend_Gurantee_End__c = null;
                            }else{
                                if(mpdDetails.GuranteeType__c.equals('市场')){
                                    if(mpdDetails.Entend_gurantee_period__c != mpdDetails.Entend_gurantee_period_Old__c){product2.Entend_gurantee_period_MD__c = mpdDetails.Entend_gurantee_period__c;}
                                    if(mpdDetails.Gurantee_Start_Date__c !=mpdDetails.Gurantee_Start_Date_Old__c ){product2.Extend_Gurantee_Start_MD__c = mpdDetails.Gurantee_Start_Date__c;}
                                    if(mpdDetails.Gurantee_End_Date__c != mpdDetails.Gurantee_End_Date_Old__c){product2.Extend_Gurantee_End_MD__c = mpdDetails.Gurantee_End_Date__c; } 
                                }
                                if(mpdDetails.GuranteeType__c.equals('服务')){
                                    if(mpdDetails.Entend_gurantee_period__c != mpdDetails.Entend_gurantee_period_Old__c){product2.Entend_gurantee_period__c = mpdDetails.Entend_gurantee_period__c;}
                                    if(mpdDetails.Gurantee_Start_Date__c !=mpdDetails.Gurantee_Start_Date_Old__c ){product2.Extend_Gurantee_Start__c = mpdDetails.Gurantee_Start_Date__c;}
                                    if(mpdDetails.Gurantee_End_Date__c != mpdDetails.Gurantee_End_Date_Old__c){product2.Extend_Gurantee_End__c = mpdDetails.Gurantee_End_Date__c;}
                                }
                            }
                            
                        }
                    }else{
                        if(mpdDetails.GuranteeType__c!=null&&mpdDetails.GuranteeType__c.equals('市场')){
                            product2.Extend_new_product_gurantee__c = false;//服务
                            product2.Extend_new_product_gurantee_MD__c = true;
                            product2.Entend_gurantee_period_MD__c = mpdDetails.Entend_gurantee_period__c;
                            product2.Extend_Gurantee_Start_MD__c = mpdDetails.Gurantee_Start_Date__c;
                            product2.Extend_Gurantee_End_MD__c = mpdDetails.Gurantee_End_Date__c;  
                        }
                        if(mpdDetails.GuranteeType__c !=null&&mpdDetails.GuranteeType__c.equals('服务')){
                            product2.Extend_new_product_gurantee_MD__c = false;//市场
                            product2.Extend_new_product_gurantee__c = true;
                            product2.Entend_gurantee_period__c = mpdDetails.Entend_gurantee_period__c;
                            product2.Extend_Gurantee_Start__c = mpdDetails.Gurantee_Start_Date__c;
                            product2.Extend_Gurantee_End__c = mpdDetails.Gurantee_End_Date__c;
                        }
                    }
                    product2.CanNotCancelledGurantee__c = mpdDetails.CanNotCancelledGurantee__c;//20201012 ljh  CHAN-BTR67N add
                    //两个价格
                    //1.NoDiscount价格
                    //Date  dateFrom_Service = mpdDetails.Intra_Trade_Service_RMB_DateFrom__c;
                    Date  dateFrom_Service = Date.toDay();
                    Date  dateTo_Service =  Date.valueOf(System.Label.dateTo_Service);
                    if(mpdDetails.Intra_Trade_Service_RMB__c==null|| mpdDetails.Intra_Trade_Service_RMB__c.format()=='0'){
                        product2.Intra_Trade_Service_RMB_Date1__c =  null;
                        product2.Intra_Trade_Service_RMB_EndDate1__c = null;
                    }else{
                        product2.Intra_Trade_Service_RMB_Date1__c =  dateFrom_Service;
                        product2.Intra_Trade_Service_RMB_EndDate1__c = dateTo_Service;
                    }
                    product2.Intra_Trade_Service_RMB_1__c  = mpdDetails.Intra_Trade_Service_RMB__c;
                    //2.多年保修计提价格
                    //Date  dateFrom_Gurantee = mpdDetails.Intra_Trade_Gurantee_RMB_DateFrom__c;
                    Date  dateFrom_Gurantee = Date.toDay();
                    Date  dateTo_Gurantee =  Date.valueOf(System.Label.dateTo_Gurantee);
                    if(mpdDetails.Intra_Trade_Gurantee_RMB__c==null || mpdDetails.Intra_Trade_Gurantee_RMB__c.format()=='0'){
                        product2.Intra_Trade_Gurantee_RMB_Date1__c =  null;
                        product2.Intra_Trade_Gurantee_RMB_End_Date1__c = null;
                    }else{
                        product2.Intra_Trade_Gurantee_RMB_Date1__c =  dateFrom_Gurantee;
                        product2.Intra_Trade_Gurantee_RMB_End_Date1__c = dateTo_Gurantee;
                    }
                    product2.Intra_Trade_Gurantee_RMB_1__c  = mpdDetails.Intra_Trade_Gurantee_RMB__c;
                    product2.Maintenance_Price_Year__c = mpdDetails.Maintenance_Price_Year__c;
                    //2021/01/19 liying start
                    product2.Virtual_Contract__c  = mpdDetails.Virtual_Contract__c; 
                    //2021/01/19 liying end
                }
                if(mpdDetails.RecordType.DeveloperName.equals('GuranteePrice')){
                    product2.Id = mpdDetails.ProductsID__c;
                    //2021/01/19 liying start
                    product2.Virtual_Contract__c  = mpdDetails.Virtual_Contract__c; 
                    //2021/01/19 liying end
                    //两个价格
                    //1.NoDiscount价格
                    //Date  dateFrom_Service_G = mpdDetails.Intra_Trade_Service_RMB_DateFrom__c;
                    Date  dateFrom_Service_G = Date.toDay();
                    Date  dateTo_Service = Date.valueOf(System.Label.dateTo_Service);
                    if(mpdDetails.Intra_Trade_Service_RMB__c==null|| mpdDetails.Intra_Trade_Service_RMB__c.format()=='0'){
                        product2.Intra_Trade_Service_RMB_Date1__c =  null;
                        product2.Intra_Trade_Service_RMB_EndDate1__c = null;
                    }else{
                        product2.Intra_Trade_Service_RMB_Date1__c =  dateFrom_Service_G;
                        product2.Intra_Trade_Service_RMB_EndDate1__c = dateTo_Service;
                    }
                    product2.Intra_Trade_Service_RMB_1__c  = mpdDetails.Intra_Trade_Service_RMB__c;  
                    //2.多年保修计提价格
                    //Date  dateFrom_Gurantee_G = mpdDetails.Intra_Trade_Gurantee_RMB_DateFrom__c;
                    Date  dateFrom_Gurantee_G = Date.toDay();
                    Date dateTo_Gurantee = Date.valueOf(System.Label.dateTo_Gurantee);
                    if(mpdDetails.Intra_Trade_Gurantee_RMB__c==null || mpdDetails.Intra_Trade_Gurantee_RMB__c.format()=='0'){
                        product2.Intra_Trade_Gurantee_RMB_Date1__c =  null;
                        product2.Intra_Trade_Gurantee_RMB_End_Date1__c = null;
                    }else{
                        product2.Intra_Trade_Gurantee_RMB_Date1__c =  dateFrom_Gurantee_G;
                        product2.Intra_Trade_Gurantee_RMB_End_Date1__c = dateTo_Gurantee;
                    }
                    product2.Intra_Trade_Gurantee_RMB_1__c  = mpdDetails.Intra_Trade_Gurantee_RMB__c;
                    product2.Maintenance_Price_Year__c = mpdDetails.Maintenance_Price_Year__c;
                }
                if(mpdDetails.RecordType.DeveloperName.equals('RepairInfo')){
                    product2.Id = mpdDetails.ProductsID__c;
                    product2.RepairListPriceLevelA__c = mpdDetails.RepairListPriceLevelA__c;
                    product2.RepairListPriceLevelB__c = mpdDetails.RepairListPriceLevelB__c;
                    product2.RepairListPriceLevelC__c = mpdDetails.RepairListPriceLevelC__c;
                    product2.CanRepairAccsessary__c = mpdDetails.CanRepairAccsessary__c;
                    product2.Repair_Product_Code__c = mpdDetails.Repair_Product_Code__c;
                    product2.PartSupplyFinishDate__c = mpdDetails.PartSupplyFinishDate__c;
                    product2.Maintenance_Price_Year__c = mpdDetails.Maintenance_Price_Year__c;
                    //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210831 Star
                    product2.Can_Repair__c = mpdDetails.Can_Repair__c;
                    product2.Service_Category1__c = mpdDetails.Service_Category1__c;
                    product2.Service_Category2__c = mpdDetails.Service_Category2__c;
                    product2.Service_Category3__c = mpdDetails.Service_Category3__c;
                    product2.Service_Category4__c = mpdDetails.Service_Category4__c;
                    product2.Service_Category5__c = mpdDetails.Service_Category5__c;
                    product2.Service_Category6__c = mpdDetails.Service_Category6__c;
                    product2.Service_Category7__c = mpdDetails.Service_Category7__c;
                    //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210831 Star
 
                }
                if(mpdDetails.RecordType.DeveloperName.equals('EquipmentInfo')){
                    product2.Id = mpdDetails.ProductsID__c;
                    product2.Asset_loaner_category__c  = mpdDetails.Asset_loaner_category__c;
                    product2.Special_Model__c  = mpdDetails.Special_Model__c;
                    product2.Fixture_Model_No__c  = mpdDetails.Fixture_Model_No__c;
                    product2.Packing_list_Fixture__c = mpdDetails.Packing_list_Fixture__c;
                    product2.Default_Fixture_Arrival_Process__c = mpdDetails.Default_Fixture_Arrival_Process__c;
                    product2.Default_Fixture_Arrival_Product__c = mpdDetails.Default_Fixture_Arrival_Product__c;
                }
                //跟新标识
                product2.IsFromSAP__c = true;
                product2Update.add(product2); 
            }
 
            if(product2Update.size()>0 && product2Update !=null ){
                Database.SaveResult[] lsr = Database.update(product2Update, false);
                Boolean flag = true;
                for (Integer tIdx = 0; tIdx < lsr.size(); tIdx++) {
                    Database.SaveResult sr = lsr[tIdx];
                    System.debug('sr.isSuccess:' + sr.isSuccess());
                    if (!sr.isSuccess()) {
                        Database.Error emsg = sr.getErrors()[0];
                        flag = false;
                    }
                }
                if(flag){
                    update mpdDetailsStatus;
                }   
            }
            //-------------------------
            //发送邮件
            sendMailtoComplete(EmailMPD);          
        }
    }
 
    private static List<String> getEmail(String label){
        List<String> toEmailList = new List<String>();
        String query = 'select id , Email from user where id in (';
        query += label.replaceall('\'','\\\'') +')';
        system.debug('===test:'+query);
        List<user> tempUserList = Database.Query(query);
        for (user toUser : tempUserList) {
            if (!String.isBlank(toUser.Email)) {
                toEmailList.add(toUser.Email);
            }
        }
        return toEmailList;
    }
    private static boolean sendMailtoComplete(list<Maintenance_Product_Data__c> EmailMPD){
        boolean rs = true;
        List<Messaging.SingleEmailMessage> sendMails = new List<Messaging.SingleEmailMessage>();
        Messaging.SingleEmailMessage messageNEW = new Messaging.SingleEmailMessage();
        String ImportantProduct = Schema.SObjectType.Maintenance_Product_Data__c.getRecordTypeInfosByDeveloperName().get('ImportantProduct').getRecordTypeId();
        String GuranteeMain = Schema.SObjectType.Maintenance_Product_Data__c.getRecordTypeInfosByDeveloperName().get('GuranteeMain').getRecordTypeId();
        String GuranteePrice = Schema.SObjectType.Maintenance_Product_Data__c.getRecordTypeInfosByDeveloperName().get('GuranteePrice').getRecordTypeId();
        String RepairInfo = Schema.SObjectType.Maintenance_Product_Data__c.getRecordTypeInfosByDeveloperName().get('RepairInfo').getRecordTypeId();
        String EquipmentInfo = Schema.SObjectType.Maintenance_Product_Data__c.getRecordTypeInfosByDeveloperName().get('EquipmentInfo').getRecordTypeId();
        String  profileId = UserInfo.getProfileId();//18位
 
        for (Maintenance_Product_Data__c mpdEmail:EmailMPD) {
            String title = '';
            String body = '';
            List<String> toMailsList = new List<String>();
            String ApplyPersion = '\''+mpdEmail.ApplyPersion__c+'\'';//申请人
            String BuchangApprovalRecommend = '\''+mpdEmail.BuchangApprovalRecommend__c+'\'';//部长推广
            //重点产品  重点产品+基础2+基础3
            if(mpdEmail.RecordTypeId.equals(ImportantProduct)){
                String label01 = ApplyPersion+','+BuchangApprovalRecommend+','+System.Label.Maintenance_ImportantProduct;
                label01 +=','+System.Label.Maintenance_base02+','+System.Label.Maintenance_base03;
                toMailsList.addall(getEmail(label01));
            }
            //多年保主数据 基础1+基础3 
            if(mpdEmail.RecordTypeId.equals(GuranteeMain)){
                String label02 = ApplyPersion+','+BuchangApprovalRecommend+','+System.Label.Maintenance_base01+','+System.Label.Maintenance_base03;
                //String label02_2 = ApplyPersion+','+BuchangApprovalRecommend+','+System.Label.Maintenance_base01+','+System.Label.Maintenance_base03;
                //if(UserInfo.getUserId().equals('00510000002ZJQTAA4')){
                    toMailsList.addall(getEmail(label02)); 
                //}
                //if(UserInfo.getUserId().equals('00510000007cXA9AAM')){
                    //toMailsList.addall(getEmail(label02_2)); 
                //}
            }
            //多年保价格  基础1+基础3+多年保价格
            if(mpdEmail.RecordTypeId.equals(GuranteePrice)){
                String label03 = ApplyPersion+','+BuchangApprovalRecommend+','+System.Label.Maintenance_GuranteePrice;
                //label03 +=','+System.Label.Maintenance_base01+','+System.Label.Maintenance_base02;
                label03 +=','+System.Label.Maintenance_base01+','+System.Label.Maintenance_base03;
                toMailsList.addall(getEmail(label03));
            }
            //修理 (申请人、部长)简档
            if(mpdEmail.RecordTypeId.equals(RepairInfo)){
                String label04_1 = System.Label.Maintenance_RepairInfo01;
                String label04_2 = System.Label.Maintenance_RepairInfo02;
                if(label04_1.contains(mpdEmail.ApplyPersion__c)){
                    label04_1 = ApplyPersion+','+BuchangApprovalRecommend+','+System.Label.Maintenance_RepairInfo01;
                    toMailsList.addall(getEmail(label04_1));
                }
                if(label04_2.contains(mpdEmail.ApplyPersion__c)){
                    label04_2 = ApplyPersion+','+BuchangApprovalRecommend+','+System.Label.Maintenance_RepairInfo02;
                    toMailsList.addall(getEmail(label04_2));
                }
            }
            //备品 (申请人、部长)
            if(mpdEmail.RecordTypeId.equals(EquipmentInfo)){
                String label05 = ApplyPersion+','+BuchangApprovalRecommend+','+System.Label.Maintenance_EquipmentInfo;
                toMailsList.addall(getEmail(label05));
            }
            title =  '产品主数据维护审批申请已批准';
            body += '以下产品主数据维护审批申请已批准,请了解。';
            body += '<br/>';
            body += '<br/>';
            body += '产品维护单链接:';
            body += '<a href="' + URL.getSalesforceBaseUrl().toExternalForm() + '/apex/MaintenanceProductData?id=' + mpdEmail.id + '">'
                    + URL.getSalesforceBaseUrl().toExternalForm() + '/apex/MaintenanceProductData?id=' + mpdEmail.id + '</a><br/>';
            body += '<br/>';
            body += '<br/>';
            body += '谢谢!';
 
            messageNEW = new Messaging.SingleEmailMessage();
            messageNEW.setSubject(title);
            messageNEW.setHtmlBody(body);
            messageNEW.setCharset('UTF-8');
            messageNEW.setToAddresses(toMailsList);
 
            List<String> toCcMailsList = new List<String>();
            toCcMailsList.add('olympus@prec-tech.com');
            messageNEW.setCcAddresses(toCcMailsList); 
 
            sendMails.add(messageNEW);
        }
        system.debug('sendMailtoComplete:' + sendMails);
        if (sendMails.size() > 0) {
            //
            Messaging.Email[] allMails = new Messaging.Email[]{};
            for(Integer j = 0; j < sendMails.size(); j++) {
                allMails.add(sendMails.get(j));
            }
            system.debug('allMails:'+allMails);
            //Messaging.sendEmail(allMails);
            Messaging.SendEmailResult[] results = Messaging.sendEmail(allMails);
            for (Integer i = 0; i < results.size(); i++) {
                if (results[i].success == false) {
                    system.debug('=====send mail error:' + results[i].errors[0].message);
                    rs = false;
                }
            }
        }
        return rs;
    }
}