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

import java.util.HashMap;
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.TempSaveTaskCmd;
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.WorkFlowContants;
import com.blueland.bpm.engine.util.WorkFlowException;
/**
 * 暂存活动(包含新建暂存、待办暂存逻辑)执行类
 * @version:  1.0
 * @author lw 
 */
@Service(WorkFlowContants.ACTION_TYPE_SAVE_ACTIVITY)
public class SaveActivityExecutor  extends AbstractProcessExecutor  implements ProcessExecutor{

	@Override
	public ProcessInstanceModel execute(ProcessInputModel processInputModel)
			throws WorkFlowException {
		Map<String, Object> workflowMap = new HashMap<String, Object>();
		
		// 设置页面fields
		//if (StringUtils.isEmpty(processInputModel.getWf_fields())) {
			processInputModel.setWf_fields(PageFieldsUtil
					.fieldsToXml(processInputModel.getFields()));
	//	}
		ProcessDefinition processDefinition = getProcessDefinition(processInputModel.getWf_procDefId());
		additionalProcessInputModel(processInputModel, processDefinition);

		// 设置参数到引擎变量中
		workflowMap.put(WorkFlowContants.WF_PROCESS_INPUT_VARIABLE_KEY,
				processInputModel);
		workflowMap.put(WorkFlowContants.WF_FIELDS_KEY,
				PageFieldsUtil.fieldsToXml(processInputModel.getFields()));
		workflowMap.putAll(processInputModel.getWf_variables());
		String sender = processInputModel.getWf_sender();
		// 设置流程发起人
		identityService
				.setAuthenticatedUserId(processInputModel.getWf_sender());
		// ----------------------------------
		ProcessInstanceModel processInstanceModel = null;
		ProcessInstance processInstance = null;
		//新建暂存
		if (StringUtils.isEmpty(processInputModel.getWf_curActInstId())
				|| StringUtils.isEmpty(processInputModel.getWf_procInstId())) {
			processInputModel
					.setWf_actionType(WorkFlowContants.ACTION_TYPE_LAUCH_SAVE_PROCESS);
			if (StringUtils.isEmpty(processInputModel.getWf_receiver())) {
				// 下一步人员为空,自动路由到环节绑定的资源中的最前一个人
				//setCurActDefIdAndNextActDefIdByStart(processInputModel);
				sender = processInputModel.getWf_sender();
			}
			// 设置流程发起人
			identityService.setAuthenticatedUserId(processInputModel
					.getWf_sender());
			// 起草流程特殊处理
			processInputModel.setWf_receiver(sender);
			// 启动流程
			processInstance = this.runtimeService.startProcessInstanceById(
					processInputModel.getWf_procDefId(),
					processInputModel.getWf_businessKey(), workflowMap);
		} else {//待办暂存
			// 查询流程
			processInstance =  getProcessInstance(processInputModel.getWf_procInstId());
		}
		if(processInputModel.getWf_receivers()!=null) {
			sender=processInputModel.getWf_receivers().get(0).getReceiveUserId();
		}
		// 根据sender获取起草环节的任务
		Task task = this.findTaskByAssignee(processInstance.getId(), sender);
		processInputModel.setWf_curActInstId(task.getId());
		// this.taskService.claim(task.getId(), sender);

		processInstanceModel = processInstanceModellBuilder.builderProcessInfo(processInputModel,
				processDefinition, processInstance, task);
		workflowMap.put(WorkFlowContants.WF_FIELDS_KEY,
				PageFieldsUtil.fieldsToXml(processInputModel.getFields()));
		workflowMap.put(WorkFlowContants.WF_CUR_COMMENT_KEY, processInputModel.getWf_curComment());
		
		Command<Void> tempSaveTaskCmd=new TempSaveTaskCmd(processInputModel.getWf_curActInstId(),
				workflowMap);
		this.managementService.executeCommand(tempSaveTaskCmd);
		return processInstanceModel;
	}


}
