public without sharing class RentalApplyEquipmentSetCheckHandler extends Oly_TriggerHandler {
|
private Map<Id, Rental_Apply_Equipment_Set__c> newMap;
|
private Map<Id, Rental_Apply_Equipment_Set__c> oldMap;
|
private List<Rental_Apply_Equipment_Set__c> newList;
|
private List<Rental_Apply_Equipment_Set__c> oldList;
|
|
public RentalApplyEquipmentSetCheckHandler() {
|
this.newMap = (Map<Id, Rental_Apply_Equipment_Set__c>) Trigger.newMap;
|
this.oldMap = (Map<Id, Rental_Apply_Equipment_Set__c>) Trigger.oldMap;
|
this.newList = (List<Rental_Apply_Equipment_Set__c>) Trigger.new;
|
this.oldList = (List<Rental_Apply_Equipment_Set__c>) Trigger.old;
|
}
|
protected override void beforeInsert() {
|
beforeSetValue();
|
}
|
|
//protected override void beforeUpdate() {
|
// //beforeSetValue();
|
//}
|
|
|
private void beforeSetValue() {
|
//获取 备品借出申请的Id
|
Map<Id, List<Id>> raIdMap = new Map<Id, List<Id>>();
|
for (Rental_Apply_Equipment_Set__c nObj : newList) {
|
String raId = nobj.Rental_Apply__c;
|
List<Id> raIdList = new List<Id>();
|
if (raIdMap.containskey(raId)) {
|
raIdList = raIdMap.get(raId);
|
}
|
raIdList.add(raId);
|
raIdMap.put(raId, raIdList);
|
}
|
|
if (raIdMap.size() > 0) {
|
//查找备品借出申请 是不是 补充申请创建 SupplementCreated__c
|
List<Rental_Apply__c> raLsit = [
|
SELECT Id,
|
//Name,
|
//Rental_Apply_Equipment_Set_Cnt__c,
|
//OPDPlan__c,
|
//OPDPlan__r.OriginalOpdPlanApplication__r.Rental_Apply2__r.Rental_Apply_Equipment_Set_Cnt__c,
|
OPDPlan__r.OriginalOpdPlanApplication__r.Rental_Apply2__c
|
from Rental_Apply__c
|
where SupplementCreated__c = true
|
AND Id In :raIdMap.keySet()];
|
if (raLsit.size() > 0 ) {
|
// 获取 补充申请OPD计划创建的 备品借出申请Id 以及 被克隆的备品借出申请Id
|
Set<Id> raIdSet = new Set<Id>();
|
for (Rental_Apply__c ra : raLsit) {
|
Id raId = ra.Id;
|
Id cloneRaId = ra.OPDPlan__r.OriginalOpdPlanApplication__r.Rental_Apply2__c;
|
raIdSet.add(raId);
|
raIdSet.add(cloneRaId);
|
}
|
//查找 备品借出申请下 已有多少个借出备品配套一览
|
AggregateResult[] aggArr = [
|
SELECT Rental_Apply__c raId, count(Id) cnt
|
FROM Rental_Apply_Equipment_Set__c
|
WHERE Rental_Apply__c
|
In :raIdSet
|
//AND Cancel_Reason__c != null
|
group by Rental_Apply__c
|
];
|
Map<String, Integer> raIdCntMap = new Map<String, Integer>();
|
|
for (AggregateResult ar : aggArr ) {
|
String raId = String.valueOf(ar.get('raId'));
|
Integer cnt = Integer.valueOf(ar.get('cnt'));
|
raIdCntMap.put(raId, cnt);
|
|
}
|
system.debug('raIdCntMap--->'+raIdCntMap);
|
system.debug('raIdMap--->'+raIdMap);
|
// 克隆生成的备品借出申请下的借出备品配套一览数量 不能超过被克隆的备品借出申请 下的借出备品配套一览数量
|
Map<Id, Boolean> raCheckMap = new Map<Id, Boolean>();
|
for (Rental_Apply__c ra : raLsit) {
|
String raId = ra.Id;
|
system.debug('raId--->'+raId);
|
Integer raCnt = raIdMap.get(raId).size() ;
|
if (raIdCntMap.containskey(raId)) {
|
system.debug('raIdCntMap--1--->'+raIdCntMap);
|
raCnt += raIdCntMap.get(raId);
|
}
|
system.debug('raCnt--->'+raCnt);
|
String cloneRaId = ra.OPDPlan__r.OriginalOpdPlanApplication__r.Rental_Apply2__c;
|
system.debug('cloneRaId--->'+cloneRaId);
|
|
Integer cloneRacnt = raIdCntMap.get(cloneRaId);
|
system.debug('cloneRacnt--->'+cloneRacnt);
|
if (raCnt > cloneRacnt) {
|
raCheckMap.put(raId,true);
|
}
|
|
}
|
if (raCheckMap.size() > 0) {
|
for (Rental_Apply_Equipment_Set__c nObj : newList) {
|
String raId = nobj.Rental_Apply__c;
|
if (System.Test.isRunningTest() == false && raCheckMap.containskey(raId)) {
|
//nObj.addError('借出备品配套一览数量 超出 原备品借出申请借出备品配套一览数量');
|
}
|
|
}
|
}
|
|
|
|
}
|
|
}
|
}
|
}
|