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

/**
 * 流程客户端操作接口门面（门面类）
 * @version: 1.0
 * @author lw
 */
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.impl.persistence.entity.HistoricProcessInstanceEntity;
import org.activiti.engine.task.Comment;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blueland.bpm.common.util.DateUtil;
import com.blueland.bpm.engine.config.model.ActivityInfoConfig;
import com.blueland.bpm.engine.core.model.ActivityDefinitionModel;
import com.blueland.bpm.engine.core.model.ActivityInstanceModel;
import com.blueland.bpm.engine.core.model.BusinessDataObject;
import com.blueland.bpm.engine.core.model.ProcessDefinitionModel;
import com.blueland.bpm.engine.core.model.ProcessInputModel;
import com.blueland.bpm.engine.core.model.ProcessInstanceModel;
import com.blueland.bpm.engine.core.model.ProcessLogModel;
import com.blueland.bpm.engine.core.model.ProcessPageContext;
import com.blueland.bpm.engine.util.WorkFlowContants;
import com.blueland.bpm.engine.util.WorkFlowException;
import com.blueland.bpm.identity.UserService;
import com.blueland.bpm.identity.model.User;

@Service
public class ProcessOperateFacade {
	@Autowired
	ProcessExecuteServiceFacde processExecuteServiceFacde;

	@Autowired
	ProcessExecuteService processExecuteService;
	@Autowired
	ProcessDefinitionService processDefinitionService;
	@Autowired
	private ProcessConfigService processConfigService;
	@Autowired
	ProcessInstanceService processInstanceService;
	@Autowired
	UserService userService;

	protected Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 执行流程下一步
	 * 
	 * @param request
	 * @param processInputModel
	 * @return
	 */
	public Map<String, Object> nextExecute(ProcessInputModel processInputModel) {
		Map<String, Object> resultMap = null;
		try {
			resultMap = processExecuteService.nextExecute(processInputModel);
		} catch (Exception e) {
			logger.error(
					"execute error,ProcessInputModel:" + processInputModel, e);
		}
		return resultMap;
	}

	/**
	 * 获取流程起草任务详细信息
	 * 
	 * @param packageId
	 * @param processDefId
	 * @param activityDefId
	 * @return
	 */
	public Map<String, Object> getProcessStartDetail(String processDefId,
			String userId,String userOrgId) {
		User loginUser = userService.getUserById(userId);
		ProcessDefinitionModel processDefinition = processDefinitionService
				.getProcessDef(processDefId);
		String workflowPage = "";
		try {
			workflowPage = processConfigService.getWorkflowPage(processDefId,
					"");
		} catch (WorkFlowException e) {
			logger.error(
					String.format("获取流程表单出现异常,processDefId[%s]", processDefId),
					e);
		}
		ProcessPageContext processPageContext = new ProcessPageContext(
				WorkFlowContants.START_OPEN_PAGE_TYPE,
				WorkFlowContants.ACTION_TYPE_LAUCH_PROCESS, workflowPage,
				userId,userOrgId);
		Map<String, Object> model = new HashMap<String, Object>();

		try {
			processDefinition.setProcessInfoConfig(processConfigService
					.getProcessInfoConfig(processDefId));
			ActivityDefinitionModel actDefModel = (ActivityDefinitionModel) processDefinitionService
					.getStartActivity(processDefId);
			workflowPage = addWorkflowPageQueryString(processDefinition, null,
					actDefModel, null, workflowPage,
					processPageContext.getOpenPageType());
			
			model.put("processDefinition", processDefinition);
			model.put("processPageContext", processPageContext);
			model.put("businessKey", java.util.UUID.randomUUID().toString());
			ActivityInfoConfig activityInfoConfig = processConfigService
					.getActivityInfoConfig(
							actDefModel.getProcDefId(),
							actDefModel.getActDefId(),
							WorkFlowContants.START_OPEN_PAGE_TYPE, model);
			actDefModel.setActivityInfoConfig(activityInfoConfig);
			model.put("curActivity", actDefModel);
			model.put("activityInfoConfig", activityInfoConfig);
		} catch (Exception e) {
			logger.error(String.format("获取流程起草表单信息出现异常,processDefId[%s]",
					processDefId), e);
		}
		return model;
	}

