高章伟
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
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
135
public without sharing class PowerBIBaseHandler{
    public void test1(){
    }
}/*
public without sharing class PowerBIBaseHandler extends Oly_TriggerHandler {
    private static Map<String, Schema.SObjectType> allTableDesc;
 
    private Map<Id, SObject> newMap;
    private Map<Id, SObject> oldMap;
    private List<SObject> newList;
    private List<SObject> oldList;
 
    public PowerBIBaseHandler() {
        this.newMap = (Map<Id, SObject>) Trigger.newMap;
        this.oldMap = (Map<Id, SObject>) Trigger.oldMap;
        this.newList = (List<SObject>) Trigger.new;
        this.oldList = (List<SObject>) Trigger.old;
    }
 
    protected override void afterInsert() {
        upsertPowerBITable();
    }
    protected override void afterUpdate() {
        upsertPowerBITable();
    }
    protected override void afterUnDelete() {
        upsertPowerBITable();
    }
    protected override void afterDelete() {
        deletePowerBITable();
    }
 
 
    ////////////////////////// 这里开始是自己的Logic //////////////////////////
    private void upsertPowerBITable() {
    system.debug('=============upsertPowerBITable');
        Schema.SObjectType tableType = null;
        Schema.SObjectField idfs = null;
        List<SObject> targeList = new List<SObject>();
        List<PowerBISyncDefine__c> pbDFList = [SELECT Name, To_Table__c
                        FROM PowerBISyncDefine__c
                        WHERE Delete_Flag__c = false
                        ORDER BY CreatedDate desc];
        Map<String, String> pbDFMap = new Map<String, String>();
        for (PowerBISyncDefine__c pbDF : pbDFList) {
            pbDFMap.put(pbDF.Name, pbDF.To_Table__c);
        }
        boolean hasLookUpId = false;
 
        for (SObject nObj : newList) {
            if (idfs == null) {
                String tableApiName = nObj.getSObjectType().getDescribe().getName();
                // SWAG-BCF4KY 20190524 LHJ 目标询价跳过处理 Start
                if (tableApiName == 'Opportunity' && nObj.get('RecordTypeId') == '01210000000QekP') {
                    Continue;
                }
                // SWAG-BCF4KY 20190524 LHJ 目标询价跳过处理 End
                if (pbDFMap.containsKey(tableApiName)) {
                    if (allTableDesc == null) {
                        allTableDesc = Schema.getGlobalDescribe();
                    }
                    // TODO 暂时只对应一个 To_Table__c
                    tableType = allTableDesc.get(pbDFMap.get(tableApiName));
                    idfs = tableType.getDescribe().fields.getMap().get('PowerBI_Source_Id__c');
                    hasLookUpId = tableType.getDescribe().fields.getMap().keySet().contains('powerbi_source_lookup_id__c');
                }
            }
            if (tableType != null) {
                SObject ups = tableType.newSObject();
                targeList.add(ups);
                ups.put('PowerBI_Source_Id__c', nObj.get('Id'));
                ups.put('CurrencyIsoCode', nObj.get('CurrencyIsoCode'));
                if (hasLookUpId) {
                    ups.put('PowerBI_Source_LookUp_Id__c', nObj.get('Id'));
                }
                if (nObj.get('Name') != null) {
                    String name80 = ((String) nObj.get('Name')).left(80);
                    ups.put('Name', name80);
                }
                // TODO weiwei
                // 读 fieldset 然后 Loop
                // TODO 暂时只对应一个 To_Table__c
                //ups.get(xxxxx) = nObj.get(XXXX)
                List<Schema.SObjectField> fieldList = tableType.getDescribe().fields.getMap().values();
                //Schema.FieldSet fs = fsMap.get('FieldSet_Api__c');
                //List<FieldSetMember> fsmList = fs.getFields();
                for (SObjectField fsm : fieldList) {
                    if(!fsm.getDescribe().isCalculated() && fsm.getDescribe().isCustom()
                        && fsm.getDescribe().getName() != 'PowerBI_Source_Id__c'
                        && fsm.getDescribe().getName() != 'PowerBI_Source_LookUp_Id__c')
                    {
                        ups.put(fsm.getDescribe().getName(), nObj.get(fsm.getDescribe().getName()));
                    }
                }
            }
        }
        if (targeList.size() > 0) {
            String listType = 'List<' + tableType + '>';
            List<SObject> castRecords = (List<SObject>)Type.forName(listType).newInstance();
            system.debug('test PBI');
            castRecords.addAll(targeList);
            Database.upsert(castRecords, idfs);
        }
    }
 
    private void deletePowerBITable() {
        Schema.SObjectType tableType = null;
        Schema.SObjectField idfs = null;
        List<SObject> targeList = new List<SObject>();
        List<PowerBISyncDefine__c> pbDFList;
 
        String tableApiName = oldList[0].getSObjectType().getDescribe().getName();
        pbDFList = [SELECT Name, To_Table__c
                        FROM PowerBISyncDefine__c
                        WHERE Name = :tableApiName
                          AND Delete_Flag__c = false
                        ORDER BY CreatedDate desc];
        if (pbDFList.size() > 0) {
            if (allTableDesc == null) {
                 allTableDesc = Schema.getGlobalDescribe();
            }
                    // TODO 暂时只对应一个 To_Table__c
            tableType = allTableDesc.get(pbDFList[0].To_Table__c);
            idfs = tableType.getDescribe().fields.getMap().get('PowerBI_Source_Id__c');
        }
        Set<String> delIds = new Set<String>();
        for (SObject nObj : oldList) {
            delIds.add(nObj.Id);
        }
        String sql = 'SELECT id FROM ' + tableType + ' WHERE PowerBI_Source_Id__c in :delIds';
        List<SObject> delList = Database.query(sql);
        Database.delete(delList);
    }
}
*/