高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
 * 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;
            }
        }
 
    }
 
}