高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
public class EventHandleController {
    public Boolean isEventDelay {get; set;}
    public Boolean isEventCancel {get; set;}
    public String title {get; set;}
    public User LoginUser {get; set;}
    public Boolean isFSE {get; set;}
    public Event targetEvent { get; set; }
    private String eventId;
    //  2021-09-15   mzy  任务管理改善开发计划 start
    public Boolean isOPDPlanFlag {get; set;}
    public Task__c taskOption {get;set;}  //用来根据记录类型显示不同的选项
    public Boolean isTaskBlank {get; set;}
    public static Integer FIELDMAX = 25;
    public Map<String,String> TaskAndOPDMapping {get;set;}
    //  2021-09-15   mzy  任务管理改善开发计划 end
 
    public EventHandleController(ApexPages.StandardController controller) {
        
    }
    public void init() {
        LoginUser = [Select Id, Name, Post__c, Employee_No__c, Department, Job_Category__c, Category4__c, Category6__c, ManagerId, Manager.Email From User Where Id = :UserInfo.getUserId()];
        String isDelay = ApexPages.currentPage().getParameters().get('isEventDelay');
        String isCancel = ApexPages.currentPage().getParameters().get('isEventCancel');
        // String isCancel = ApexPages.currentPage().getParameters().get('isEventCancel');
        this.eventId = ApexPages.currentPage().getParameters().get('Id');
        //2021-04-22  mzy WLIG-BX3DQ5 【委托】Outlook日历和SFDC日历同步 add  查询结果增加Opd_Plan__r.Status__c, Opd_Plan__c,subject 字段  start
        this.targetEvent = [SELECT Id,Task_ID__c,Opd_Plan__c,Opd_Plan__r.Status__c,Subject,delayReasonSelect__c,delayReasonSelectFSE__c,cancelReasonSelect__c,cancelReasonSelectFSE__c,delayReasonOther__c,cancelReasonOther__c,EventStatus__c FROM Event WHERE Id = :eventId];
        //2021-04-22  mzy WLIG-BX3DQ5 【委托】Outlook日历和SFDC日历同步 add  查询结果增加 Opd_Plan__r.Status__c,Opd_Plan__c,,subject 字段  end
        
        //  2021-09-15   mzy  任务管理改善开发计划 start
        if(this.targetEvent.Opd_Plan__c != null){
            isOPDPlanFlag = true;
        }else {
            isOPDPlanFlag = false;
        }
        if(String.isNotBlank(targetEvent.Task_ID__c)){
           List<Task__c> taskOptionList = [SELECT Id,delayReasonSelect__c,delayReasonSelectFSE__c,cancelReasonSelect__c,cancelReasonSelectFSE__c FROM Task__c WHERE Id = :targetEvent.Task_ID__c];
            if(taskOptionList.size()>0){
                taskOption = taskOptionList.get(0);
                isTaskBlank = false;
            }else{
                isTaskBlank = true;
            }
        }else {
            //如果没有则显事件的理由
            isTaskBlank = true;
        }
        //  2021-09-15   mzy  任务管理改善开发计划 end
 
 
 
        if ('true'.equals(isDelay)) {
            this.isEventDelay = true;
            this.title = '请确认延期理由';
            this.isEventCancel = false;
        }
        if ('true'.equals(isCancel)) {
            this.isEventCancel = true;
            this.title = '请确认取消理由';
            this.isEventDelay = false;
        }
        if (LoginUser.Job_Category__c != null &&  LoginUser.Job_Category__c.equals('销售服务')) {
            isFSE = true;
        }else{
            isFSE = false;
        }
 
        
        //2021-11-02  mzy  任务管理改善开发计划  start
        TaskAndOPDMapping = new Map<String,String>();
        TaskEventReportOPD__c mpdmapping = null;
        if('true'.equals(isDelay)){
           mpdmapping = TaskEventReportOPD__c.getValues('TaskAndOPD_Delay');
        }else {
           mpdmapping = TaskEventReportOPD__c.getValues('TaskAndOPD_Cancel');
        }
        for (Integer i = 1; i <= FIELDMAX; i++) {
            if(mpdmapping!=null){
                String lpadI = ('00' + i).right(3);
                String SSColumn = 'SS_Column_' + lpadI + '__c';
                String taskSC = String.valueOf(mpdMapping.get(SSColumn));
                if(String.isNotBlank(taskSC)){
                    String FRColumn = 'From_Column_' + lpadI + '__c';
                    String OPDFC = String.valueOf(mpdMapping.get(FRColumn));
                    TaskAndOPDMapping.put(taskSC, OPDFC);
                }
            }
        }
        //2021-11-02  mzy  任务管理改善开发计划  end
        System.System.debug('自定义映射:'+TaskAndOPDMapping);
    }
    public PageReference save() {
        Savepoint sp = Database.setSavepoint();
        try {
 
            String delayReason = '';
            String cancelReason = '';
            System.debug('isEventDelay---'+isEventDelay);
            System.debug('isEventCancel---'+isEventCancel);
            System.debug('isFSE---'+isFSE);
            System.debug('delayReasonSelect---'+targetEvent.delayReasonSelect__c);
            if (this.isFSE ) {
                //  2021-09-15   mzy  任务管理改善开发计划 start
                if(isTaskBlank){       
                    //任务为空             
                    if (isEventDelay && String.isBlank(targetEvent.delayReasonSelectFSE__c)) {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '延期理由不能为空'));
                        return null;
                    }
                    if (isEventCancel && String.isBlank(targetEvent.cancelReasonSelectFSE__c)) {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '取消理由不能为空'));
                        return null;
                    }
                    