	/**
	 * 获取流程待办任务详细信息
	 * 
	 * @param procDefId
	 * @param procInstId
	 * @param curActInstId
	 * @param actDefId
	 * @return
	 */
	public Map<String, Object> getProcessToDoDetail(String procDefId,
			String procInstId, String curActInstId, String actDefId,
			String userId,String userOrgId) {
		return getProcessToDoDetail(procDefId, procInstId, curActInstId,
				actDefId, userId, userOrgId,true);
	}

	/**
	 * 获取流程待办任务详细信息
	 * 
	 * @param procDefId
	 * @param procInstId
	 * @param curActInstId
	 * @param actDefId
	 * @return
	 */
	public Map<String, Object> getProcessToDoDetail(String procDefId,
			String procInstId, String curActInstId, String actDefId,
			String userId,String userOrgId,boolean isClaim) {
		ProcessDefinitionModel processDefinition = processDefinitionService
				.getProcessDef(procDefId);
		String workflowPage = "";
		try {
			workflowPage = processConfigService.getWorkflowPage(procDefId,
					actDefId);
		} catch (WorkFlowException e) {
			logger.error(String.format(
					"获取流程表单出现异常,processDefId[%s],activityDefId[%s]", procDefId,
					actDefId), e);
		}
		ActivityInstanceModel task = processInstanceService
				.getTask(curActInstId);
		User receiver=userService.getUserById(task.getReceiver());
	
		ProcessPageContext processPageContext = new ProcessPageContext(
				WorkFlowContants.TODO_OPEN_PAGE_TYPE,
				WorkFlowContants.ACTION_TYPE_EXECUTE_ACTIVITY, workflowPage,receiver.getUserCode(),receiver.getOrgId());

		Map<String, Object> model = new HashMap<String, Object>();

		try {
			processDefinition.setProcessInfoConfig(processConfigService
					.getProcessInfoConfig(procDefId));
			//ProcessInstanceModel processInstanceModel=processInstanceService.getProcessInstanceById(procInstId);
			HistoricProcessInstanceEntity processInstance = (HistoricProcessInstanceEntity) processInstanceService
					.getHistoryService().createHistoricProcessInstanceQuery()
					.processInstanceId(procInstId).singleResult();
			ActivityDefinitionModel actDefModel = (ActivityDefinitionModel) processDefinitionService
					.getActivity(procDefId, actDefId);
			
			
			String fieldVariables = "";
			if (processInstance.getProcessInstanceId().equals(
					task.getProcInstId())
					&& !actDefModel.isMulti()) {
				fieldVariables = processInstanceService
						.getTaskFieldVariable(curActInstId);
			} else {
				fieldVariables = processInstanceService
						.getProcessFieldVariable(processInstance
								.getProcessInstanceId());
			}
			if (isClaim) {
				processInstanceService.getTaskService().claim(curActInstId,
						receiver.getUserId());
			}
			
			Map<String, Object> allComment = getProcessComments(procInstId);
			workflowPage = addWorkflowPageQueryString(processDefinition,
					processInstance, actDefModel, task, workflowPage,
					processPageContext.getOpenPageType());
			model.put("allComment", allComment);
			model.put("processDefinition", processDefinition);
			model.put("processInstance", processInstance);
			model.put("task", task);
			model.put("fields", fieldVariables);
			model.put("processPageContext", processPageContext);
			
			//by lw 为中烟特殊业务
			BusinessDataObject businessDataObject = null;
			BusinessDataObject throughBusinessDataObject = null;
			if (processInstance.getProcessInstanceId().equals(
					task.getProcInstId())
					&& !actDefModel.isMulti()) {
				businessDataObject = (BusinessDataObject) processInstanceService.getTaskVariables(curActInstId).get(WorkFlowContants.WF_BUSINESS_DATA_OBJECT_KEY);
				throughBusinessDataObject = (BusinessDataObject) processInstanceService.getTaskVariables(curActInstId).get(WorkFlowContants.WF_THROUGH_BUSINESS_DATA_OBJECT_KEY);
			} else {
				businessDataObject = (BusinessDataObject) processInstanceService.getProcessInstanceVariables(processInstance
						.getProcessInstanceId()).get(WorkFlowContants.WF_BUSINESS_DATA_OBJECT_KEY);
				throughBusinessDataObject = (BusinessDataObject) processInstanceService.getProcessInstanceVariables(processInstance
						.getProcessInstanceId()).get(WorkFlowContants.WF_THROUGH_BUSINESS_DATA_OBJECT_KEY);
			}
			model.put("businessKey", processInstance.getBusinessKey());
			model.put("businessDataObject", businessDataObject);
			model.put("throughBusinessDataObject", businessDataObject);
			ActivityInfoConfig activityInfoConfig = processConfigService
					.getActivityInfoConfig(
							actDefModel.getProcDefId(),
							actDefModel.getActDefId(),
							WorkFlowContants.TODO_OPEN_PAGE_TYPE, model);
			actDefModel.setActivityInfoConfig(activityInfoConfig);
			model.put("curActivity", actDefModel);
			model.put("activityInfoConfig", activityInfoConfig);
		} catch (Exception e) {
			logger.error(String.format(
					"获取流程起草表单信息出现异常,processDefId[%s],activityDefId[%s]",
					procDefId, actDefId), e);
		}
		return model;
	}

