高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
 * 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;
        }
        
    }
}