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