public without sharing class RentalApplySequenceTriggerHandler extends Oly_TriggerHandler { private Map newMap; private Map oldMap; private List newList; private List oldList; public RentalApplySequenceTriggerHandler() { this.newMap = (Map) Trigger.newMap; this.oldMap = (Map) Trigger.oldMap; this.newList = (List) Trigger.new; this.oldList = (List) Trigger.old; } protected override void afterUpdate() { List rasList = new List(); for (Rental_Apply_Sequence__c ras : newList) { Rental_Apply_Sequence__c old = oldMap.get(ras.Id); // 无效Flag变为True if (old.Invalid_Flag__c != ras.Invalid_Flag__c && ras.Invalid_Flag__c) { rasList.add(ras); } } updateSingleSequence(rasList); } protected override void afterDelete() { updateSingleSequence(oldList); } // 混合序列变为单一序列时,更新序列号 private void updateSingleSequence(List rasList) { // 先确认是否还原为单一序列 List externalKeyList = new List(); for (Rental_Apply_Sequence__c ras : rasList) { externalKeyList.add(ras.ExternalKey__c); } if (externalKeyList != null && externalKeyList.size() > 0) { // 先检索出混合序列,从更新序列中排除 List arList = [ SELECT ExternalKey__c, COUNT(Apply_Set_Detail_ExternalKey__c) num FROM Rental_Apply_Sequence__c WHERE Invalid_Flag__c = false AND ExternalKey__c IN: externalKeyList GROUP BY ExternalKey__c]; // 更新externalKey List List updateKey = new List(); List mixKey = new List(); for (AggregateResult ar : arList) { // 单一序列判断 if ((Integer)ar.get('num') == 1) { updateKey.add((String)ar.get('ExternalKey__c')); } // 混合序列判断 if ((Integer)ar.get('num') > 1) { mixKey.add((String)ar.get('ExternalKey__c')); } } // 检索出单一序列,更新单一序列 List updList = [ SELECT Id, Series_No__c, Apply_Set_Detail__r.Queue_Number__c FROM Rental_Apply_Sequence__c WHERE Invalid_Flag__c = false AND ExternalKey__c IN: updateKey AND Series_Unequal_Queue_Flag__c = true ORDER BY ExternalKey__c]; for (Rental_Apply_Sequence__c ras : updList) { ras.Series_No__c = ras.Apply_Set_Detail__r.Queue_Number__c; } // 更新单一序列信息 update updList; // 检索出混合序列,更新混合序列 List updMixList = [ SELECT Id, Series_No__c, ExternalKey__c FROM Rental_Apply_Sequence__c WHERE Invalid_Flag__c = false AND ExternalKey__c IN: mixKey ORDER BY ExternalKey__c, Series_No__c]; String compareKey = null; Integer defaultNum = 1; for (Rental_Apply_Sequence__c ras : updMixList) { if (compareKey != ras.ExternalKey__c) { compareKey = ras.ExternalKey__c; defaultNum = 1; ras.Series_No__c = 1; } else { ras.Series_No__c = defaultNum; } defaultNum++; } // 更新混合序列信息 update updMixList; } } }