高章伟
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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/**
 * 引合チーム「副担当」重複のチェック、「副担当」をチームに自動同期などの処理
 */
public without sharing class OpportunityMemberTrigger {
    // OppId ==> OpportunityTeamMember(Opportunity_sub_owner__c)
    public static Map<Id, OpportunityTeamMember> syncMBOpportunityOtmMap = new Map<Id, OpportunityTeamMember>();
    // OppId ==> OpportunityTeamMember(Opportunity_stms_owner__c)
    public static Map<Id, OpportunityTeamMember> syncMBOpportunityOtmMapSTMS = new Map<Id, OpportunityTeamMember>();
 
    // on OpportunityTeamMember (after insert, after update)
    // 引合チーム「副担当」重複エラートリガ
    public static void opportunityMemberDuplicateError(List<OpportunityTeamMember> newList, Map<Id, OpportunityTeamMember> newMap, List<OpportunityTeamMember> oldList, Map<Id, OpportunityTeamMember> oldMap) {
        Map<Id, Integer> oppTmCntMap = new Map<Id, Integer>();
        Map<Id, Integer> oppTmCntMapSTMS = new Map<Id, Integer>();
        for (OpportunityTeamMember om : newList) {
            if (om.TeamMemberRole == '副担当') {
                oppTmCntMap.put(om.OpportunityId, 0);
            }
            if (om.TeamMemberRole == 'STMS担当') {
                oppTmCntMapSTMS.put(om.OpportunityId, 0);
            }
        }
        if (oppTmCntMap.size() > 0) {
            // 副担当が既に設定済みかを抽出する(Afterなので、Trigger.newのデータも入っています)
            List<OpportunityTeamMember> oppTmList = [
                    select Id, OpportunityId, TeamMemberRole from OpportunityTeamMember
                    where OpportunityId IN :oppTmCntMap.keySet()
                    and TeamMemberRole = '副担当'];
            if (oppTmList.size() > 0) {
                for (OpportunityTeamMember oppTm : oppTmList) {
                    oppTmCntMap.put(oppTm.OpportunityId, oppTmCntMap.get(oppTm.OpportunityId) + 1);
                    OpportunityTeamMember om = newMap.get(oppTm.Id);
                    if (om != null) {
                        // プログラムのロジックからのではない場合
                        if (OpportunityMemberTrigger.syncMBOpportunityOtmMap.get(om.OpportunityId) == null) {
                            om.addError('不能直接追加修改删除[副担当],请从询价画面上操作。');
                        } else if (om.TeamMemberRole == '副担当' && oppTmCntMap.get(oppTm.OpportunityId) > 1) {
                            om.addError(System.Label.Error_Message_OppTeamMember01);
                        }
                    }
                }
            }
        }
        // CHAN-BB38N4 start stms 副担当以及没有了,去掉这个逻辑
        //if (oppTmCntMapSTMS.size() > 0) {
        //    // STMS担当が既に設定済みかを抽出する(Afterなので、Trigger.newのデータも入っています)
        //    List<OpportunityTeamMember> oppTmList = [
        //            select Id, OpportunityId, TeamMemberRole from OpportunityTeamMember
        //            where OpportunityId IN :oppTmCntMapSTMS.keySet()
        //            and TeamMemberRole = 'STMS担当'];
        //    if (oppTmList.size() > 0) {
        //        for (OpportunityTeamMember oppTm : oppTmList) {
        //            oppTmCntMapSTMS.put(oppTm.OpportunityId, oppTmCntMapSTMS.get(oppTm.OpportunityId) + 1);
        //            OpportunityTeamMember om = newMap.get(oppTm.Id);
        //            if (om != null) {
        //                // プログラムのロジックからのではない場合
        //                if (OpportunityMemberTrigger.syncMBOpportunityOtmMapSTMS.get(om.OpportunityId) == null) {
        //                    om.addError('不能直接追加修改删除[STMS担当],请从询价画面上操作。');
        //                }
        //                if (om.TeamMemberRole == 'STMS担当' && oppTmCntMapSTMS.get(oppTm.OpportunityId) > 1) {
        //                    om.addError(System.Label.Error_Message_OppTeamMember02);
        //                }
        //            }
        //        }
        //    }
        //}
        //// CHAN-BB38N4 start stms 副担当以及没有了,去掉这个逻辑
    }
 
    // on OpportunityTeamMember (after delete)
    // 引合チーム「副担当」直接削除チェック
    public static void opportunityMemberDeleteError(List<OpportunityTeamMember> newList, Map<Id, OpportunityTeamMember> newMap, List<OpportunityTeamMember> oldList, Map<Id, OpportunityTeamMember> oldMap) {
        Map<Id, Id> oppIdMap = new Map<Id, Id>();
        Map<Id, Id> oppIdMapSTMS = new Map<Id, Id>();
        for (OpportunityTeamMember om : oldList) {
            if (om.TeamMemberRole == '副担当') {
                oppIdMap.put(om.OpportunityId, om.OpportunityId);
            }
            if (om.TeamMemberRole == 'STMS担当') {
                oppIdMapSTMS.put(om.OpportunityId, om.OpportunityId);
            }
        }
        if (oppIdMap.size() > 0) {
            Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>([
                        select Id, Opportunity_sub_owner__c from Opportunity
                        where Id IN :oppIdMap.keySet()]);
            for (OpportunityTeamMember om : oldList) {
                if (om.TeamMemberRole == '副担当' && String.isBlank(oppMap.get(om.OpportunityId).Opportunity_sub_owner__c) == false) {
                    // プログラムのロジックからのではない場合
                    if (OpportunityMemberTrigger.syncMBOpportunityOtmMap.get(om.OpportunityId) == null) {
                        om.addError('不能直接追加修改删除[副担当],请从询价画面上操作。');
                    }
                }
            }
        }
        // CHAN-BB38N4 start stms 副担当以及没有了,去掉这个逻辑
        //if (oppIdMapSTMS.size() > 0) {
        //    Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>([
        //                select Id, Opportunity_stms_owner__c from Opportunity
        //                where Id IN :oppIdMapSTMS.keySet()]);
        //    for (OpportunityTeamMember om : oldList) {
        //        if (om.TeamMemberRole == 'STMS担当' && String.isBlank(oppMap.get(om.OpportunityId).Opportunity_stms_owner__c) == false) {
        //            // プログラムのロジックからのではない場合
        //            if (OpportunityMemberTrigger.syncMBOpportunityOtmMapSTMS.get(om.OpportunityId) == null) {
        //                om.addError('不能直接追加修改删除[STMS担当],请从询价画面上操作。');
        //            }
        //        }
        //    }
        //}
        //// CHAN-BB38N4 start stms 副担当以及没有了,去掉这个逻辑
    }
 
    // データ操作の共通関数
    public static void delInsOpportunityTeamMember(Map<Id, OpportunityTeamMember> otmMap) {
        syncMBOpportunityOtmMap.putAll(otmMap);
        List<OpportunityTeamMember> delOppTMList = new List<OpportunityTeamMember>();
        // oppId ==> userId
        Id breakOppId = null;
 
        //OLY_OCM-199 0512 start
        List<Id> userIdList = new List<Id>();
        for (OpportunityTeamMember otm : otmMap.values()) {
            if (otm.UserId != null) {
                userIdList.add(otm.UserId);
            }
        }
        Map<Id, User> IsActiveMap = new Map<Id, User>([SELECT Id, IsActive From User where IsActive = false AND Id IN :userIdList]);
        for (Id oppId : otmMap.keyset()) {
            if (IsActiveMap.containsKey(otmMap.get(oppId).UserId) == true) {
                otmMap.remove(oppId);
            }
        }
        //OLY_OCM-199 0512 end
 
        // CHAN-AVS9S2 修正:手动共享删除
        // HWAG-B6J4ZN 手动共享不能删除给FSE主担当的共享 start
        List<OpportunityShare> osList = [select id,UserOrGroupId,OpportunityId from OpportunityShare where OpportunityId in :otmMap.keySet() and rowcause = 'Manual'];
 
        Map<Id, AggregateResult> aggMap =
            new Map<Id, AggregateResult>([select Opportunity__r.id Id,
                // SWAG-B7LADC 2018/12/21 获取出对应医院所有的副担当,并检索出其ID start
                                          Max(Opportunity__r.Hospital__r.FSE_GI_Main_Leader__r.Alias__c) FSE_GI,
                                          Max(Opportunity__r.Hospital__r.FSE_SP_Main_Leader__r.Alias__c) FSE_SP,
                                          Max(Opportunity__r.Hospital__r.FSE_ENG_Main_Leader__r.Alias__c) FSE_ENG,
                                          Max(Opportunity__r.Hospital__r.FSE_ENG_Vice_Leader__c) FSE_ENG_VICE,
                                          Max(Opportunity__r.Hospital__r.FSE_GI_Vice_Leader__c) FSE_GI_VICE,
                                          Max(Opportunity__r.Hospital__r.FSE_SP_Vice_Leader__c) FSE_SP_VICE
                    // SWAG-B7LADC 2018/12/21 获取出对应医院所有的副担当,并检索出其ID end
                                          from Statu_Achievements__c
                                          where ForecastAccuracyObject__c = true and DeliveryDate__c != null
                                                          and Opportunity__r.id != null
                                                          and Opportunity__r.id in :otmMap.keySet()
                                                          group by Opportunity__r.id]);
        // SWAG-B7LADC 2018/12/21 获取出对应医院所有的副担当,并检索出其ID start
        list<string> allUserNameList = new list<string>();
        for(AggregateResult temAgg: aggMap.values()){
            if(temAgg.get('FSE_GI') != null){
                allUserNameList.add(String.valueOf(temAgg.get('FSE_GI')));
            }
            if(temAgg.get('FSE_SP') != null){
                allUserNameList.add(String.valueOf(temAgg.get('FSE_SP')));
            }
            if(temAgg.get('FSE_ENG') != null){
                allUserNameList.add(String.valueOf(temAgg.get('FSE_ENG')));
            }
            if(temAgg.get('FSE_ENG_VICE') != null){
                for(String UsrName : String.valueOf(temAgg.get('FSE_ENG_VICE')).split('[,\\,]')){
                     allUserNameList.add(UsrName);
                }
            }
            if(temAgg.get('FSE_GI_VICE') != null){
                for(String UsrName : String.valueOf(temAgg.get('FSE_GI_VICE')).split('[,\\,]')){
                     allUserNameList.add(UsrName);
                }
            }
            if(temAgg.get('FSE_SP_VICE') != null){
                for(String UsrName : String.valueOf(temAgg.get('FSE_SP_VICE')).split('[,\\,]')){
                     allUserNameList.add(UsrName);
                }
            } 
        }
        list<User> allFSEUserlist = [select id from User where Alias__c in: allUserNameList];
        list<ID> allFSEUserIDList = new list<ID>();
        for(User TempUser: allFSEUserlist){
            allFSEUserIDList.add(TempUser.id);
        }
        
        // SWAG-B7LADC 2018/12/21 获取出对应医院所有的副担当,并检索出其ID end
        
        // SWAG-B7LADC 2018/12/21 FSE 手动不删除 start
 
        List<OpportunityShare> DelosList = new List<OpportunityShare>();
        system.debug('aggMap:' + aggMap);
        system.debug('aggMap:' + osList);
 
        if(aggMap.size()>0){
            for ( Opportunityshare ops : osList) {
                if (!allFSEUserIDList.contains((ID)ops.UserOrGroupId)
                    && ops.UserOrGroupId != Label.Group_ServiceManagementID) {
                    //system.debug('ops.UserOrGroupId:' + ops.UserOrGroupId);
                    DelosList.add(ops);
                }
            }
        }
        // SWAG-B7LADC 2018/12/21 FSE 手动不删除 end
         // HWAG-B6J4ZN 手动共享不能删除给FSE主担当的共享 end
        if (DelosList.size() > 0) delete DelosList;
 
        // CHAN-AVS9S2
 
        for (OpportunityTeamMember otm :
                [select Id, OpportunityId, UserId
                 from OpportunityTeamMember where OpportunityId in :otmMap.keySet() and TeamMemberRole = '副担当' order by OpportunityId, CreatedDate desc]
            ) {
            // 前提、副担当1人だけです。
            if (breakOppId == null || breakOppId != otm.OpportunityId) {
                System.debug('breakOppId:' + breakOppId + 'OpportunityId:' + otm.OpportunityId);
                if (breakOppId != null && otmMap.get(breakOppId) != null && otmMap.get(breakOppId).UserId == null) {
                    otmMap.remove(breakOppId);
                }
                breakOppId = otm.OpportunityId;
                System.debug('breakOppId:' + breakOppId);
            }
            System.debug('otmMap.UserId:' + otmMap.get(otm.OpportunityId).UserId);
            System.debug('otm.UserId:' + otm.UserId);
            // 削除か、新規か を判断
            // 要注意 otmMap の UserId が nullの場合も削除します。
            if (otmMap.get(otm.OpportunityId).UserId != otm.UserId) {
                System.debug('delOppTMList.add(otm)');
                delOppTMList.add(otm);
            } else {
                System.debug('otmMap.remove(otm.OpportunityId)');
                otmMap.remove(otm.OpportunityId);
            }
        }
        // otmMapに残っているデータをloopして、UserIdがnullのものを削除
        // 例えば、削除対象になってけど、チームに副担当が居ない場合、先ほどのfor (OpportunityTeamMember otm :) よりremoveしきれないです。
        for (Id oppId : otmMap.keyset()) {
            if (otmMap.get(oppId).UserId == null) {
                otmMap.remove(oppId);
            }
        }
        if (delOppTMList.size() > 0) {
            delete delOppTMList;
        }
        if (otmMap.size() > 0) {
            insert otmMap.values();
            List<OpportunityShare> newShare = new List<OpportunityShare>();
            for (OpportunityTeamMember otm : otmMap.values()) {
                newShare.add(new OpportunityShare(
// Edit に固定 by xud 20131203
                                 UserOrGroupId = otm.UserId, OpportunityId = otm.OpportunityId, OpportunityAccessLevel = 'Edit'
                             ));
            }
            if (newShare.size() > 0) {
                Database.SaveResult[] lsr = Database.insert(newShare, false); //insert the new shares
                for (Integer sIdx = 0; sIdx < lsr.size(); sIdx++) {
                    Database.SaveResult sr = lsr[sIdx];
                    if (!sr.isSuccess()) {
                        Database.Error emsg = sr.getErrors()[0];
                        system.debug('\nERROR ADDING OPPORTUNITY SHARING:' + emsg);
                    }
                }
            }
        }
    }
 
     // CHAN-BB38N4 start stms 副担当以及没有了,去掉这个逻辑
    // データ操作の共通関数
    //public static void delInsOpportunityTeamMemberSTMS(Map<Id, OpportunityTeamMember> otmMap) {
    //    syncMBOpportunityOtmMapSTMS.putAll(otmMap);
    //    List<OpportunityTeamMember> delOppTMList = new List<OpportunityTeamMember>();
    //    // oppId ==> userId
    //    Id breakOppId = null;
 
    //    //OLY_OCM-199 0512 start
    //    List<Id> userIdList = new List<Id>();
    //    for (OpportunityTeamMember otm : otmMap.values()) {
    //        if (otm.UserId != null) {
    //            userIdList.add(otm.UserId);
    //        }
    //    }
    //    Map<Id, User> IsActiveMap = new Map<Id, User>([SELECT Id, IsActive From User where IsActive = false AND Id IN :userIdList]);
    //    for (Id oppId : otmMap.keyset()) {
    //        if (IsActiveMap.containsKey(otmMap.get(oppId).UserId) == true) {
    //            otmMap.remove(oppId);
    //        }
    //    }
    //    //OLY_OCM-199 0512 end
 
    //    // CHAN-AVS9S2 修正:手动共享删除
    //    // HWAG-B6J4ZN 手动共享不能删除给FSE主担当的共享 start
    //    List<OpportunityShare> osList = [select id,UserOrGroupId,OpportunityId from OpportunityShare where OpportunityId in :otmMap.keySet() and rowcause = 'Manual'];
 
    //    Map<Id, AggregateResult> aggMap =
    //        new Map<Id, AggregateResult>([select Opportunity__r.id Id,
    //            // SWAG-B7LADC 2018/12/21 获取出对应医院所有的副担当,并检索出其ID start
    //                                      Max(Opportunity__r.Hospital__r.FSE_GI_Main_Leader__r.Alias__c) FSE_GI,
    //                                      Max(Opportunity__r.Hospital__r.FSE_SP_Main_Leader__r.Alias__c) FSE_SP,
    //                                      Max(Opportunity__r.Hospital__r.FSE_ENG_Main_Leader__r.Alias__c) FSE_ENG,
    //                                      Max(Opportunity__r.Hospital__r.FSE_ENG_Vice_Leader__c) FSE_ENG_VICE,
    //                                      Max(Opportunity__r.Hospital__r.FSE_GI_Vice_Leader__c) FSE_GI_VICE,
    //                                      Max(Opportunity__r.Hospital__r.FSE_SP_Vice_Leader__c) FSE_SP_VICE
    //                // SWAG-B7LADC 2018/12/21 获取出对应医院所有的副担当,并检索出其ID end
    //                                      from Statu_Achievements__c
    //                                      where ForecastAccuracyObject__c = true and DeliveryDate__c != null
    //                                                      and Opportunity__r.id != null
    //                                                      and Opportunity__r.id in :otmMap.keySet()
    //                                                      group by Opportunity__r.id]);
    //    // SWAG-B7LADC 2018/12/21 获取出对应医院所有的副担当,并检索出其ID start
    //    list<string> allUserNameList = new list<string>();
    //    for(AggregateResult temAgg: aggMap.values()){
    //        if(temAgg.get('FSE_GI') != null){
    //            allUserNameList.add(String.valueOf(temAgg.get('FSE_GI')));
    //        }
    //        if(temAgg.get('FSE_SP') != null){
    //            allUserNameList.add(String.valueOf(temAgg.get('FSE_SP')));
    //        }
    //        if(temAgg.get('FSE_ENG') != null){
    //            allUserNameList.add(String.valueOf(temAgg.get('FSE_ENG')));
    //        }
    //        if(temAgg.get('FSE_ENG_VICE') != null){
    //            for(String UsrName : String.valueOf(temAgg.get('FSE_ENG_VICE')).split('[,\\,]')){
    //                 allUserNameList.add(UsrName);
    //            }
    //        }
    //        if(temAgg.get('FSE_GI_VICE') != null){
    //            for(String UsrName : String.valueOf(temAgg.get('FSE_GI_VICE')).split('[,\\,]')){
    //                 allUserNameList.add(UsrName);
    //            }
    //        }
    //        if(temAgg.get('FSE_SP_VICE') != null){
    //            for(String UsrName : String.valueOf(temAgg.get('FSE_SP_VICE')).split('[,\\,]')){
    //                 allUserNameList.add(UsrName);
    //            }
    //        } 
    //    }
    //    list<User> allFSEUserlist = [select id from User where Alias__c in: allUserNameList];
    //    list<ID> allFSEUserIDList = new list<ID>();
    //    for(User TempUser: allFSEUserlist){
    //        allFSEUserIDList.add(TempUser.id);
    //    }
    //    // SWAG-B7LADC 2018/12/21 获取出对应医院所有的副担当,并检索出其ID end
        
    //    // SWAG-B7LADC 2018/12/21 FSE 手动不删除 start
 
    //    List<OpportunityShare> DelosList = new List<OpportunityShare>();
    //    system.debug('aggMap:' + aggMap);
    //    system.debug('aggMap:' + osList);
    //    if(aggMap.size()>0){
    //        for ( Opportunityshare ops : osList) {
    //            if (!allFSEUserIDList.contains( (ID) ops.UserOrGroupId)
    //                && ops.UserOrGroupId != Label.Group_ServiceManagementID  ) {
    //                DelosList.add(ops);
    //                //system.debug('ops.UserOrGroupId:' + ops.UserOrGroupId);
    //            }
    //        }
    //    }
    //    // SWAG-B7LADC 2018/12/21 FSE 手动不删除 end
    //     // HWAG-B6J4ZN 手动共享不能删除给FSE主担当的共享 end
    //    if (DelosList.size() > 0) delete DelosList;
 
    //    // CHAN-AVS9S2
 
    //    for (OpportunityTeamMember otm :
    //            [select Id, OpportunityId, UserId
    //             from OpportunityTeamMember where OpportunityId in :otmMap.keySet() and TeamMemberRole = 'STMS担当' order by OpportunityId, CreatedDate desc]
    //        ) {
    //        // 前提、STMS担当1人だけです。
    //        if (breakOppId == null || breakOppId != otm.OpportunityId) {
    //            if (breakOppId != null && otmMap.get(breakOppId) != null && otmMap.get(breakOppId).UserId == null) {
    //                otmMap.remove(breakOppId);
    //            }
    //            breakOppId = otm.OpportunityId;
    //        }
    //        // 削除か、新規か を判断
    //        // 要注意 otmMap の UserId が nullの場合も削除します。
    //        if (otmMap.get(otm.OpportunityId).UserId != otm.UserId) {
    //            delOppTMList.add(otm);
    //        } else {
    //            otmMap.remove(otm.OpportunityId);
    //        }
    //    }
    //    for (Id oppId : otmMap.keyset()) {
    //        if (otmMap.get(oppId).UserId == null) {
    //            otmMap.remove(oppId);
    //        }
    //    }
    //    if (delOppTMList.size() > 0) {
    //        delete delOppTMList;
    //    }
    //    if (otmMap.size() > 0) {
    //        insert otmMap.values();
    //        List<OpportunityShare> newShare = new List<OpportunityShare>();
    //        for (OpportunityTeamMember otm : otmMap.values()) {
    //            newShare.add(new OpportunityShare(
    //                             UserOrGroupId = otm.UserId, OpportunityId = otm.OpportunityId, OpportunityAccessLevel = 'Edit'
    //                         ));
    //        }
    //        if (newShare.size() > 0) {
    //            Database.SaveResult[] lsr = Database.insert(newShare, false); //insert the new shares
    //            for (Integer sIdx = 0; sIdx < lsr.size(); sIdx++) {
    //                Database.SaveResult sr = lsr[sIdx];
    //                if (!sr.isSuccess()) {
    //                    Database.Error emsg = sr.getErrors()[0];
    //                    system.debug('\nERROR ADDING OPPORTUNITY SHARING:' + emsg);
    //                }
    //            }
    //        }
    //    }
    //}
    // // CHAN-BB38N4 start stms 副担当以及没有了,去掉这个逻辑
}