| | |
| | | /** |
| | | * 20220210 lt UpdateIdentification()--更新产品上的“预留产品”标识 |
| | | * 预留产品对象会关联一个产品主数据 |
| | | * 创建预留产品时,将产品主数据上的“预留产品”标识 更新为 true |
| | | * 20220210 lt 更新产品上的“预留产品”标识 |
| | | * 产品 有 有效预留产品 “预留产品”标识 true |
| | | * 产品 没有 有效预留产品(无预留产品 或 预留产品 无效) “预留产品”标识 false |
| | | * (1) 新建 有效预留产品 --true |
| | | * (2) 更新 “是否有效” false 变为 true --true |
| | | * (3) 更新 “产品” 后 --true |
| | | * (4) 更新 “是否有效” true 变为 false 的产品下无有效预留产品 --false |
| | | * (5) 更新 “产品” 前的产品 无预留产品 或 无有效预留产品 --false |
| | | */ |
| | | public without sharing class LastbuyProductHandler extends Oly_TriggerHandler { |
| | | private Map<Id, LastbuyProduct__c> newMap; |
| | |
| | | } |
| | | |
| | | protected override void beforeInsert(){ |
| | | |
| | | //SWAG-CKADFQ 【委托】【Last Buy】Last Buy预留管理改善新需求 start |
| | | SetUniqueKey(null); |
| | | //SWAG-CKADFQ 【委托】【Last Buy】Last Buy预留管理改善新需求 end |
| | | } |
| | | |
| | | protected override void beforeUpdate(){ |
| | | //SWAG-CKADFQ 【委托】【Last Buy】Last Buy预留管理改善新需求 start |
| | | CheckInquiryWinAboutupdate(); |
| | | SetUniqueKeyOnUpdate(); |
| | | //SWAG-CKADFQ 【委托】【Last Buy】Last Buy预留管理改善新需求 end |
| | | } |
| | | protected override void afterInsert(){ |
| | | UpdateIdentification(); |
| | | ReservedPro(); |
| | | } |
| | | |
| | | //更新标识 |
| | | private void UpdateIdentification(){ |
| | | |
| | | //存产品的ID |
| | | List<Id> pList = new List<Id>(); |
| | | for(LastbuyProduct__c lbp : newList){ |
| | | if(lbp.ProductName__c != null){ |
| | | pList.add(lbp.ProductName__c); |
| | | protected override void afterUpdate(){ |
| | | ReservedPro(); |
| | | } |
| | | |
| | | protected override void afterDelete(){ |
| | | ReservedPro(); |
| | | } |
| | | //SWAG-CKADFQ 【委托】【Last Buy】Last Buy预留管理改善新需求 start |
| | | protected override void beforeDelete(){ |
| | | CheckInquiryWin(); |
| | | } |
| | | //SWAG-CKADFQ 【委托】【Last Buy】Last Buy预留管理改善新需求 end |
| | | private void ReservedPro(){ |
| | | //1.新建或者更新产品后,若预留产品有效,“预留产品”标识为true |
| | | if(trigger.isInsert || trigger.isUpdate){ |
| | | |
| | | //存产品的ID |
| | | List<Id> pList = new List<Id>(); |
| | | for(LastbuyProduct__c lbp : newList){ |
| | | if(lbp.ProductName__c != null && lbp.effectiveFLG__c == true){ |
| | | pList.add(lbp.ProductName__c); |
| | | } |
| | | } |
| | | |
| | | List<Product2> proList = [select id, LastbuyProductFLG__c from Product2 where Id in :pList]; |
| | | List<Product2> pflgList = new List<Product2>(); |
| | | |
| | | for(Product2 pro : proList){ |
| | | if(pro.LastbuyProductFLG__c == false){ |
| | | pro.LastbuyProductFLG__c = true; |
| | | pflgList.add(pro); |
| | | } |
| | | } |
| | | |
| | | if(pflgList.size() > 0){ |
| | | update pflgList; |
| | | } |
| | | } |
| | | |
| | | //2.(1)"是否有效" 变为 false 检索产品主数据下的所有预留产品,全无效则把产品主数据的"预留产品"标识 变为 false |
| | | // (2)"产品" 预留产品 更改 产品主数据之前的产品 -- 检索产品主数据下的所有预留产品(没有/全无效) |
| | | // "预留产品"标识 变为 false |
| | | if(trigger.isUpdate || trigger.isDelete){ |
| | | //"是否有效" 变为 "否" 时 所对应的产品主数据Id &&预留产品 更改 产品主数据之前的产品 |
| | | List<Id> pfList = new List<Id>(); |
| | | |
| | | if(trigger.isUpdate){ |
| | | 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); |
| | | } |
| | | } |
| | | //产品发生变化时,检索两个产品的标识 |
| | | if(oldLbp1.ProductName__c != lbp1.ProductName__c){ |
| | | pfList.add(oldLbp1.ProductName__c); |
| | | } |
| | | } |
| | | } |
| | | else if(trigger.isDelete){ |
| | | for(LastbuyProduct__c lbp2 : oldList){ |
| | | pfList.add(lbp2.ProductName__c); |
| | | } |
| | | } |
| | | |
| | | |
| | | System.debug('lt123需检索所有预留产品的产品ID-pfList'+pfList); |
| | | |
| | | //检索产品下的所有预留产品,全无效将产品主数据下的标识置为false |
| | | if(pfList.size() > 0){ |
| | | //Map<产品Id,预留产品> |
| | | Map<String,LastbuyProduct__c> lbpMap = new Map<String,LastbuyProduct__c>(); |
| | | //发生变化的产品Id下的所有预留产品 |
| | | List<LastbuyProduct__c> lbpList = [select id, effectiveFLG__c,ProductName__c from LastbuyProduct__c where ProductName__c in :pfList]; |
| | | //p1List effectiveFLG__c为true 的产品Id |
| | | List<Id> p1List = new List<Id>(); |
| | | //p2List 这个产品所有的预留产品都无效 的产品Id |
| | | List<Id> p2List = new List<Id>(); |
| | | |
| | | if(lbpList.size() > 0){ //产品有预留产品 检查预留产品的"是否有效" |
| | | 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); |
| | | } |
| | | } |
| | | |
| | | } |
| | | else{ //产品 没有预留产品 标识false |
| | | p2List = pfList; |
| | | } |
| | | |
| | | List<Product2> pro1List = [select Id, LastbuyProductFLG__c from Product2 where Id in :p2List]; |
| | | List<Product2> prflgList = new List<Product2>(); |
| | | for(Product2 pro1 : pro1List){ |
| | | pro1.LastbuyProductFLG__c = false; |
| | | prflgList.add(pro1); |
| | | } |
| | | if(prflgList.size() > 0){ |
| | | update prflgList; |
| | | } |
| | | } |
| | | } |
| | | |
| | | List<Product2> proList = [select id, LastbuyProductFLG__c from Product2 where Id in :pList]; |
| | | List<Product2> pflgList = new List<Product2>(); |
| | | |
| | | for(Product2 lbp : proList){ |
| | | if(lbp.LastbuyProductFLG__c == false){ |
| | | lbp.LastbuyProductFLG__c = true; |
| | | pflgList.add(lbp); |
| | | } |
| | | //SWAG-CKADFQ 【委托】【Last Buy】Last Buy预留管理改善新需求 start |
| | | private void SetUniqueKey(Set<Id> lbps){ |
| | | if(trigger.isInsert) { |
| | | for (LastbuyProduct__c Lastbuyobj : this.newList){ |
| | | Lastbuyobj.productInquiryOnly__c = Lastbuyobj.ProductName__c+'_'+Lastbuyobj.InquiryCode__c; |
| | | } |
| | | } |
| | | |
| | | if(pflgList.size() > 0){ |
| | | update pflgList; |
| | | } else { |
| | | if(lbps!=null&&lbps.size() > 0){ |
| | | for(String id : lbps){ |
| | | LastbuyProduct__c Lastbuyobj = this.newMap.get(id); |
| | | Lastbuyobj.productInquiryOnly__c = Lastbuyobj.ProductName__c+'_'+Lastbuyobj.InquiryCode__c; |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | private void CheckInquiryWin(){ |
| | | if(trigger.isBefore && trigger.isDelete){ |
| | | List<Id> InquiryId = new List<Id>(); |
| | | List<Id> InquiryWinId = new List<Id>(); |
| | | for(LastbuyProduct__c Lastbuyobj : oldList){ |
| | | InquiryId.add(Lastbuyobj.InquiryCode__c); |
| | | } |
| | | List<Opportunity> OppList = [select Id,SAP_Send_OK__c from Opportunity where Id =:InquiryId]; |
| | | System.debug('OppList++'+OppList); |
| | | for(Opportunity oppobj : OppList){ |
| | | if(oppobj.SAP_Send_OK__c){ |
| | | // InquiryWinId.add(oppobj.Id); |
| | | throw new ControllerUtil.myException('预留询价已经WIN不允许删除'); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 询价发生变化时验证 |
| | | private void CheckInquiryWinAboutupdate(){ |
| | | if(trigger.isBefore && trigger.isUpdate){ |
| | | List<Id> LastbuyId = new List<Id>(); |
| | | List<Id> oppId = new List<Id>(); |
| | | for(LastbuyProduct__c Lastbuyobj : newList){ |
| | | // 只取询价发生变化的数据 |
| | | if (Lastbuyobj.InquiryCode__c != oldMap.get(Lastbuyobj.Id).InquiryCode__c) { |
| | | LastbuyId.add(Lastbuyobj.Id); |
| | | oppId.add(oldMap.get(Lastbuyobj.Id).InquiryCode__c); |
| | | } |
| | | } |
| | | List<Opportunity> oppList = [select Id,SAP_Send_OK__c from Opportunity where Id =:oppId]; |
| | | for(Opportunity opp : oppList){ |
| | | if(opp.SAP_Send_OK__c){ |
| | | throw new ControllerUtil.myException('预留的询价已经WIN,不允许替换,请取消WIN后再替换'); |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | // 询价或产品发生变化时更新唯一标识 |
| | | private void SetUniqueKeyOnUpdate() { |
| | | Set<Id> updateUniKeys = new Set<Id>(); |
| | | for(LastbuyProduct__c Lastbuyobj : this.newList){ |
| | | LastbuyProduct__c old_Lastbuyobj = oldMap.get(Lastbuyobj.Id); |
| | | // 产品或询价变化的时候都加进来 |
| | | if (Lastbuyobj.InquiryCode__c != old_Lastbuyobj.InquiryCode__c || |
| | | Lastbuyobj.ProductName__c != old_Lastbuyobj.ProductName__c || |
| | | String.isBlank(Lastbuyobj.productInquiryOnly__c)) { |
| | | updateUniKeys.add(Lastbuyobj.Id); |
| | | } |
| | | } |
| | | SetUniqueKey(updateUniKeys); |
| | | } |
| | | //SWAG-CKADFQ 【委托】【Last Buy】Last Buy预留管理改善新需求 end |
| | | |
| | | } |