public without sharing class RentalApplyEquipmentCheckHandler extends Oly_TriggerHandler { private Map newMap; private Map oldMap; private List newList; private List oldList; public RentalApplyEquipmentCheckHandler() { this.newMap = (Map) Trigger.newMap; this.oldMap = (Map) Trigger.oldMap; this.newList = (List) Trigger.new; this.oldList = (List) Trigger.old; } protected override void beforeInsert() { beforeSetValue(); } private void beforeSetValue() { //获取 key-备品借出申请的Id value-借出备品配套一览Id集合 Map> raIdMap = new Map>(); for (Rental_Apply_Equipment_Set_Detail__c nObj : newList) { if (String.isNotBlank(nObj.UniqueKey__c)) { String raId = nobj.Rental_Apply__c; String raesId = nobj.Rental_Apply_Equipment_Set__c; system.debug('raesId--->' + raesId); Map raesIdMap = new Map(); if (raIdMap.containskey(raId)) { raesIdMap = raIdMap.get(raId); } raesIdMap.put(raesId, raesId); raIdMap.put(raId, raesIdMap); } } if (raIdMap.size() > 0) { //查找备品借出申请 是不是 补充申请创建 SupplementCreated__c List raLsit = [ SELECT Id, OPDPlan__r.OriginalOpdPlanApplication__r.Rental_Apply2__c from Rental_Apply__c where SupplementCreated__c = true AND OPDPlan__c != null AND Id In :raIdMap.keySet()]; if (raLsit.size() > 0 ) { // 获取 补充申请OPD计划创建的 备品借出申请Id 以及 被克隆的备品借出申请Id Set raIdSet = new Set(); 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); } Map raIdCntMap = new Map(); List raesList = new List(); //查找备品借出申请下 借出备品配套一览 raesList = [ SELECT Id, Rental_Apply__c, Skip__c FROM Rental_Apply_Equipment_Set__c WHERE Rental_Apply__c In :raIdSet ]; Map> raIdMaps = new Map>(); Set raesIdSet = new Set(); for (Rental_Apply_Equipment_Set__c raes : raesList) { String raId = raes.Rental_Apply__c; String raesId = raes.Id; Map raesIdMap = new Map(); if (raIdMaps.containskey(raId)) { raesIdMap = raIdMaps.get(raId); } raesIdMap.put(raesId, raes); raIdMaps.put(raId, raesIdMap); raesIdSet.add(raesId); } //Map raesIdCntMap = new Map(); //AggregateResult[] aggArr = [ // SELECT Rental_Apply_Equipment_Set__c raesId, count(Id) cnt // FROM Rental_Apply_Equipment_Set_Detail__c // WHERE Rental_Apply_Equipment_Set__c // In :raesIdSet // group by Rental_Apply_Equipment_Set__c // ]; //for (AggregateResult ar : aggArr ) { // String raesId = String.valueOf(ar.get('raesId')); // system.debug('raesId---1>' + raesId); // Integer cnt = Integer.valueOf(ar.get('cnt')); // raesIdCntMap.put(raesId, cnt); //} //system.debug('raesIdCntMap--->' + raesIdCntMap); Map raCheckMap = new Map(); for (Rental_Apply__c ra : raLsit) { String raId = ra.Id; Map raesIdMap = raIdMap.get(raId); Integer raCnt = 0; Map raesMap = raIdMaps.get(raId); for (Id raesId : raesIdMap.keySet()) { //if (raIdMaps.containskey(raId)) { //if (raIdMaps.get(raId).containskey(raesId)) { //raesMap = raIdMaps.get(raId); if (raesMap.containskey(raesId)) { Rental_Apply_Equipment_Set__c rase = raesMap.get(raesId); if (!rase.Skip__c) { raCnt += 1; } } } for (Id raesId : raesMap.keySet()) { Rental_Apply_Equipment_Set__c rase = raesMap.get(raesId); if (rase.Skip__c) { raCnt += 1; } } system.debug('raCnt-1------>' + raCnt); //if (raIdMaps.containskey(raId)) { // raCnt += raIdMaps.get(raId).size(); //} //raCnt += raesIdCntMap.size(); //system.debug('raCnt-2------>' + raCnt); String cloneRaId = ra.OPDPlan__r.OriginalOpdPlanApplication__r.Rental_Apply2__c; Integer cloneRacnt = 0; if (raIdMaps.containskey(cloneRaId)) { cloneRacnt = raIdMaps.get(cloneRaId).size(); } system.debug('cloneRacnt------>' + cloneRacnt); if ( raCnt > cloneRacnt) { raCheckMap.put(raId, true); } } //List updateRaseList = new Rental_Apply_Equipment_Set__c(); Map updateRaseMap = new Map(); for (Rental_Apply_Equipment_Set_Detail__c nObj : newList) { String raId = nobj.Rental_Apply__c; if (System.Test.isRunningTest() == false && raCheckMap.containskey(raId)) { nObj.addError('借出备品配套一览数量 超出 原备品借出申请借出备品配套一览数量'); } String raesId = nobj.Rental_Apply_Equipment_Set__c; Rental_Apply_Equipment_Set__c rase = new Rental_Apply_Equipment_Set__c(); rase.Id = raesId; rase.Skip__c = true; updateRaseMap.put(raesId,rase); } if (updateRaseMap.size() > 0) { update updateRaseMap.values(); } } } } }