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.springframework.stereotype.Service;

import com.blueland.bpm.common.util.StringUtils;
import com.blueland.bpm.engine.core.cmd.AddCallActivityCmd;
import com.blueland.bpm.engine.core.cmd.AddMultiInstanceCmd;
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.util.PageFieldsUtil;
import com.blueland.bpm.engine.util.ProcessDefinitionUtils;
import com.blueland.bpm.engine.util.WorkFlowContants;
import com.blueland.bpm.engine.util.WorkFlowException;
/**
 * 多实例（内嵌子例程）补发执行类
 * @version:  1.0
 * @author lw 
 */
@Service(WorkFlowContants.ACTION_TYPE_ADD_MULTIINSTANCE_ACTIVITY)
public class AddMultiInstanceActivityExecutor  extends AbstractProcessExecutor  implements ProcessExecutor{

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

		// 根据wfCurActInstId获取当前任务实例

		Map<String, Object> workflowMap = new HashMap<String, Object>();
		// 设置页面fields
		if (StringUtils.isEmpty(processInputModel.getWf_fields())) {
			processInputModel.setWf_fields(PageFieldsUtil
					.fieldsToXml(processInputModel.getFields()));
		}
		workflowMap.put(WorkFlowContants.WF_FIELDS_KEY,
				processInputModel.getWf_fields());
		// ----------------------------------
		// 根据wfCurActInstId获取当前任务实例
		List<Task> tasks=processInstanceService.getProcessTasks(processInstance.getProcessInstanceId(), processInputModel.getWf_sender(), processInputModel.getWf_curActInstId());
		Task task=tasks.get(0);
		if (task == null) {
			throw new WorkFlowException(ExceptionErrorCode.B2060,String.format(
					"未找到流程实例ID[%S]、任务实例ID[%S]、发送人[%S]对应的待办任务！",
					processInputModel.getWf_procInstId(),
					processInputModel.getWf_curActInstId(),processInputModel.getWf_sender()));
		}
		// 设置参数到引擎变量中
		workflowMap.put(WorkFlowContants.WF_PROCESS_INPUT_VARIABLE_KEY,
				processInputModel);
		workflowMap.putAll(processInputModel.getWf_variables());
		Command<Void> addMultiInstanceCmd =null;
		boolean multiInstance=ProcessDefinitionUtils.isMultiInstance(processInputModel,repositoryService);
		if(multiInstance||processInstance.getId().equals(processInstance.getTopProcessInstanceId())){//多实例
			addMultiInstanceCmd = new AddMultiInstanceCmd(
					task.getId(),
					convertReceivers(processInputModel.getWf_receiver()),
					workflowMap);
		}else{
			addMultiInstanceCmd = new AddCallActivityCmd(//子流程
					task.getId(),
					convertReceivers(processInputModel.getWf_receiver()),
					workflowMap);
		}
		this.managementService.executeCommand(addMultiInstanceCmd);
		ProcessInstanceModel processInstanceModel = processInstanceModellBuilder
				.builderProcessInfo(processInputModel,processDefinition, processInstance, task);
		return processInstanceModel;
	}
	

}