	/**
	 * 获取流程在办任务详细信息
	 * 
	 * @param procDefId
	 * @param procInstId
	 * @param curActInstId
	 * @param actDefId
	 * @return
	 */
	public Map<String, Object> getProcessZaibanDetail(String procDefId,
			String procInstId, String curActInstId, String actDefId,String userId,String userOrgId) {
		User loginUser = userService.getUserById(userId);
		ProcessDefinitionModel processDefinition = processDefinitionService
				.getProcessDef(procDefId);
		String workflowPage = "";
		try {
			workflowPage = processConfigService.getWorkflowPage(procDefId,
					actDefId);
		} catch (WorkFlowException e) {
			logger.error(String.format(
					"获取流程表单出现异常,processDefId[%s],activityDefId[%s]", procDefId,
					actDefId), e);
		}
		ProcessPageContext processPageContext = new ProcessPageContext(
				WorkFlowContants.ZAIBAN_OPEN_PAGE_TYPE, "", workflowPage, userId, userOrgId);

		Map<String, Object> model = new HashMap<String, Object>();

		try {
			processDefinition.setProcessInfoConfig(processConfigService
					.getProcessInfoConfig(procDefId));
			
			HistoricProcessInstanceEntity processInstance = (HistoricProcessInstanceEntity) processInstanceService
					.getHistoryService().createHistoricProcessInstanceQuery()
					.processInstanceId(procInstId).singleResult();
			ActivityInstanceModel task = processInstanceService.getHistoryTask(curActInstId);
			ActivityDefinitionModel actDefModel = task.getActivityDefinition();
			String fieldVariables = (String) processInstanceService.getHistoryTaskVariables(curActInstId).get(WorkFlowContants.WF_FIELDS_KEY);
			Map<String, Object> allComment = getProcessComments(procInstId);
			workflowPage = addWorkflowPageQueryString(processDefinition,
					processInstance, actDefModel, task, workflowPage,
					processPageContext.getOpenPageType());
			model.put("allComment", allComment);
			model.put("processDefinition", processDefinition);
			model.put("processInstance", processInstance);
			model.put("task", task);
			model.put("fields", fieldVariables);
			model.put("processPageContext", processPageContext);
			model.put("businessKey", processInstance.getBusinessKey());
			ActivityInfoConfig activityInfoConfig = processConfigService
					.getActivityInfoConfig(
							actDefModel.getProcDefId(),
							actDefModel.getActDefId(),
							WorkFlowContants.ZAIBAN_OPEN_PAGE_TYPE, model);
			actDefModel.setActivityInfoConfig(activityInfoConfig);
			model.put("curActivity", actDefModel);
			model.put("activityInfoConfig", activityInfoConfig);
			
		} catch (Exception e) {
			logger.error(String.format(
					"获取流程起草表单信息出现异常,processDefId[%s],activityDefId[%s]",
					procDefId, actDefId), e);
		}
		return model;
	}
	
	
	
	

