public without sharing class RentalApplyEquipmentCheckHandler extends Oly_TriggerHandler {
	private Map<Id, Rental_Apply_Equipment_Set_Detail__c> newMap;
	private Map<Id, Rental_Apply_Equipment_Set_Detail__c> oldMap;
	private List<Rental_Apply_Equipment_Set_Detail__c> newList;
	private List<Rental_Apply_Equipment_Set_Detail__c> oldList;

	public RentalApplyEquipmentCheckHandler() {
		this.newMap = (Map<Id, Rental_Apply_Equipment_Set_Detail__c>) Trigger.newMap;
		this.oldMap = (Map<Id, Rental_Apply_Equipment_Set_Detail__c>) Trigger.oldMap;
		this.newList = (List<Rental_Apply_Equipment_Set_Detail__c>) Trigger.new;
		this.oldList = (List<Rental_Apply_Equipment_Set_Detail__c>) Trigger.old;
	}
	protected override void beforeInsert() {
		beforeSetValue();
	}

	private void beforeSetValue() {
		//获取 key-备品借出申请的Id value-借出备品配套一览Id集合
		Map<Id, Map<Id, Id>> raIdMap = new Map<Id, Map<Id, Id>>();
		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<Id, Id> raesIdMap = new Map<Id, Id>();
				if (raIdMap.containskey(raId)) {
					raesIdMap = raIdMap.get(raId);
				}
				raesIdMap.put(raesId, raesId);
				raIdMap.put(raId, raesIdMap);
			}
		}

		if (raIdMap.size() > 0) {
			//查找备品借出申请 是不是 补充申请创建 SupplementCreated__c
			List<Rental_Apply__c> 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<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);
				}

				Map<String, Integer> raIdCntMap = new Map<String, Integer>();
				List<Rental_Apply_Equipment_Set__c> raesList = new List<Rental_Apply_Equipment_Set__c>();
				//查找备品借出申请下 借出备品配套一览
				raesList = [
				               SELECT Id, Rental_Apply__c, Skip__c
				               FROM Rental_Apply_Equipment_Set__c
				               WHERE Rental_Apply__c
				               In :raIdSet
				           ];


				Map<Id, Map<Id, Rental_Apply_Equipment_Set__c>> raIdMaps = new Map<Id, Map<Id, Rental_Apply_Equipment_Set__c>>();
				Set<Id> raesIdSet = new Set<Id>();
				for (Rental_Apply_Equipment_Set__c raes : raesList) {
					String raId = raes.Rental_Apply__c;
					String raesId = raes.Id;
					Map<Id, Rental_Apply_Equipment_Set__c> raesIdMap = new Map<Id, Rental_Apply_Equipment_Set__c>();
					if (raIdMaps.containskey(raId)) {
						raesIdMap = raIdMaps.get(raId);
					}
					raesIdMap.put(raesId, raes);
					raIdMaps.put(raId, raesIdMap);
					raesIdSet.add(raesId);
				}
				//Map<String, Integer> raesIdCntMap = new Map<String, Integer>();
				//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<Id, Boolean> raCheckMap = new Map<Id, Boolean>();
				for (Rental_Apply__c ra : raLsit) {
					String raId = ra.Id;
					Map<Id, Id> raesIdMap = raIdMap.get(raId);
					Integer  raCnt = 0;
					Map<Id, Rental_Apply_Equipment_Set__c> 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<Rental_Apply_Equipment_Set__c> updateRaseList = new Rental_Apply_Equipment_Set__c();
				Map<Id,Rental_Apply_Equipment_Set__c> updateRaseMap = new Map<Id,Rental_Apply_Equipment_Set__c>();
				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();
				}
			}
		}

	}
}