高章伟
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
public without sharing class AgencyReportHeaderHandler extends Oly_TriggerHandler {
    private Map<Id, Agency_Report_Header__c> newMap;
    private Map<Id, Agency_Report_Header__c> oldMap;
    private List<Agency_Report_Header__c> newList;
    private List<Agency_Report_Header__c> oldList;
 
    public AgencyReportHeaderHandler() {
        this.newMap = (Map<Id, Agency_Report_Header__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Agency_Report_Header__c>) Trigger.oldMap;
        this.newList = (List<Agency_Report_Header__c>) Trigger.new;
        this.oldList = (List<Agency_Report_Header__c>) Trigger.old;
    }
 
    protected override void beforeInsert() {
        beforeExecute();
    }
 
    protected override void beforeUpdate() {
        beforeExecute();
        shareAgency_Report_Header_ToRole(this.newList);
    }
 
    protected override void afterInsert() {
        shareAgency_Report_Header_ToRole(this.newList);
    }
 
    protected override void afterUndelete() {
        shareAgency_Report_Header_ToRole(this.newList);
    }
 
    //Before処理
    private void beforeExecute() {
        for(Agency_Report_Header__c nObj : newList) {
            nObj.Agency__c = nObj.Owner_Agency_ID__c;
            nObj.Agency_ID__c = String.valueOf(nObj.Owner_Agency_ID__c).substring(0,15);
        }
    }
 
    /**
     * 设定 apex share to role
     * @param accId UserRoleとGroupを取得するため
     */
    private static void setAgency_Report_Header_Share(Id accId, List<Id> pList) {
        Id grpId = MergeAgencyActivityBatch.accIdGrpIdMap(accId);
        if(grpId != null) {
            List<Agency_Report_Header__Share> shareList = new List<Agency_Report_Header__Share>();
            Set<Id> sharePIdSet = new Set<Id>();
            for (Agency_Report_Header__Share share : [SELECT Id, ParentId
                    FROM Agency_Report_Header__Share
                    WHERE UserOrGroupId = :grpId AND RowCause = 'Manual']) {
                sharePIdSet.add(share.ParentId);
            }
            for (Id pId : pList) {
                if (sharePIdSet.contains(pId) == false) {
                    Agency_Report_Header__Share apexShare = new Agency_Report_Header__Share(
                            RowCause = 'Manual',
                            ParentId = pId,
                            UserOrGroupId = grpId,
                            AccessLevel = 'Edit');
                    shareList.add(apexShare);
                }
            }
            if (System.Test.isRunningTest() && grpId == null) {
                // 没有造 User 的旧Test 也让可以过
                return;
            }
            insert shareList;
        }
    }
 
    // afterInsert, afterUndelete
    /**
     * 一定要是同一经销商的
     * @param nList {Id, Agency__c}
     */
    public static void shareAgency_Report_Header_ToRole(List<Agency_Report_Header__c> nList) {
        Set<Id> targetAgencyIdSet = new Set<Id>();
        Map<Id, List<Id>> tMap = new Map<Id, List<Id>>();
        for (Agency_Report_Header__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);
        }
        if (targetAgencyIdSet.size() > 0) {
            List<Id> targetIdList = new List<Id> (targetAgencyIdSet);
            System.assertEquals(1, targetIdList.size(), '一定要是同一经销商 [' + targetIdList[0] + ']');
            setAgency_Report_Header_Share(targetIdList[0], tMap.get(targetIdList[0]));
        }
    }
}