	/**
	 * 获取流程在办任务详细信息
	 * 
	 * @param procDefId
	 * @param procInstId
	 * @param curActInstId
	 * @param actDefId
	 * @return
	 */
	public Map<String, Object> getProcessZaibanDetail2(String procDefId,
			String procInstId, String curActInstId, String actDefId,String userId,String userOrgId) {
		User loginUser = userService.getUserById(userId);
		ProcessDefinitionModel processDefinition = processDefinitionService
				.getProcessDef(procDefId);
		String workflowPage = "";
		try {
			workflowPage = processConfigService.getWorkflowPage(procDefId,
					actDefId);
		} catch (WorkFlowException e) {
			logger.error(String.format(
					"获取流程表单出现异常,processDefId[%s],activityDefId[%s]", procDefId,
					actDefId), e);
		}
		ProcessPageContext processPageContext = new ProcessPageContext(
				WorkFlowContants.ZAIBAN_OPEN_PAGE_TYPE, WorkFlowContants.ACTION_TYPE_EXECUTE_ACTIVITY, workflowPage, userId, userOrgId);

		Map<String, Object> model = new HashMap<String, Object>();

		try {
			processDefinition.setProcessInfoConfig(processConfigService
					.getProcessInfoConfig(procDefId));
			
			HistoricProcessInstanceEntity processInstance = (HistoricProcessInstanceEntity) processInstanceService
					.getHistoryService().createHistoricProcessInstanceQuery()
					.processInstanceId(procInstId).singleResult();
			ActivityInstanceModel curtask = processInstanceService
					.getTask(curActInstId);
			ActivityInstanceModel task = processInstanceService
					.getHistoryTask(curtask.getPrevActInstId());
			ActivityDefinitionModel actDefModel = task.getActivityDefinition();
			String fieldVariables = (String) processInstanceService.getHistoryTaskVariables(curActInstId).get(WorkFlowContants.WF_FIELDS_KEY);
			Map<String, Object> allComment = getProcessComments(procInstId);
			workflowPage = addWorkflowPageQueryString(processDefinition,
					processInstance, actDefModel, task, workflowPage,
					processPageContext.getOpenPageType());
			model.put("allComment", allComment);
			model.put("processDefinition", processDefinition);
			model.put("processInstance", processInstance);
			model.put("task", task);
			model.put("fields", fieldVariables);
			model.put("processPageContext", processPageContext);
			model.put("businessKey", processInstance.getBusinessKey());
			ActivityInfoConfig activityInfoConfig = processConfigService
					.getActivityInfoConfig(
							actDefModel.getProcDefId(),
							actDefModel.getActDefId(),
							WorkFlowContants.ZAIBAN_OPEN_PAGE_TYPE, model);
			actDefModel.setActivityInfoConfig(activityInfoConfig);
			model.put("curActivity", actDefModel);
			model.put("activityInfoConfig", activityInfoConfig);
			
		} catch (Exception e) {
			logger.error(String.format(
					"获取流程起草表单信息出现异常,processDefId[%s],activityDefId[%s]",
					procDefId, actDefId), e);
		}
		return model;
	}
	
