高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
force-app/main/default/classes/LastbuyProductHandler.cls
@@ -24,6 +24,10 @@
        UpdateIdentification();
    }
    protected override void afterUpdate(){
        Invalid();
    }
    //更新标识
    private void UpdateIdentification(){
    
@@ -38,10 +42,10 @@
        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);
        for(Product2 pro : proList){
            if(pro.LastbuyProductFLG__c == false){
                pro.LastbuyProductFLG__c = true;
                pflgList.add(pro);
            }
        }
@@ -50,4 +54,81 @@
        }
        
    }
    //当"是否有效"发生变化且变成"否"时,检索同一产品的所有预留产品,
    //如果这个产品所有的预留产品都无效了,把产品主数据上的预留产品标签设置成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;
            }
        }
    }
}