Li Jun
2022-04-06 fb04e7c01d119c60632b4298d18fd93f3ccb3d79
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
public without sharing class AgencyContactHandler extends Oly_TriggerHandler {
    @TestVisible
    private Map<Id, Agency_Contact__c> newMap;
    @TestVisible
    private Map<Id, Agency_Contact__c> oldMap;
    @TestVisible
    private List<Agency_Contact__c> newList;
    @TestVisible
    private List<Agency_Contact__c> oldList;
 
    public AgencyContactHandler() {
        this.newMap = (Map<Id, Agency_Contact__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Agency_Contact__c>) Trigger.oldMap;
        this.newList = (List<Agency_Contact__c>) Trigger.new;
        this.oldList = (List<Agency_Contact__c>) Trigger.old;
    }
 
    protected override void beforeInsert() {
        beforeSetValue();
    }
    protected override void beforeUpdate() {
        beforeSetValue();
        shareAgency_Contact_ToRole(this.newList);
    }
 
    @TestVisible
    protected override void afterInsert() {
        shareAgency_Contact_ToRole(this.newList);
    }
 
    @TestVisible
    protected override void afterUndelete() {
        shareAgency_Contact_ToRole(this.newList);
    }
 
    private void beforeSetValue() {
        Set<String> nameSet = new Set<String>();
        Set<String> ahIdSet = new Set<String>();
        Map<String, Agency_Contact__c> keyMap = new Map<String, Agency_Contact__c>();
        for (Agency_Contact__c nObj : newList) {
            if (String.isBlank(nObj.Agency_ID__c)) {
                if (String.isBlank(nObj.getCloneSourceId()) == false) {
                    // 要注意 画面からしかないので、ここで selectする
                    Agency_Contact__c srcObj = [Select Id, Agency_Hospital__c, Contact__c, Hospital_ID18__c, Hospital_Name__c from Agency_Contact__c where Id = :nObj.getCloneSourceId()];
                    if (String.isBlank(nObj.Agency_Hospital__c) == false
                            && String.isBlank(srcObj.Agency_Hospital__c)
                            && nObj.Hospital_ID18__c != srcObj.Hospital_ID18__c) {
                        nObj.Agency_Hospital__c.addError('请选择:' + srcObj.Hospital_Name__c);
                    }
                    if (String.isBlank(srcObj.Contact__c) == false) {
                        nObj.Contact__c = srcObj.Contact__c;
                    }
                }
                nObj.Agency_ID__c = nObj.Agency_ID_F__c;
            }
            nameSet.add(nObj.Name_Encrypted__c);
            ahIdSet.add(nObj.Agency_Hospital__c);
            // PIPL update Yin Mingjie 21/02/2022 start
            // keyMap.put(nObj.Agency_Hospital__c + nObj.Name, nObj);
            keyMap.put(nObj.Agency_Hospital__c + nObj.Name_Encrypted__c, nObj);
            // PIPL update Yin Mingjie 21/02/2022 end
        }
 
        // PIPL update Yin Mingjie 21/02/2022 start
        // List<Agency_Contact__c> ars = [select Id, Agency_Hospital__r.Name, Agency_Hospital__c, Name from Agency_Contact__c 
        //     where Name in :nameSet and Agency_Hospital__c in :ahIdSet and Agency_Hospital__c != null];
        List<Agency_Contact__c> ars = [select Id, Agency_Hospital__r.Name, Agency_Hospital__c, Name, Name_Encrypted__c from Agency_Contact__c 
            where Name_Encrypted__c in :nameSet and Agency_Hospital__c in :ahIdSet and Agency_Hospital__c != null];
        // PIPL update Yin Mingjie 21/02/2022 end
        for (Agency_Contact__c ar : ars) {
            // PIPL update Yin Mingjie 21/02/2022 start
            // String key = ar.Agency_Hospital__c + ar.Name;
            String key = ar.Agency_Hospital__c + ar.Name_Encrypted__c;
            // PIPL update Yin Mingjie 21/02/2022 end
            //system.debug('========3333333key'+key);
            if (keyMap.containsKey(key)) {
                Agency_Contact__c a = keyMap.get(key);
                if (a.Id == ar.Id) continue;
                //system.debug('========3333333');
                a.addError('该客户人员名字已存在,在'+ar.Agency_Hospital__r.Name+'医院,请修改'); 
            }
        }
    }
 
    /**
     * 设定 apex share to role
     * @param accId UserRoleとGroupを取得するため
     */
    private static void setAgency_Contact_Share(Id accId, List<Id> pList) {
        Id grpId = MergeAgencyActivityBatch.accIdGrpIdMap(accId);
        if(grpId != null||Test.isRunningTest()) {
            List<Agency_Contact__Share> shareList = new List<Agency_Contact__Share>();
            Set<Id> sharePIdSet = new Set<Id>();
            for (Agency_Contact__Share share : [SELECT Id, ParentId
                    FROM Agency_Contact__Share
                    WHERE UserOrGroupId = :grpId AND RowCause = 'Manual']) {
                sharePIdSet.add(share.ParentId);
            }
            for (Id pId : pList) {
                if (sharePIdSet.contains(pId) == false) {
                    Agency_Contact__Share apexShare = new Agency_Contact__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_ID__c}
     */
    public static void shareAgency_Contact_ToRole(List<Agency_Contact__c> nList) {
        Set<Id> targetAgencyIdSet = new Set<Id>();
        Map<Id, List<Id>> tMap = new Map<Id, List<Id>>();
        for (Agency_Contact__c nObj : nList) {
            if (nObj.Agency_ID__c == '000000000000000') continue;
            targetAgencyIdSet.add(nObj.Agency_ID__c);
            List<Id> tList = tMap.get(nObj.Agency_ID__c);
            if (tList == null) {
                tList = new List<Id>();
            }
            tList.add(nObj.Id);
            tMap.put(nObj.Agency_ID__c, tList);
        }
        if (targetAgencyIdSet.size() > 0) {
            List<Id> targetIdList = new List<Id> (targetAgencyIdSet);
            System.assertEquals(1, targetIdList.size(), '一定要是同一经销商 [' + targetIdList[0] + ']');
            setAgency_Contact_Share(targetIdList[0], tMap.get(targetIdList[0]));
        }
    }
}