binxie
2023-06-26 de9565270a88b0749d17c1961cd41399c8483c96
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
trigger ReportCPileUp on Report__c (after delete, after insert, after update) {
    if((!Test.isRunningTest())&&UserInfo.getUserId()==System.Label.ByPassTrigger){
        return;
    }
// TODO 今後 Owner_System__c を設定する場合があります。
//    // LastModifiedDateとCreatedDateの比較での判断は無理、WF経由なので新規時も時間がずれてる
//    // なのでここで設定するしかないです。
//    if (Trigger.isBefore) {
//        for (Report__c rpt : Trigger.new) {
//            if (Trigger.isInsert || Trigger.isUpdate && Trigger.oldMap.get(rpt.Id).get('Status__c') != '申請中') {
//                if (rpt.Status__c == '申請中') {
//                    rpt.Submit_report_day__c = Date.today();
//                }
//            }
//        }
//    } else {
        // 商談に Num_Of_OPD__c を積み上げ
        List<String> oppIds = new List<String>();
    
        RecordType rt = [select Id from RecordType where IsActive = true and DeveloperName = 'OPD' and SObjectType = 'Report__c'];
    
        if (Trigger.isDelete) {
            for (Report__c rpt : Trigger.old) {
                if (rpt.RecordTypeId == rt.Id) {
                    if (String.isBlank(rpt.Opportunity1__c) == false) oppIds.add(rpt.Opportunity1__c);
                    if (String.isBlank(rpt.Opportunity2__c) == false) oppIds.add(rpt.Opportunity2__c);
                    if (String.isBlank(rpt.Opportunity3__c) == false) oppIds.add(rpt.Opportunity3__c);
                    if (String.isBlank(rpt.Opportunity4__c) == false) oppIds.add(rpt.Opportunity4__c);
                    if (String.isBlank(rpt.Opportunity5__c) == false) oppIds.add(rpt.Opportunity5__c);
                }
            }
        } 
        if (Trigger.isInsert) {
            for (Report__c rpt : Trigger.new) {
                // OPD
                if (rpt.RecordTypeId == rt.Id) {
                    if (String.isBlank(rpt.Opportunity1__c) == false)
                        oppIds.add(rpt.Opportunity1__c);
                    if (String.isBlank(rpt.Opportunity2__c) == false)
                        oppIds.add(rpt.Opportunity2__c);
                    if (String.isBlank(rpt.Opportunity3__c) == false)
                        oppIds.add(rpt.Opportunity3__c);
                    if (String.isBlank(rpt.Opportunity4__c) == false)
                        oppIds.add(rpt.Opportunity4__c);
                    if (String.isBlank(rpt.Opportunity5__c) == false)
                        oppIds.add(rpt.Opportunity5__c);
                }
            }
        }
        if (Trigger.isUpdate) {
            for (Report__c rpt : Trigger.new) {
                Report__c oldrpt = Trigger.oldMap.get(rpt.Id);
                Boolean statusChange = false;
                if (oldrpt.Status__c != rpt.Status__c) {
                    StatusChange = true;
                }
                
                // OPD
                if (rpt.RecordTypeId == rt.Id) {
                    if (oldrpt.Opportunity1__c != rpt.Opportunity1__c || statusChange) {
                        if (String.isBlank(rpt.Opportunity1__c) == false) {
                            oppIds.add(rpt.Opportunity1__c);
                        }
                        if (oldrpt.Opportunity1__c !=  null) {
                            oppIds.add(oldrpt.Opportunity1__c);
                        }
                    }
                    if (oldrpt.Opportunity2__c != rpt.Opportunity2__c || statusChange) {
                        if (String.isBlank(rpt.Opportunity2__c) == false) {
                            oppIds.add(rpt.Opportunity2__c);
                        }
                        if (oldrpt.Opportunity2__c !=  null) {
                            oppIds.add(oldrpt.Opportunity2__c);
                        }
                    }
                    if (oldrpt.Opportunity3__c != rpt.Opportunity3__c || statusChange) {
                        if (String.isBlank(rpt.Opportunity3__c) == false) {
                            oppIds.add(rpt.Opportunity3__c);
                        }
                        if (oldrpt.Opportunity3__c !=  null) {
                            oppIds.add(oldrpt.Opportunity3__c);
                        }
                    }
                    if (oldrpt.Opportunity4__c != rpt.Opportunity4__c || statusChange) {
                        if (String.isBlank(rpt.Opportunity4__c) == false) {
                            oppIds.add(rpt.Opportunity4__c);
                        }
                        if (oldrpt.Opportunity4__c !=  null) {
                            oppIds.add(oldrpt.Opportunity4__c);
                        }
                    }
                    if (oldrpt.Opportunity5__c != rpt.Opportunity5__c || statusChange) {
                        if (String.isBlank(rpt.Opportunity5__c) == false) {
                            oppIds.add(rpt.Opportunity5__c);
                        }
                        if (oldrpt.Opportunity5__c !=  null) {
                            oppIds.add(oldrpt.Opportunity5__c);
                        }
                    }
                }
            }
        }
        
        if (oppIds.size() > 0) {
            // 先に親をlockする
            List<Opportunity> opps = new List<Opportunity>();
            List<Opportunity> oppList = ControllerUtil.oppSelectForLock(oppIds);
    
            // 各種カウント
            Map<Id, Integer> opdCntMap = new Map<Id, Integer>();           // oppId => OPD件数
            List<Report__c> opdOppList = ControllerUtil.selectReportForAggregateOpdToOpp(oppIds);
System.debug('opdOppList.size():::::' + opdOppList.size());
            for (Report__c rpt : opdOppList) {
System.debug('rpt.Opportunity1__c:::::' + rpt.Opportunity1__c);
System.debug('rpt.Opportunity2__c:::::' + rpt.Opportunity2__c);
System.debug('rpt.Opportunity3__c:::::' + rpt.Opportunity3__c);
System.debug('rpt.Opportunity4__c:::::' + rpt.Opportunity4__c);
System.debug('rpt.Opportunity5__c:::::' + rpt.Opportunity5__c);
                if (String.isBlank(rpt.Opportunity1__c) == false) {
                    if (opdCntMap.containsKey(rpt.Opportunity1__c)) {
                        opdCntMap.put(rpt.Opportunity1__c, opdCntMap.get(rpt.Opportunity1__c) + 1);
                    } else {
                        opdCntMap.put(rpt.Opportunity1__c, 1);
                    }
                } else {
                    opdCntMap.put(rpt.Opportunity1__c, 0);
                }
                if (String.isBlank(rpt.Opportunity2__c) == false) {
                    if (opdCntMap.containsKey(rpt.Opportunity2__c)) {
                        opdCntMap.put(rpt.Opportunity2__c, opdCntMap.get(rpt.Opportunity2__c) + 1);
                    } else {
                        opdCntMap.put(rpt.Opportunity2__c, 1);
                    }
                } else {
                    opdCntMap.put(rpt.Opportunity2__c, 0);
                }
                if (String.isBlank(rpt.Opportunity3__c) == false) {
                    if (opdCntMap.containsKey(rpt.Opportunity3__c)) {
                        opdCntMap.put(rpt.Opportunity3__c, opdCntMap.get(rpt.Opportunity3__c) + 1);
                    } else {
                        opdCntMap.put(rpt.Opportunity3__c, 1);
                    }
                } else {
                    opdCntMap.put(rpt.Opportunity3__c, 0);
                }
                if (String.isBlank(rpt.Opportunity4__c) == false) {
                    if (opdCntMap.containsKey(rpt.Opportunity4__c)) {
                        opdCntMap.put(rpt.Opportunity4__c, opdCntMap.get(rpt.Opportunity4__c) + 1);
                    } else {
                        opdCntMap.put(rpt.Opportunity4__c, 1);
                    }
                } else {
                    opdCntMap.put(rpt.Opportunity4__c, 0);
                }
                if (String.isBlank(rpt.Opportunity5__c) == false) {
                    if (opdCntMap.containsKey(rpt.Opportunity5__c)) {
                        opdCntMap.put(rpt.Opportunity5__c, opdCntMap.get(rpt.Opportunity5__c) + 1);
                    } else {
                        opdCntMap.put(rpt.Opportunity5__c, 1);
                    }
                } else {
                    opdCntMap.put(rpt.Opportunity5__c, 0);
                }
            }
    
            for (Opportunity opp : oppList) {
                if (opp.Num_Of_OPD__c != (opdCntMap.containsKey(opp.Id) ? opdCntMap.get(opp.Id) : 0)) {
                    opps.add(opp);
                    opp.Num_Of_OPD__c = opdCntMap.containsKey(opp.Id) ? opdCntMap.get(opp.Id) : 0;
                }
            }
            
            if (opps.size() > 0) {
                StaticParameter.EscapeNFM007Trigger = true;
                StaticParameter.EscapeOpportunityBefUpdTrigger = true;
                StaticParameter.EscapeOpportunityHpDeptUpdTrigger = true;
                StaticParameter.EscapeSyncOpportunityTrigger = true;
System.debug('ReportCPileUpから、商談トリガをEscape');
System.debug('EscapeNFM007Trigger:::::' + StaticParameter.EscapeNFM007Trigger);
System.debug('EscapeOpportunityBefUpdTrigger:::::' + StaticParameter.EscapeOpportunityBefUpdTrigger);
System.debug('EscapeOpportunityHpDeptUpdTrigger:::::' + StaticParameter.EscapeOpportunityHpDeptUpdTrigger);
System.debug('EscapeSyncOpportunityTrigger:::::' + StaticParameter.EscapeSyncOpportunityTrigger);
                ControllerUtil.updOppList(opps);
            }
        }
//    }
}