package com.blueland.bpm.engine.core.executor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blueland.bpm.engine.core.cmd.CallBackTaskCmd;
import com.blueland.bpm.engine.core.model.ExceptionErrorCode;
import com.blueland.bpm.engine.core.model.ProcessInputModel;
import com.blueland.bpm.engine.core.model.ProcessInstanceModel;
import com.blueland.bpm.engine.core.service.ProcessInstanceService;
import com.blueland.bpm.engine.util.PageFieldsUtil;
import com.blueland.bpm.engine.util.WorkFlowContants;
import com.blueland.bpm.engine.util.WorkFlowException;
/**
 * 在办撤回活动(默认退回至上一步活动)执行类
 * @version:  1.0
 * @author lw 
 */
@Service(WorkFlowContants.ACTION_TYPE_CALLBACK_ACTIVITY)
public class CallbackActivityExecutor extends AbstractProcessExecutor implements ProcessExecutor{
	@Autowired
	ProcessInstanceService processInstanceService;

	@Override
	public ProcessInstanceModel execute(ProcessInputModel processInputModel)
			throws WorkFlowException {
		ProcessDefinition processDefinition =getProcessDefinition(processInputModel.getWf_procDefId());
		additionalProcessInputModel(processInputModel, processDefinition);
		ProcessInstance processInstance = getProcessInstance(processInputModel.getWf_procInstId());

		List<Task> tasks = processInstanceService.getProcessTasks(
				processInputModel.getWf_procInstId(),
				processInputModel.getWf_sendUserId(),
				processInputModel.getWf_curActInstId());
		if (tasks == null || tasks.isEmpty() ||tasks.size() > 1) {
			throw new WorkFlowException(ExceptionErrorCode.B2060,String.format(
					"未找到任务实例ID[%S],发送人[%S]对应的待办任务！",
					processInputModel.getWf_curActInstId(),
					processInputModel.getWf_sendUserId()));
		}
		Task task = tasks.get(0);
		if (task == null) {
			throw new WorkFlowException(ExceptionErrorCode.B2060,String.format(
					"未找到任务实例ID[%S],发送人[%S]对应的待办任务！",
					processInputModel.getWf_curActInstId(),
					processInputModel.getWf_sendUserId()));
		}
		ProcessInputModel preProcessInputModel = (ProcessInputModel) processInstanceService
				.getHistoryTaskVariables(task.getId()).get(
						WorkFlowContants.WF_PROCESS_INPUT_VARIABLE_KEY);
		processInputModel.setWf_receiver(task.getSender());
		Map<String, Object> workflowMap = new HashMap<String, Object>();
		workflowMap.putAll(processInputModel.getWf_variables());
		// 设置页面fields
		if (StringUtils.isEmpty(processInputModel.getWf_fields())) {
			processInputModel.setWf_fields(PageFieldsUtil
					.fieldsToXml(processInputModel.getFields()));
		}
		workflowMap.put(WorkFlowContants.WF_FIELDS_KEY,
				preProcessInputModel.getWf_fields());
		// 设置参数到引擎变量中
		workflowMap.put(WorkFlowContants.WF_PROCESS_INPUT_VARIABLE_KEY,
				processInputModel);
		Command<Void> callBackTaskCmd = new CallBackTaskCmd(task.getId(),
				task.getPreTaskDefKey(), workflowMap);
		this.managementService.executeCommand(callBackTaskCmd);
		ProcessInstanceModel processInstanceModel = processInstanceModellBuilder
				.builderProcessInfo(processInputModel, processDefinition,
						processInstance, task);
		return processInstanceModel;
	}

}