	/*
	
	*//**
	 * 获取流程在办任务详细信息
	 * 
	 * @param procDefId
	 * @param procInstId
	 * @param curActInstId
	 * @param actDefId
	 * @return
	 *//*
	public Map<String, Object> getProcessZaibanDetail2(String procDefId,
			String procInstId, String curActInstId, String actDefId,String userId,String userOrgId) {
		User loginUser = userService.getUserById(userId);
		ProcessDefinitionModel processDefinition = processDefinitionService
				.getProcessDef(procDefId);
		String workflowPage = "";
		try {
			workflowPage = processConfigService.getWorkflowPage(procDefId,
					actDefId);
		} catch (WorkFlowException e) {
			logger.error(String.format(
					"获取流程表单出现异常,processDefId[%s],activityDefId[%s]", procDefId,
					actDefId), e);
		}
		ProcessPageContext processPageContext = new ProcessPageContext(
				WorkFlowContants.ZAIBAN_OPEN_PAGE_TYPE, "", workflowPage, userId, userOrgId);

		Map<String, Object> model = new HashMap<String, Object>();

		try {
			processDefinition.setProcessInfoConfig(processConfigService
					.getProcessInfoConfig(procDefId));
			
			HistoricProcessInstanceEntity processInstance = (HistoricProcessInstanceEntity) processInstanceService
					.getHistoryService().createHistoricProcessInstanceQuery()
					.processInstanceId(procInstId).singleResult();
			ActivityInstanceModel task = processInstanceService.getHistoryTask(curActInstId);
			ActivityDefinitionModel actDefModel = task.getActivityDefinition();
			String fieldVariables = (String) processInstanceService.getHistoryTaskVariables(curActInstId).get(WorkFlowContants.WF_FIELDS_KEY);
			Map<String, Object> allComment = getProcessComments(procInstId);
			workflowPage = addWorkflowPageQueryString(processDefinition,
					processInstance, actDefModel, task, workflowPage,
					processPageContext.getOpenPageType());
			model.put("allComment", allComment);
			model.put("processDefinition", processDefinition);
			model.put("processInstance", processInstance);
			model.put("task", task);
			model.put("fields", fieldVariables);
			model.put("processPageContext", processPageContext);
			model.put("businessKey", processInstance.getBusinessKey());
			ActivityInfoConfig activityInfoConfig = processConfigService
					.getActivityInfoConfig(
							actDefModel.getProcDefId(),
							actDefModel.getActDefId(),
							WorkFlowContants.ZAIBAN_OPEN_PAGE_TYPE, model);
			actDefModel.setActivityInfoConfig(activityInfoConfig);
			model.put("curActivity", actDefModel);
			model.put("activityInfoConfig", activityInfoConfig);
			
		} catch (Exception e) {
			logger.error(String.format(
					"获取流程起草表单信息出现异常,processDefId[%s],activityDefId[%s]",
					procDefId, actDefId), e);
		}
		return model;
	}*/

	/**
	 * 获取流程办结任务详细信息
	 * 
	 * @param request
	 * @param procDefId
	 * @param procInstId
	 * @param actDefId
	 * @return
	 */
	public Map<String, Object> getProcessFinishDetail(String procDefId,
			String procInstId, String actDefId, String userId,String userOrgId) {
		ProcessDefinitionModel processDefinition = processDefinitionService
				.getProcessDef(procDefId);
		String workflowPage = "";
		try {
			workflowPage = processConfigService.getWorkflowPage(procDefId,
					actDefId);
		} catch (WorkFlowException e) {
			logger.error(String.format(
					"获取流程表单出现异常,processDefId[%s],activityDefId[%s]", procDefId,
					procInstId), e);
		}
		ProcessPageContext processPageContext = new ProcessPageContext(
				WorkFlowContants.FINISH_OPEN_PAGE_TYPE, "", workflowPage, userId, userOrgId);
		Map<String, Object> model = new HashMap<String, Object>();

		try {
			processDefinition.setProcessInfoConfig(processConfigService
					.getProcessInfoConfig(procDefId));
			HistoricProcessInstance processInstance = this.processInstanceService
					.getHistoryService().createHistoricProcessInstanceQuery()
					.processInstanceId(procInstId).singleResult();
			ActivityInstanceModel hisTaskInst = processInstanceService
					.getTaskByProcessFinished(procInstId, actDefId);
			ActivityDefinitionModel actDefModel = hisTaskInst.getActivityDefinition();
			String fieldVariables = processInstanceService
					.getProcessFieldVariableByFinished(procInstId);
			Map<String, Object> allComment = getProcessComments(procInstId);
			workflowPage = addWorkflowPageQueryString(processDefinition,
					processInstance, actDefModel, hisTaskInst, workflowPage,
					processPageContext.getOpenPageType());
			model.put("processDefinition", processDefinition);
			model.put("processInstance", processInstance);
			model.put("allComment", allComment);
			model.put("task", hisTaskInst);
			model.put("fields", fieldVariables);
			model.put("processPageContext", processPageContext);
			ActivityInfoConfig activityInfoConfig = processConfigService
					.getActivityInfoConfig(
							actDefModel.getProcDefId(),
							actDefModel.getActDefId(),
							WorkFlowContants.FINISH_OPEN_PAGE_TYPE, model);
			actDefModel.setActivityInfoConfig(activityInfoConfig);
			model.put("curActivity", actDefModel);
			model.put("activityInfoConfig", activityInfoConfig);
			model.put("businessKey", processInstance.getBusinessKey());
		} catch (Exception e) {
			logger.error(String.format(
					"获取流程起草表单信息出现异常,processDefId[%s],activityDefId[%s]",
					procDefId, actDefId), e);
		}
		return model;
	}

