public without sharing class RentalApplySequenceTriggerHandler extends Oly_TriggerHandler {

	private Map<Id, Rental_Apply_Sequence__c> newMap;
	private Map<Id, Rental_Apply_Sequence__c> oldMap;
	private List<Rental_Apply_Sequence__c> newList;
	private List<Rental_Apply_Sequence__c> oldList;

	public RentalApplySequenceTriggerHandler() {
		this.newMap = (Map<Id, Rental_Apply_Sequence__c>) Trigger.newMap;
		this.oldMap = (Map<Id, Rental_Apply_Sequence__c>) Trigger.oldMap;
		this.newList = (List<Rental_Apply_Sequence__c>) Trigger.new;
		this.oldList = (List<Rental_Apply_Sequence__c>) Trigger.old;
	}

	protected override void afterUpdate() {
		List<Rental_Apply_Sequence__c> rasList = new List<Rental_Apply_Sequence__c>();
		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<Rental_Apply_Sequence__c> rasList) {
		// 先确认是否还原为单一序列
		List<String> externalKeyList = new List<String>();
		for (Rental_Apply_Sequence__c ras : rasList) {
			externalKeyList.add(ras.ExternalKey__c);
		}

		if (externalKeyList != null && externalKeyList.size() > 0) {
			// 先检索出混合序列，从更新序列中排除
			List<AggregateResult> 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<String> updateKey = new List<String>();
			List<String> mixKey = new List<String>(); 
			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<Rental_Apply_Sequence__c> 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<Rental_Apply_Sequence__c> 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;
		}
    }
}