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 CancelPostponePlanHandler  extends Oly_TriggerHandler {
 
    private Map<Id, CancelPostponePlan__c> newMap;
    private Map<Id, CancelPostponePlan__c> oldMap;
    private List<CancelPostponePlan__c> newList;
    private List<CancelPostponePlan__c> oldList;
    //2021/02/07 liying start SWAG-BXVDPJ 
    public static Integer FIELDMAX = 100;
    //2021/02/07 liying end SWAG-BXVDPJ 
    public CancelPostponePlanHandler() {
        this.newMap = (Map<Id, CancelPostponePlan__c>) Trigger.newMap;
        this.oldMap = (Map<Id, CancelPostponePlan__c>) Trigger.oldMap;
        this.newList = (List<CancelPostponePlan__c>) Trigger.new;
        this.oldList = (List<CancelPostponePlan__c>) Trigger.old;
    }
 
    protected override void afterInsert() {
        ChangeStatus();
    }
 
    protected override void afterUpdate() {
        ChangeStatus();
    }
 
    // protected override void afterUpdate() {
    //     CleanOPDLendSort();
    // }
    private void ChangeStatus () {
 
        List<OPDPlan__c> oPDPlans = new List<OPDPlan__c>();
        List<String> clrarRaList = new List<String>();
        // SWAG-BUF6J5 20201111  you start
        Set<Id> opdIdSet = new Set<Id>(); 
        Set<String> cancelIdSet = new Set<String>(); //手动调回审批
        // SWAG-BUF6J5 20201111  you end
        //2021-08-11  mzy   SWAG-C5RAZV  事件——计划 延期 调查  start
        //存放每条OPD对应的取消理由
        Map<String,String> opdCancelReasonMap = new Map<String,String>();
        //2021-08-11  mzy   SWAG-C5RAZV  事件——计划 延期 调查  end
 
        //备品智能化 2023-12-19 dzk 希望到货日与OPD延期完全关联 start
        Map<Id,Rental_Apply__c> renOPDRequestDayMap = new Map<Id,Rental_Apply__c>();
        List<Rental_Apply__c> renAppUpdateList = new List<Rental_Apply__c>();
        for (CancelPostponePlan__c cppOPD : newList) {
            opdIdSet.add(cppOPD.CancelOPDPlan__c);
        }
        System.debug('opdIdSet======' + opdIdSet);
        List<Rental_Apply__c> renAppList = [SELECT Id, OPDPlan__c, Request_shipping_day__c, OPDPlanDate__c
                                                            FROM Rental_Apply__c
                                                            WHERE OPDPlan__c =: opdIdSet];
        System.debug('opdIdSet======' + renAppList);
        for(Rental_Apply__c ren : renAppList){
            renOPDRequestDayMap.put(ren.OPDPlan__c,ren);
            
            System.debug(ren.Request_shipping_day__c.addDays(-7) > Date.today());
            System.debug('opdIdSet======1' + renOPDRequestDayMap);
            System.debug('opdIdSet======1' + ren.Request_shipping_day__c);
        }
        //备品智能化 2023-12-19 dzk 希望到货日与OPD延期完全关联 end
        // 20240227 ssm 备品可视化补充 start
        List<OPDPlan__c> opdplanList = [select Id, OPDLendSort__c from OPDPlan__c where Id = :opdIdSet];
        Map<String, OPDPlan__c> opdplanMap = new Map<String, OPDPlan__c>();
        for (OPDPlan__c opd : opdplanList) {
            opdplanMap.put(opd.Id, opd);
        }
        // 20240227 ssm 备品可视化补充 end
        for (CancelPostponePlan__c cpp : newList) {
            // 20240227 ssm 备品可视化补充 start
            OPDPlan__c opdplan = opdplanMap != null && opdplanMap.get(cpp.CancelOPDPlan__c) != null ? opdplanMap.get(cpp.CancelOPDPlan__c) : new OPDPlan__c();
            // 20240227 ssm 备品可视化补充 end
 
            // if (oldMap.get(cpp.Id).get('Status__c') != cpp.Status__c && cpp.Status__c == '取消成功') {
            if (Trigger.isInsert && cpp.Status__c == '取消成功' ||
                Trigger.isUpdate && oldMap.get(cpp.Id).get('Status__c') != cpp.Status__c && cpp.Status__c == '取消成功') {
                // SWAG-BUF6J5 20201111  you start
                //opdIdSet.add(cpp.CancelOPDPlan__c);
                cancelIdSet.add(cpp.CancelOPDPlan__c);
                // SWAG-BUF6J5 20201111  you end
                opdplan.Id =  cpp.CancelOPDPlan__c;
                opdplan.Status__c = '取消';
                // CHAN-BBACTU LHJ 记录最新延期 Start
                opdplan.OPD_DelayCancel_Last__c = cpp.id;
                // CHAN-BBACTU LHJ 记录最新延期 End
                // ***SWAG-BZCAHL***XHL***20210329***Start*********
                if ('备品不足'.equals(cpp.cancelReasonCombobox__c)) {
                    opdplan.CancelOpdPlan__c = true;    
                }
                // ***SWAG-BZCAHL***XHL***20210329***End*********
                oPDPlans.add(opdplan);
 
                //2021-08-11  mzy   SWAG-C5RAZV  事件——计划 延期 调查  start
                opdCancelReasonMap.put(opdplan.Id ,cpp.cancelReasonCombobox__c);
                //2021-08-11  mzy   SWAG-C5RAZV  事件——计划 延期 调查  end
 
 
            // } else if (oldMap.get(cpp.Id).get('Status__c') != cpp.Status__c && cpp.Status__c == '延期成功') {
            } else if (Trigger.isInsert && cpp.Status__c == '延期成功' ||
                    Trigger.isUpdate && oldMap.get(cpp.Id).get('Status__c') != cpp.Status__c && cpp.Status__c == '延期成功') {
                
                opdplan.Id =  cpp.CancelOPDPlan__c;
                // SWAG-BXDC25 mzy 审批后,更新【OPD计划实施日期temp】  Start
                //opdplan.OPDPlan_ImplementDate__c = cpp.NextPlanDate__c;
                opdplan.OPDPlan_ImplementDate_temp__c = cpp.NextPlanDate__c;
                // SWAG-BXDC25 mzy 审批后,更新【OPD计划实施日期temp】  End           
                // CHAN-BBACTU LHJ 记录最新延期 Start
                opdplan.OPD_DelayCancel_Last__c = cpp.id;
                // CHAN-BBACTU LHJ 记录最新延期 End
                // SWAG-BXW4AH  XHL 2021/2/7 【委托】 OPD计划再申请逻辑设置 Start    
                if (Trigger.isInsert && cpp.cancelReasonCombobox__c == '系统自动延期' ) {
                        //OPD计划已进行 系统自动延期 标记(只在OPD计划取消/延期新增的时候进行标记)
                        opdplan.SystemDelayMark__c = true;
                        // 20240227 ssm 备品可视化补充 start
                        // 系统自动延期成功时,给上月残留标记设为true,本月排序号覆盖到上月排序号
                        opdplan.OPDLendSortBefore__c = opdplan.OPDLendSort__c;//备品出借优先度 赋值给 备品出借优先度(上月)
                        opdplan.Last_Month_Untreated__c = true; // 20231228 dzk 上个月未处理完标识
                        // 20240227 ssm 备品可视化补充 end
                }
                // SWAG-BXW4AH  XHL 2021/2/7 【委托】 OPD计划再申请逻辑设置 End
                //2022-1-11 yjk 设置是否日报延期
                opdplan.isDailyReportDelay__c = cpp.isDailyReportDelay__c;
                oPDPlans.add(opdplan);
                //2023-12-19 dzk 希望到货日与OPD延期完全关联 start
                if(renOPDRequestDayMap.size() > 0){
                    Rental_Apply__c renApp = new Rental_Apply__c();
                    renApp.Id = renOPDRequestDayMap.get(cpp.CancelOPDPlan__c).Id;
                    // renApp.Request_shipping_day__c = cpp.NextPlanDate__c.addDays(-3);
                    // 借用机会可视化 - 延期改造 20240111 zyh start
                    // 如果希望到货日-7<=今天或者已经推送过,走下方去判断
                    if (renOPDRequestDayMap.get(cpp.CancelOPDPlan__c).Request_shipping_day__c - 7 <= Date.today()) {
                        // 如果修改后 > 修改前
                        System.debug('yq-----'+cpp.NextPlanDate__c);
                        System.debug('yq-----'+renOPDRequestDayMap.get(cpp.CancelOPDPlan__c).OPDPlanDate__c);
                        if (cpp.NextPlanDate__c > renOPDRequestDayMap.get(cpp.CancelOPDPlan__c).OPDPlanDate__c) {
                            renApp.Request_shipping_day__c = cpp.NextPlanDate__c.addDays(-3);
                            if (renApp.Request_shipping_day__c - 7 > Date.today()) {
                                // 清空排队、分配信息
                                clrarRaList.add(renApp.Id);
                            }
                            renAppUpdateList.add(renApp);
                        }
                    } else {
                        renApp.Request_shipping_day__c = cpp.NextPlanDate__c.addDays(-3);
                        renAppUpdateList.add(renApp);
                    }
                    // 借用机会可视化 - 延期改造 20240111 zyh end
                    // renAppUpdateList.add(renApp);
                }
            
            }
        }
        try {
            if(renAppUpdateList.size() > 0){
                update renAppUpdateList;
            }
            // 借用机会可视化-排队分配信息清空 2024-1-12 zyh start
            if (clrarRaList.size() > 0) {
                RentalApplyTriggerHandler.clearAssignAndQueue(clrarRaList);
            }
            // 借用机会可视化-排队分配信息清空 2024-1-12 zyh end
        } catch (DmlException e) {}
        //2023-12-19 dzk 希望到货日与OPD延期完全关联 end
        if (cancelIdSet.size() > 0) {
            List<Approval.ProcessWorkitemRequest> requests = new List<Approval.ProcessWorkitemRequest> ();
            Map<ID,ProcessInstance> piMap = New Map<ID,ProcessInstance>([Select Id from ProcessInstance where TargetObjectId IN :cancelIdSet]);
            system.debug('==cancelIdSet=='+cancelIdSet);
            for(ProcessInstanceWorkItem wi : [Select Id from ProcessInstanceWorkItem where ProcessInstanceId IN :piMap.keySet()]){
                Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
                req2.setAction('Removed');
                req2.setWorkitemId(wi.Id);
                requests.add(req2);
            }
            system.debug('==requests.size()=='+requests.size());
            if (requests.size() > 0) {
                Approval.ProcessResult[] processResults = null;
                processResults = Approval.process(requests, true);
                system.debug('==调回opd计划审批得结果=='+processResults);
            }
        }
 
        if (oPDPlans.size() > 0) {
            update oPDPlans;
        }
        // SWAG-BUF6J5 20201111  you start
        if (cancelIdSet.size() > 0) {//查询opd计划下得所有备品借出申请  更新 状态
            List<Rental_Apply__c> Rentals = new List<Rental_Apply__c>();
            // 20230222 ljh DB202301265636 start
            // 20230202 ljh DB202301265636 start
            // List<Rental_Apply__c> RentalList = [select id,Cancel_Reason__c , Loaner_cancel_request__c,OPDPlan__c from Rental_Apply__c where OPDPlan__c in: cancelIdSet and Status__c <> '取消'];
            List<Rental_Apply__c> RentalList = [select id,Cancel_Reason__c , Loaner_cancel_request__c,OPDPlan__c from Rental_Apply__c where OPDPlan__c in: cancelIdSet and Status__c <> '取消' AND Shipment_request_Cnt__c = 0];
            // 20230202 ljh DB202301265636 end
            // 学会取消批准的时候 发邮件 不上线了
            // List<Rental_Apply__c> RentalList = new List<Rental_Apply__c>();
            // List<Rental_Apply__c> RentalCancleAlertList = new List<Rental_Apply__c>();
            // List<Rental_Apply__c> RentalListTemp = [select id,Cancel_Reason__c , Loaner_cancel_request__c,OPDPlan__c,Shipment_request_Cnt__c from Rental_Apply__c where OPDPlan__c in: cancelIdSet and Status__c <> '取消'];
            // for(Rental_Apply__c ra:RentalListTemp){
            //     if(ra.Shipment_request_Cnt__c == 0){
            //         RentalList.add(ra);
            //     }else{
            //         ra.CancelDay__c = Date.today();
            //         RentalCancleAlertList.add(ra);
            //     }
            // }
            // 20230222 ljh DB202301265636 end
            Set<String> cancelIdSet1 = new Set<String>(); //手动调回审批
            SS_Batch_Column_Mapping__c mpdMapping = SS_Batch_Column_Mapping__c.getValues('Rental_Apply_OPD_Cancle');
            //2021/02/07 liying SWAG-BXVDPJ start           
            Map<String,String> opdMap = new Map<String,String>();
            for (Integer i = 1; i <= FIELDMAX; i++) {
                String lpadI = ('00' + i).right(3);
                String fromColumn = 'From_Column_' + lpadI + '__c';
                //2021-06-30  mzy  Outlook日历和SFDC日历 开发 update  start  
                if(mpdMapping!=null){
                    //注:这个得判空,否则转字符串会报错
                    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));
                        opdMap.put(apiStr,ssApiStr);
                        //2021/02/07 liying SWAG-BXVDPJ end      
                    }
                }
                //2021-06-30  mzy  Outlook日历和SFDC日历 开发 update  start
 
            }
            for (CancelPostponePlan__c cpp : newList) {
                if (RentalList.size() > 0) {
                  for (Rental_Apply__c rtl : RentalList) {
                      if(rtl.OPDPlan__c == cpp.CancelOPDPlan__c && cpp.if_HaveRental_Apply__c == false){
                          cancelIdSet1.add(rtl.id);
                          // 2021/02/07 liying SWAG-BXVDPJ start
                          if(opdMap.containsKey(cpp.cancelReasonCombobox__c)){
                             rtl.Cancel_Reason__c = opdMap.get(cpp.cancelReasonCombobox__c).split(';')[1];
                             rtl.Loaner_cancel_reason__c = opdMap.get(cpp.cancelReasonCombobox__c).split(';')[0];   
                             rtl.Loaner_cancel_request__c = opdMap.get(cpp.cancelReasonCombobox__c).split(';')[0];  
                          }
                          rtl.Status__c = '取消';
                          rtl.if_HaveOPD_Apply__c = true;
                          rtl.if_HaveOPDPlanCan__c = true;//备品借出申请审批调回得时候 状态会变成 “草案中”,打标机是为了防止这样
                          Rentals.add(rtl);
                      }
                        
                  }
                }
            } 
            if (cancelIdSet1.size() > 0) {
                List<Approval.ProcessWorkitemRequest> requests = new List<Approval.ProcessWorkitemRequest> ();
                Map<ID,ProcessInstance> piMap = New Map<ID,ProcessInstance>([Select Id from ProcessInstance where TargetObjectId IN :cancelIdSet1]);
                system.debug('==cancelIdSet1=='+cancelIdSet1);
                for(ProcessInstanceWorkItem wi : [Select Id from ProcessInstanceWorkItem where ProcessInstanceId IN :piMap.keySet()]){
                    Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
                    req2.setAction('Removed');
                    req2.setWorkitemId(wi.Id);
                    requests.add(req2);
                }
                system.debug('==requests.size()=='+requests.size());
                if (requests.size() > 0) {
                    Approval.ProcessResult[] processResults = null;
                    processResults = Approval.process(requests, true);
                    system.debug('==调回审批得结果=='+processResults);
                }
            }
            if(Rentals.size()>0){
                update Rentals;
            }
            // 20230222 ljh DB202301265636 start
            // 学会取消批准的时候 发邮件 不上线了
            // if(RentalCancleAlertList.size() > 0 ){
            //     update RentalCancleAlertList;
            // } 
            // 20230222 ljh DB202301265636 end
        }
        // SWAG-BUF6J5 20201111  you end
        //2021-08-11  mzy   SWAG-C5RAZV  事件——计划 延期 调查  start
        //0.取消
        if(cancelIdSet.size()>0){
           List<Task__c> needCancelTasks = new List<Task__c>();
           List<String> taskIds = new List<String>();
           //查询 OPD所 关联的任务 ,将取消理由和状态赋值
           List<OPDPlan__c>  OPDList = [SELECT Id,Task_ID__c,Task_ID__r.Initial_Task__c,Task_ID__r.taskType__c  FROM OPDPlan__c where Id in : cancelIdSet AND Task_ID__r.taskStatus__c != '04 取消'];
          
           if(OPDList.size()>0){
                for(OPDPlan__c op : OPDList){
 
                    if(op.Task_ID__c != null){
                        task__c t = new task__c();
                        t.Id = op.Task_ID__c;
                        t.TaskStatus__c = '04 取消';
                        t.cancelDate__c = date.today();
                        t.cancelReasonOther__c = '';
                        String cancelLY = opdCancelReasonMap.get(op.Id);
 
                        System.debug('取消理由====:'+cancelLY);
                        if('询价跟进'.equals(op.Task_ID__r.taskType__c)){
                            t.cancelReason__c ='其他';
                            t.cancelReasonSelect__c = '其他';
                            t.cancelReasonOther__c = cancelLY ;
                        }else if('医院取消试用'.equals(cancelLY)){
                           t.cancelReason__c = '客户时间变更或冲突';
                           t.cancelReasonSelect__c = '客户事件变更或冲突';
                        }else if('计划调整变更'.equals(cancelLY) || '工作计划修改'.equals(cancelLY)){
                           t.cancelReason__c ='与其他突发事件冲突';
                           t.cancelReasonSelect__c = '与其他突发事件冲突';
                        }else if('问题已解决无需OPD'.equals(cancelLY)){
                           t.cancelReason__c ='问题已解决无需OPD';
                           t.cancelReasonSelect__c = '问题已解决无需OPD';
                        }else if('手术取消'.equals(cancelLY)){
                           t.cancelReason__c ='无合适病例(适合病例流失)OPD取消';
                           t.cancelReasonSelect__c = '无合适病例(适合病例流失)OPD取消';
                        }else if('客户已采购竞品,失单'.equals(cancelLY)){                        
                           t.cancelReason__c ='客户已采购竞品,失单';
                           t.cancelReasonSelect__c = '客户已采购竞品,失单';
                        }else if('计划重复'.equals(cancelLY)){
                           t.cancelReason__c ='计划重复';
                           t.cancelReasonSelect__c = '计划重复'; 
                        }else if('信息填写错误'.equals(cancelLY)){
                            t.cancelReason__c ='信息填写错误';
                            t.cancelReasonSelect__c = '信息填写错误'; 
                        }else if('担当离职'.equals(cancelLY) 
                            || '申请单合并'.equals(cancelLY) || '没完成备品申请'.equals(cancelLY)
                            || '其他'.equals(cancelLY)  || '修理对应'.equals(cancelLY) || '备品不足'.equals(cancelLY)){
                           t.cancelReason__c ='其他';
                           t.cancelReasonSelect__c = '其他';
                           t.cancelReasonOther__c = (cancelLY == '备品不足'?'备品原因':cancelLY );
                        }
 
                        taskIds.add(t.Id);
 
                        needCancelTasks.add(t);
 
                        if(op.Task_ID__r.Initial_Task__c != null){
                           task__c Initial_Task = new task__c();
                           Initial_Task.Id = op.Task_ID__r.Initial_Task__c ;
                           Initial_Task.cancelReason__c =t.cancelReason__c;
                           Initial_Task.cancelReasonSelect__c =t.cancelReasonSelect__c;
                           Initial_Task.cancelReasonOther__c = t.cancelReasonOther__c;
                           needCancelTasks.add(Initial_Task);
 
                           //taskIds.add(Initial_Task.Id);
                        }
                    }
                    
                }
 
                if(needCancelTasks.size()>0){
                    update needCancelTasks;
                }
           }
 
            //查询 任务所对应的事件 ,将取消理由和状态赋值
            if(taskIds.size()>0){
               List<Event> elist = [SELECT ID,Subject,Task_ID__c,EventStatus__c FROM Event WHERE Task_ID__c IN :taskIds];
                if(elist.size()>0){
                    for(Event e : elist){
                       for(Task__c t : needCancelTasks){
                            if(String.ValueOf(e.Task_ID__c).contains(t.Id)){
                                if(!'05 延期'.equals(e.EventStatus__c)){
                                    if(e.Subject!=null&&String.isNotBlank(String.valueOf(e.Subject))&&String.valueOf(e.Subject).indexOf('【已取消】') == -1 ){
                                        e.Subject += '【已取消】';
                                    }else if(e.Subject==null ||String.isBlank(String.valueOf(e.Subject))){
                                        e.Subject  = '【已取消】';
                                    }
                                    e.EventStatus__c = '04 取消';
                                }
                                e.cancelReason__c = t.cancelReason__c;
                                e.cancelReasonSelect__c = t.cancelReasonSelect__c;
                                e.cancelReasonOther__c = t.cancelReasonOther__c;
                            }
                       }
                    }
                    
                    update elist;
               }               
            }
 
        }
 
        //2021-08-11  mzy   SWAG-C5RAZV  事件——计划 延期 调查  end
    }
 
    // private void CleanOPDLendSort(){
    //   List<OPDPlan__c> needCleanOPDLendSort = new List<OPDPlan__c>();
    //   for (CancelPostponePlan__c cpp : newList) {
    //      OPDPlan__c opdplan = new OPDPlan__c();
    //      if (oldMap.get(cpp.Id).get('Status__c') != cpp.Status__c && cpp.Status__c == '延期成功') {
    //              opdplan.Id =  cpp.CancelOPDPlan__c;
    //              opdplan.OPDLendSort__c = null;
    //              needCleanOPDLendSort.add(opdplan);
    //       }
    //   }
    //   if(needCleanOPDLendSort.size()>0){
    //        update needCleanOPDLendSort;
    //   }
    // }
    // 2020-11-04 mzy SWAG-BSC5WP 延期成功后,取消 备品优先度 的值,只保留 备品出借优先度草案中 end
}