public without sharing class CancelPostponePlanHandler extends Oly_TriggerHandler { private Map newMap; private Map oldMap; private List newList; private List 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) Trigger.newMap; this.oldMap = (Map) Trigger.oldMap; this.newList = (List) Trigger.new; this.oldList = (List) Trigger.old; } protected override void afterInsert() { ChangeStatus(); } protected override void afterUpdate() { ChangeStatus(); } // protected override void afterUpdate() { // CleanOPDLendSort(); // } private void ChangeStatus () { List oPDPlans = new List(); List clrarRaList = new List(); // SWAG-BUF6J5 20201111 you start Set opdIdSet = new Set(); Set cancelIdSet = new Set(); //手动调回审批 // SWAG-BUF6J5 20201111 you end //2021-08-11 mzy SWAG-C5RAZV 事件——计划 延期 调查 start //存放每条OPD对应的取消理由 Map opdCancelReasonMap = new Map(); //2021-08-11 mzy SWAG-C5RAZV 事件——计划 延期 调查 end //备品智能化 2023-12-19 dzk 希望到货日与OPD延期完全关联 start Map renOPDRequestDayMap = new Map(); List renAppUpdateList = new List(); for (CancelPostponePlan__c cppOPD : newList) { opdIdSet.add(cppOPD.CancelOPDPlan__c); } System.debug('opdIdSet======' + opdIdSet); List 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 opdplanList = [select Id, OPDLendSort__c from OPDPlan__c where Id = :opdIdSet]; Map opdplanMap = new Map(); 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 requests = new List (); Map piMap = New Map([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 Rentals = new List(); // 20230222 ljh DB202301265636 start // 20230202 ljh DB202301265636 start // List 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 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 RentalList = new List(); // List RentalCancleAlertList = new List(); // List 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 cancelIdSet1 = new Set(); //手动调回审批 SS_Batch_Column_Mapping__c mpdMapping = SS_Batch_Column_Mapping__c.getValues('Rental_Apply_OPD_Cancle'); //2021/02/07 liying SWAG-BXVDPJ start Map opdMap = new Map(); 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 requests = new List (); Map piMap = New Map([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 needCancelTasks = new List(); List taskIds = new List(); //查询 OPD所 关联的任务 ,将取消理由和状态赋值 List 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 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 needCleanOPDLendSort = new List(); // 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 }