	/**
	 * 获取流程办结任务详细信息
	 * 
	 * @param request
	 * @param procDefId
	 * @param procInstId
	 * @param actDefId
	 * @return
	 */
	public Map<String, Object> getProcessFinishDetail(String procInstId,String userId,String userOrgId) {
		User loginUser = userService.getUserById(userId);
		Map<String, Object> model = new HashMap<String, Object>();
		HistoricProcessInstance processInstance = processInstanceService
				.getHistoryService().createHistoricProcessInstanceQuery()
				.processInstanceId(procInstId).singleResult();
		ProcessDefinitionModel processDefinition = processDefinitionService
				.getProcessDef(processInstance.getProcessDefinitionId());
		String procDefId = processDefinition.getProcDefId();
		String workflowPage = "";
		try {
			workflowPage = processConfigService.getWorkflowPage(
					processInstance.getProcessDefinitionId(), "");
		} catch (WorkFlowException e) {
			logger.error(String.format(
					"获取流程表单出现异常,processDefId[%s],activityDefId[%s]", procDefId,
					procInstId), e);
		}
		ProcessPageContext processPageContext = new ProcessPageContext(
				WorkFlowContants.FINISH_OPEN_PAGE_TYPE, "", workflowPage, userId, userOrgId);
		processDefinition.setProcessInfoConfig(processConfigService
				.getProcessInfoConfig(procDefId));
		ActivityInstanceModel hisTaskInst = processInstanceService
				.getLastTaskNotFinished(procInstId,processInstance.getEndActivityId());
		if (hisTaskInst == null)
			return model;
		ActivityDefinitionModel actDefModel = hisTaskInst.getActivityDefinition();
		String fieldVariables = processInstanceService
				.getProcessFieldVariableByFinished(procInstId);
		Map<String, Object> allComment = getProcessComments(procInstId);

		workflowPage = addWorkflowPageQueryString(processDefinition,
				processInstance, actDefModel, hisTaskInst, workflowPage,
				processPageContext.getOpenPageType());
		model.put("workflowPage", workflowPage);
		model.put("processDefinition", processDefinition);
		model.put("processInstance", processInstance);
		model.put("allComment", allComment);
		model.put("task", hisTaskInst);
		model.put("fields", fieldVariables);
		model.put("processPageContext", processPageContext);
		model.put("businessKey", processInstance.getBusinessKey());
		ActivityInfoConfig activityInfoConfig = processConfigService
				.getActivityInfoConfig(actDefModel.getProcDefId(),
						actDefModel.getActDefId(),
						WorkFlowContants.FINISH_OPEN_PAGE_TYPE, model);
		actDefModel.setActivityInfoConfig(activityInfoConfig);
		model.put("activityInfoConfig", activityInfoConfig);
		model.put("curActivity", actDefModel);
		return model;
	}

