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
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
/**
 * 客户同步
 */
public without sharing class AccountTrigger {
    // 已处理数据、AccountのTriggerの場合、使わない、WFがいろいろあるので。
    //public static Map<Id, Account> accountMap = new Map<Id, Account>();
    
    // 同步到 Account2__c (after insert, after update, before delete) 
    public static void syncAccount2(List<Account> newList, Map<Id, Account> newMap, List<Account> oldList, Map<Id, Account> oldMap) {
        List<Account2__c> acc2List = new List<Account2__c>();
        List<Account2__c> afterInsertAcc2List = new List<Account2__c>();
        if (Trigger.isInsert || Trigger.isUpdate) {
            system.debug('=====afterUpsertToAccount2 start');
            for (Account acc : newList) {
                if (Trigger.isInsert || (Trigger.isUpdate
                        && (acc.ParentId != oldMap.get(acc.Id).ParentId
                                || acc.Name != oldMap.get(acc.Id).Name
                        )
                )) {
                    Account2__c acc2 = new Account2__c(Account_OrgId__c = ('' + acc.Id).substring(0, 15), Account_Org__c = acc.Id,
                        Name = acc.Name, RecordType_DeveloperName__c = acc.RecordType_DeveloperName__c
                    );
                    acc2List.add(acc2);
                    if (Trigger.isInsert) {
                        afterInsertAcc2List.add(acc2);
                    }
                    // 1階層目、2階層目の場合、
                    if (acc.ParentId == null
                            || (acc.ParentId != null && acc.Parent_Parent__c == null)
                    ) {
                        acc2.Account__c = acc.Id;           // 主従項目
                    }
                    // 3階層目の場合
                    else {
                        acc2.Account__c = acc.ParentId;     // 主従項目
                    }
                    // TODO acc2のIdをAccountに書き戻す!必要がないかも!!確認中
                }
            }
            if (acc2List.size() > 0) {
                upsert acc2List Account_OrgId__c;
            }
            if (afterInsertAcc2List.size() > 0) {
                List<Account> afterInsertAcc2UpdateAccountList = new List<Account>();
                Set<Id> afterInsertAcc2UpdateAccountIdSet = new Set<Id>();
                for (Account2__c acc2 : afterInsertAcc2List) {
//                    // ありえないはず
//                    if (afterInsertAcc2UpdateAccountIdSet.contains(acc2.Account_OrgId__c)) {
//                        continue;
//                    } else {
                        Account acc = new Account(Id = acc2.Account_OrgId__c, Account2__c = acc2.Id);
//                    }
                    afterInsertAcc2UpdateAccountList.add(acc);
                }
                update afterInsertAcc2UpdateAccountList;
            }
        } else if (Trigger.isDelete) {
            system.debug('=====beforeDeleteToAccount2 start');
            List<Id> accIds = new List<Id>();
            for (Account acc : oldList) {
                accIds.add(acc.Id);
            }
            if (accIds.size() > 0) {
                List<Account2__c> delList = [select Id, Account_Org__c from Account2__c where Account_Org__c in : accIds];
                if (delList.size() > 0) {
                    delete delList;
                }
            }
        }
    }
    // 2023年8月9日注释 删除对象MB_Account__c 解除引用
    // 同步到 MB_Account__c
    // public static void syncMBAccount(List<Account> newList, Map<Id, Account> newMap, List<Account> oldList, Map<Id, Account> oldMap) {
    //     // MB_客户插入用
    //     List<MB_Account__c> ins_mb_accounts = new List<MB_Account__c>();
    //     // MB_客户删除用
    //     List<Account> del_accounts = new List<Account>();
    //     List<MB_Account__c> del_mb_accounts = new List<MB_Account__c>();
        
    //     if (Trigger.isInsert) {
    //         for (Account acc : newList) {
    //             MB_Account__c mbacc = new MB_Account__c(
    //                   Account__c = acc.Id
    //                 , CurrencyIsoCode = acc.CurrencyIsoCode
    //         // CIC 125725 start
    //         //                    , OwnerId = acc.OwnerId
    //                 , Opp_OCM_text__c = acc.HP_146POCM_Category_From_Dept__c
    //                 , State_Text__c = acc.Province_formula__c
    //                 , Opportunity_Category_text__c = acc.Department_Class_Name__c
    //         // CIC 125725 end
    //             );
    //             ins_mb_accounts.add(mbacc);
    //         }
    //     }
    //     // CIC 130175 start
    //     else if (Trigger.isUpdate) {
    //         // NFM001 のところ、before insertと updateにて、OCM分类 と 省Text を更新しているはず
    //         // 診療科の場合、ここは各レコードのischanged 見るだけ にします。
    //         List<Account> accDpts = new List<Account>();
    //         List<Account> accAlls = new List<Account>();
    //         for (Account nr : newList) {
    //             system.debug('aaaaaa1:'+nr.RecordTypeId);
    //             system.debug('aaaaaa2:'+oldMap.get(nr.Id).RecordTypeId);
    //             system.debug('aaaaaa3:'+nr.OCM_Category__c);
    //             system.debug('aaaaaa4:'+oldMap.get(nr.Id).OCM_Category__c);
    //             system.debug('aaaaaa5:'+nr.State_Text__c);
    //             system.debug('aaaaaa6:'+oldMap.get(nr.Id).State_Text__c);
    //             if (nr.RecordTypeId !=  oldMap.get(nr.Id).RecordTypeId
    //                     || nr.OCM_Category__c !=  oldMap.get(nr.Id).OCM_Category__c
    //                     || nr.State_Text__c !=  oldMap.get(nr.Id).State_Text__c
    //             ) {
    //                 system.debug('xxxxxxxxxxxxxxx');
    //                 if (String.isBlank(nr.Parent_Parent__c) == false) {
    //                     accDpts.add(nr);
    //                     accAlls.add(nr);
    //                 } else {
    //                     accAlls.add(nr);
    //                 }
    //             }
    //         }
    //         Map<Id, Account> accDptMap = new Map<Id, Account>();
    //         if (accDpts.size() > 0) {
    //             system.debug('1111111111111');
    //             String[] needSendTypes = new String[] {'診療科 その他', '診療科 呼吸科', '診療科 婦人科', '診療科 普外科', '診療科 泌尿科', '診療科 消化科', '診療科 耳鼻喉科'};
    //             //  LD 20200807 优化account record type ID 读取方法 Start
 
    //             // 原逻辑
    //             // List<RecordType> rects = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN :needSendTypes];
                
    //             // for (RecordType rect : rects) {
    //             //  needSendRectMap.put(rect.Id, rect.Name);
    //             // }
 
    //             // 新逻辑
    //             String [] RecordTypeId = new String[] {System.Label.Department_OTH , System.Label.Department_BF , System.Label.Department_GYN , System.Label.Department_GS , System.Label.Department_URO , System.Label.Department_GI , System.Label.Department_ENT };
    //             system.debug('2222222222');
    //             Map<String, String> needSendRectMap = new Map<String, String>();
    //             needSendRectMap.put(RecordTypeId[0], needSendTypes[0]);
    //             needSendRectMap.put(RecordTypeId[1], needSendTypes[1]);
    //             needSendRectMap.put(RecordTypeId[2], needSendTypes[2]);
    //             needSendRectMap.put(RecordTypeId[3], needSendTypes[3]);
    //             needSendRectMap.put(RecordTypeId[4], needSendTypes[4]);
    //             needSendRectMap.put(RecordTypeId[5], needSendTypes[5]);
    //             needSendRectMap.put(RecordTypeId[6], needSendTypes[6]);
 
    //             //  LD 20200807 优化account record type ID 读取方法 End
    //             for (Account a : accDpts) {
    //                 if (needSendRectMap.get(a.RecordTypeId) == null) {
    //                     continue;
    //                 }
    //                 system.debug('333333333');
    //                 accDptMap.put(a.Id, a);
    //             }
    //         }
    //         // MBこどもを更新
    //         // 20230718 Lightning删除对象 LHJ Start
    //         //ControllerUtil.updMBChildFromDpt(accDptMap);
            
    //         //if (accAlls.size() > 0) {
    //         //    ControllerUtil.updMBAccountList(accAlls);
    //         //    system.debug('4444444444444');
    //         //}
    //         // 20230718 Lightning删除对象 LHJ End
    //     }
    //     // CIC 130175 end
    //     else if (Trigger.isDelete) {
    //         for (Account acc : oldList) {
    //             del_accounts.add(acc);
    //         }
    //     }
    //     // 20230718 Lightning删除对象 LHJ Start
    //     //if (del_accounts.size() > 0) {
    //     //    ControllerUtil.delMBAccountList(del_accounts);
    //     //}
    //     //if (ins_mb_accounts.size() > 0) {
    //     //    ControllerUtil.insMBAccountList(ins_mb_accounts);
    //     //}
    //     // 20230718 Lightning删除对象 LHJ End
    // }
 
    ////CHAN-ADE32V 检查战略科室中的科室名是否重复 by wei
    public static void checkDepartment(List<Account> newList, Map<Id, Account> newMap, List<Account> oldList, Map<Id, Account> oldMap) {
        System.debug('=============> start');
 
        Map<String, Account> dpartmentMap = new Map<String, Account>();
        Map<String, String> dpartmentMapDB = new Map<String, String>();
        Map<String, String> classMap = new Map<String, String>();
        List<String> accountDep = new List<String>();
 
        for (Account rd : newList) {
            Account old = oldMap == null ? new Account() : oldMap.get(rd.Id);
            if (rd.Department_Name__c != old.Department_Name__c){
                if (classMap.containsKey(rd.Department_Class__c)){
                    Account temp = dpartmentMap.get(rd.Department_Class__c + rd.Department_Name__c);
                    if (temp != null) {
                        //if (temp.Department_Name__c == rd.Department_Name__c) {
                        //    temp.addError('科室录入重复');
                        //} else {
                            //accountDep.add(rd.Department_Class__c);
                        //    dpartmentMap.put(rd.Department_Class__c + rd.Department_Name__c,rd);    
                        //}
                        temp.addError('科室录入重复');
                    } else {
                        dpartmentMap.put(rd.Department_Class__c + rd.Department_Name__c,rd);
                    }
                } else {
                    classMap.put(rd.Department_Class__c,rd.Department_Name__c);
                    accountDep.add(rd.Department_Class__c);
                    dpartmentMap.put(rd.Department_Class__c + rd.Department_Name__c,rd);
                } 
            }
        } 
        System.debug('classMap============〉' +  classMap);
         System.debug('accountDep============〉' +  accountDep);
          System.debug('dpartmentMap============〉' +  dpartmentMap);
 
        if (accountDep.size() > 0) {
            List<Account>  dpartmentChk = [select Id,Department_Class__c,Department_Name__c from Account where Department_Class__c in : accountDep];
 
            for (Account chk : dpartmentChk) {
 
                dpartmentMapDB.put(chk.Department_Class__c + chk.Department_Name__c , chk.Id);
           
            }
            System.debug('dpartmentMapDB============〉' +  dpartmentMapDB);
            for (String tmpStr : dpartmentMap.keySet()) {
                if (dpartmentMapDB.containsKey(tmpStr)){
                    if (Trigger.isInsert) {
                        Account tmpInsert = dpartmentMap.get(tmpStr);
                        tmpInsert.addError('科室已经存在');
                    } else if (Trigger.isUpdate){
                        Account tmpUpdate = dpartmentMap.get(tmpStr);
                        if (String.valueOf(tmpUpdate.Id) != String.valueOf(dpartmentMapDB.get(tmpStr))) {
                            tmpUpdate.addError('科室已经存在');
                        } else {
                            continue;
                        }
                    }
                } else {
                    continue;
                }
            }
        }   
        System.debug('=============> end');   
    }
    
 
    public static void changeOwnerId(List<Account> newList, Map<Id, Account> newMap, List<Account> oldList, Map<Id, Account> oldMap) {
        System.debug('===========> start');
        List<Account> accList = new List<Account>();
        
        if (Trigger.isUpdate) {
            Boolean flag = false;
            for (Account a : newList) {
                if(Trigger.isUpdate && a.OwnerId != oldMap.get(a.Id).OwnerId){
                    flag = true;                                            //取得所有战略科室所有人改变的战略科室id
                }
            }
            if(flag == false){
                return;
            }
            String[] departmentTypes = new String[] {'戦略科室分類 その他' , '戦略科室分類 呼吸科', '戦略科室分類 婦人科', '戦略科室分類 普外科', '戦略科室分類 泌尿科', '戦略科室分類 消化科', '戦略科室分類 耳鼻喉科','戦略科室分類ET'};
            //  LD 20200807 优化account record type ID 读取方法 Start
 
            // 原逻辑
            // List<RecordType> rects = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN :departmentTypes];
            
            // for (RecordType rect : rects) {
            //  rectMap.put(rect.Id, rect.Name);
            // }
 
            // 新逻辑
            String[] StrategicDepartmentId = new String[] {System.Label.Department_Class_OTH , System.Label.Department_Class_BF , System.Label.Department_Class_GYN , System.Label.Department_Class_GS , System.Label.Department_Class_URO , System.Label.Department_Class_GI , System.Label.Department_Class_ENT , System.Label.Department_Class_ET};
            Map<String, String> rectMap = new Map<String, String>();
            rectMap.put(StrategicDepartmentId[0], departmentTypes[0]);
            rectMap.put(StrategicDepartmentId[1], departmentTypes[1]);
            rectMap.put(StrategicDepartmentId[2], departmentTypes[2]);
            rectMap.put(StrategicDepartmentId[3], departmentTypes[3]);
            rectMap.put(StrategicDepartmentId[4], departmentTypes[4]);
            rectMap.put(StrategicDepartmentId[5], departmentTypes[5]);
            rectMap.put(StrategicDepartmentId[6], departmentTypes[6]);
            rectMap.put(StrategicDepartmentId[7], departmentTypes[7]);
 
            //  LD 20200807 优化account record type ID 读取方法 End
 
            List<String> dcIds = new List<String>();   
            //List<String> contactIds = new List<String>();                
            for (Account a : newList) {
                if (rectMap.get(a.RecordTypeId) == null) {
                    continue;
                }
                if(Trigger.isUpdate && a.OwnerId != oldMap.get(a.Id).OwnerId){
                    dcIds.add(a.Id);                                            //取得所有战略科室所有人改变的战略科室id
                }
 
            }
 
            
            if(dcIds.size() > 0){
                List<String> upIds = new List<String>();
                List<Account> accDepList = [select Id, OwnerId,ParentId from Account where ParentId in :dcIds];  //取出所有的战略科室所有人改变的科室信息
                
                Map<String, Account> accoppMap = new Map<String, Account>();
                for (Account a : accDepList) {
                    if(!accoppMap.containsKey(a.ParentId)){
                    upIds.add(a.ParentId); 
                    }
                    accoppMap.put(a.ParentId, a);
                }
 
                Map<id, Account> accMap = new Map<id, Account>();
                if(upIds.size() > 0){
                    //所有人改变的战略科室
                    List<Account> acckList = [select id,OwnerId from Account where id in :upIds];
                    for(Account acc : acckList){
                        accMap.put(acc.id, acc);
                    }
                }
 
                List<Account> accUpdateList = new List<Account>();
                for (Account a : accDepList) {
                    Account accz = new Account();
                    accz = accMap.get(a.ParentId);
                    a.OwnerId = accz.OwnerId;
                    accUpdateList.add(a);
                }
 
                //取战略科室所有人改变的客户人员信息
                List<String> conIds = new List<String>();
                List<Contact> ConList = [select id, Name, OwnerId, Strategic_dept_Class__c from Contact where Strategic_dept_Class__c in :dcIds];
                Map<String,Contact> contaMap = new Map<String,Contact>();
                for(Contact c :ConList){
                    if(!contaMap.containsKey(c.Strategic_dept_Class__c)){
                        conIds.add(c.Strategic_dept_Class__c);
                    }
                    contaMap.put(c.Strategic_dept_Class__c, c);
                }
 
                Map<id, Account> conMap = new Map<id, Account>();
                if(conIds.size() > 0){
                    //所有人改变的战略科室
                    List<Account> acckconList = [select id,OwnerId from Account where id in :conIds];
                    for(Account acc : acckconList){
                        conMap.put(acc.id, acc);
                    }
                }
 
                List<Contact> conUpdateList = new List<Contact>();
                for (Contact c : ConList) {
                    Account accc = new Account();
                    accc = conMap.get(c.Strategic_dept_Class__c);
 
                    c.OwnerId = accc.OwnerId;
                    conUpdateList.add(c);
                }
                update accUpdateList;
                update conUpdateList;
 
            }
 
        }
        System.debug('===========> end');
    }
 
    public static void setDepartmentOwner(List<Account> newList, Map<Id, Account> newMap, List<Account> oldList, Map<Id, Account> oldMap) {
        Boolean flag = false;
        for (Account a : newList) {
            if (a.OwnerId != oldMap.get(a.Id).OwnerId){
                flag = true;
            }
        }
        if (flag == false){
            return;
        }
 
        String[] departmentTypes = new String[] {'戦略科室分類 その他', '戦略科室分類 呼吸科', '戦略科室分類 婦人科', '戦略科室分類 普外科', '戦略科室分類 泌尿科', '戦略科室分類 消化科', '戦略科室分類 耳鼻喉科','戦略科室分類ET'};
        //  LD 20200807 优化account record type ID 读取方法 Start
 
        // 原逻辑
        // List<RecordType> rects = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN :departmentTypes];
 
        // for (RecordType rect : rects) {
        //  rectMap.put(rect.Id, rect.Name);
        // }
 
        // 新逻辑
        String[] StrategicDepartmentId = new String[] {System.Label.Department_Class_OTH , System.Label.Department_Class_BF , System.Label.Department_Class_GYN , System.Label.Department_Class_GS , System.Label.Department_Class_URO , System.Label.Department_Class_GI , System.Label.Department_Class_ENT , System.Label.Department_Class_ET};
        Map<String, String> rectMap = new Map<String, String>();
        rectMap.put(StrategicDepartmentId[0], departmentTypes[0]);
        rectMap.put(StrategicDepartmentId[1], departmentTypes[1]);
        rectMap.put(StrategicDepartmentId[2], departmentTypes[2]);
        rectMap.put(StrategicDepartmentId[3], departmentTypes[3]);
        rectMap.put(StrategicDepartmentId[4], departmentTypes[4]);
        rectMap.put(StrategicDepartmentId[5], departmentTypes[5]);
        rectMap.put(StrategicDepartmentId[6], departmentTypes[6]);
        rectMap.put(StrategicDepartmentId[7], departmentTypes[7]);
        //  LD 20200807 优化account record type ID 读取方法 End
 
        List<String> dcIds = new List<String>();
        for (Account a : newList) {
            if (rectMap.get(a.RecordTypeId) == null) {
                continue;
            }
            if(a.OwnerId != oldMap.get(a.Id).OwnerId){
                dcIds.add(a.Id);
            }
 
        }
 
        Map<String, Account> updMap = new Map<String, Account>();
        if(dcIds.size() > 0){
            //停用:不再从战略科室获取担当人信息
            //for (String dcId : dcIds) {
            //    String hpid = newMap.get(dcId).ParentId;
            //    if (updMap.containsKey(hpid) == true) {
            //        Account acc = updMap.get(hpid);
            //        String rt = rectMap.get(newMap.get(dcId).RecordTypeId);
            //        String owner = newMap.get(dcId).OwnerId;
            //        if (rt == '戦略科室分類 消化科') {
            //            acc.GI_Main__c = owner;
            //        } else if (rt == '戦略科室分類 呼吸科') {
            //            acc.BF_owner__c = owner;
            //        } else if (rt == '戦略科室分類 普外科') {
            //            acc.SP_Main__c = owner;
            //        } else if (rt == '戦略科室分類 耳鼻喉科') {
            //            acc.ENT_owner_ID__c = owner;
            //        } else if (rt == '戦略科室分類 泌尿科') {
            //            acc.URO_owner_ID__c = owner;
            //        } else if (rt == '戦略科室分類 婦人科') {
            //            acc.GYN_owner__c = owner;
            //        } else if (rt == '戦略科室分類ET') {
            //            acc.ET_owner__c = owner;
            //        }
            //    } else {
            //        Account acc = new Account(Id = hpid);
            //        String rt = rectMap.get(newMap.get(dcId).RecordTypeId);
            //        String owner = newMap.get(dcId).OwnerId;
            //        if (rt == '戦略科室分類 消化科') {
            //            acc.GI_Main__c = owner;
            //        } else if (rt == '戦略科室分類 呼吸科') {
            //            acc.BF_owner__c = owner;
            //        } else if (rt == '戦略科室分類 普外科') {
            //            acc.SP_Main__c = owner;
            //        } else if (rt == '戦略科室分類 耳鼻喉科') {
            //            acc.ENT_owner_ID__c = owner;
            //        } else if (rt == '戦略科室分類 泌尿科') {
            //            acc.URO_owner_ID__c = owner;
            //        } else if (rt == '戦略科室分類 婦人科') {
            //            acc.GYN_owner__c = owner;
            //        } else if (rt == '戦略科室分類ET') {
            //            acc.ET_owner__c = owner;
            //        }
            //        updMap.put(hpid, acc);
            //    }
            //}
        }
 
        if (updMap.values() != null && updMap.values().size() > 0) {
            update updMap.values();
        }
    }
 
    // FSE-GI主担当 / FSE-SP主担当 变更时更新 修理
    public static void fseChangeToUpdRepair(List<Account> newList, Map<Id, Account> newMap, List<Account> oldList, Map<Id, Account> oldMap) {
        List<String> accIdList = new List<String> ();
        List<Repair__c> repList = new List<Repair__c> ();
 
        for (Account acc : newList) {
            if (acc.RecordTypeId == System.label.Hospita && (oldMap.get(acc.Id).FSE_GI_Main_Leader__c != acc.FSE_GI_Main_Leader__c || oldMap.get(acc.Id).FSE_SP_Main_Leader__c != acc.FSE_SP_Main_Leader__c)) {
                accIdList.add(acc.Id);
            }
        }
 
        if (accIdList.size() > 0) {
            repList = [select Id, Hospital__c, Account__r.Parent.RecordTypeId, FSE_Work_Location__c from Repair__c where Hospital__c in : accIdList];
            if (repList.size() > 0) {
                List<Repair__c> updRepair = new List<Repair__c> ();
                Map <Id, Account> workLocationList = new Map <Id, Account> ([select Id, Name, FSE_GI_Main_Leader__r.Work_Location__c, FSE_SP_Main_Leader__r.Work_Location__c from Account Where Id in : accIdList]);
                for (Repair__c re : repList) {
                    if (((re.Account__r.Parent.RecordTypeId == System.label.gastroenterology || 
                        re.Account__r.Parent.RecordTypeId == System.label.pneumology || 
                        re.Account__r.Parent.RecordTypeId == System.label.ET) && 
                      oldMap.get(re.Hospital__c).FSE_GI_Main_Leader__c != newMap.get(re.Hospital__c).FSE_GI_Main_Leader__c &&
                      workLocationList.get(re.Hospital__c).FSE_GI_Main_Leader__r.Work_Location__c != re.FSE_Work_Location__c) ||
                      (( re.Account__r.Parent.RecordTypeId == System.label.general_surgery || 
                        re.Account__r.Parent.RecordTypeId == System.label.urology_department || 
                        re.Account__r.Parent.RecordTypeId == System.Label.otolaryngology_department ||
                        re.Account__r.Parent.RecordTypeId == System.Label.gynecology_department || 
                        re.Account__r.Parent.RecordTypeId == System.label.Other) &&
                      oldMap.get(re.Hospital__c).FSE_SP_Main_Leader__c != newMap.get(re.Hospital__c).FSE_SP_Main_Leader__c &&
                      workLocationList.get(re.Hospital__c).FSE_SP_Main_Leader__r.Work_Location__c != re.FSE_Work_Location__c)) {
                        updRepair.add(new Repair__c(Id = re.Id));
                    }
                }
 
                if (updRepair.size() > 0) {
                    update updRepair;
                }
            }
        }
    }
 
    //20231107 lt DB202310352742 政府等级(汇总)字段copy创建一个文本字段 start
    //Grade_Formula__c 公式拷文本,公式发生变化,对应的逻辑就要发生变化
 
    /*
    IF(CONTAINS(TEXT(Grade__c),'一级') , 'A.一级',
    IF(CONTAINS(TEXT(Grade__c),'二级') , 'B.二级',
    IF(CONTAINS(TEXT(Grade__c),'三级') , 'C.三级',
    ''
    )
    )
    )
    **/
    public static void GradeFormulaText(List<Account> newList, Map<Id, Account> newMap, List<Account> oldList, Map<Id, Account> oldMap) {
        List<Account> updaccList = new List<Account>();
        List<String> accIdsList = new List<String>();
        if (Trigger.isInsert || Trigger.isUpdate) {
            for (Account acc : newList){
                if(Trigger.isInsert || 
                   (Trigger.isUpdate && acc.Grade__c != oldMap.get(acc.Id).Grade__c )
                ){
                    accIdsList.add(acc.Id);
                }
            }
 
            if(accIdsList.Size() > 0 ){
                List<Account> saccList = [select id,Grade__c,Grade_Formula_PBI__c from Account where id in :accIdsList];
                
                if(saccList.Size() > 0){
                    for(Account sacc : saccList){
                        Account updacc = new Account();
                        updacc.Id = sacc.Id;
                        if(sacc.Grade__c != null){
                            if(sacc.Grade__c.contains('一级')){
                                updacc.Grade_Formula_PBI__c = 'A.一级';
                            }else if(sacc.Grade__c.contains('二级')){
                                updacc.Grade_Formula_PBI__c = 'B.二级';
                            }else if(sacc.Grade__c.contains('三级')){
                                updacc.Grade_Formula_PBI__c = 'C.三级';
                            }
                        }
            
                        if(String.ISNotBLANK(updacc.Grade_Formula_PBI__c)){
                            updaccList.add(updacc);
                        }
                    }
                }
            }
 
            if(updaccList.Size() > 0){
                update updaccList;
            }
        }
    }
 
    //20231107 lt DB202310352742 政府等级(汇总)字段copy创建一个文本字段 end
 
    
}