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

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

import org.activiti.engine.history.NativeHistoricTaskInstanceQuery;
import org.activiti.engine.impl.NativeExecutionQueryImpl;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
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.DeleteCallActivityCmd;
import com.blueland.bpm.engine.core.cmd.DeleteMultiInstanceCmd;
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.WorkFlowContants;
import com.blueland.bpm.engine.util.WorkFlowException;
/**
 * 多实例（内嵌子例程）撤销执行类
 * @version:  1.0
 * @author lw 
 */
@Service(WorkFlowContants.ACTION_TYPE_DELETE_MULTIINSTANCE_ACTIVITY)
public class DeleteMultiInstanceActivityExecutor  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());
		Long taskSize  = 0l;
		/*
		 * WorkFlowException(ExceptionErrorCode.B2050,
		 * "该流程只有当前一条任务实例，不允许删除（可转派给他人或自由跳转）！"); }
		 */
		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());
		// 设置参数到引擎变量中
		workflowMap.put(WorkFlowContants.WF_PROCESS_INPUT_VARIABLE_KEY,
				processInputModel);
		workflowMap.putAll(processInputModel.getWf_variables());
		//CounterSignCmd counterSignCmd=new CounterSignCmd("remove",convertReceivers(processInputModel.getWf_receiver()),curTask.getId());
		Command<Void> deleteMultiInstanceCmd =null;
		if(processInstance.getId().equals(processInstance.getTopProcessInstanceId())){//多实例
			deleteMultiInstanceCmd = new DeleteMultiInstanceCmd(processInstance.getId(),processInputModel.getWf_nextActDefId(),processInputModel.getWf_curActInstId(),
					convertReceivers(processInputModel.getWf_receiver()), workflowMap);
		}else{//子流程
			deleteMultiInstanceCmd = new DeleteCallActivityCmd(processInstance.getId(),processInputModel.getWf_nextActDefId(),processInputModel.getWf_curActInstId(),
					convertReceivers(processInputModel.getWf_receiver()), workflowMap);
		}
		this.managementService.executeCommand(deleteMultiInstanceCmd);
		ProcessInstanceModel processInstanceModel =null;
		try{
		 processInstanceModel = processInstanceModellBuilder
				.builderProcessInfo(processInputModel,processDefinition, processInstance, null);
		}catch(Exception e){}
		return processInstanceModel;
	}

}
