liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
public without sharing class AssetModifyBelongsChangeHandler extends Oly_TriggerHandler {
    //CHAN-B7J4SE
    private Map<Id, AssetModifyBelongsChange__c> newMap;
    private Map<Id, AssetModifyBelongsChange__c> oldMap;
    private List<AssetModifyBelongsChange__c> newList;
    private List<AssetModifyBelongsChange__c> oldList;
    private List<String> idList;
    private List<Asset> assetList;
 
    public AssetModifyBelongsChangeHandler(){
        this.newMap = (Map<Id, AssetModifyBelongsChange__c>) Trigger.newMap;
        this.oldMap = (Map<Id, AssetModifyBelongsChange__c>) Trigger.oldMap;
        this.newList = (List<AssetModifyBelongsChange__c>) Trigger.new;
        this.oldList = (List<AssetModifyBelongsChange__c>) Trigger.old;
        this.idList = new List<String>();
        this.assetList = new List<Asset>();
    }
 
    protected override void beforeUpdate(){
        updateambChange_Account();
    }
    private void updateambChange_Account() {
 
        for(AssetModifyBelongsChange__c ambc :newList){
            if(oldMap.get(ambc.Id).get('Change_status__c') != ambc.Change_status__c && ambc.Change_status__c == '批准'){
                idList.add(ambc.Id);
            }
        }
        // 虚拟维修合同
        Map <string, Maintenance_Contract__c> v_MCMap = new  map <string, Maintenance_Contract__c>();
        // 虚拟维修合同对应询价名称
        Map <string, string> v_MCOppMap = new  map <string, string>();
        // 需要插入的虚拟维修合同保有设备
        List <Maintenance_Contract_Asset__c> v_MCdetinList = new  List<Maintenance_Contract_Asset__c>();
        // 需要更新的虚拟维修合同保有设备
        List <Maintenance_Contract_Asset__c> v_MCdetUpList = new  List<Maintenance_Contract_Asset__c>();
        // 新的变更合同对应的新保有设备 中间匹配用
        Map <string, List<Maintenance_Contract_Asset__c>> v_MCdetnewMap = new  map <string, List<Maintenance_Contract_Asset__c>>();
 
        //需要作成两个map add by rentx LJPH-C493WW  
        // 合同询价编码   维修合同/保有设备集合
        Map<String,List<Maintenance_Contract_Asset__c>> OppAssMap = new Map<String,List<Maintenance_Contract_Asset__c>>();
        // 目标客户  合同询价编码set
        Map <string, Set<Maintenance_Contract__c>> vMOppMap = new  map <string, Set<Maintenance_Contract__c>>();
        // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 start
        Map<String, List<Id>> rpMcaNameMap = new Map<String, List<Id>>(); 
        Map<String, String> ChangeRepAssMap = new Map<String, String>();
        // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 end
 
        //add by rentx LJPH-C493WW  
        //LJPH-C493WW  fy    【委托】 【多年保合同】多年保设备转移造成的合同问题  start
        Map<String,Maintenance_Contract__c> MainCMap=new Map<String,Maintenance_Contract__c>();
        //LJPH-C493WW  fy    【委托】 【多年保合同】多年保设备转移造成的合同问题  end
        if(idList != null && idList.size() > 0){
            
            List<AssetModifyBelongsChangeDetail__c> ambcDetailList ;
            ambcDetailList = [select Id,Asset__c,
                                    ChangeAndChangeDetail__r.AccountField__c,
                                    ChangeAndChangeDetail__r.rawAccount__c,
                                    ChangeAndChangeDetail__r.Name,
                                    ChangeAndChangeDetail__r.ChangeReason__c
                                from AssetModifyBelongsChangeDetail__c
                                where ChangeAndChangeDetail__c In :idList];
 
            // SWAG-BCL2YW ---XHL---20190528---UpdateStart                    
            List<String> accountFieldList = new List<String>();
            // 保有设备IDlist,变更时,新建维修合同用
            Set<Id> assetidList = new Set<Id>();
            //获取目标客户的Id                    
            for (AssetModifyBelongsChangeDetail__c ambcDetail :ambcDetailList){
 
                accountFieldList.add(ambcDetail.ChangeAndChangeDetail__r.AccountField__c);
                // 获取变更的保有设备ID
                assetidList.add(ambcDetail.Asset__c);
            }
            Map<Id,Maintenance_Contract_Asset__c> maAssetMap = new Map<Id, Maintenance_Contract_Asset__c>();
            
            // Schema.DescribeSobjectResult d = Maintenance_Contract_Asset__c.sObjectType.getDescribe();
            // Map<String, Schema.SObjectField> fieldMap = d.fields.getMap();
 
            if(assetidList.size() > 0){
                for (Maintenance_Contract_Asset__c Mca :[select id,startDateGurantee__c,
                    startDateGurantee_Text__c,endDateGurantee__c,Asset__c,
                    endDateGurantee_Text__c,Return_Flag__c,ChangeAcc_flag__c,
                    Asset__r.guarantee_period__c,Asset__r.Extend_Gurantee_DateTo__c,
                    DeliveryDate__c,No_discount__c,V_MC_Name__c,Asset_SerialNumber__c,
                    //LJPH-C493WW  fy    【委托】 【多年保合同】多年保设备转移造成的合同问题  start
                    Maintenance_Contract__c,
                    Maintenance_Contract__r.Add_Number__c,
                    //LJPH-C493WW  fy    【委托】 【多年保合同】多年保设备转移造成的合同问题  end
                    Asset__r.Proviston_period__c,Maintenance_Contract__r.Management_Code__c
                    from Maintenance_Contract_Asset__c
                    where Asset__c in : assetidList
                     and Asset_Status__c = true
                    and Maintenance_Contract__r.RecordType.DeveloperName = 'VM_Contract']) {
                    maAssetMap.put(Mca.Asset__c,Mca); 
                    //add by rentx LJPH-C493WW 20210715 
                    if (!OppAssMap.containsKey(Mca.Maintenance_Contract__r.Management_Code__c)) {
                        OppAssMap.put(mca.Maintenance_Contract__r.Management_Code__c,new List<Maintenance_Contract_Asset__c>());
                    }
                    OppAssMap.get(Mca.Maintenance_Contract__r.Management_Code__c).add(Mca);
                    // Oppvmidmap.put(mca.Maintenance_Contract__r.Management_Code__c, mac.Maintenance_Contract__c);
                    //add by rentx LJPH-C493WW 20210715 
 
                }
            }
            System.debug('97::: OppAssMap '+OppAssMap);
            if (accountFieldList.size() > 0){
 
                Map<Id, Account> accMap = new Map<Id, Account>();
                List<Account> accs = ControllerUtil.selectAccountForTrigger(accountFieldList);
                
                for(Account acc : accs) {
 
                    accMap.put(acc.Id, acc);
                }
                // 所有客户对应的保有设备
                Map<Id,List<Maintenance_Contract_Asset__c>> changeAccMap = new Map<Id,List<Maintenance_Contract_Asset__c>>();
 
                for (AssetModifyBelongsChangeDetail__c ambcDetail :ambcDetailList){
 
                    String accountIdStr = ambcDetail.ChangeAndChangeDetail__r.AccountField__c;
                    System.debug('accountIdStr:'+ ambcDetail.ChangeAndChangeDetail__r.AccountField__c);
                    Account acc = accMap.get(accountIdStr);
                    Asset asset = new Asset();
                    //判断目标客户的类型是否是"战略科室ET"
                    if (acc != null && acc.RecordTypeId == System.Label.ET) {
 
                        asset.Department_Class__c = accountIdStr;
                        asset.Hospital__c = acc.ParentId;
                    } else {
 
                        asset.Department_Class__c = acc.ParentId;
                        asset.Hospital__c = acc.Parent.ParentId;
                    }
 
                    asset.Id = ambcDetail.Asset__c;
                    asset.AccountId = accountIdStr;
                    asset.rawAccounts__c = ambcDetail.ChangeAndChangeDetail__r.rawAccount__c;
                    asset.Nameno__c = ambcDetail.ChangeAndChangeDetail__c;
                    asset.ChangeReasons__c = ambcDetail.ChangeAndChangeDetail__r.ChangeReason__c;
                    assetList.add(asset);
                    // 变更虚拟维修合同信息整理 start
                    if (maAssetMap.containsKey(asset.Id)) {
                        // if (changeAccMap.containsKey(accountIdStr)) {
                        //     changeAccMap.get(accountIdStr).add(maAssetMap.get(asset.Id));
                        // }else{
                        //     List<Maintenance_Contract_Asset__c> mcatempList = new List<Maintenance_Contract_Asset__c>();
                        //     mcatempList.add(maAssetMap.get(asset.Id));
                        //     changeAccMap.put(accountIdStr,mcatempList);
                        // }
                        // if(!v_MCOppMap.containsKey(accountIdStr)){
                        //     v_MCOppMap.put(accountIdStr, maAssetMap.get(asset.Id).Maintenance_Contract__r.Management_Code__c);
                        // }
 
                        //update by rentx LJPH-C493WW  start
                        //LJPH-C493WW  fy    【委托】 【多年保合同】多年保设备转移造成的合同问题  start
                        if (!vMOppMap.containsKey(accountIdStr)) {
                            vMOppMap.put(accountIdStr, new Set<Maintenance_Contract__c>());
                            // vMOppMap.put(accountIdStr, new Set<Maintenance_Contract_Asset__c>());
                        }
                        Maintenance_Contract__c maintenancecontc=new Maintenance_Contract__c();
                        maintenancecontc.Id = maAssetMap.get(asset.Id).Maintenance_Contract__c;
                        maintenancecontc.Add_Number__c = maAssetMap.get(asset.Id).Maintenance_Contract__r.Add_Number__c;
                        maintenancecontc.Management_Code__c = maAssetMap.get(asset.Id).Maintenance_Contract__r.Management_Code__c;
                        vMOppMap.get(accountIdStr).add(maintenancecontc);
                        // vMOppMap.get(accountIdStr).add(maAssetMap.get(asset.Id));
                        //LJPH-C493WW  fy    【委托】 【多年保合同】多年保设备转移造成的合同问题  end
                        //update by rentx LJPH-C493WW  end
                        System.debug('maAssetMap.get(asset.Id)=====:'+maAssetMap.get(asset.Id));
                        // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 start
                        if(ambcDetail.ChangeAndChangeDetail__r.ChangeReason__c == '总院分院间调用'){
                            ChangeRepAssMap.put(maAssetMap.get(asset.Id).Maintenance_Contract__r.Management_Code__c, ambcDetail.ChangeAndChangeDetail__r.ChangeReason__c);
                        }
                        // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 end
                    }
                }
                System.debug('154::: vMOppMap '+vMOppMap);
 
                vMOppMap.remove(null);
 
                System.debug('156::: vMOppMap '+vMOppMap);
 
                //
                if (vMOppMap.size() > 0) {
                    // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 start
                    List<String> mcaIdList = new List<String>();
                    Map<String, List<String>> RepairIdMap = new Map<String, List<String>>();
                    for (String accountId : vMOppMap.keySet()) {
                        System.debug('accountId1:' + accountId);
                        Set<Maintenance_Contract__c> mcSet = vMOppMap.get(accountId);
                        for(Maintenance_Contract__c mc : mcSet){
                            System.debug('ChangeRepAssMap1:' + ChangeRepAssMap);
                            System.debug('mc.Management_Code__c1:' + mc.Management_Code__c);
                            if(ChangeRepAssMap.containsKey(mc.Management_Code__c)){
                                mcaIdList.add(mc.Id);
                            }
                        }
                    }
                    System.debug('mcaIdList1:' + mcaIdList);
                    if(mcaIdList.size() > 0){
                        List<Repair__c> RepairList = [select Id, VM_Maintenance_Contract__c from Repair__c where VM_Maintenance_Contract__c    IN :mcaIdList];
                        if(RepairList.size() > 0){
                            for(Repair__c rp : RepairList){
                                List<String> newRepairIdList = new List<String>();
                                if(RepairIdMap.containsKey(rp.VM_Maintenance_Contract__c)){
                                    newRepairIdList = RepairIdMap.get(rp.VM_Maintenance_Contract__c);
                                    newRepairIdList.add(rp.Id);
                                    RepairIdMap.put(rp.VM_Maintenance_Contract__c, newRepairIdList);
                                } else{
                                    newRepairIdList.add(rp.Id);
                                    RepairIdMap.put(rp.VM_Maintenance_Contract__c, newRepairIdList);
                                }
                            }
                        }
                    }
                    // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 end
                    for (String str : vMOppMap.keySet()) {
                        //update by rentx LJPH-C493WW  start
                        Set<Maintenance_Contract__c> vset = vMOppMap.get(str);
                        //Set<Maintenance_Contract_Asset__c> vset = vMOppMap.get(str);
                        //LJPH-C493WW  fy    【委托】 【多年保合同】多年保设备转移造成的合同问题  start
                        for (Maintenance_Contract__c mcodes : vset) {
                        //for (Maintenance_Contract_Asset__c mcodes : vset) {
                            Maintenance_Contract__c vmc = new Maintenance_Contract__c();
                            vmc.Name = accMap.get(str).Parent.Parent.Name + '多年保修';
                            vmc.Status__c = '契約';
                            system.debug('mcodes==='+mcodes);
                            // List<String> mcodestrList= mcodes.split('@');
                            // String mcode =mcodes.Maintenance_Contract__r.Management_Code__c;
                            // String mcid=(String)mcodes.Maintenance_Contract__c;
                            String mcode =mcodes.Management_Code__c;
                            String mcid=(String)mcodes.Id;
                            // String addNumber =mcodestrList[1];
                            Map<String,Decimal> addnumMap = new Map<String,Decimal>();
                            if (String.isNotBlank(mcode) && mcode.substring(mcode.length()-3,mcode.length()-2) == '-' ) {
                                String startstr = mcode.substring(0,mcode.length()-2);
                                Integer count = Integer.valueOf(mcode.substring(mcode.length()-2,mcode.length()));
                                
                                vmc.Management_CodeVM__c = startstr + String.valueOf(count +1).leftPad(2,'0');
                                // mcodes.Maintenance_Contract__r.Add_Number__c=count +1;
                                // addnumMap.put(mcid,mcodes.Maintenance_Contract__r.Add_Number__c);
                                mcodes.Add_Number__c=count +1;
                                addnumMap.put(mcid,mcodes.Add_Number__c);
                            }else{
                                    system.debug('mcodes.Maintenance_Contract__r.Add_Number__c'+mcodes.Add_Number__c);
                                    if(mcodes.Add_Number__c==null||mcodes.Add_Number__c==0){
                                    // if(mcodes.Maintenance_Contract__r.Add_Number__c==null||mcodes.Maintenance_Contract__r.Add_Number__c==0){
                                        vmc.Management_CodeVM__c = mcode + '-01';
                                        // mcodes.Maintenance_Contract__r.Add_Number__c=1;
                                        // addnumMap.put(mcid,mcodes.Maintenance_Contract__r.Add_Number__c);
                                        mcodes.Add_Number__c=1;
                                        addnumMap.put(mcid,mcodes.Add_Number__c);
                                    }else{
                                        // Decimal addnum = mcodes.Maintenance_Contract__r.Add_Number__c+1;
                                        // if(mcodes.Maintenance_Contract__r.Add_Number__c<=9){
                                        Decimal addnum = mcodes.Add_Number__c+1;
                                        if(mcodes.Add_Number__c<=9){
                                            vmc.Management_CodeVM__c = mcode + '-0'+addnum;
                                        }else{
                                            vmc.Management_CodeVM__c = mcode + '-'+addnum;
                                        }
                                        addnumMap.put(mcid,addnum);
                                    }
                                    system.debug('vmc.Management_CodeVM__c ======'+vmc.Management_CodeVM__c);
                                    system.debug('addnumMap ======'+addnumMap);
                            }
                            // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 start
                            if(ChangeRepAssMap.containsKey(mcodes.Management_Code__c) && RepairIdMap.containsKey(mcodes.Id)){
                                rpMcaNameMap.put(vmc.Management_CodeVM__c, RepairIdMap.get(mcodes.Id));
                            }
                            System.debug('rpMcaNameMap====' + rpMcaNameMap);
                            // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 end
                            //LJPH-C493WW  fy    【委托】 【多年保合同】多年保设备转移造成的合同问题  end
                            vmc.Department__c = accMap.get(str).Id;
                            vmc.Department_Class__c = accMap.get(str).ParentId;
                            vmc.Hospital__c = accMap.get(str).Parent.ParentId;
                            vmc.RecordTypeId = System.Label.VM_ContractType;
                            v_MCMap.put(mcode,vmc);
 
                            List<Maintenance_Contract_Asset__c> mcatempList = new List<Maintenance_Contract_Asset__c>();
                            for (Maintenance_Contract_Asset__c mac : OppAssMap.get(mcode)) {
                                Maintenance_Contract_Asset__c mactemp = mac.clone();
                                //mactemp.V_MC_Name__c = null;
                                Maintenance_Contract__c mctrobject= new Maintenance_Contract__c();
                                mactemp.Maintenance_Contract__c = null;
                                mactemp.Extend_Gurantee_DateTo__c = mac.Asset__r.Extend_Gurantee_DateTo__c;
                                mactemp.guarantee_period__c = mac.Asset__r.guarantee_period__c;
                                mactemp.Proviston_period__c = mac.Asset__r.Proviston_period__c;
                                mcatempList.add(mactemp);
                                mac.ChangeAcc_flag__c = true;
                                mac.endDateGurantee_changeAcc__c = Date.today();
                                mac.Extend_Gurantee_DateTo__c = mac.Asset__r.Extend_Gurantee_DateTo__c;
                                mac.guarantee_period__c = mac.Asset__r.guarantee_period__c;
                                mac.Proviston_period__c = mac.Asset__r.Proviston_period__c;
                                //LJPH-C493WW  fy    【委托】 【多年保合同】多年保设备转移造成的合同问题  start
                                mctrobject.Id=mac.Maintenance_Contract__c;
                                mctrobject.Add_Number__c=addnumMap.get(mac.Maintenance_Contract__c);
                                MainCMap.put(mctrobject.Id,mctrobject);
                                system.debug('mctrobject.Id===='+mctrobject.Id);
                                system.debug('mctrobject.Add_Number__c======='+mctrobject.Add_Number__c);
                                //LJPH-C493WW  fy    【委托】 【多年保合同】多年保设备转移造成的合同问题  end
                                // 给总院分院间调用的维修合同保有设备的总院分院间变更标识赋true thh 20220426 start
                                if(ChangeRepAssMap.containsKey(mac.Maintenance_Contract__r.Management_Code__c)){
                                    mac.General_Branch_Changeflag__c = true;
                                }
                                // 给总院分院间调用的维修合同保有设备的总院分院间变更标识赋true thh 20220426 end 
                                v_MCdetUpList.add(mac);
                            }
                            v_MCdetnewMap.put(mcode, mcatempList);
 
                        }
                        /*
                        Maintenance_Contract__c vmc = new Maintenance_Contract__c();
                        vmc.Name = accMap.get(str).Parent.Parent.Name + '多年保修';
                        vmc.Status__c = '契約';
                        String oppNostr =  v_MCOppMap.get(str);
 
                        if (String.isNotBlank(oppNostr) && oppNostr.substring(oppNostr.length()-3,oppNostr.length()-2) == '-' ) {
                            String startstr = oppNostr.substring(0,oppNostr.length()-2);
                            Integer count = Integer.valueOf(oppNostr.substring(oppNostr.length()-2,oppNostr.length()));
                            
                            vmc.Management_CodeVM__c = startstr + String.valueOf(count +1).leftPad(2,'0');
                        }else{
                            vmc.Management_CodeVM__c = oppNostr + '-01';
                        }
                        //vmc.Management_CodeVM__c = 'DN-' + v_MCOppMap.get(str) + '-01';
                        vmc.Department__c = accMap.get(str).Id;
                        vmc.Department_Class__c = accMap.get(str).ParentId;
                        vmc.Hospital__c = accMap.get(str).Parent.ParentId;
                        //vmc.Opportunity__c = oppMap.get(InquiryNoStr).Id;
                        vmc.RecordTypeId = System.Label.VM_ContractType;
                        v_MCMap.put(str,vmc);
                        // 更新现有维修合同保有设备
                        // 新规新的维修合同保有设备
                        List<Maintenance_Contract_Asset__c> mcatempList = new List<Maintenance_Contract_Asset__c>();
                        for (Maintenance_Contract_Asset__c mac : changeAccMap.get(str)) {
                            Maintenance_Contract_Asset__c mactemp = mac.clone();
                            //mactemp.V_MC_Name__c = null;
                            mactemp.Maintenance_Contract__c = null;
                            mactemp.Extend_Gurantee_DateTo__c = mac.Asset__r.Extend_Gurantee_DateTo__c;
                            mactemp.guarantee_period__c = mac.Asset__r.guarantee_period__c;
                            mactemp.Proviston_period__c = mac.Asset__r.Proviston_period__c;
                            mcatempList.add(mactemp);
                            mac.ChangeAcc_flag__c = true;
                            mac.endDateGurantee_changeAcc__c = Date.today();
                            mac.Extend_Gurantee_DateTo__c = mac.Asset__r.Extend_Gurantee_DateTo__c;
                            mac.guarantee_period__c = mac.Asset__r.guarantee_period__c;
                            mac.Proviston_period__c = mac.Asset__r.Proviston_period__c;
                            v_MCdetUpList.add(mac);
                        }
                        v_MCdetnewMap.put(str, mcatempList);
                        */
                        //update by rentx LJPH-C493WW  end
 
 
                    }
                    
                }
                // 变更虚拟维修合同信息整理 end
            }
            // SWAG-BCL2YW ---XHL---20190528---UpdateEnd
            
        }
 
        System.debug('256::: v_MCMap '+v_MCMap);
 
        if (v_MCMap.size() > 0) {
            insert v_MCMap.values();
            for (String str : v_MCdetnewMap.keySet()) {
                for (Maintenance_Contract_Asset__c mca : v_MCdetnewMap.get(str)) {
                    //mca.V_MC_Name__c = v_MCMap.get(str).Name;
                    mca.Maintenance_Contract__c = v_MCMap.get(str).Id;
                    mca.startDateGurantee_changgeAcc__c = Date.today();
                    mca.ChangeAcc_flag__c = true;
                    // 给总院分院间调用的维修合同保有设备的总院分院间变更标识赋true thh 20220426 start
                    if(ChangeRepAssMap.containsKey(mca.Maintenance_Contract__r.Management_Code__c)){
                        mca.General_Branch_Changeflag__c = true;
                    }
                    // 给总院分院间调用的维修合同保有设备的总院分院间变更标识赋true thh 20220426 end 
                    v_MCdetinList.add(mca);
                }
            }
            // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 Start
            List<Repair__c> updateRpList = new List<Repair__c>();
            Map<Id, Repair__c> rpMap = new Map<Id, Repair__c>();
            List<Repair__c> rpList = new List<Repair__c>();
            List<String> rpIdList = new List<String>();
            for(String str : v_MCMap.keySet()){
                Maintenance_Contract__c mc = v_MCMap.get(str);
                rpIdList = rpMcaNameMap.get(mc.Management_CodeVM__c);
            }
            if(rpIdList != null && rpIdList.size() > 0){
                rpList = [select Id, VM_Maintenance_Contract__c from Repair__c where Id IN : rpIdList];
            }
            if(rpList.size() > 0){
                for(Repair__c rp : rpList){
                    rpMap.put(rp.Id, rp);
                }
            }
            for(String str : v_MCMap.keySet()){
                Maintenance_Contract__c mc = v_MCMap.get(str);
                if(rpMcaNameMap.containsKey(mc.Management_CodeVM__c)){
                    List<String> newrpIdList = rpMcaNameMap.get(mc.Management_CodeVM__c);
                    if(newrpIdList != null && newrpIdList.size() > 0){
                        for(Id rpId : newrpIdList){
                            Repair__c rp = rpMap.get(rpId);
                            rp.VM_Maintenance_Contract__c = mc.Id;
                            updateRpList.add(rp);
                        }
                    }
                }
            }
            if(updateRpList.size() > 0){
                update updateRpList;
            }
            // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 end
            // 插入变更新合同的维修合同保有设备
            insert v_MCdetinList;
            // 更新现有合同的维修合同保有设备
            update v_MCdetUpList;
            
        }
        if(assetList != null && assetList.size() > 0 ){
            update assetList;
        }
        //LJPH-C493WW  fy    【委托】 【多年保合同】多年保设备转移造成的合同问题  start
        if(MainCMap != null && MainCMap.size() > 0){
            List<Maintenance_Contract__c> mainList=MainCMap.values();
            update mainList;
        }
        //LJPH-C493WW  fy    【委托】 【多年保合同】多年保设备转移造成的合同问题  end
    }
 
    //zzm 20240222 DB202401391766 【保有设备变更】新增cl6 cl5 cl4  质量负责人信息start
    protected override void afterInsert(){
        updateProgressManager();
    }
    private void updateProgressManager() {
        List<String> ambsIds = new List<String>();
        // List<User> users = new List<User>();
        // Map<String,AssetModifyBelongsChange__c> ambcMaps = new Map<String,AssetModifyBelongsChange__c>();
        Map<Id,User> userAmbcMap = new Map<Id,User>();
        for(AssetModifyBelongsChange__c ambc :newList){
            ambsIds.add(ambc.Id);
            // if(newMap.get(ambc.Id).get('OwnerId') != null){
            //     User tmpUser = (User)oldMap.get(ambc.Id).get('OwnerId');
            //     userAmbcMap.put(ambc.Id,tmpUser);
                
            // }
        }
        List<AssetModifyBelongsChange__c> ambsList = new List<AssetModifyBelongsChange__c>([
            select id,CreatedById,SalesdepartmentNew__c,SalesdepartmentOld__c,
            CreatedBy.SalesManager__c,
            SubmitOwner__r.Salesdepartment__c,
            CreatedBy.BuchangApprovalManagerSales__c,
            CreatedBy.ZongjianApprovalManager__c 
            from AssetModifyBelongsChange__c where id in :ambsIds
        ]);
       
        for(AssetModifyBelongsChange__c ambc :ambsList) {
            ambc.CL6__c = ambc.CreatedBy.SalesManager__c;
            ambc.CL5__c = ambc.CreatedBy.BuchangApprovalManagerSales__c;
            ambc.CL4__c = ambc.CreatedBy.ZongjianApprovalManager__c;
            if(ambc.SubmitOwner__r.Salesdepartment__c =='1.华北'  
                || ambc.SubmitOwner__r.Salesdepartment__c =='2.东北'
                || ambc.SubmitOwner__r.Salesdepartment__c =='3.西北') {
                ambc.quality__c = System.Label.qualityBF;
            } else if(ambc.SubmitOwner__r.Salesdepartment__c =='4.西南' || ambc.SubmitOwner__r.Salesdepartment__c =='6.华南') {
                ambc.quality__c = System.Label.qualityHN;
            } else if(ambc.SubmitOwner__r.Salesdepartment__c =='5.华东') {
                ambc.quality__c = System.Label.qualityHD;
            }
        }
        update ambsList;
         
        
        
    }
    //zzm 20240222 DB202401391766 【保有设备变更】新增cl6 cl5 cl4 cl3 质量负责人信息 end
  
}