/**
 * 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();
    }

    //更新标识
    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 lbp : proList){
            if(lbp.LastbuyProductFLG__c == false){
                lbp.LastbuyProductFLG__c = true;
                pflgList.add(lbp);
            }
        }

        if(pflgList.size() > 0){
            update pflgList;
        }
        
    }
}