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
public without sharing class AccountDelayApplyHandler extends Oly_TriggerHandler {
    private Map<Id, Account_Delay_Apply__c> newMap;
    private Map<Id, Account_Delay_Apply__c> oldMap;
    private List<Account_Delay_Apply__c> newList;
    private List<Account_Delay_Apply__c> oldList;
 
    public AccountDelayApplyHandler() {
        this.newMap = (Map<Id, Account_Delay_Apply__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Account_Delay_Apply__c>) Trigger.oldMap;
        this.newList = (List<Account_Delay_Apply__c>) Trigger.new;
        this.oldList = (List<Account_Delay_Apply__c>) Trigger.old;
    }
    
    protected override void beforeInsert(){
        // OpenWindow();
 
        OpenWindow1();
    }
    protected override void afterInsert(){
        MultiOption();
    }
    protected override void beforeUpdate(){
        MultiOption();
        setColumn();
        // OpenWindow();
        CustomizePageJudge();
    }
    protected override void afterUpdate(){
        MultiOption();
        setAccColumn();
    }
 
   //多选项数据统计
    private void MultiOption(){
        //创建参数 key:Sobject对象 value:发生改变的选项列表字段集合 (哪个选项列表发生变化就把哪个当做参数)
        Map<SObject,List<String>> data = new Map<SObject,List<String>>();
        for (Account_Delay_Apply__c ada : newList) {
            List<String> columsList = new List<String>();
            Account_Delay_Apply__c oldada = null;
            if (Trigger.isUpdate) {
                oldada = oldMap.get(ada.Id);
            }
 
            //变更理由(可多选)
            if (oldada == null || oldada.ChangeReason__c != ada.ChangeReason__c) {
                columsList.add('ChangeReason__c');
            }
 
            data.put(ada, columsList);
        }
 
        SplitOptionListUtil util = new SplitOptionListUtil();
        util.makeObject(data);
 
    }
 
    // //营业窗口
    // private void OpenWindow(){
    //     Map<String, OCM_Management_Province__c> mpMap = new Map<String, OCM_Management_Province__c>();
    //     Set<String> accOCSMset = new Set<String>();
 
    //     for(Account_Delay_Apply__c ada1 : newList){
    //         Account_Delay_Apply__c oldada1 = null;
    //         if(Trigger.isUpdate){
 
    //             oldada1 = oldMap.get(ada1.Id);
 
    //             if(ada1.OCM_man_province_HP__c != oldada1.OCM_man_province_HP__c){
    //                accOCSMset.add(ada1.OCM_man_province_HP__c);
    //             }
    //        }
    //         if(Trigger.isInsert){
    //             accOCSMset.add(ada1.OCM_man_province_HP__c);
    //         }
 
    //     }
        
    //    if(null !=accOCSMset){
    //         List<OCM_Management_Province__c> mpList = 
    //                 [select id, Name, Window1__c from OCM_Management_Province__c where Name =: accOCSMset];
 
    //         for (OCM_Management_Province__c mp : mpList) {
    //             mpMap.put(mp.Name, mp);
    //         }
    //     }
 
    //     for(Account_Delay_Apply__c ada2 : newList){
    //         if(Trigger.isInsert || Trigger.isUpdate){
    //             if(null!=mpMap && mpMap.containsKey(ada2.OCM_man_province_HP__c)){
    //                 OCM_Management_Province__c omp = mpMap.get(ada2.OCM_man_province_HP__c);
    //                  if (omp != null) {
    //                      ada2.OpenWindow__c = omp.Window1__c;
    //                  } 
    //              }
    //         }
            
    //     }
        
    // }
 
    //营业窗口
    private void OpenWindow1(){
        Map<String, OCM_Management_Province__c> mpMap = new Map<String, OCM_Management_Province__c>();
        List<OCM_Management_Province__c> mpList = new List<OCM_Management_Province__c>();
        Set<String> accId = new Set<String>();
        Set<String> accOCSMset = new Set<String>();
 
        for(Account_Delay_Apply__c ada1 : newList){
            Account_Delay_Apply__c oldada1 = null;
            if(Trigger.isInsert){
                accId.add(ada1.Hospital__c);
            }
            
        }
 
        if(accId.size() > 0){
            List<Account> accList = [SELECT Id, Name, OCM_man_province_HP__c
                                     FROM Account 
                                     WHERE id in :accId];
            for(Account acc : accList){
                accOCSMset.add(acc.OCM_man_province_HP__c);
            }
            
        }
      
        if(null !=accOCSMset){
             mpList = [select id, Name, Window1__c from OCM_Management_Province__c where Name =: accOCSMset];
        }
 
        for(Account_Delay_Apply__c ada2 : newList){
            for(OCM_Management_Province__c omp : mpList){
                if(Trigger.isInsert){
                    ada2.OpenWindow__c = omp.Window1__c;
                }
            }
        }
        
    }
 
    //提交时设置old字段的值
    private void setColumn(){
 
        List<String> accIds = new List<String>();
        List<Account_Delay_Apply__c> alist = new List<Account_Delay_Apply__c>();
        
        for (Account_Delay_Apply__c ada :newList ) {
            //判断更新前后的状态(是否有效)是否不同 若不同,则判断更新后的状态是否为'审批中'
            if ((ada.Is_Active__c != oldMap.get(ada.Id).Is_Active__c) && ada.Is_Active__c == '申请中' ) {
                alist.add(ada);
                accIds.add(ada.Hospital__c);
            }
        }
        if (accIds.size() == 0) {
            return;
        }else{
                Map<String,Account> accMap = new Map<String,Account>();
                List<Account> statusList = [SELECT Id, Name, Website, Site,
                                                   Is_Active__c, Feature__c, Attribute_Type__c, Speciality_Type__c, 
                                                   Abbreviation__c, Street__c, City_Master__c, Field3_companyname__c, 
                                                   State_Master__c, Grade__c, Address__c, Alias_Name2__c,
                                                //    Including_departments__c,
                                                // 2023-12-14 WYL add start
                                                Sparepartsdeliveryaddress1__c,Sparepartsdeliveryaddress2__c,Sparepartsdeliveryaddress3__c,Sparepartsdeliveryaddress4__c,Sparepartsdeliveryaddress5__c,
                                                Sparepartsdeliveryaddress6__c,Sparepartsdeliveryaddress7__c,Sparepartsdeliveryaddress8__c,Sparepartsdeliveryaddress9__c,Sparepartsdeliveryaddress10__c,
                                                // 2023-12-14 WYL add end
                                                   Town__c, Postal_Code__c
                                            FROM Account 
                                            WHERE id in :accIds];
 
                for (Account acc :statusList ) {
                    accMap.put(acc.Id, acc);
                }
 
                for (Account_Delay_Apply__c ada : alist) {
                    ada.Feature_old__c = accMap.get(ada.Hospital__c).Feature__c;
                    ada.Attribute_Type_old__c = accMap.get(ada.Hospital__c).Attribute_Type__c;
                    ada.Speciality_Type_old__c = accMap.get(ada.Hospital__c).Speciality_Type__c;
                    ada.Abbreviation_old__c = accMap.get(ada.Hospital__c).Abbreviation__c;
                    ada.Street_old__c = accMap.get(ada.Hospital__c).Street__c;
                    ada.HospitalName_old__c = accMap.get(ada.Hospital__c).Name;
                    ada.City_Master_old__c = accMap.get(ada.Hospital__c).City_Master__c;
                    ada.Field3_companyname_old__c = accMap.get(ada.Hospital__c).Field3_companyname__c;
                    ada.State_Master_old__c = accMap.get(ada.Hospital__c).State_Master__c;
                    ada.Grade_old__c = accMap.get(ada.Hospital__c).Grade__c;
                    //ada.Address_Together_old__c = accMap.get(ada.Hospital__c).Address__c;
                    ada.Website_old__c = accMap.get(ada.Hospital__c).Website;
                    ada.Site_old__c = accMap.get(ada.Hospital__c).Site;
                    ada.Alias_Name2_old__c = accMap.get(ada.Hospital__c).Alias_Name2__c;
                    ada.Town_old__c = accMap.get(ada.Hospital__c).Town__c;
                    ada.Postal_Code_old__c = accMap.get(ada.Hospital__c).Postal_Code__c;
                    // ada.Including_departments_old__c = accMap.get(ada.Hospital__c).Including_departments__c;
                    // 2023-12-14 WYL add start
                    ada.Sparepartsdeliveryaddress1_old__c = accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress1__c;
                    ada.Sparepartsdeliveryaddress2_old__c = accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress2__c;
                    ada.Sparepartsdeliveryaddress3_old__c = accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress3__c;
                    ada.Sparepartsdeliveryaddress4_old__c = accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress4__c;
                    ada.Sparepartsdeliveryaddress5_old__c = accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress5__c;
                    ada.Sparepartsdeliveryaddress6_old__c = accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress6__c;
                    ada.Sparepartsdeliveryaddress7_old__c = accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress7__c;
                    ada.Sparepartsdeliveryaddress8_old__c = accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress8__c;
                    ada.Sparepartsdeliveryaddress9_old__c = accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress9__c;
                    ada.Sparepartsdeliveryaddress10_old__c = accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress10__c;
                    // 2023-12-14 WYL add end
                }
 
                // for (Account acc : statusList) {
                //     acc.Status = '申请中';
                // }
                system.debug('更新客戶1');
            update statusList;   
        }
 
    }
 
    //变更有效时反写客户
    private void setAccColumn(){
 
        List<String> accIds = new List<String>();
        List<String> adaIds = new List<String>();
        // List<Account_Delay_Apply__c> alist = new List<Account_Delay_Apply__c>();
        // List<Account> acclist = new List<Account>();
        List<Account> updacclist = new List<Account>();
        
        for (Account_Delay_Apply__c ada :newList ) {
            //判断更新前后的状态(是否有效)是否不同 若不同,则判断更新后的状态是否为'审批中'
            if ((ada.Is_Active__c != oldMap.get(ada.Id).Is_Active__c) && ada.Is_Active__c == '审批通过' ) {
                // alist.add(ada);
                adaIds.add(ada.Id);
                accIds.add(ada.Hospital__c);
            }
        }
        if (accIds.size() == 0) {
            return;
        }else{
                Map<String,Account> accMap = new Map<String,Account>();
                List<Account> statusList = [SELECT Id, Name, Website, Site,
                                                   Is_Active__c, Feature__c, Attribute_Type__c, Speciality_Type__c, 
                                                   Abbreviation__c, Street__c, City_Master__c, Field3_companyname__c, 
                                                   State_Master__c, Grade__c, Address__c, Alias_Name2__c,
                                                   //2023-10-15 新增 WYL     DB202306499625 start
                                                //    Including_departments__c,
                                                //    BF_owner__c,
                                                //    GYN_owner__c,
                                                //    SP_Main__c,
                                                //    URO_owner_ID__c,
                                                //    GI_Main__c,
                                                //    ENT_owner_ID__c,
                                                   //2023-10-15 新增 WYL     DB202306499625 end
                                                // 2023-12-14 WYL add start
                                                Sparepartsdeliveryaddress1__c,Sparepartsdeliveryaddress2__c,Sparepartsdeliveryaddress3__c,Sparepartsdeliveryaddress4__c,Sparepartsdeliveryaddress5__c,
                                                Sparepartsdeliveryaddress6__c,Sparepartsdeliveryaddress7__c,Sparepartsdeliveryaddress8__c,Sparepartsdeliveryaddress9__c,Sparepartsdeliveryaddress10__c,
                                                //2023-12-14 WYL add end
                                                   Town__c, Postal_Code__c, HospitalType__c, InstitutionalType__c,WhetherRiskPassing__c,IsUnlock__c
                                            FROM Account 
                                            WHERE id in :accIds];
                
                List<Account_Delay_Apply__c> adastatusList = [SELECT Id, Name, Website__c, Site__c,
                                                    Is_Active__c, Feature__c, Attribute_Type__c, Speciality_Type__c, 
                                                    Abbreviation__c, Street__c, City_Master__c, Field3_companyname__c, 
                                                    State_Master__c, Grade__c, Alias_Name2__c, 
                                                    Town__c, Postal_Code__c, HospitalName__c, Hospital__c,
                                                    HospitalType__c, InstitutionalType__c,WhetherRiskPassing__c
                                                    // Including_departments__c
                                                    // 2023-12-14 WYL add start
                                                    ,Sparepartsdeliveryaddress1__c,Sparepartsdeliveryaddress2__c,Sparepartsdeliveryaddress3__c,Sparepartsdeliveryaddress4__c,Sparepartsdeliveryaddress5__c
                                                    ,Sparepartsdeliveryaddress6__c,Sparepartsdeliveryaddress7__c,Sparepartsdeliveryaddress8__c,Sparepartsdeliveryaddress9__c,Sparepartsdeliveryaddress10__c
                                                    //2023-12-14 WYL add end
                                            FROM Account_Delay_Apply__c 
                                            WHERE id in :adaIds];                           
 
                for (Account acc :statusList ) {
                    accMap.put(acc.Id, acc);
                }
 
                for (Account_Delay_Apply__c ada : adastatusList) {
                    accMap.get(ada.Hospital__c).Feature__c = ada.Feature__c;
                    accMap.get(ada.Hospital__c).Attribute_Type__c = ada.Attribute_Type__c;
                    accMap.get(ada.Hospital__c).Speciality_Type__c = ada.Speciality_Type__c;
                    accMap.get(ada.Hospital__c).Abbreviation__c = ada.Abbreviation__c;
                    accMap.get(ada.Hospital__c).Street__c = ada.Street__c;
                    accMap.get(ada.Hospital__c).Name = ada.HospitalName__c;
                    accMap.get(ada.Hospital__c).City_Master__c = ada.City_Master__c;
                    accMap.get(ada.Hospital__c).Field3_companyname__c = ada.Field3_companyname__c;
                    accMap.get(ada.Hospital__c).State_Master__c = ada.State_Master__c;
                    accMap.get(ada.Hospital__c).Grade__c = ada.Grade__c; 
                    // accMap.get(ada.Hospital__c).Address__c = ada.Address_Together__c;
                    accMap.get(ada.Hospital__c).Website = ada.Website__c;
                    accMap.get(ada.Hospital__c).Site = ada.Site__c;
                    accMap.get(ada.Hospital__c).Alias_Name2__c = ada.Alias_Name2__c;
                    accMap.get(ada.Hospital__c).Town__c = ada.Town__c;
                    accMap.get(ada.Hospital__c).Postal_Code__c = ada.Postal_Code__c;
                    accMap.get(ada.Hospital__c).HospitalType__c = ada.HospitalType__c;
                    accMap.get(ada.Hospital__c).InstitutionalType__c = ada.InstitutionalType__c;
                    accMap.get(ada.Hospital__c).WhetherRiskPassing__c= ada.WhetherRiskPassing__c;
                    // 2023-10-15 医院担当 WYL    DB202306499625 start
                    // accMap.get(ada.Hospital__c).Including_departments__c = ada.Including_departments__c;
                    // accMap.get(ada.Hospital__c).BF_owner__c=Includes_department_user_authentication(ada.Including_departments__c, 'BF',accMap.get(ada.Hospital__c).BF_owner__c);
                    // accMap.get(ada.Hospital__c).GYN_owner__c=Includes_department_user_authentication(ada.Including_departments__c, 'GYN',accMap.get(ada.Hospital__c).GYN_owner__c);
                    // accMap.get(ada.Hospital__c).SP_Main__c=Includes_department_user_authentication(ada.Including_departments__c, 'SP',accMap.get(ada.Hospital__c).SP_Main__c);
                    // accMap.get(ada.Hospital__c).URO_owner_ID__c=Includes_department_user_authentication(ada.Including_departments__c, 'URO',accMap.get(ada.Hospital__c).URO_owner_ID__c);
                    // accMap.get(ada.Hospital__c).GI_Main__c=Includes_department_user_authentication(ada.Including_departments__c, 'GI',accMap.get(ada.Hospital__c).GI_Main__c);
                    // accMap.get(ada.Hospital__c).ENT_owner_ID__c = Includes_department_user_authentication(ada.Including_departments__c, 'ENT',accMap.get(ada.Hospital__c).ENT_owner_ID__c);
                     // 2023-10-15 医院担当 WYL DB202306499625 end
                    // 2023-12-14 WYL add start
                    accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress1__c = ada.Sparepartsdeliveryaddress1__c;
                    accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress2__c = ada.Sparepartsdeliveryaddress2__c;
                    accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress3__c = ada.Sparepartsdeliveryaddress3__c;
                    accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress4__c = ada.Sparepartsdeliveryaddress4__c;
                    accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress5__c = ada.Sparepartsdeliveryaddress5__c;
                    accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress6__c = ada.Sparepartsdeliveryaddress6__c;
                    accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress7__c = ada.Sparepartsdeliveryaddress7__c;
                    accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress8__c = ada.Sparepartsdeliveryaddress8__c;
                    accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress9__c = ada.Sparepartsdeliveryaddress9__c;
                    accMap.get(ada.Hospital__c).Sparepartsdeliveryaddress10__c = ada.Sparepartsdeliveryaddress10__c;
                    // 2023-12-14 WYL add end
                    accMap.get(ada.Hospital__c).IsUnlock__c= true; //跳过验证规则
                    system.debug('原数据==='+accMap.get(ada.Hospital__c).IsUnlock__c);
                    updacclist.add(accMap.get(ada.Hospital__c));
 
                }
            //if(updacclist.size() > 0){
            //    update updacclist;
            //}
            if(updacclist.size()>0){
            //一个客户更新失败
            List<String> failedaccList = new List<String>();
            system.debug('更新客戶3');
            Database.SaveResult[] saveAccResults = Database.update(updacclist,false);
            for(Integer i = 0;i<saveAccResults.size();i++) {
                if(!saveAccResults.get(i).isSuccess() ){ 
                    //将更新失败的客户添加到集合中
                    failedaccList.add(String.valueOf( updacclist.get(i).id ).substring(0,15));
                }
            }
            
            //更新成功后,清除客户上的 是否解锁
            List<Account> successAccList = new List<Account>();
            for(Account statusacc:statusList){
                if(failedaccList.contains( String.valueOf( statusacc.id ).substring(0,15)  )){
                    system.debug('更新失败=='+saveAccResults);
                    //更新失败,则不清除标识
                }else {
                    system.debug('清除标识==='+statusacc.IsUnlock__c);
                    //更新成功,清除标识
                    statusacc.IsUnlock__c = false;
                    successAccList.add(statusacc);
                }
            }
 
            //清除标识
            if(successAccList.size()>0){
                system.debug('更新客戶2');
                update successAccList;
            }
            
        }
               
        }
 
    }
 
    public void CustomizePageJudge(){
 
        for (Account_Delay_Apply__c ada : newList){          
            System.debug('lt123-----new审批步骤-----'+ada.ApprovalSteps__c);
            System.debug('lt123-----old审批步骤-----'+oldMap.get(ada.Id).ApprovalSteps__c);     
            if(ada.ApprovalSteps__c != oldMap.get(ada.Id).ApprovalSteps__c){
                System.debug('lt123-----审批步骤改变-----');
                if(ada.CustomizePageFlg__c == true){
                    System.debug('lt123-----ada.CustomizePageFlg__c-----'+ada.CustomizePageFlg__c);
                    ada.CustomizePageFlg__c = false;
                }else{
                    System.debug('lt123-----else,ada.CustomizePageFlg__c-----'+ada.CustomizePageFlg__c);
                    //  ada.addError('请点击进入客户详情页面,在‘批准历史’栏目下进行批准或拒绝操作');
                    ada.addError('请点击链接(相关项)确认具体信息后审批。');
                }
            }
        }
    }
    // 2023-10-15 WYL 新增     DB202306499625 start 
    // public String Includes_department_user_authentication(String Including_departments,String ksdd,String user_Id){
    //     Map<String, String> ddMaps = new Map<String, String>();
    //     ddMaps.put('GI', '消化科');
    //     ddMaps.put('BF', '呼吸科');
    //     ddMaps.put('SP', '普外科');
    //     ddMaps.put('URO', '泌尿科');
    //     ddMaps.put('GYN', '妇科');
    //     ddMaps.put('ENT', '耳鼻喉科');
    //     if (Including_departments.contains(ddMaps.get(ksdd))) {
    //         return user_Id;
    //     }
    //     return '00510000000fSYI';
    // }
    // 2023-10-15 WYL 新增    DB202306499625 end
}