public without sharing class CampaignCopyToOPDPlanHandler extends Oly_TriggerHandler { private Map newMap; private Map oldMap; private List newList; private List oldList; public static boolean HandlerFlag = false; public CampaignCopyToOPDPlanHandler() { this.newMap = (Map) Trigger.newMap; this.oldMap = (Map) Trigger.oldMap; this.newList = (List) Trigger.new; this.oldList = (List) Trigger.old; } protected override void afterUpdate() { //将符合条件的学会拷贝成OPDPlan CampaignCopyToOPDPlan(); //学会发生改变,对应的OPDPlan也会发生相应的改变 listen(); //add by rentx LZHU-BX3DU8 2021-01-13 start //在学会上学会的状态变成“已提交报告”的时候,查一下关联的opd计划,把状态变成完成 changeOPDPlanStatusBeOver(); //add by rentx LZHU-BX3DU8 2021-01-13 end // 2022-01-26 ssm 决裁信息变化时更新OPD计划 start updateApprovedToOPDPlan(); // 2022-01-26 ssm 决裁信息变化时更新OPD计划 end } private void CampaignCopyToOPDPlan() { //记录类型 : 1.学会/会议 --> Society 2.社外培训 --> Training_event 3.服务培训/学会会议 -->Service_trainig 6.营业部专用会议 -->BusinessTraining List recordTypeIds = new List(); recordTypeIds.add(Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('Society').getRecordTypeId()); //学会/会议 recordTypeIds.add(Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('Training_event').getRecordTypeId()); //社外培训 recordTypeIds.add(Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('Service_trainig').getRecordTypeId()); //服务培训/学会会议 recordTypeIds.add(Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('BusinessTraining').getRecordTypeId()); //营业部专用会议 //用来保存将学会转换成的OPDPlan List ResultList = new List (); //System.debug('CamMzyNewList :'+ newList.size()); List idList = new List(); for ( Campaign c : newList) { OPDPlan__c tempOPD = new OPDPlan__c(); //RecordTypeId 摘要中 //System.debug('CamMzyStauts :'+ c.Status); //System.debug('CamMzyStauts :'+ c.RecordTypeId); //判断是由其他状态变成 公开中 ,且需要借出备品 时插入数据 Campaign oldCampaign = oldMap.get(c.Id); if (!String.isEmpty(String.valueOf(oldCampaign.Status)) && !oldCampaign.Status.equals('公开中')) { if ( c.Status != null && c.Status.equals('公开中') && recordTypeIds.contains(c.RecordTypeId) && String.isNotBlank(c.Is_LendProduct__c) && String.valueOf(c.Is_LendProduct__c).equals('是')) { // System.debug('zzz:' + c); //转换 (对应字段的管理) // 学会的开始日期 --> OPD计划实施日期temp tempOPD.OPDPlan_ImplementDate_temp__c = c.StartDate ; // OPD 查找 市场活动 的关系字段 tempOPD.Campaign__c = c.Id ; // 学会生成OPD计划时, OPD计划状态为 计划中 tempOPD.Status__c = '计划中'; // 学会名称 --> OPD计划科室 //tempOPD.Account_Laboratory__c = c.Name2__c ; // 学会所有人 --> OPD计划所有人 tempOPD.OwnerId = c.OwnerId; // 学会会议的出借备品信息-->OPD计划出借备品信息 tempOPD.PlanProdDetail__c = c.LoadNum__c; tempOPD.OPDType__c = '学会';//2020-10-21 zh OPD计划类型为学会 SWAG-BS6BYA tempOPD.RentalReson__c = '演示';// 20220804 ljh SWAG-CGG7X9 默认 演示 //20201118 you SWAG-BVGAGD start tempOPD.Meeting_Cooperate__c = c.Meeting_Cooperate__c; tempOPD.Meeting_Cooperate2__c = c.Meeting_Cooperate2__c; tempOPD.Meeting_Cooperate3__c = c.Meeting_Cooperate3__c; tempOPD.Meeting_Cooperate4__c = c.Meeting_Cooperate4__c; //20201118 you SWAG-BVGAGD end //20201208 liying SFDC-BVY95C start tempOPD.Open_scope__c = c.Open_scope__c; //20201208 liying SFDC-BVY95C end //20211223 OBPM LY start tempOPD.IF_Approved__c = c.IF_Approved__c;//是否申请决裁 tempOPD.Approved_Status__c = c.Approved_Status__c;//决裁状态 tempOPD.Approved_No__c = c.Meeting_Approved_No__c;//决裁编码 //20211223 OBPM LY end idList.add(c.Id); //写入集合中 ResultList.add(tempOPD); } } System.debug('mmmmmmm:' + ResultList);//test by tiger 20240205 } //20201022 zh 小表同步opd SWAG-BS6BYA Map camToOPD = new Map(); if (idList.size() > 0) { //查询指定OPDPlan mzy List OPDPlanList = [select Campaign__c from OPDPlan__c where Campaign__c in :idList]; if (OPDPlanList.size() <= 0) { //添加 insert ResultList; //20231123 sx 备品智能化添加 RentalApplyOtherTrigger 更新学会trigger 逻辑 start List campIds = new List(); for (OPDPlan__c opd : ResultList ) { camToOPD.put(opd.Campaign__c, opd.Id); //20231113 sxadd 备品借用计划可视化项目 start //1.需要裁决=是 有裁决号 且需要备品 2.需要裁决=否 无决裁号 自动生产备品 if((opd.IF_Approved__c && String.isNotBlank(opd.Approved_No__c)) || (!opd.IF_Approved__c && String.isBlank(opd.Approved_No__c))){ try { System.debug('20231113 sxadd====' + opd.Id); LexOPDSupplementaryController.newRentalApply(opd.Id); } catch (Exception e) { System.debug('eeee======='+e.getMessage()); } } //20231113 sxadd 备品借用计划可视化项目 end } //20240105 sx注释 du OPD计划出借小表更新 start // List updateProds = new List(); // List planProds = [SELECT Id, Campaign__c, OPD_Plan__c FROM Plan_Rental_Equipment__c WHERE Campaign__c in :idList]; // if (planProds.size() > 0) { // for (Plan_Rental_Equipment__c plan : planProds) { // plan.OPD_Plan__c = camToOPD.get(plan.Campaign__c); // updateProds.add(plan); // } // } // if (updateProds.size() > 0) { // update updateProds; // } //20240105 sx注释 du OPD计划出借小表更新 end } } } // 2020-11-10 mzy update 学会取消或者延期, 不直接更改opd计划的状态和时间,而是新建一个取消延期对象 start // 构建需要取消的OPD计划 @TestVisible private CancelPostponePlan__c QX_OPDPlanBuild(ID OpdID) { CancelPostponePlan__c tempCpp = new CancelPostponePlan__c(); tempCpp.CancelOPDPlan__c = OpdID; tempCpp.Status__c = '取消成功'; tempCpp.cancelReasonCombobox__c = '学会取消'; tempCpp.RecordTypeId = '012C80000000NGoIAM';// change by tiger 20240130 //tempCpp.RecordTypeId = '01210000000gQyL'; return tempCpp; } // 构建需要延期的OPD计划 @TestVisible private CancelPostponePlan__c YQ_OPDPlanBuild(OPDPlan__c Opd, Date startDate) { CancelPostponePlan__c tempCpp = new CancelPostponePlan__c(); tempCpp.CancelOPDPlan__c = Opd.id; tempCpp.Status__c = '延期成功'; tempCpp.cancelReasonCombobox__c = '学会延期'; tempCpp.NextPlanDate__c = startDate; tempCpp.RecordTypeId = '012C80000000NGpIAM';// change by tiger 20240130 //tempCpp.RecordTypeId = '01210000000gQyM'; // 2021-01-21 mzy add 会议的时间大于OPD计划的时间,则为延期 , 会议的时间小于OPD计划的时间,则为提前 if(Opd.OPDPlan_ImplementDate__c < StartDate){ tempCpp.ApplyType__c = '延期'; }else if(Opd.OPDPlan_ImplementDate__c > StartDate){ tempCpp.ApplyType__c = '提前'; } // 2021-01-21 mzy add 会议的时间大于OPD计划的时间,则为延期 , 会议的时间小于OPD计划的时间,则为提前 return tempCpp; } // 2020-11-10 mzy update 学会取消或者延期, 不直接更改opd计划的状态和时间,而是新建一个取消延期对象 end private void listen() { // 2020-11-10 mzy update 学会取消或者延期, 不直接更改opd计划的状态和时间,而是新建一个取消延期对象 start //如果学会的状态发生改变时,OPDplan的状态也将发生改变 // 进入了一次不进入第二次 if(CampaignCopyToOPDPlanHandler.HandlerFlag){ return; } CampaignCopyToOPDPlanHandler.HandlerFlag = true; map QXMap = new Map(); //保存取消的学会 Map YQMap = new Map(); //保存延期的学会 Map hyhzMap = new Map();//SWAG-BVGAGD 20201119 you 会议合作本部1,2,3,4带值 for (Campaign c : newList) { //目的 :学会变化 : 状态变为取消 / 开始日期发生改变 Campaign oldCampaign = oldMap.get(c.Id); if (!String.isEmpty(String.valueOf(oldCampaign.Status)) && !oldCampaign.Status.equals('取消')) { // 状态从非取消变为取消 if ( (c.Status != null && c.Status.equals('取消')) ) { // 取消的学会 QXMap.put(c.id, c); } // 在非取消时,开始日期发生变化为延期 else if (c.StartDate != oldCampaign.StartDate) { // 延期的学会 YQMap.put(c.id, c); } //SWAG-BVGAGD 20201119 you start if(c.Meeting_Cooperate__c != oldCampaign.Meeting_Cooperate__c || c.Meeting_Cooperate2__c != oldCampaign.Meeting_Cooperate2__c || c.Meeting_Cooperate3__c != oldCampaign.Meeting_Cooperate3__c || c.Meeting_Cooperate4__c != oldCampaign.Meeting_Cooperate4__c || c.Open_scope__c != oldCampaign.Open_scope__c){ hyhzMap.put(c.id, c); } //SWAG-BVGAGD 20201119 you end } } //用来保存新建的取消(延期)对象,包括取消和延期两种情况 List QX_List = new List(); //System.debug('Test : '+idList); //查询取消学会的OPDPlan List QX_OPDPlan = new List(); //查询延期学会的OPDPlan List YQ_OPDPlan = new List(); //SWAG-BVGAGD 20201119 you List oPDPlans = new List(); // 需要更新的OPD计划的ID集合 Set targetIDSet = new Set(); targetIDSet.addAll(QXMap.keySet()); targetIDSet.addAll(YQMap.keySet()); //SWAG-BVGAGD 20201119 you start targetIDSet.addAll(hyhzMap.keySet()); //SWAG-BVGAGD 20201119 you end if (targetIDSet.size() > 0) { List targetOPDPlanList = [select id , Campaign__c ,OPDPlan_ImplementDate__c from OPDPlan__c where Campaign__c != null and status__c != '取消' and Campaign__c in: targetIDSet ]; for (OPDPlan__c tempOPD : targetOPDPlanList) { // 如果学会取消,那么生成取消的 取消(延期)对象 if (QXMap.containsKey(tempOPD.Campaign__c)) { QX_List.add(QX_OPDPlanBuild(tempOPD.id)); } // 如果学会延期,那么生成延期的 取消(延期)对象,并且记录下来新的学会开始日期 else if (YQMap.containsKey(tempOPD.Campaign__c)) { Date startDate = YQMap.get(tempOPD.Campaign__c)?.StartDate; QX_List.add(YQ_OPDPlanBuild(tempOPD, startDate)); } //SWAG-BVGAGD 20201119 you start // add by gzw 注释掉,单独操作 start // for (Campaign c : newList) { // if(c.id==tempOPD.Campaign__c){ // tempOPD.Meeting_Cooperate__c = c.Meeting_Cooperate__c; // tempOPD.Meeting_Cooperate2__c = c.Meeting_Cooperate2__c; // tempOPD.Meeting_Cooperate3__c = c.Meeting_Cooperate3__c; // tempOPD.Meeting_Cooperate4__c = c.Meeting_Cooperate4__c; // tempOPD.Open_scope__c = c.Open_scope__c; // oPDPlans.add(tempOPD); // } // } // add by gzw 注释掉,单独操作 end //SWAG-BVGAGD 20201119 you end } } if (QX_List.size() > 0 && QX_List != null) { insert QX_List; } // add by gzw OPDPlan__c 单独循环,否则OPDPlan_ImplementDate__c 会还原,报错 start if (targetIDSet.size() > 0) { List targetOPDPlanList = [select id , Campaign__c from OPDPlan__c where Campaign__c != null and status__c != '取消' and Campaign__c in: targetIDSet ]; for (OPDPlan__c tempOPD : targetOPDPlanList) { //SWAG-BVGAGD 20201119 you start for (Campaign c : newList) { if(c.id==tempOPD.Campaign__c){ tempOPD.Meeting_Cooperate__c = c.Meeting_Cooperate__c; tempOPD.Meeting_Cooperate2__c = c.Meeting_Cooperate2__c; tempOPD.Meeting_Cooperate3__c = c.Meeting_Cooperate3__c; tempOPD.Meeting_Cooperate4__c = c.Meeting_Cooperate4__c; tempOPD.Open_scope__c = c.Open_scope__c; oPDPlans.add(tempOPD); } } //SWAG-BVGAGD 20201119 you end } } // add by gzw OPDPlan__c 单独循环,否则OPDPlan_ImplementDate__c 会还原,报错 end //SWAG-BVGAGD 20201119 you start if (oPDPlans.size() > 0) { // System.debug('oPDPlans++++++' + oPDPlans); update oPDPlans; } //SWAG-BVGAGD 20201119 you end // 2020-11-10 mzy update 学会取消或者延期, 不直接更改opd计划的状态和时间,而是新建一个取消延期对象 end /* 2020-11-10 mzy update 学会取消或者延期,OPD计划相应的发生改变 //创建Map保存 学会Id 和 OPDPlan的Id Map QXIdsMap = new Map(); for (OPDPlan__c tempOPD : QX_OPDPlan){ QXIdsMap.put(tempOPD.Campaign__c,tempOPD.id); } //用来保存发生变化的OPDPlan List UpdateOPDPlan = new List(); for (Campaign c : newList){ OPDPlan__c tempOPD = new OPDPlan__c(); // System.debug('====111: ' + IdsMap.get(c.id)); // 判断如果id为空则不进行更新操作 if(IdsMap.get(c.id) != null && !String.isEmpty(IdsMap.get(c.id))){ tempOPD.id = IdsMap.get(c.id); //模拟条件 //if(c.Status.equals('草案中')){ tempOPD.Status__c = '计划中' ; }else // 学会会议延期 更新OPD计划的实施日期 tempOPD.OPDPlan_ImplementDate__c = c.StartDate ; //学会会议取消,OPD计划取消 if(c.Status.equals('取消')) { tempOPD.Status__c = '取消'; } UpdateOPDPlan.add(tempOPD); //else { // tempOPD.Status__c = '完毕'; // } // UpdateOPDPlan.add(tempOPD); } } //更新状态 update UpdateOPDPlan ;*/ } //add by rentx LZHU-BX3DU8 2021-01-13 start private void changeOPDPlanStatusBeOver(){ //获取状态变为 '完毕未报告' 的学会的id List camIds = new List(); for ( Campaign c : newList) { if (c.Status != null && c.Status == '已提交报告' && oldMap.get(c.Id).Status != null && oldMap.get(c.Id).Status != '已提交报告') { camIds.add(c.Id); } } //获取学会下的OPD计划 if (camIds.size() > 0) { List opdlist = [select id,status__c from OPDPlan__c WHERE OPDType__c = '学会' and status__c != '完毕' and Campaign__c in: camIds]; //将OPD计划的状态更新为 完毕 if (opdlist != null && opdlist.size() > 0) { for (OPDPlan__c opdc : opdlist) { opdc.Status__c= '完毕'; } update opdlist; } } } //add by rentx LZHU-BX3DU8 2021-01-13 end // 2022-01-26 ssm 决裁信息变化时更新OPD计划 start private void updateApprovedToOPDPlan() { List cam_ids = new List(); for (Campaign cam : this.newList) { Campaign old_cam = this.oldMap.get(cam.Id); if (old_cam != null && ( old_cam.IF_Approved__c != cam.IF_Approved__c || old_cam.Approved_Status__c != cam.Approved_Status__c || old_cam.Meeting_Approved_No__c != cam.Meeting_Approved_No__c )) { cam_ids.add(cam.Id); } } if (cam_ids.size() > 0) { List plans = [select Id, Campaign__c, IF_Approved__c, Approved_Status__c, Approved_No__c from OPDPlan__c where Campaign__c in :cam_ids]; if (plans != null && plans.size() > 0) { for (OPDPlan__c plan : plans) { Campaign cam = this.newMap.get(plan.Campaign__c); plan.IF_Approved__c = cam.IF_Approved__c; plan.Approved_Status__c = cam.Approved_Status__c; plan.Approved_No__c = cam.Meeting_Approved_No__c; } update plans; } } } // 2022-01-26 ssm 决裁信息变化时更新OPD计划 end }