                    delayReason = targetEvent.delayReasonSelectFSE__c;
                    cancelReason = targetEvent.cancelReasonSelectFSE__c;
                }else {
                    //任务不为空      
                    if (isEventDelay && String.isBlank(taskOption.delayReasonSelectFSE__c)) {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '延期理由不能为空'));
                        return null;
                    }
                    if (isEventCancel && String.isBlank(taskOption.cancelReasonSelectFSE__c)) {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '取消理由不能为空'));
                        return null;
                    }
                    delayReason = taskOption.delayReasonSelectFSE__c;
                    cancelReason = taskOption.cancelReasonSelectFSE__c;
                }
 
                //  2021-09-15   mzy  任务管理改善开发计划 end
            }else{
                //  2021-09-15   mzy  任务管理改善开发计划 start
                if(isTaskBlank){  
                    //任务为空                    
                    if (isEventDelay && String.isBlank(targetEvent.delayReasonSelect__c)) {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '延期理由不能为空'));
                        return null;
                    }
                    if (isEventCancel && String.isBlank(targetEvent.cancelReasonSelect__c)) {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '取消理由不能为空'));
                        return null;
                    }
                    delayReason = targetEvent.delayReasonSelect__c;
                    cancelReason = targetEvent.cancelReasonSelect__c;
                }else {
                    if (isEventDelay && String.isBlank(taskOption.delayReasonSelect__c)) {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '延期理由不能为空'));
                        return null;
                    }
                    if (isEventCancel && String.isBlank(taskOption.cancelReasonSelect__c)) {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '取消理由不能为空'));
                        return null;
                    }
                    delayReason = taskOption.delayReasonSelect__c;
                    cancelReason = taskOption.cancelReasonSelect__c;
                }
            }
            if ('其他'.equals(delayReason) && String.isBlank(targetEvent.delayReasonOther__c) && isEventDelay) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '延期理由为其他时请输入延期理由(其他)!'));
                return null;
            }
            if ('其他'.equals(cancelReason) && String.isBlank(targetEvent.cancelReasonOther__c) && isEventCancel) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,  '取消理由为其他时请输入取消理由(其他)!'));
                return null;
            }
 
            // 2021-04-22  mzy WLIG-BX3DQ5 【委托】Outlook日历和SFDC日历同步 add start 
            //如果有OPD计划的话   根据取消理由  新建一条OPD计划取消延期
            //有OPD计划  , 创建一条 OPD计划取消延期
            CancelPostponePlan__c tempCancelPostponePlan = new  CancelPostponePlan__c();
            if(targetEvent.Opd_Plan__c != null){
                //判断OPD状态是否是取消,如果是取消则不创建OPD计划取消延期对象
                if(!'取消'.equals(targetEvent.Opd_Plan__r.Status__c)){
                    //0.OPD计划
                    tempCancelPostponePlan.CancelOPDPlan__c =  targetEvent.Opd_Plan__c;
                    //要进行取消
                    if(isEventCancel){
                        //1.取消状态
                        tempCancelPostponePlan.Status__c = '取消成功';
                        //2.记录类型
                        Id recodeTypeId = Schema.SObjectType.CancelPostponePlan__c.getRecordTypeInfosByDeveloperName().get('CancelType').getRecordTypeId();
                        tempCancelPostponePlan.RecordTypeId = recodeTypeId;
                        //3.取消理由
                        tempCancelPostponePlan.cancelReasonCombobox__c = TaskAndOPDMapping.get(cancelReason);
                        /*
                        if('客户事件变更或冲突'.equals(cancelReason)){
                           tempCancelPostponePlan.cancelReasonCombobox__c = '医院取消试用';
                        }else if('与其他突发事件冲突'.equals(cancelReason)){
                           tempCancelPostponePlan.cancelReasonCombobox__c = '计划调整变更';
                        }else if('问题已经解决无需拜访'.equals(cancelReason)){
                           tempCancelPostponePlan.cancelReasonCombobox__c = '问题已解决无需OPD';
                        }else if('无合适病例(适合病例流失)OPD取消'.equals(cancelReason)){
                           tempCancelPostponePlan.cancelReasonCombobox__c = '手术取消';
                        }else if('问题已解决无需OPD'.equals(cancelReason)){
                           tempCancelPostponePlan.cancelReasonCombobox__c = '问题已解决无需OPD';
                        }else if('客户已采购竞品,失单'.equals(cancelReason)){
                           tempCancelPostponePlan.cancelReasonCombobox__c = '客户已采购竞品,失单';
                        }else if('客户取消OPD'.equals(cancelReason)){
                           tempCancelPostponePlan.cancelReasonCombobox__c = '医院取消试用';
                        }else if('计划重复'.equals(cancelReason)){
                           tempCancelPostponePlan.cancelReasonCombobox__c = '计划重复';
                        }else if('其他'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        }
                        //2021-09-08 Yjk 补充条件
                        else if('已失单'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        }else if('已订货'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        }else if('与客户安排冲突'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        }else if('上月已跟进'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        }else if('项目暂停'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        }else if('医院建设中'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        }else if('项目取消'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        }else if('任务分配不准'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        }else if('订货推迟'.equals(cancelReason)){
                            tempCancelPostponePlan.cancelReasonCombobox__c = '其他';
                        } */
                      
                        
                        //insert tempCancelPostponePlan;
 
                        System.debug('延期对象的Id:'+tempCancelPostponePlan.Id);
                    }
                }                
            }
            // 2021-04-22  mzy WLIG-BX3DQ5 【委托】Outlook日历和SFDC日历同步 add end
 
            
            task__c task = new task__c();
            if (targetEvent.Task_ID__c != null) {
                task = [SELECT Id,Initial_Task__c,isDelay__c,delayReason__c,delayReasonOther__c,cancelReason__c,cancelReasonOther__c,taskStatus__c,cancelDate__c FROM task__c WHERE Id = :targetEvent.Task_ID__c];
 
                if (isEventDelay) {
                    task.isDelay__c = true;
                    task.delayReason__c = delayReason;
                    task.delayReasonOther__c = targetEvent.delayReasonOther__c;
                    //2021-08-06  mzy  SWAG-C5L4JK   【委托】任务:从事件做取消理由没有带回(源)任务  start
                    if(this.isFSE){
                        task.delayReasonSelectFSE__c = delayReason;
                        targetEvent.delayReasonSelectFSE__c =delayReason; //  2021-09-15   mzy  任务管理改善开发计划 start
                    }else{
                        task.delayReasonSelect__c = delayReason; 
                        targetEvent.delayReasonSelect__c =delayReason;   //  2021-09-15   mzy  任务管理改善开发计划 start
                    }
                    //2021-08-06  mzy  SWAG-C5L4JK   【委托】任务:从事件做取消理由没有带回(源)任务  start
                    
                    // //  2021-09-15   mzy  任务管理改善开发计划 start
                    // if(isTaskBlank==false){
                    //     //如果任务不为空
                    //     if(this.isFSE){
                    //         targetEvent.delayReasonSelectFSE__c =delayReason;
                    //     }else{
                    //         targetEvent.delayReasonSelect__c =delayReason;
                    //     }
                    // }
                    // //  2021-09-15   mzy  任务管理改善开发计划 end
                    targetEvent.delayReason__c = delayReason;
                    
 
                    //2021-08-06  mzy  SWAG-C5L4JK   【委托】任务:从事件做取消理由没有带回(源)任务  start
                    //如果有源任务,则将延期理由同步到源任务上
                    if(task.Initial_Task__c != null){
                        task__c InitialTask = new task__c();
                        InitialTask.Id = task.Initial_Task__c;
                        InitialTask.isDelay__c = true;
                        InitialTask.delayReason__c = task.delayReason__c;
                        InitialTask.delayReasonOther__c = task.delayReasonOther__c;
                        if(this.isFSE){
                            InitialTask.delayReasonSelectFSE__c = delayReason;
                        }else{
                            InitialTask.delayReasonSelect__c = delayReason;
                        }
                        update InitialTask;
                    }
                    //2021-08-06  mzy  SWAG-C5L4JK   【委托】任务:从事件做取消理由没有带回(源)任务  end
                    update task;
                    update targetEvent;
                    //2021-04-25 mzy  WLIG-BX3DQ5 【委托】Outlook日历和SFDC日历同步 update 将 isEventDelay 改为 isDelay
                    return new PageReference('/apex/SimpleEventRegister?isDelay=true&taskId='+task.Id+'&id='+targetEvent.Id);
                }else if (isEventCancel) {
                    task.cancelReason__c = cancelReason;
                    task.cancelReasonOther__c = targetEvent.cancelReasonOther__c;
                    //2021-04-25 mzy  WLIG-BX3DQ5 【委托】Outlook日历和SFDC日历同步 update start
                    //task.cancelReasonSelectFSE__c = targetEvent.cancelReasonSelectFSE__c == null? '':targetEvent.cancelReasonSelectFSE__c;
                    //task.cancelReasonSelect__c = targetEvent.cancelReasonSelect__c == null? '':('客户时间变更或冲突'.equals(targetEvent.cancelReasonSelect__c)==true ?'客户事件变更或冲突':targetEvent.cancelReasonSelect__c);            
                    //2021-04-25 mzy WLIG-BX3DQ5 【委托】Outlook日历和SFDC日历同步 update end
                    if(this.isFSE){
                        task.cancelReasonSelectFSE__c = cancelReason;
                        targetEvent.cancelReasonSelectFSE__c =cancelReason==null?'':cancelReason; //  2021-09-15   mzy  任务管理改善开发计划 start
                    }else{
                        task.cancelReasonSelect__c = cancelReason;
                        System.debug('理由:'+cancelReason);
                        //targetEvent.cancelReasonSelect__c =cancelReason ==null?'':('客户事件变更或冲突'.equals(cancelReason)==true?'客户时间变更或冲突':cancelReason);//  2021-09-15   mzy  任务管理改善开发计划 start
                        targetEvent.cancelReasonSelect__c =cancelReason ==null?'':cancelReason; //  2021-09-15   mzy  任务管理改善开发计划 start
                    }
                    task.taskStatus__c = '04 取消';
                    task.cancelDate__c = date.today();
                    update task;
                    //2021-06-09 mzy WLIG-BX3DQ5 【委托】Outlook日历和SFDC日历同步 update start
                    String subjectName = targetEvent.Subject;
                    if(String.isNotBlank(subjectName)&&subjectName.contains('【')){
                        targetEvent.Subject = subjectName.split('【')[0] + '【已取消】';
                    }else {
                        targetEvent.Subject = subjectName +'【已取消】';
                    }
                    //2021-06-09 mzy WLIG-BX3DQ5 【委托】Outlook日历和SFDC日历同步 update end
                    // //  2021-09-15   mzy  任务管理改善开发计划 start
                    // //如果任务不为空
                    // if(this.isFSE){
                    //     targetEvent.cancelReasonSelectFSE__c =cancelReason==null?'':cancelReason;
                    // }else{
                    //     targetEvent.cancelReasonSelect__c =cancelReason ==null?'':('客户事件变更或冲突'.equals(cancelReason)==true?'客户时间变更或冲突':cancelReason);
                    // }
                    // //  2021-09-15   mzy  任务管理改善开发计划 end
                    targetEvent.EventStatus__c = '04 取消';
                    targetEvent.cancelReason__c = cancelReason; //  2021-09-15   mzy  任务管理改善开发计划 start
                    update targetEvent;
 
                    // //  2021-09-15   mzy  任务管理改善开发计划 start
                    if('取消成功'.equals(tempCancelPostponePlan.Status__c)){
                        insert tempCancelPostponePlan;
                    }
                    // //  2021-09-15   mzy  任务管理改善开发计划 end
 
 
 
                    //2021-08-06  mzy  SWAG-C5L4JK   【委托】任务:从事件做取消理由没有带回(源)任务  start
                    //如果有源任务,则将取消理由同步到源任务上
                    if(task.Initial_Task__c != null){
                        task__c InitialTask = new task__c();
                        InitialTask.Id = task.Initial_Task__c;
                        InitialTask.taskStatus__c = '04 取消';
                        InitialTask.cancelDate__c = date.today();
                        InitialTask.cancelReason__c = task.cancelReason__c;
                        InitialTask.cancelReasonOther__c = task.cancelReasonOther__c;
                        InitialTask.cancelReasonSelectFSE__c = task.cancelReasonSelectFSE__c;
                        InitialTask.cancelReasonSelect__c = task.cancelReasonSelect__c;
                        update InitialTask;
                    }
                    //2021-08-06  mzy  SWAG-C5L4JK   【委托】任务:从事件做取消理由没有带回(源)任务  end
 
                    return new PageReference('/');
                }
            }else{
 
 
                if (isEventDelay) {
                    targetEvent.delayReason__c = delayReason;
                    //20210723 zh SWAG-C5784H 【委托】事件无法延期 start
                    update targetEvent;
                    return new PageReference('/apex/SimpleEventRegister?isDelay=true&id='+targetEvent.Id);
                    //20210723 zh SWAG-C5784H 【委托】事件无法延期 end
                }else if (isEventCancel) {
                    targetEvent.Subject += '【已取消】';
                    targetEvent.EventStatus__c = '04 取消';
                    //2021-08-19  mzy SWAG-C5784H 【委托】事件无法延期 start
                    targetEvent.cancelReason__c = cancelReason;
                    //2021-08-19  mzy SWAG-C5784H 【委托】事件无法延期 end
                    update targetEvent;
                }
                
                return new PageReference('/');
            }
            
            return null;
        } catch (Exception e) {
            System.debug('错误信息 :'+ e.getLineNumber());
            
            System.debug('错误信息 :'+ e);
            ApexPages.addMessages(e);
            Database.rollback(sp);
            return null;
        }
        // return null;
    }
 
}