高章伟
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
public without sharing class ProductScoreTableHistoryHandler extends Oly_TriggerHandler {
 
    private Map<Id, Product_Score_Table_History__c> newMap;
    private Map<Id, Product_Score_Table_History__c> oldMap;
    private List<Product_Score_Table_History__c> newList;
    private List<Product_Score_Table_History__c> oldList;
 
    public ProductScoreTableHistoryHandler() {
        this.newMap = (Map<Id, Product_Score_Table_History__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Product_Score_Table_History__c>) Trigger.oldMap;
        this.newList = (List<Product_Score_Table_History__c>) Trigger.new;
        this.oldList = (List<Product_Score_Table_History__c>) Trigger.old;
    }
 
    protected override void beforeInsert() {
        setproduct_CategoryPriceInfo();
    }
 
    protected override void beforeUpdate() {
        //setproduct_CategoryPriceInfo();
    }
 
    private void setproduct_CategoryPriceInfo() {
        Id PSTH_RecordTypeId = Schema.SObjectType.Product_Score_Table_History__c.getRecordTypeInfosByDeveloperName().get('TargetPDCA').getRecordTypeId();
 
        //产品分类价格表
        List<String> pstIdList = new List<String>();
        for (Product_Score_Table_History__c psth : newList) {
            if (psth.RecordTypeId == PSTH_RecordTypeId && String.isNotBlank(psth.UniqueKey__c)) {
                pstIdList.add(psth.UniqueKey__c.split(':')[1] + ':' + psth.Category3__c +':'+ psth.Category4__c);
            }
        }
        
        Map<String, Product_CategoryPrice_Table__c> pstIdMap = new Map<String, Product_CategoryPrice_Table__c>();
        for(Product_CategoryPrice_Table__c tmpObj : [SELECT Id, UniqueKey__c
                                                     FROM Product_CategoryPrice_Table__c
                                                     WHERE UniqueKey__c IN :pstIdList]){
                                                         pstIdMap.put(tmpObj.UniqueKey__c, tmpObj);
                                                     }
        
        for (Product_Score_Table_History__c psth : newList) {
            if (psth.RecordTypeId == PSTH_RecordTypeId && String.isNotBlank(psth.UniqueKey__c)) {
                String pstUniqueKey = psth.UniqueKey__c.split(':')[1] + ':' + psth.Category3__c +':'+ psth.Category4__c;
                if (pstIdMap.containsKey(pstUniqueKey)) {
                    psth.Product_CategoryPrice_Table__c = pstIdMap.get(pstUniqueKey).Id;
                }
            }
        }
    }
}