/** * 20220210 lt UpdateIdentification()--更新产品上的“预留产品”标识 * 预留产品对象会关联一个产品主数据 * 创建预留产品时,将产品主数据上的“预留产品”标识 更新为 true */ public without sharing class LastbuyProductHandler extends Oly_TriggerHandler { private Map newMap; private Map oldMap; private List newList; private List oldList; public LastbuyProductHandler() { this.newMap = (Map) Trigger.newMap; this.oldMap = (Map) Trigger.oldMap; this.newList = (List) Trigger.new; this.oldList = (List) Trigger.old; } protected override void beforeInsert(){ } protected override void afterInsert(){ UpdateIdentification(); } protected override void afterUpdate(){ Invalid(); } //更新标识 private void UpdateIdentification(){ //存产品的ID List pList = new List(); for(LastbuyProduct__c lbp : newList){ if(lbp.ProductName__c != null){ pList.add(lbp.ProductName__c); } } List proList = [select id, LastbuyProductFLG__c from Product2 where Id in :pList]; List pflgList = new List(); for(Product2 pro : proList){ if(pro.LastbuyProductFLG__c == false){ pro.LastbuyProductFLG__c = true; pflgList.add(pro); } } if(pflgList.size() > 0){ update pflgList; } } //当"是否有效"发生变化且变成"否"时,检索同一产品的所有预留产品, //如果这个产品所有的预留产品都无效了,把产品主数据上的预留产品标签设置成false。 //当"是否有效"发生变化且变成"是"时,产品主数据上的预留产品标签设置成true。 private void Invalid(){ List pfList = new List(); //"是否有效" 变为 "否" 时 所对应的产品主数据Id List ptList = new List(); //"是否有效" 变为 "是" 时 所对应的产品主数据Id for(LastbuyProduct__c lbp1 : newList){ LastbuyProduct__c oldLbp1 = oldMap.get(lbp1.Id); if(oldLbp1.effectiveFLG__c != lbp1.effectiveFLG__c){ if(lbp1.effectiveFLG__c == false){ pfList.add(lbp1.ProductName__c); }else{ //lbp1.effectiveFLG__c == true ptList.add(lbp1.ProductName__c); } } } //"预留产品"标识 变"false" if(pfList.size() > 0){ //Map<产品Id,预留产品> Map lbpMap = new Map(); //发生变化的产品Id下的所有预留产品 List lbpList = [select id, effectiveFLG__c,ProductName__c from LastbuyProduct__c where ProductName__c in :pfList]; System.debug('lt123变化的预留产品lbpList'+lbpList); //p1List effectiveFLG__c为true 的产品Id List p1List = new List(); //p2List 这个产品所有的预留产品都无效 的产品Id List p2List = new List(); for(LastbuyProduct__c lbm : lbpList){ lbpMap.put(lbm.ProductName__c,lbm); if(lbm.effectiveFLG__c == true){ p1List.add(lbm.ProductName__c); } } if(p1List.size() > 0){ for(Id p1 : p1List){ if(!lbpMap.containsKey(p1)){ p2List.add(p1); } } }else{ for(LastbuyProduct__c lbm : lbpList){ p2List.add(lbm.ProductName__c); } } List pro1List = [select Id, LastbuyProductFLG__c from Product2 where Id in :p2List]; List prflgList = new List(); for(Product2 pro1 : pro1List){ pro1.LastbuyProductFLG__c = false; System.debug('lt123预留产品标识'+pro1.LastbuyProductFLG__c); prflgList.add(pro1); } if(prflgList.size() > 0){ update prflgList; } } //"预留产品"标识 变"true" if(ptList.size() > 0){ List protList = [select Id, LastbuyProductFLG__c from Product2 where Id in :ptList]; List ptflgList = new List(); for(Product2 prot : protList){ prot.LastbuyProductFLG__c = true; ptflgList.add(prot); } if(ptflgList.size() > 0){ update ptflgList; } } } }