package com.cim.idm.mwmsextend.checkrecord.management.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.cim.idm.framework.action.dowork.ActionProcessor;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.framework.exception.IDMFrameDBErrorSignal;
import com.cim.idm.framework.exception.ext.DuplicateNameSignal;
import com.cim.idm.framework.exception.ext.FrameworkErrorSignal;
import com.cim.idm.framework.exception.ext.NotFoundSignal;
import com.cim.idm.framework.orm.info.DataInfo;
import com.cim.idm.framework.orm.service.CommonServiceDAO;
import com.cim.idm.mwmsextend.checkrecord.management.data.CheckRecord;
import com.cim.idm.mwmsextend.checkrecord.management.data.CheckRecordKey;
import com.cim.idm.mwmsextend.checkrecord.management.CheckRecordService;
import com.cim.idm.mwmsextend.checkrecord.management.info.CreateInfo;
import com.cim.idm.mwmsextend.checkrecord.management.info.RemoveInfo;
import com.cim.idm.mwmsextend.checkrecord.management.info.SetEventInfo;
@Service("checkRecordService")
public class CheckRecordServiceImpl extends CommonServiceDAO<CheckRecordKey,CheckRecord> implements CheckRecordService{
	
	private ActionProcessor<CheckRecordKey,CheckRecord,CreateInfo> createActionProcessor;
	private ActionProcessor<CheckRecordKey,CheckRecord,RemoveInfo> removeActionProcessor;
	private ActionProcessor<CheckRecordKey,CheckRecord,SetEventInfo> setEventActionProcessor;
	
	public CheckRecordServiceImpl() {
		createActionProcessor = new ActionProcessor<>(this);
		removeActionProcessor = new ActionProcessor<>(this);
		setEventActionProcessor = new ActionProcessor<>(this);
	}

	@Override
	public CheckRecord[] create(EventInfo eventInfo,
			List<CreateInfo> createInfoList) throws NotFoundSignal,
			DuplicateNameSignal, FrameworkErrorSignal {
		CheckRecordKey[] keyList= new CheckRecordKey[createInfoList.size()];
		for(int i=0;i<createInfoList.size();i++){
			keyList[i]=new CheckRecordKey(createInfoList.get(i).getPlanDate(),createInfoList.get(i).getCheckPlanName(),
					createInfoList.get(i).getSiteName(),createInfoList.get(i).getMaterialPackingName(),
					createInfoList.get(i).getActLocation(),createInfoList.get(i).getCheckMaterialType(),createInfoList.get(i).getCheckPackingType());}
		CreateInfo[] createInfoArray=new CreateInfo[createInfoList.size()];
		createInfoArray = createInfoList.toArray(createInfoArray);
		CheckRecord[] dataInfoArray=createActionProcessor.doWorkForBatch(keyList,eventInfo,createInfoArray,"create");
		return dataInfoArray;
	}
	
	@Override
	public CheckRecord create(EventInfo eventInfo, CreateInfo createInfo)
			throws NotFoundSignal, DuplicateNameSignal, FrameworkErrorSignal {
		return createActionProcessor.doWork(new CheckRecordKey(createInfo.getPlanDate(),createInfo.getCheckPlanName(),createInfo.getSiteName(),createInfo.getMaterialPackingName(),
				createInfo.getActLocation(),createInfo.getCheckMaterialType(),createInfo.getCheckPackingType()),eventInfo,createInfo,"create");
	}

	@Override
	public List<CheckRecord> select(String condition, Object[] bindSet)
			throws NotFoundSignal, IDMFrameDBErrorSignal {
		return this.select(condition, bindSet, CheckRecord.class);
	}

	@Override
	public CheckRecord remove(CheckRecordKey key, EventInfo eventInfo,
			RemoveInfo removeInfo) throws NotFoundSignal, DuplicateNameSignal,
			FrameworkErrorSignal {
		return removeActionProcessor.doWork(key,eventInfo,removeInfo,"remove");
	}

	@Override
	public CheckRecord[] remove(List<CheckRecordKey> keyList, EventInfo eventInfo,
			List<RemoveInfo> removeInfo) throws NotFoundSignal, DuplicateNameSignal,
			FrameworkErrorSignal {
		CheckRecordKey[] keyArray= new CheckRecordKey[keyList.size()];
		keyArray = keyList.toArray(keyArray);
		RemoveInfo[] removeInfoArray = new RemoveInfo[removeInfo.size()];
		removeInfoArray = removeInfo.toArray(removeInfoArray);
		CheckRecord[] dataInfoArray = removeActionProcessor.doWorkForBatch(keyArray, eventInfo, removeInfoArray, "remove");
		return dataInfoArray;
	}

	@Override
	public CheckRecord setEvent(CheckRecordKey key, EventInfo eventInfo,
			SetEventInfo setEventInfo) throws NotFoundSignal,
			DuplicateNameSignal, FrameworkErrorSignal {
		return setEventActionProcessor.doWork(key,eventInfo,setEventInfo,"setEvent");
	}

	@Override
	public CheckRecord[] setEvent(List<CheckRecordKey> keyList,
			EventInfo eventInfo, SetEventInfo setEventInfo)
			throws NotFoundSignal, DuplicateNameSignal, FrameworkErrorSignal {
		CheckRecordKey[] keyArray= new CheckRecordKey[keyList.size()];
		keyArray = keyList.toArray(keyArray);
		CheckRecord[] dataInfoArray= setEventActionProcessor.doWorkAtOnce(keyArray,eventInfo,setEventInfo,"setEvent");
		return dataInfoArray;
	}

	@Override
	public CheckRecord[] setEvent(List<CheckRecordKey> keyList,
			EventInfo eventInfo, List<SetEventInfo> setEventInfoList)
			throws NotFoundSignal, DuplicateNameSignal, FrameworkErrorSignal {
		CheckRecordKey[] keyArray=new CheckRecordKey[keyList.size()];
		keyArray=keyList.toArray(keyArray);
		SetEventInfo[] setEventInfoArray=new SetEventInfo[setEventInfoList.size()];
		setEventInfoArray=setEventInfoList.toArray(setEventInfoArray);
		CheckRecord[] dataInfoArray = setEventActionProcessor.doWorkForBatch(keyArray,eventInfo,setEventInfoArray,"setEvent");
		return dataInfoArray;
	}
}
