高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
public without sharing class AgencyHospitalHandler extends Oly_TriggerHandler {
    private Map<Id, Agency_Hospital_Link__c> newMap;
    private Map<Id, Agency_Hospital_Link__c> oldMap;
    private List<Agency_Hospital_Link__c> newList;
    private List<Agency_Hospital_Link__c> oldList;
 
    public AgencyHospitalHandler() {
        this.newMap = (Map<Id, Agency_Hospital_Link__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Agency_Hospital_Link__c>) Trigger.oldMap;
        this.newList = (List<Agency_Hospital_Link__c>) Trigger.new;
        this.oldList = (List<Agency_Hospital_Link__c>) Trigger.old;
    }
 
    protected override void beforeInsert() {
        beforeExecute();
        //AgencyHospital_Number_Summary(this.newList, this.oldMap);
    }
 
    protected override void beforeUpdate() {
        beforeExecute();
        shareAgency_Hospital_Link_ToRole(this.newList, this.oldMap);
    }
 
    protected override void afterInsert() {
        shareAgency_Hospital_Link_ToRole(this.newList, this.oldMap);
        AgencyHospital_Number_Summary(this.newList, this.oldMap);
    }
 
    protected override void afterUndelete() {
        shareAgency_Hospital_Link_ToRole(this.newList, this.oldMap);
        //AgencyHospital_Number_Summary(this.newList, this.oldMap);
    }
 
    //lt 20210824 start
    protected override void afterUpdate() {
        AgencyHospital_Number_Summary(this.newList, this.oldMap);
    }
    
    protected override void afterdelete() {
        AgencyHospital_Number_Summary(this.newList, this.oldMap);
    }
    //lt 20210824 end
 
 
    
    //protected override void beforeUpdate() {
    //    shareAgency_Hospital_Link_ToRole(this.newList);
    //}
 
    //Before処理
    private void beforeExecute() {
        Map<Id, List<Agency_Hospital_Link__c>> accMap = new Map<Id, List<Agency_Hospital_Link__c>>();
        for (Agency_Hospital_Link__c nObj : newList) {
            if(accMap.containsKey(nObj.Hospital__c)){
                nObj.Agency_ID__c = String.valueOf(nObj.Agency__c).substring(0, 15);
                nObj.Name = nObj.Hospital_Name_readonly__c;
                accMap.get(nObj.Hospital__c).add(nObj);
            }else{
                List<Agency_Hospital_Link__c> temp = new List<Agency_Hospital_Link__c>();
                nObj.Agency_ID__c = String.valueOf(nObj.Agency__c).substring(0, 15);
                nObj.Name = nObj.Hospital_Name_readonly__c;
                temp.add(nObj);
                accMap.put(nObj.Hospital__c,temp);
            }
            
        }
 
        // List<Account> accList = [select Id, Name from Account where id in :accMap.keySet()];
        // // for (Account acc : accList) {
        // //     Agency_Hospital_Link__c nObj = accMap.get(acc.Id);
        // //     nObj.Name = acc.name;
        // // }
        // for (Account acc : accList) {
        //     List<Agency_Hospital_Link__c> nObjList = accMap.get(acc.Id);
        //     for(Agency_Hospital_Link__c nObj : nObjList){
        //         nObj.Name = acc.name;
        //     }
        // }
    }
 
    /**
     * 设定 apex share to role
     * @param accId UserRoleとGroupを取得するため
     */
    /*
    private static void setAgency_Hospital_Link_Share(Id accId, List<Id> pList) {
        System.debug('accId:'+accId);
        Id grpId = MergeAgencyActivityBatch.accIdGrpIdMap(accId);
        List<Agency_Hospital_Link__Share> shareList = new List<Agency_Hospital_Link__Share>();
        Set<Id> sharePIdSet = new Set<Id>();
        for (Agency_Hospital_Link__Share share : [SELECT Id, ParentId
                FROM Agency_Hospital_Link__Share
                WHERE UserOrGroupId = :grpId]) {
            sharePIdSet.add(share.ParentId);
        }
        for (Id pId : pList) {
            if (sharePIdSet.contains(pId) == false) {
                Agency_Hospital_Link__Share apexShare = new Agency_Hospital_Link__Share(
                        RowCause = 'Manual',
                        ParentId = pId,
                        UserOrGroupId = grpId,
                        AccessLevel = 'Edit');
                shareList.add(apexShare);
            }
        }
        if (Test.isRunningTest() && grpId == null) {
            // 没有造 User 的旧Test 也让可以过
            return;
        }
        insert shareList;
    }
    */
 
    // private static void setAgency_Hospital_Link_Share(Id accId, List<Id> pList) {
    //     System.debug('accId:' + accId);
    //     Id grpId = MergeAgencyActivityBatch.accIdGrpIdMap(accId);
    //     if (grpId != null) {
    //         List<Agency_Hospital_Link__Share> shareList = new List<Agency_Hospital_Link__Share>();
    //         Set<Id> sharePIdSet = new Set<Id>();
    //         for (Agency_Hospital_Link__Share share : [SELECT Id, ParentId
    //                 FROM Agency_Hospital_Link__Share
    //                 WHERE UserOrGroupId = :grpId AND RowCause = 'Manual']) {
    //             sharePIdSet.add(share.ParentId);
    //         }
    //         for (Id pId : pList) {
    //             if (sharePIdSet.contains(pId) == false) {
    //                 Agency_Hospital_Link__Share apexShare = new Agency_Hospital_Link__Share(
    //                     RowCause = 'Manual',
    //                     ParentId = pId,
    //                     UserOrGroupId = grpId,
    //                     AccessLevel = 'Edit');
    //                 shareList.add(apexShare);
    //             }
    //         }
    //         if (Test.isRunningTest() && grpId == null) {
    //             // 没有造 User 的旧Test 也让可以过
    //             return;
    //         }
    //         insert shareList;
    //     }
 
    // }
 
    private static void setAgency_Hospital_Link_Share(List<Id> targetIdList,Map<Id, List<Id>> tmap){
        
            System.debug('传进来的targetIdList:' + targetIdList);
            System.debug('传进来的tmap:' + tmap);
            
            Map<Id,Id> grpIds = AgencyHospitalHandler.accIdGrpIdMap(targetIdList);
 
            //结果集合
            List<Agency_Hospital_Link__Share> shareList = new List<Agency_Hospital_Link__Share>();
 
            //一个供应商查出来对应的多个结果
            Map<Id,Set<Id>> shareMap = new Map<Id,Set<Id>>();
 
            //查出所有分享grpId的分享
            List<Agency_Hospital_Link__Share> ahList =[SELECT Id, ParentId,Parent.Agency__c
                    FROM Agency_Hospital_Link__Share
                    WHERE UserOrGroupId = :grpIds.values() AND RowCause = 'Manual'];
 
            for(Id accId : grpIds.keySet()){
                if (grpIds.get(accId) != null) {
                    
                    for (Agency_Hospital_Link__Share share : ahList) {
 
                        if(shareMap.containsKey(accId) && share.Parent.Agency__c == accId){
 
                            shareMap.get(accId).add(share.ParentId);
 
                        }else{
 
                            Set<Id> sharePIdSet = new Set<Id>();
                            sharePIdSet.add(share.ParentId);
                            shareMap.put(accId,sharePIdSet);
 
                        }
                        
                    }
    
                    if (Test.isRunningTest() && grpIds.get(accId) == null) {
                        // 没有造 User 的旧Test 也让可以过
                        return;
                    }       
                }
            }
 
 
            System.debug('shareMap====为'+shareMap);
            
            for (Id tId : tmap.keySet()) {
                for(Id pId : tmap.get(tId)){
                    // 因有的经销商医院没有共享给经销商 添加 !shareMap.containsKey(tId) XHL 20210818
                    if (!shareMap.containsKey(tId) || (shareMap.containsKey(tId) && shareMap.get(tId).contains(pId) == false)) {
                        Agency_Hospital_Link__Share apexShare = new Agency_Hospital_Link__Share(
                        RowCause = 'Manual',
                        ParentId = pId,
                        UserOrGroupId = grpIds.get(tId),
                        AccessLevel = 'Edit');
    
                        shareList.add(apexShare);
                    }
                }
            }
            
 
            System.debug('上传的List为'+shareList);
            insert shareList;
    }
 
 
    //返回一个Map<供应商Id,需要的Id>
    private static Map<Id, Id> accIdGrpIdMap;
    public static Map<Id,Id> accIdGrpIdMap(List<Id> accIds) {
 
 
        List<UserRole> roles = [SELECT Id, PortalAccountId
                    FROM UserRole
                    WHERE PortalRole = 'Worker'
                    AND PortalAccountId in :accIds];
 
        List<Id> Ids = new List<Id>();
 
        for(UserRole ur : roles){
            Ids.add(ur.Id);
        }
 
        List<Group> groups = [SELECT Id, RelatedId
                    FROM Group
                    WHERE Type = 'RoleAndSubordinates'
                    AND RelatedId = :Ids];
 
        for(Id accId : accIds){
 
            if (AgencyHospitalHandler.accIdGrpIdMap == null) { AgencyHospitalHandler.accIdGrpIdMap = new Map<Id, Id>(); }
 
 
            if (AgencyHospitalHandler.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) {
                    AgencyHospitalHandler.accIdGrpIdMap.put(roleIdAccId.get(grp.RelatedId), grp.Id);
                }
            }
        }
        return AgencyHospitalHandler.accIdGrpIdMap;
    }
 
 
    // afterInsert, afterUndelete
    /**
     * 一定要是同一经销商的
     * @param nList {Id, Agency__c}
     */
    /*
    public static void shareAgency_Hospital_Link_ToRole(List<Agency_Hospital_Link__c> nList) {
        Set<Id> targetAgencyIdSet = new Set<Id>();
        Map<Id, List<Id>> tMap = new Map<Id, List<Id>>();
        for (Agency_Hospital_Link__c nObj : nList) {
            targetAgencyIdSet.add(nObj.Agency__c);
            List<Id> tList = tMap.get(nObj.Agency__c);
            if (tList == null) {
                tList = new List<Id>();
            }
            tList.add(nObj.Id);
            tMap.put(nObj.Agency__c, tList);
        }
        system.debug('targetAgencyIdSet:'+targetAgencyIdSet);
        if (targetAgencyIdSet.size() > 0) {
            List<Id> targetIdList = new List<Id> (targetAgencyIdSet);
            System.assertEquals(1, targetIdList.size(), '一定要是同一经销商 [' + targetIdList[0] + ']');
            setAgency_Hospital_Link_Share(targetIdList[0], tMap.get(targetIdList[0]));
        }
    }
    */
    /*
    1.经销商医院新增 经销商活动对象 为真 创建 共享
    2.经销商医院变更 经销商活动对象 为真 判断 未共享 创建 共享
    3.经销商医院变更 经销商活动对象 由真变成假 判断 共享是否存在 ,存在删除
     */
    public static void shareAgency_Hospital_Link_ToRole(List<Agency_Hospital_Link__c> nList, Map<Id, Agency_Hospital_Link__c> oldMap) {
        Set<Id> targetAgencyIdSet = new Set<Id>();
        Map<Id, List<Id>> tMap = new Map<Id, List<Id>>();
        //Agency_Hospital_Link__c old = null;
        Map<Id, List<Id>> deleteShareMap = new  Map<Id, List<Id>>();
        for (Agency_Hospital_Link__c nObj : nList) {
 
            if (nObj.Agency_Campaign_Obj__c) {
                //1.经销商医院新增 经销商活动对象 为真 创建 共享
                //2.经销商医院变更 经销商活动对象 为真 判断 未共享 创建 共享
                targetAgencyIdSet.add(nObj.Agency__c);
                List<Id> tList = tMap.get(nObj.Agency__c);
                if (tList == null) {
                    tList = new List<Id>();
                }
                tList.add(nObj.Id);
                tMap.put(nObj.Agency__c, tList);
 
            } else {
                if (Trigger.isUpdate) {
                    if (nObj.Agency_Campaign_Obj__c != oldMap.get(nObj.Id).Agency_Campaign_Obj__c) {
                        //3.经销商医院变更 经销商活动对象 由真变成假 判断 共享是否存在 ,存在删除
                        List<Id> deleteShareIdList = new List<Id>();
                        if (deleteShareMap.containsKey(nObj.Agency__c)) {
                            deleteShareIdList = deleteShareMap.get(nObj.Agency__c);
                        }
                        deleteShareIdList.add(nObj.Id);
                        deleteShareMap.put(nObj.Agency__c, deleteShareIdList);
                    }
                }
            }
 
        }
 
        System.debug('targetAgencyIdSet:' + targetAgencyIdSet);
        if (targetAgencyIdSet.size() > 0) {
            List<Id> targetIdList = new List<Id> (targetAgencyIdSet);
            //System.assertEquals(1, targetIdList.size(), '一定要是同一经销商 [' + targetIdList[0] + ']');
            //setAgency_Hospital_Link_Share(targetIdList[0], tMap.get(targetIdList[0]));
            System.debug('拿到的tMap的长度========'+tMap.size());
            
            //2021/7/30 参数传map
            setAgency_Hospital_Link_Share(targetIdList,tMap);
 
        }
 
        if (deleteShareMap.size() > 0) {
 
            deleteAgency_Hospital_Link_Share(deleteShareMap);
        }
    }
    /**
     * 删除 经销商医院的共享权限
     * @Author   XHL
     * @DateTime 2021-07-01
     * @param    deleteShareMap [key=经销商Id,value= 经销商医院IdList]
     */
    public static void deleteAgency_Hospital_Link_Share(Map<Id, List<Id>> deleteShareMap) {
        // 经销商Id
        List<Id> dealerIdList = new List<Id>(deleteShareMap.keyset());
        // 待删除的共享权限的经销商医院的Id
        List<Id> deleteShareIdList = new List<Id>();
        for (Id accountId : deleteShareMap.keyset()) {
            deleteShareIdList.addAll(deleteShareMap.get(accountId));
        }
 
        // 获取经销商的用户角色
        List<UserRole> roles = [SELECT Id, PortalAccountId
                                FROM UserRole
                                WHERE PortalAccountId IN :dealerIdList
                                AND PortalRole = 'Worker'];
        Map<Id, Id> roleIdAccId = new Map<Id, Id>();
        for (UserRole ur : roles) {
            roleIdAccId.put(ur.Id, ur.PortalAccountId);
        }
        // 获取经销商的用户角色 所在的小组
        List<Id> groupIdList = new List<Id>();
        List<Group> groups = [SELECT Id, RelatedId
                              FROM Group
                              WHERE RelatedId = :roleIdAccId.keySet()
                                                AND Type = 'RoleAndSubordinates'];
        for (Group grp : groups) {
            groupIdList.add(grp.Id);
        }
        List<Agency_Hospital_Link__Share> deleteShareList = new List<Agency_Hospital_Link__Share>();
        for (Agency_Hospital_Link__Share share : [SELECT Id, ParentId
                FROM Agency_Hospital_Link__Share
                WHERE UserOrGroupId IN :groupIdList AND RowCause = 'Manual' AND AccessLevel = 'Edit' order by UserOrGroupId]) {
            if (deleteShareIdList.contains(share.ParentId)) {
                deleteShareList.add(share);
            }
        }
        if (deleteShareList.size() > 0) {
            delete deleteShareList;
        }
 
    }
 
    //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);
    //}
 
 
    //SWAG-C5XBY2  精琢科技  lt  2021-08-24  start
    public static void AgencyHospital_Number_Summary(List<Agency_Hospital_Link__c> sList,Map<Id, Agency_Hospital_Link__c> oldMap){
 
        List<String> cdeal = new List<String>();
        
        if(sList != null){
            for(Agency_Hospital_Link__c Obj : sList){
                // 修改  经销商医院数据 统计
                if(oldMap != null){
                    if (Obj.Agency_Campaign_Obj__c != oldMap.get(Obj.Id).Agency_Campaign_Obj__c || (System.Label.onlyupdate == '1' && UserInfo.getUserId() == '00510000005sEEMAA2')){
                        cdeal.add(Obj.Agency__c);   //变化的经销商
                    }      
            }
                // 新建  经销商医院数据 统计
                else{
                    if(Obj.Agency_Campaign_Obj__c == true){
                        cdeal.add(Obj.Agency__c);
                    }
                }
            }
        }
        //删除
        else{
            if(oldMap != null){
                for(Id Obj : oldMap.keyset()){
                cdeal.add(oldMap.get(Obj).Agency__c);
                }
            }
        }
        
        //统计(经销商,数量)
        if(cdeal.size() > 0){
            //    List<AggregateResult> reout = [select count(Id) ahl, Agency__c from Agency_Hospital_Link__c where Agency__c in :cdeal and Agency_Campaign_Obj__c = true group by Agency__c];
            //    for(AggregateResult acc : reout){
            //     numMap.put((String)acc.get('Agency__c'), (Integer)acc.get('ahl'));
            //    }
    
            //    List<AggregateResult> reoutim = [select count(Id) ahlim, Agency__c from Agency_Hospital_Link__c where Agency__c in :cdeal and Agency_Campaign_Obj__c = true and (Hospital_Category__c ='H0' or Hospital_Category__c ='H1' or Hospital_Category__c ='M0') group by Agency__c];
            //    for(AggregateResult acc1 : reoutim){
            //     numimMap.put((String)acc1.get('Agency__c'), (Integer)acc1.get('ahlim'));
            //    }
            Map<String,Integer> numMap = new Map<String,Integer>();
            Map<String,Integer> numimMap = new Map<String,Integer>();
            List<Agency_Hospital_Link__c> reout = [select Agency__c,Hospital_Category__c from Agency_Hospital_Link__c where Agency__c in :cdeal and Agency_Campaign_Obj__c = true];
            for(Agency_Hospital_Link__c acc : reout){
    
                if(numMap.containskey(acc.Agency__c)){
                    numMap.put(acc.Agency__c, numMap.get(acc.Agency__c)+1);
                }else{
                    numMap.put(acc.Agency__c,1);
                }
    
                if(acc.Hospital_Category__c == 'H0' || acc.Hospital_Category__c == 'H1' || acc.Hospital_Category__c == 'M0'){
                    if(numimMap.containskey(acc.Agency__c)){
                        numimMap.put(acc.Agency__c, numimMap.get(acc.Agency__c)+1);
                    }else{
                        numimMap.put(acc.Agency__c,1);
                    }
                }
            }
            
            //赋值0
            Map<String,Account> acc1 = new Map<String,Account>();
            List<Account> accList = new List<Account>();
            for(String unacc : cdeal){
                Account ac = new Account();
                ac.Id = unacc;
                ac.AgencyHospital_Number__c = numMap.containsKey(unacc)?numMap.get(unacc):0;
                ac.AgencyHospital_Number_Important__c = numimMap.containsKey(unacc)?numimMap.get(unacc):0;
                acc1.put(unacc,ac);            
            }
            for(Account a : acc1.values()){
                accList.add(a);
            }
            if(accList.size()>0){
                update accList;
            }
               
        }
    }
    //SWAG-C5XBY2  精琢科技  lt  2021-08-24  end
}