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

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
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.checkplan.management.data.CheckPlan;
import com.cim.idm.mwmsextend.checkplan.management.data.CheckPlanKey;
import com.cim.idm.mwmsextend.checkplan.management.CheckPlanService;
import com.cim.idm.mwmsextend.checkplan.management.info.CreateInfo;
import com.cim.idm.mwmsextend.checkplan.management.info.RemoveInfo;
import com.cim.idm.mwmsextend.checkplan.management.info.SetEventInfo;

@Service("checkPlanService")
public class CheckPlanServiceImpl extends CommonServiceDAO<CheckPlanKey,CheckPlan> implements CheckPlanService{

	private ActionProcessor<CheckPlanKey,CheckPlan,CreateInfo> createActionProcessor;
	private ActionProcessor<CheckPlanKey,CheckPlan,RemoveInfo> removeActionProcessor;
	private ActionProcessor<CheckPlanKey,CheckPlan,SetEventInfo> setEventActionProcessor;

	public CheckPlanServiceImpl() {
		createActionProcessor = new ActionProcessor<>(this);
		removeActionProcessor = new ActionProcessor<>(this);
		setEventActionProcessor = new ActionProcessor<>(this);
	}
	
	@Override
	public CheckPlan[] create(EventInfo eventInfo,
			List<CreateInfo> createInfoList) throws NotFoundSignal,
			DuplicateNameSignal, FrameworkErrorSignal {
		CheckPlanKey[] keyList= new CheckPlanKey[createInfoList.size()];
		for(int i=0;i<createInfoList.size();i++){
			keyList[i]=new CheckPlanKey(createInfoList.get(i).getPlanDate(),createInfoList.get(i).getCheckPlanName(),
					createInfoList.get(i).getSiteName(),createInfoList.get(i).getMaterialPackingName());}
		CreateInfo[] createInfoArray=new CreateInfo[createInfoList.size()];
		createInfoArray = createInfoList.toArray(createInfoArray);
		CheckPlan[] dataInfoArray=createActionProcessor.doWorkForBatch(keyList,eventInfo,createInfoArray,"create");
		return dataInfoArray;
	}
	
	@Override
	public CheckPlan create(EventInfo eventInfo, CreateInfo createInfo)
			throws NotFoundSignal, DuplicateNameSignal, FrameworkErrorSignal {
		return createActionProcessor.doWork(new CheckPlanKey(createInfo.getPlanDate(),createInfo.getCheckPlanName(),createInfo.getSiteName(),createInfo.getMaterialPackingName()),eventInfo,createInfo,"create");
	}

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

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

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

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

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

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