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;
|
|
}
|
}
|