buli
2023-06-05 18bf19076af649baa562d3d9cf264386ff1677f9
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
trigger SyncOpportunity on Opportunity (after delete, after insert, after update) {
    // SWAG-CE55BX 预测优化 start
    if (StaticParameter.EscapeOppandStaTrigger) {
        return;
    }
    // SWAG-CE55BX 预测优化 end
    if (StaticParameter.EscapeSyncOpportunityTrigger) {
System.debug('Escape、EscapeSyncOpportunityTrigger:::::' + StaticParameter.EscapeSyncOpportunityTrigger);
        return;
    }
    
    List<String> oppIds = new List<String>();
    List<Opportunity> opps = new List<Opportunity>();
System.debug('SyncOpportunity start');
    
    if (Trigger.isDelete) {
        for (Opportunity opp : Trigger.old) {
            oppIds.add(opp.Id);
            opps.add(opp);
        }
    } else {
System.debug('SyncOpportunity loop Trigger.new');
        for (Opportunity opp : Trigger.new) {
            // 古いデータの更新を禁止
            if (Trigger.isUpdate) {
                //20221229 lt DB202211430986 询价历史不使用字段
                // Boolean noChange = false;
                Opportunity oldOpp = (Opportunity) Trigger.oldMap.get(opp.Id);
                // for (String colApiName : ControllerUtil.oppColumnList) {
                //     colApiName = colApiName.trim();
                //     if (opp.oldData_flg__c && 
                //             Trigger.oldMap.get(opp.Id).get(colApiName) != Trigger.newMap.get(opp.Id).get(colApiName)) {
                //         noChange = true;
                //         break;
                //     }
                // }
                // if (noChange) {
                //     opp.addError('不能修改旧数据');
                //     continue;
                // }
                //20221229 lt DB202211430986 询价历史不使用字段
                // 更新だけど、Opportunity2__c の同期対象になるか?
                if (oldOpp.OwnerId == opp.OwnerId
                        && oldOpp.State_Text__c == opp.State_Text__c
                        && oldOpp.StageName == opp.StageName
                        && oldOpp.Wholesale_Price__c == opp.Wholesale_Price__c
                        && oldOpp.Estimation_List_Price__c == opp.Estimation_List_Price__c
                ) {
                    System.debug('Opportunity2__c の同期対象ではない!');
                    continue;
                }
            }
System.debug('SyncOpportunity sync target:id=' + opp.Id);
            oppIds.add(opp.Id);
            opps.add(opp);
        }
    }
    
    Map<String, Opportunity2__c> opp2Map = new Map<String, Opportunity2__c>();
    if (oppIds.size() == 0) return;
    List<Opportunity2__c> opp2s = ControllerUtil.opp2SelectForSync(oppIds);
    for (Opportunity2__c opp2 : opp2s) {
        opp2Map.put(opp2.Opportunity__c, opp2);
    }
    
    List<Opportunity2__c> insOpp2 = new List<Opportunity2__c>();
    List<Opportunity2__c> updelOpp2 = new List<Opportunity2__c>();
    for (Opportunity opp : opps) {
        // 見付かったら更新/削除
        if (opp2Map.containsKey(opp.Id)) {
            Opportunity2__c opp2 = opp2Map.get(opp.Id);
            opp2.OwnerId = (opp.Owner_IsActive__c == true) ? opp.OwnerId : UserInfo.getUserId();
            opp2.Owner_System__c = (opp.Owner_IsActive__c == true) ? opp.OwnerId : UserInfo.getUserId();
            opp2.State_Text__c = opp.State_Text__c;
            opp2.StageName_Text__c = opp.StageName;
            opp2.Wholesale_Price_T__c = opp.Wholesale_Price__c;
            opp2.Amount_Without_Tax_T__c = opp.Amount_Without_Tax__c;
            opp2.Estimation_List_Price_T__c = opp.Estimation_List_Price__c;
            opp2.Estimation_List_Price_Without_Tax_T__c = opp.Estimation_List_Price_Without_Tax__c;
            updelOpp2.add(opp2);
        // 新規
        } else {
            Opportunity2__c opp2 = new Opportunity2__c(
                Opportunity__c = opp.Id,
                OwnerId = (opp.Owner_IsActive__c == true) ? opp.OwnerId : UserInfo.getUserId(),
                Owner_System__c = (opp.Owner_IsActive__c == true) ? opp.OwnerId : UserInfo.getUserId(),
                State_Text__c = opp.State_Text__c,
                StageName_Text__c = opp.StageName,
                CurrencyIsoCode = opp.CurrencyIsoCode,
                Wholesale_Price_T__c = opp.Wholesale_Price__c,
                Amount_Without_Tax_T__c = opp.Amount_Without_Tax__c,
                Estimation_List_Price_T__c = opp.Estimation_List_Price__c,
                Estimation_List_Price_Without_Tax_T__c = opp.Estimation_List_Price_Without_Tax__c
            );
            insOpp2.add(opp2);
        }
    }
    
    if (Trigger.isDelete) ControllerUtil.delOpp2List(updelOpp2);
    if (Trigger.isUpdate) ControllerUtil.updOpp2List(updelOpp2);
    if (!Trigger.isDelete) ControllerUtil.insOpp2List(insOpp2);
}