liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
global class MergeAgencyActivityBatch implements Database.Batchable<sObject>, Database.Stateful {
    global Map<Id, Contact> oldIdNewContactMap = new Map<Id, Contact>();    // 7. Agency_Report_Header 和 8. Agency_Report 里用到
    global Map<Id, Id>       oldIdNewUserIdMap = new Map<Id, Id>();         // Agency_XXX table Owner 設定用
    global Map<Id, Id>       newIdOldUserIdMap = new Map<Id, Id>();         // Agency_XXX table Owner 設定用
    global Map<Id, Agency_Hospital_Link__c> newAgencyHospitalMap = new Map<Id, Agency_Hospital_Link__c>(); // .客户人员(new, key is Hospital__c)
    
    public String       oldAgencyCode;
    public String       newAgencyCode;
    public Boolean      forceFlg;
    public List<String> emailMessages;
    public String       oldAgencyId;
    public String       newAgencyId;
 
    global Integer totalCount = 0; // 总件数
    global Integer failedCount = 0;
 
    @TestVisible
    private static List<String> messagesForTest;
 
    /**
     * @param forceFlg true:不对有没有New Agency的数据进行check
     */
    global MergeAgencyActivityBatch(String oldAgencyCode, String newAgencyCode, Boolean forceFlg) {
        this.oldAgencyCode = oldAgencyCode;
        this.newAgencyCode = newAgencyCode;
        this.forceFlg      = forceFlg;
        this.emailMessages = new List<String>();
    }
 
    // TODO katsu 暫定 1万 超えない前提
    global Database.QueryLocator start(Database.BatchableContext bc) {
        Account[] accList;
        accList = [SELECT Id
                FROM Account
                WHERE Management_Code__c = :this.oldAgencyCode];
        if (accList.size() != 1) {
            this.emailMessages.add('没有找到 或 有多条 旧经销商 [' + this.oldAgencyCode + '] 的数据, size=' + accList.size());
            return Database.getQueryLocator([SELECT Id FROM Agency_Hospital_Link__c LIMIT 0]);
        } else {
            this.oldAgencyId = accList[0].Id;
        }
        accList = [SELECT Id
                FROM Account
                WHERE Management_Code__c = :this.newAgencyCode];
        if (accList.size() != 1) {
            this.emailMessages.add('没有找到 或 有多条 新经销商 [' + this.newAgencyCode + '] 的数据, size=' + accList.size());
            return Database.getQueryLocator([SELECT Id FROM Agency_Hospital_Link__c LIMIT 0]);
        } else {
            this.newAgencyId = accList[0].Id;
        }
        if (forceFlg == false && [SELECT Id
                FROM Agency_Contact__c
                WHERE Agency_Hospital__r.Agency__r.Management_Code__c = :this.newAgencyCode].size() > 0) {
 
            this.emailMessages.add('.客户人员 里不可以有 New Agency [' + this.newAgencyCode + '] 的数据');
            return Database.getQueryLocator([SELECT Id FROM Agency_Hospital_Link__c LIMIT 0]);
        }
        if (forceFlg == false && [SELECT Id
                FROM Agency_Opportunity__c
                WHERE Agency_Hospital_All__r.Agency__r.Management_Code__c = :this.newAgencyCode].size() > 0) {
 
            this.emailMessages.add('经销商询价 里不可以有 New Agency [' + this.newAgencyCode + '] 的数据');
            return Database.getQueryLocator([SELECT Id FROM Agency_Hospital_Link__c LIMIT 0]);
        }
        if (forceFlg == false && [SELECT Id
                FROM Agency_Report_Header__c
                WHERE Agency__r.Management_Code__c = :this.newAgencyCode].size() > 0) {
 
            this.emailMessages.add('.周报一览 里不可以有 New Agency [' + this.newAgencyCode + '] 的数据');
            return Database.getQueryLocator([SELECT Id FROM Agency_Hospital_Link__c LIMIT 0]);
        }
        if (forceFlg == false && [SELECT Id
                FROM Agency_Report__c
                WHERE Agency__r.Management_Code__c = :this.newAgencyCode].size() > 0) {
 
            this.emailMessages.add('.周报明细 里不可以有 New Agency [' + this.newAgencyCode + '] 的数据');
            return Database.getQueryLocator([SELECT Id FROM Agency_Hospital_Link__c LIMIT 0]);
        }
 
        // 事前のデータを取得
        // 1. New Agency_Hospital 新的营销关系
        for (Agency_Hospital_Link__c nObj : [SELECT Id
                    , Agency__c, Agency__r.Management_Code__c, Hospital__c
                FROM Agency_Hospital_Link__c
                WHERE Agency__r.Management_Code__c = :this.newAgencyCode
                ORDER BY CreatedDate]) {
            // map的重复不用考虑
            newAgencyHospitalMap.put(nObj.Hospital__c, nObj);
        }
 
        // 5. New Contact
        String Contact_Agency_RT = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        Map<Id, Contact>         newContactMap = new Map<Id, Contact>();     // 取引先責任者(new)
        Map<String, Contact> newContactNameMap = new Map<String, Contact>(); // 取引先責任者(new, key is Name)
        for (Contact nObj : [SELECT Id
                    , AccountId, Name, Agency_User__c
                FROM Contact
                WHERE RecordTypeId = :Contact_Agency_RT
                  AND Account.Management_Code__c = :this.newAgencyCode]) {
 
            // Contact.Name重复 check
            Contact oObj = newContactNameMap.get(nObj.Name);
            if (oObj != null) {
                this.emailMessages.add('客户人员 ' + oObj.Name + ' 重复, 请确认 ' + oObj.Id + ' , ' + nObj.Id + ' 的数据');
                return Database.getQueryLocator([SELECT Id FROM Agency_Hospital_Link__c LIMIT 0]);
            }
            newContactMap.put(nObj.Id, nObj);
            newContactNameMap.put(nObj.Name, nObj);
        }
 
        // 6. Old Contact
        Map<Id, Contact>         oldContactMap = new Map<Id, Contact>();
        Map<String, Contact> oldContactNameMap = new Map<String, Contact>(); // 取引先責任者(new, key is Name)
        for (Contact nObj : [SELECT Id, Name
                    , Account.Management_Code__c, Agency_User__c, FirstName, LastName
                FROM Contact
                WHERE RecordTypeId = :Contact_Agency_RT
                  AND Agency_User__c = true
                  AND Account.Management_Code__c = :this.oldAgencyCode]) {
 
            // Contact.Name重复 check
            Contact oObj = oldContactNameMap.get(nObj.Name);
            if (oObj != null) {
                this.emailMessages.add('客户人员 ' + oObj.Name + ' 重复, 请确认 ' + oObj.Id + ' , ' + nObj.Id + ' 的数据');
                return Database.getQueryLocator([SELECT Id FROM Agency_Hospital_Link__c LIMIT 0]);
            }
            oldContactMap.put(nObj.Id, nObj);
            oldContactNameMap.put(nObj.Name, nObj);
        }
 
        // 新旧UserのMapping
        Map<String, User> newUserNameMap = new Map<String, User>();
        for (User nObj : [SELECT Id, Name
                    , ContactId
                 FROM User
                WHERE IsPortalEnabled = true
                  AND IsActive = true
                  AND Contact.AccountId = :this.newAgencyId
                  AND ContactId IN :newContactMap.keySet()]) {
            newUserNameMap.put(nObj.Name, nObj);
        }
        Map<String, User> oldUserNameMap = new Map<String, User>();
        for (User nObj : [SELECT Id, Name
                    , ContactId
                 FROM User
                WHERE IsPortalEnabled = true
                  AND IsActive = true
                  AND Contact.AccountId = :this.oldAgencyId
                  AND ContactId IN :oldContactMap.keySet()]) {
            oldUserNameMap.put(nObj.Name, nObj);
        }
 
        // 5. New User 件数必须和 6 一样(force y也不让过)
        for (User oObj : oldUserNameMap.values()) {
            User nObj = newUserNameMap.get(oObj.Name);
            if (nObj == null) {
                this.emailMessages.add('User:' + oObj.Name + ' 在 ' + newAgencyCode + ' 里不存在, 请确认有没有造新的用户');
                return Database.getQueryLocator([SELECT Id FROM Agency_Hospital_Link__c LIMIT 0]);
            }
            this.oldIdNewUserIdMap.put(oObj.Id, nObj.Id);
            this.newIdOldUserIdMap.put(nObj.Id, oObj.Id);
        }
        if (this.oldIdNewUserIdMap.size() == 0) {
            this.emailMessages.add('没有找到 新经销商的用户数据');
            return Database.getQueryLocator([SELECT Id FROM Agency_Hospital_Link__c LIMIT 0]);
        }
 
        // 5. New Contact 件数必须和 6 一样(补到 一样 or 超过)
        List<Contact> upsList = new List<Contact>();
        for (Contact oObj : oldContactMap.values()) {
            Contact nObj = newContactNameMap.get(oObj.Name);
            Boolean isTarget = false;
            if (nObj == null) {
                nObj = new Contact(Agency_User__c = true
                    , RecordTypeId = Contact_Agency_RT
                    , FirstName = oObj.FirstName
                    , LastName = oObj.LastName
                );
                isTarget = true;
            }
            if (nObj.Agency_User__c != true) {
                isTarget = true;
            }
            nObj.AccountId = this.newAgencyId;
            nObj.Agency_User__c = true;
            this.oldIdNewContactMap.put(oObj.Id, nObj);
            if (isTarget) {
                upsList.add(nObj);
            }
        }
        List<Database.UpsertResult> urList = Database.upsert(upsList, Contact.Fields.Id, false);
        Integer errorCnt = setUpstError(urList, Contact.getSObjectType(), upsList);
        totalCount += upsList.size();
        if (errorCnt > 0) {
            failedCount += errorCnt;
            return Database.getQueryLocator([SELECT Id FROM Agency_Hospital_Link__c LIMIT 0]);
        }
 
        // 7. Agency_Report_Header
        List<Agency_Report_Header__c> oldArhList = [SELECT Id, Name
                    , Old_OwnerId__c, Old_Agency__c, Old_Agency_Person2__c
                    , OwnerId, Agency__c, Agency_Person2__c
                FROM Agency_Report_Header__c
                WHERE Agency__r.Management_Code__c = :this.oldAgencyCode];
        for (Agency_Report_Header__c ups : oldArhList) {
            if (false == newIdOldUserIdMap.containsKey(ups.OwnerId)) {
                ups.Old_OwnerId__c        = ups.OwnerId;
                ups.OwnerId               = oldIdNewUserIdMap.get(ups.OwnerId);
            }
            if (ups.Agency__c != this.newAgencyId) {
                ups.Old_Agency__c         = ups.Agency__c;
                ups.Agency__c             = this.newAgencyId;
                ups.Agency_ID__c          = this.newAgencyId.mid(0, 15);
            }
            if (ups.Agency_Person2__c != this.oldIdNewContactMap.get(ups.Agency_Person2__c).Id) {
                ups.Old_Agency_Person2__c = ups.Agency_Person2__c;
                ups.Agency_Person2__c     = this.oldIdNewContactMap.get(ups.Agency_Person2__c).Id;
            }
        }
        List<Database.SaveResult> srList = Database.update(oldArhList, false);
        AgencyReportHeaderHandler.shareAgency_Report_Header_ToRole(oldArhList);
        errorCnt = setSaveError(srList, Agency_Report_Header__c.getSObjectType(), oldArhList);
        totalCount += oldArhList.size();
        if (errorCnt > 0) {
            failedCount += errorCnt;
            return Database.getQueryLocator([SELECT Id FROM Agency_Hospital_Link__c LIMIT 0]);
        }
 
        // Batch実行
        // 2. Old Agency_Hospital 为基础
        return Database.getQueryLocator([SELECT Id
                    , Agency__c, Agency__r.Management_Code__c, Hospital__c, Hospital__r.Management_Code__c
                FROM Agency_Hospital_Link__c
                WHERE Agency__r.Management_Code__c = :this.oldAgencyCode
            ]
        );
    }
 
    global void execute(Database.BatchableContext BC, List<Agency_Hospital_Link__c> oldAgencyHospitalList) {
        Map<Id, Agency_Opportunity__c> oldAgencyOpportunityMap = new Map<Id, Agency_Opportunity__c>();   // 经销商询价(old)
        Map<Id, User> ocsmUserMap = new Map<Id, User>(); //ocsm内部ユーザー
 
        // 事前のデータを取得
        // 1. ocsm内部ユーザー
        ocsmUserMap = new Map<Id, User>([SELECT Id FROM User WHERE IsPortalEnabled = false]);
        // 2. Old Agency_Hospital 旧的营销关系 -> 新的营销关系
        Map<Id, Agency_Hospital_Link__c>  oldIdNewAgencyHospitalMap = new Map<Id, Agency_Hospital_Link__c>(); // 3. Agency_Contact, 4. Agency_Opportunity, 8. Agency_Report 里用到
        for (Agency_Hospital_Link__c oObj : oldAgencyHospitalList) {
            Agency_Hospital_Link__c nObj = this.newAgencyHospitalMap.get(oObj.Hospital__c);
            if (nObj == null) {
                this.emailMessages.add('没有找到新的营销关系 请确认是否存在 ' + oObj.Hospital__r.Management_Code__c + ' 的数据');
            } else {
                oldIdNewAgencyHospitalMap.put(oObj.Id, nObj);
            }
        }
 
        // 3. Agency_Contact
        List<Agency_Contact__c> oldAgencyContactList = [SELECT Id, Name
                    , Old_OwnerId__c, Old_Agency_Hospital__c, Old_Agency_ID__c
                    , OwnerId, Agency_Hospital__c, Agency_ID__c
                FROM Agency_Contact__c
                WHERE Agency_Hospital__c IN :oldAgencyHospitalList];
        for (Agency_Contact__c ups : oldAgencyContactList) {
            if (false == newIdOldUserIdMap.containsKey(ups.OwnerId)) {
                ups.Old_OwnerId__c        = ups.OwnerId;
                ups.OwnerId               = oldIdNewUserIdMap.get(ups.OwnerId);
            }
            if (ups.Agency_Hospital__c != oldIdNewAgencyHospitalMap.get(ups.Agency_Hospital__c).Id) {
                ups.Old_Agency_Hospital__c  = ups.Agency_Hospital__c;
                ups.Agency_Hospital__c      = oldIdNewAgencyHospitalMap.get(ups.Agency_Hospital__c).Id;
            }
            if (ups.Agency_ID__c != this.newAgencyId.mid(0, 15)) {
                ups.Old_Agency_ID__c        = ups.Agency_ID__c;
                ups.Agency_ID__c            = this.newAgencyId.mid(0, 15);
            }
        }
        List<Database.SaveResult> srList = Database.update(oldAgencyContactList, false);
        AgencyContactHandler.shareAgency_Contact_ToRole(oldAgencyContactList);
        failedCount += setSaveError(srList, Agency_Contact__c.getSObjectType(), oldAgencyContactList);
        totalCount += oldAgencyContactList.size();
 
        // 4. Agency_Opportunity
        List<Agency_Opportunity__c> oldAgencyOpportunityList = [SELECT Id, Name
                    , Old_OwnerId__c, Old_Agency__c, Old_Agency_Hospital__c, Old_Agency_Hospital_All__c, Old_Agency_Hospital_Target__c, Old_TargetInputKey__c
                    , OwnerId, Agency__c, Agency_ID__c, Agency_Hospital__c, Agency_Hospital_All__c, Agency_Hospital_Target__c, TargetInputKey__c
                FROM Agency_Opportunity__c
                WHERE Agency_Hospital_All__c IN :oldAgencyHospitalList];
        for (Agency_Opportunity__c ups : oldAgencyOpportunityList) {
            if (false == newIdOldUserIdMap.containsKey(ups.OwnerId)) {
                if (true == ocsmUserMap.containsKey(ups.OwnerId)) {
                    //ocsm内部ユーザー:更新しない  
                } else {
                    ups.Old_OwnerId__c        = ups.OwnerId;
                    ups.OwnerId               = oldIdNewUserIdMap.get(ups.OwnerId);
                }
            }
            if (ups.Agency__c != this.newAgencyId) {
                ups.Old_Agency__c               = ups.Agency__c;
                ups.Agency__c                   = this.newAgencyId;
                ups.Agency_ID__c                = this.newAgencyId.mid(0, 15);
            }
            if (String.isNotBlank(ups.Agency_Hospital__c)
                    && ups.Agency_Hospital__c != oldIdNewAgencyHospitalMap.get(ups.Agency_Hospital__c).Id) {
                ups.Old_Agency_Hospital__c      = ups.Agency_Hospital__c;
                ups.Agency_Hospital__c          = oldIdNewAgencyHospitalMap.get(ups.Agency_Hospital__c).Id;
            }
            if (String.isNotBlank(ups.Agency_Hospital_All__c)
                    && ups.Agency_Hospital_All__c != oldIdNewAgencyHospitalMap.get(ups.Agency_Hospital_All__c).Id) {
                ups.Old_Agency_Hospital_All__c  = ups.Agency_Hospital_All__c;
                ups.Agency_Hospital_All__c      = oldIdNewAgencyHospitalMap.get(ups.Agency_Hospital_All__c).Id;
            }
            if (String.isNotBlank(ups.Agency_Hospital_Target__c)
                    && ups.Agency_Hospital_Target__c != oldIdNewAgencyHospitalMap.get(ups.Agency_Hospital_Target__c).Id) {
                ups.Old_Agency_Hospital_Target__c = ups.Agency_Hospital_Target__c;
                ups.Agency_Hospital_Target__c   = oldIdNewAgencyHospitalMap.get(ups.Agency_Hospital_Target__c).Id;
                ups.Old_TargetInputKey__c       = ups.TargetInputKey__c;
                ups.TargetInputKey__c           = ups.Agency_Hospital_Target__c + ups.TargetInputKey__c.mid(18, 1300);
            }
        }
        srList = Database.update(oldAgencyOpportunityList, false);
        AgencyOpportunityHandler.shareAgency_Opportunity_ToRole(oldAgencyOpportunityList);
        failedCount += setSaveError(srList, Agency_Opportunity__c.getSObjectType(), oldAgencyOpportunityList);
        totalCount += oldAgencyOpportunityList.size();
 
        // 8. Agency_Report
        List<Agency_Report__c> oldAgencyReportList = [SELECT Id, Name
                    , Old_Agency__c, Old_Agency_Hospital__c, Old_Person_In_Charge2__c
                    , Agency__c, Agency_ID__c, Agency_Hospital__c, Person_In_Charge2__c
                FROM Agency_Report__c
                WHERE Agency__r.Management_Code__c = :this.oldAgencyCode
                  AND Agency_Hospital__c IN :oldAgencyHospitalList];
        for (Agency_Report__c ups : oldAgencyReportList) {
            if (ups.Agency__c != this.newAgencyId) {
                ups.Old_Agency__c               = ups.Agency__c;
                ups.Agency__c                   = this.newAgencyId;
                ups.Agency_ID__c                = this.newAgencyId.mid(0, 15);
            }
            if (String.isNotBlank(ups.Agency_Hospital__c)
                    && ups.Agency_Hospital__c != oldIdNewAgencyHospitalMap.get(ups.Agency_Hospital__c).Id) {
                ups.Old_Agency_Hospital__c      = ups.Agency_Hospital__c;
                ups.Agency_Hospital__c          = oldIdNewAgencyHospitalMap.get(ups.Agency_Hospital__c).Id;
            }
            if (ups.Person_In_Charge2__c != this.oldIdNewContactMap.get(ups.Person_In_Charge2__c).Id) {
                ups.Old_Person_In_Charge2__c = ups.Person_In_Charge2__c;
                ups.Person_In_Charge2__c     = this.oldIdNewContactMap.get(ups.Person_In_Charge2__c).Id;
            }
        }
        srList = Database.update(oldAgencyReportList, false);
        failedCount += setSaveError(srList, Agency_Report__c.getSObjectType(), oldAgencyReportList);
        totalCount += oldAgencyReportList.size();
    }
 
    global void finish(Database.BatchableContext BC) {
        MergeAgencyActivityBatch.messagesForTest = this.emailMessages;
        // 发 mail
        BatchEmailUtil eb = new BatchEmailUtil();
        String title = '经销商用户更名 ' + this.oldAgencyCode + ' -> ' + this.newAgencyCode;
        String ccStr = null;
        if(this.emailMessages.size() == 0){
            eb.successMail(title, totalCount);
        }else{
            eb.failedMail(title, String.join(this.emailMessages, '\n'),
                totalCount, totalCount - failedCount, failedCount);
        }
        eb.send();
    }
 
    @TestVisible
    private Integer setUpstError(List<Database.UpsertResult> srList, Schema.sObjectType obj
            , List<SObject> upsList) {
        Integer errorCnt = 0;
        List<String> errMsgList = new List<String>();
        String objectName = obj.getDescribe().getName();
        String objectLabel = obj.getDescribe().getLabel();
        for (Integer i = 0; i < srList.size(); i++) {
            SObject eObj = upsList[i];
            Database.UpsertResult sr = srList[i];
            if (!sr.isSuccess()) {
                errorCnt++;
                for (Database.Error err : sr.getErrors()) {
                    String message = objectLabel + ':'
                            + objectName + '=' + eObj.get('Name')
                            + ', ' + err.getStatusCode() + ':' + err.getMessage()
                            + ((err.getFields() != null && err.getFields().size() > 0) ? ', Field:' + String.valueOf(err.getFields()) : '');
                    this.emailMessages.add(message);
                }
            }
        }
        return errorCnt;
    }
 
    @TestVisible
    private Integer setSaveError(List<Database.SaveResult> srList, Schema.sObjectType obj
            , List<SObject> upsList) {
        Integer errorCnt = 0;
        String objectName = obj.getDescribe().getName();
        String objectLabel = obj.getDescribe().getLabel();
        for (Integer i = 0; i < srList.size(); i++) {
            SObject eObj = upsList[i];
            Database.SaveResult sr = srList[i];
            if (!sr.isSuccess()) {
                errorCnt++;
                for (Database.Error err : sr.getErrors()) {
                    String message = objectLabel + ':'
                            + objectName + '=' + eObj.get('Name')
                            + ', ' + err.getStatusCode() + ':' + err.getMessage()
                            + ((err.getFields() != null && err.getFields().size() > 0) ? ', Field:' + String.valueOf(err.getFields()) : '');
                    this.emailMessages.add(message);
                }
            }
        }
        return errorCnt;
    }
 
    private static Map<Id, Id> accIdGrpIdMap;
    public static Id accIdGrpIdMap(Id accId) {
        if (MergeAgencyActivityBatch.accIdGrpIdMap == null) { MergeAgencyActivityBatch.accIdGrpIdMap = new Map<Id, Id>(); }
        if (MergeAgencyActivityBatch.accIdGrpIdMap.get(accId) == null) {
            List<UserRole> roles = [SELECT Id, PortalAccountId
                    FROM UserRole
                    WHERE PortalAccountId = :accId
                    AND PortalRole = 'Worker'];
            Map<Id, Id> roleIdAccId = new Map<Id, Id>();
            for (UserRole ur : roles) {
                roleIdAccId.put(ur.Id, ur.PortalAccountId);
            }
            List<Group> groups = [SELECT Id, RelatedId
                    FROM Group
                    WHERE RelatedId = :roleIdAccId.keySet()
                    AND Type = 'RoleAndSubordinates'];
            for (Group grp : groups) {
                MergeAgencyActivityBatch.accIdGrpIdMap.put(roleIdAccId.get(grp.RelatedId), grp.Id);
            }
        }
        return MergeAgencyActivityBatch.accIdGrpIdMap.get(accId);
    }
}