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
public without sharing class CampaignCostHandler extends Oly_TriggerHandler {
 
    @TestVisible
    private Map<Id, Campaign_Cost__c> newMap;
    @TestVisible
    private Map<Id, Campaign_Cost__c> oldMap;
    @TestVisible
    private List<Campaign_Cost__c> newList;
    @TestVisible
    private List<Campaign_Cost__c> oldList;
    private id HuaBeiCostID;
 
    public CampaignCostHandler() {
        this.newMap = (Map<Id, Campaign_Cost__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Campaign_Cost__c>) Trigger.oldMap;
        this.newList = (List<Campaign_Cost__c>) Trigger.new;
        this.oldList = (List<Campaign_Cost__c>) Trigger.old;
        Group HuaBeiCost =
            [SELECT Id FROM Group WHERE DeveloperName = 'X00_Meetingfee_Huabei'];
        HuaBeiCostID = HuaBeiCost.id;
    }
    protected override void afterInsert() {
        ShareWork();
    }
    protected override void afterUpdate() {
        ShareWork();
    }
    protected override void afterUndelete() {
        ShareWork();
    }
    @TestVisible
    private void ShareWork() {
        list<Campaign_Cost__c> updateCamCostList = new list<Campaign_Cost__c>();
        list<id> updateCamCostIdList = new list<id>();
        for (Campaign_Cost__c tempCamCost : newList) {
            if (checkUpdateShare(tempCamCost)) {
                updateCamCostList.add(tempCamCost);
                updateCamCostIdList.add(tempCamCost.id);
            }
        }
        if (updateCamCostIdList.size() > 0) {
            UpdateShare(updateCamCostList, updateCamCostIdList);
        }
    }
    // 通过是否新建、取消删除或经理部长总监换人,担当所属部门换到1、华北或者从1、华为换为别的
    // 判断是否需要更新这条数据共享规则
    @TestVisible
    private boolean checkUpdateShare(Campaign_Cost__c tempCamCost) {
 
        if (Trigger.isInsert || Trigger.isUndelete) {
            return true;
        } else if ( tempCamCost.Sponsor_Ones_Post__c != oldMap.get(tempCamCost.id).Sponsor_Ones_Post__c) {
            return true;
        } else if ( tempCamCost.JingliApprovalManager__c != tempCamCost.OwnerId &&
                    tempCamCost.JingliApprovalManager__c != oldMap.get(tempCamCost.id).JingliApprovalManager__c) {
            return true;
        } else if ( tempCamCost.BuchangApprovalManager__c != tempCamCost.OwnerId &&
                    tempCamCost.BuchangApprovalManager__c != oldMap.get(tempCamCost.id).BuchangApprovalManager__c) {
            return true;
        } else if ( tempCamCost.ZongjianApprovalManager__c != tempCamCost.OwnerId &&
                    tempCamCost.ZongjianApprovalManager__c != oldMap.get(tempCamCost.id).ZongjianApprovalManager__c) {
            return true;
        }
 
        return false;
    }
    // 更新共享规则
    @TestVisible
    private void UpdateShare(list<Campaign_Cost__c> updateCamCostList,
                             list<id> updateCamCostIdList) {
        list<Campaign_Cost__Share> deleteCamCostShareList =
            [SELECT Id, ParentId, UserOrGroupId, AccessLevel
             FROM Campaign_Cost__Share
             where ParentId in : updateCamCostIdList
             and AccessLevel = 'Read'
            ];
        if (deleteCamCostShareList.size() > 0) {
            delete deleteCamCostShareList;
        }
        list<Campaign_Cost__Share> InsertCamCostShareList =
            new list<Campaign_Cost__Share>();
        for (Campaign_Cost__c tempCamCost : updateCamCostList) {
            List<Campaign_Cost__Share> CamCostShareList =
                SetUpCamCostShareList(tempCamCost);
            if (CamCostShareList.size() > 0) {
                InsertCamCostShareList.addAll(CamCostShareList);
            }
        }
        if (InsertCamCostShareList.size() > 0) {
            insert InsertCamCostShareList;
        }
 
    }
 
    @TestVisible
    private List<Campaign_Cost__Share> SetUpCamCostShareList(Campaign_Cost__c tempCamCost) {
        List<Campaign_Cost__Share> CamCostShareList = new List<Campaign_Cost__Share>();
        if (tempCamCost.JingliApprovalManager__c != null
                &&  tempCamCost.JingliApprovalManager__c != tempCamCost.OwnerId  ) {
            Campaign_Cost__Share temCamCostShare =
                new Campaign_Cost__Share(ParentId = tempCamCost.id,
                                         UserOrGroupId = tempCamCost.JingliApprovalManager__c,
                                         AccessLevel = 'Read');
            CamCostShareList.add(temCamCostShare);
        }
        if (tempCamCost.BuchangApprovalManager__c != null
                &&  tempCamCost.BuchangApprovalManager__c != tempCamCost.OwnerId ) {
            Campaign_Cost__Share temCamCostShare =
                new Campaign_Cost__Share(ParentId = tempCamCost.id,
                                         UserOrGroupId = tempCamCost.BuchangApprovalManager__c,
                                         AccessLevel = 'Read');
            CamCostShareList.add(temCamCostShare);
        }
        if (tempCamCost.ZongjianApprovalManager__c != null
                &&  tempCamCost.ZongjianApprovalManager__c != tempCamCost.OwnerId) {
            Campaign_Cost__Share temCamCostShare =
                new Campaign_Cost__Share(ParentId = tempCamCost.id,
                                         UserOrGroupId = tempCamCost.ZongjianApprovalManager__c,
                                         AccessLevel = 'Read');
            CamCostShareList.add(temCamCostShare);
        }
        if (tempCamCost.Sponsor_Ones_Post__c != null && tempCamCost.Sponsor_Ones_Post__c.equals('1.华北')) {
            Campaign_Cost__Share temCamCostShare =
                new Campaign_Cost__Share(ParentId = tempCamCost.id,
                                         UserOrGroupId = HuaBeiCostID,
                                         AccessLevel = 'Read');
            CamCostShareList.add(temCamCostShare);
        }
        return CamCostShareList;
 
    }
}