	private String addWorkflowPageQueryString(
			ProcessDefinitionModel processDefinition,
			HistoricProcessInstance hisProcessInstance,
			ActivityDefinitionModel activityImpl, ActivityInstanceModel task,
			String workflowPage, String openPageType) {
		if (StringUtils.isEmpty(workflowPage))
			return workflowPage;
		StringBuffer buf = new StringBuffer();
		// 流程定义数据
		buf.append(workflowPage).append("?");
		buf.append("processDefinitionId=")
				.append(processDefinition.getProcDefId()).append("&");
		buf.append("processDefinitionName=")
				.append(processDefinition.getProcDefName())
				.append("&");
		buf.append("processDefinitionKey=")
				.append(processDefinition.getProcDefKey())
				.append("&");
		if (hisProcessInstance != null) {
			buf.append("processInstanceId=").append(hisProcessInstance.getId())
					.append("&");
			buf.append("businessKey=")
					.append(hisProcessInstance.getBusinessKey()).append("&");
			buf.append("startUserId=")
					.append(hisProcessInstance.getStartUserId()).append("&");
			if (hisProcessInstance.getEndTime() != null
					&& hisProcessInstance.getEndActivityId() != null) {
				buf.append("isProcessFinish=").append("true").append("&");
			} else {
				buf.append("isProcessFinish=").append("false").append("&");
			}
		}
		if (task != null) {
			buf.append("curActivityInstanceId=")
					.append(task.getActInstId()).append("&");
			buf.append("activityDefinitionId=").append(task.getActivityDefinition().getActDefId())
					.append("&");
			buf.append("activityDefinitionName=")
					.append(task.getActivityDefinition().getActDefName()).append("&");
		} /*
		 * else if (hisTask != null) {
		 * buf.append("curActivityInstanceId=").append(hisTask.getId())
		 * .append("&"); buf.append("activityDefinitionId=")
		 * .append(hisTask.getTaskDefinitionKey()).append("&");
		 * buf.append("activityDefinitionName=").append(hisTask.getName())
		 * .append("&"); }
		 */else {
			buf.append("activityDefinitionId=")
					.append(activityImpl.getActDefId()).append("&");
			buf.append("activityDefinitionName=")
					.append(activityImpl.getActDefName()).append("&");
		}

		buf.append("openPageType=").append(openPageType).append("&");
		return buf.toString();

	}

	public Map<String, Object> getProcessComments(String procInstId) {
		List<ProcessLogModel> processComments=processInstanceService.getProcessComments(procInstId);
		String allComment = "";
		Map<String, Object> processCommentMap = new HashMap<String, Object>();
		try {
			if (processComments.isEmpty())
				return processCommentMap;
			for (ProcessLogModel processComment:processComments) {
				Comment comment=processComment.getComment();
				String fullMessage = comment.getFullMessage();
				fullMessage = !StringUtils.isEmpty(fullMessage) ? fullMessage
						.replaceAll("\r\n", "<br/>") : fullMessage;
				String conmentStr = "<div id='replayData' style='text-align:left;border:1px solid #c5d3da;height: auto;margin-top:-1px;'><div style='text-align:left;float: left;margin-left: 20px;color: #006bbd;'>"
						+ fullMessage
						+ "</div><br/><br/><div style='float: right;'>"
						//+ processComment.getHistoricTaskInstance().getName()
						+ processComment.getSendUserName()
						+ "("
						+ DateUtil.convertDateToString("yyyy-MM-dd HH:mm:ss",
								comment.getTime())
						+ ")</div><br style='clear:both;'/></div>";
				allComment += conmentStr;
				if (StringUtils.isNotEmpty(comment.getDisplayArea())
						&& processCommentMap
								.containsKey(comment.getDisplayArea())) {
					List groupConments = (List) processCommentMap.get(comment
							.getDisplayArea());
					groupConments.add(conmentStr);
				} 
			}
		} catch (Exception e) {
			logger.error("", e);
		}
		processCommentMap.put("defaultComment", allComment);
		return processCommentMap;
	}
	
	

	public ProcessExecuteService getProcessExecuteService() {
		return processExecuteService;
	}

	public ProcessDefinitionService getProcessDefinitionService() {
		return processDefinitionService;
	}

	public ProcessConfigService getProcessConfigService() {
		return processConfigService;
	}

	public ProcessInstanceService getProcessInstanceService() {
		return processInstanceService;
	}

}
