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

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
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.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.SuspensionState;

import com.fasterxml.jackson.annotation.JsonIgnore;
/**
 * 流程实例对象
 * 
 * @version: 1.0
 * @author lw
 */
public class ProcessInstanceModel implements Serializable {
	private static final long serialVersionUID = 1L;
	/**
	 * 流程定义ID
	 */
	String procDefId;
	/**
	 * 流程定义名称
	 */
	String procDefName;
	/**
	 * 流程实例ID
	 */
	String procInstId;

	/**
	 * 顶层流程实例父ID
	 */
	String topProcInstId;
	/**
	 * 流程实例父ID
	 */
	String parentProcInstId;
	/**
	 * 流程标题
	 */
	String procTitle;
	/**
	 * 流程起草人
	 */
	String startUserId;
	/**
	 * 流程起草人
	 */
	String startUserName;
	/**
	 * 流程起草组织
	 */
	String starterOrgId;
	/**
	 * 流程起草组织
	 */
	String starterOrgName;
	/**
	 * 流程创建时间
	 */
	Date createTime;
	/**
	 * 流程完成时间
	 */
	Date finishTime;
	/**
	 * 流程开始环节ID
	 */
	String startActivityId;
	/**
	 * 流程结束环节ID
	 */
	String endActivityId;

	/**
	 * 业务主键
	 */
	String businessKey;
	/**
	 * 流程状态
	 */
	String procState;
	/**
	 * 是否包含子流
	 */
	boolean hasSubProcess;
	/**
	 * 动作类型
	 */
	String actionType;
	
	/**
	 * 流程定义
	 */
	ProcessDefinitionModel processDefinition;
	
	/**
	 * 当前环节实例ID--待定
	 */
	ActivityInstanceModel currentActivity;
	/**
	 * 下一步环节实例对象--待定
	 */
	List<ActivityInstanceModel> nextActivity;
	
	
	/**
	 * 子流程-流程实例ID列表
	 */
	List<String> subProcInstIds;
	
	/**
	 * 流程全局变量--待定
	 */
	Map processVariables = new HashMap();
	/**
	 * 输入对象--待定
	 */
	ProcessInputModel processInputModel;
	/**
	 * 租衣Id
	 */
	String tenantId;
	
	
	public static ProcessInstanceModel buildAndProcDef(HistoricProcessInstance processInstance,ProcessDefinitionModel processDefinition){
		ProcessInstanceModel processInstanceModel=new ProcessInstanceModel();
		if(processInstance==null)
			return processInstanceModel;
		processInstanceModel.procTitle=processInstance.getProcTitle();
		processInstanceModel.businessKey=processInstance.getBusinessKey();
		processInstanceModel.procInstId=processInstance.getId();
		processInstanceModel.createTime=processInstance.getStartTime();
		processInstanceModel.finishTime=processInstance.getEndTime();
		processInstanceModel.startActivityId=processInstance.getStartActivityId();
		processInstanceModel.endActivityId=processInstance.getEndActivityId();
		processInstanceModel.startUserId=processInstance.getStartUserId();
		processInstanceModel.startUserName=processInstance.getStartUserName();
		processInstanceModel.parentProcInstId=processInstance.getSuperProcessInstanceId();
		processInstanceModel.topProcInstId=processInstance.getTopProcessInstanceId();
		processInstanceModel.procDefId=processInstance.getProcessDefinitionId();
		processInstanceModel.procDefName=processInstance.getProcessDefinitionName();
		processInstanceModel.starterOrgId=processInstance.getStarterOrgId();
		processInstanceModel.starterOrgName=processInstance.getStarterOrgName();
		processInstanceModel.processDefinition=processDefinition;
		processInstanceModel.procState =  String.valueOf(processInstance.getProcState());
		processInstanceModel.tenantId=processInstance.getTenantId();
		processInstanceModel.processDefinition=processDefinition;
		return processInstanceModel;
	}
	
	public static ProcessInstanceModel buildAndProcDef(ExecutionEntity processInstance,ProcessDefinitionModel processDefinition){
		ProcessInstanceModel processInstanceModel=new ProcessInstanceModel();
		if(processInstance==null)
			return processInstanceModel;
		processInstanceModel.procTitle=processInstance.getProcTitle();
		processInstanceModel.businessKey=processInstance.getBusinessKey();
		processInstanceModel.procInstId=processInstance.getId();
		processInstanceModel.createTime=new Date();
		processInstanceModel.startActivityId=processInstance.getCurrentActivityId();
		processInstanceModel.startUserId=processInstance.getSendUserId();
		processInstanceModel.startUserName=processInstance.getSendUserName();
		processInstanceModel.parentProcInstId=processInstance.getSuperExecution()!=null?processInstance.getSuperExecution().getProcessInstanceId():processInstance.getParentId();
		processInstanceModel.topProcInstId=processInstance.getTopProcessInstanceId();
		processInstanceModel.procDefId=processInstance.getProcessDefinitionId();
		processInstanceModel.procDefName=processInstance.getProcessDefinitionName();
		processInstanceModel.starterOrgId=processInstance.getSenderOrgId();
		processInstanceModel.starterOrgName=processInstance.getSenderOrgName();
		processInstanceModel.processDefinition=processDefinition;
		processInstanceModel.procState =  String.valueOf(SuspensionState.ACTIVE);
		processInstanceModel.tenantId=processInstance.getTenantId();
		processInstanceModel.processDefinition=processDefinition;
		return processInstanceModel;
	}
	
	public static ProcessInstanceModel build(HistoricProcessInstance processInstance){
		return buildAndProcDef(processInstance,null);
	}
	
	
	public String getProcInstId() {
		return procInstId;
	}


	public void setProcInstId(String procInstId) {
		this.procInstId = procInstId;
	}


	public String getParentProcInstId() {
		return parentProcInstId;
	}


	public void setParentProcInstId(String parentProcInstId) {
		this.parentProcInstId = parentProcInstId;
	}


	public String getProcTitle() {
		return procTitle;
	}


	public void setProcTitle(String procTitle) {
		this.procTitle = procTitle;
	}


	public String getStartUserId() {
		return startUserId;
	}


	public void setStartUserId(String startUserId) {
		this.startUserId = startUserId;
	}


	public String getStartUserName() {
		return startUserName;
	}


	public void setStartUserName(String startUserName) {
		this.startUserName = startUserName;
	}

	


	public String getStarterOrgId() {
		return starterOrgId;
	}

	public void setStarterOrgId(String starterOrgId) {
		this.starterOrgId = starterOrgId;
	}

	public String getStarterOrgName() {
		return starterOrgName;
	}

	public void setStarterOrgName(String starterOrgName) {
		this.starterOrgName = starterOrgName;
	}

	public Date getCreateTime() {
		return createTime;
	}


	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}


	public Date getFinishTime() {
		return finishTime;
	}


	public void setFinishTime(Date finishTime) {
		this.finishTime = finishTime;
	}


	public String getStartActivityId() {
		return startActivityId;
	}


	public void setStartActivityId(String startActivityId) {
		this.startActivityId = startActivityId;
	}


	public String getEndActivityId() {
		return endActivityId;
	}


	public void setEndActivityId(String endActivityId) {
		this.endActivityId = endActivityId;
	}


	public String getBusinessKey() {
		return businessKey;
	}


	public void setBusinessKey(String businessKey) {
		this.businessKey = businessKey;
	}


	public String getProcState() {
		return procState;
	}


	public void setProcState(String procState) {
		this.procState = procState;
	}


	public boolean isHasSubProcess() {
		return hasSubProcess;
	}


	public void setHasSubProcess(boolean hasSubProcess) {
		this.hasSubProcess = hasSubProcess;
	}


	public String getActionType() {
		return actionType;
	}


	public void setActionType(String actionType) {
		this.actionType = actionType;
	}


	public Map getProcessVariables() {
		return processVariables;
	}


	public void setProcessVariables(Map processVariables) {
		this.processVariables = processVariables;
	}


	public ProcessInputModel getProcessInputModel() {
		return processInputModel;
	}


	public void setProcessInputModel(ProcessInputModel processInputModel) {
		this.processInputModel = processInputModel;
	}


	public ActivityInstanceModel getCurrentActivity() {
		return currentActivity;
	}


	public void setCurrentActivity(ActivityInstanceModel currentActivity) {
		this.currentActivity = currentActivity;
	}


	public List<ActivityInstanceModel> getNextActivity() {
		return nextActivity;
	}


	public void setNextActivity(List<ActivityInstanceModel> nextActivity) {
		this.nextActivity = nextActivity;
	}
	
	public ProcessDefinitionModel getProcessDefinition() {
		return processDefinition;
	}


	public void setProcessDefinition(ProcessDefinitionModel processDefinition) {
		this.processDefinition = processDefinition;
	}


	public String getProcDefId() {
		return procDefId;
	}

	public void setProcDefId(String procDefId) {
		this.procDefId = procDefId;
	}

	public String getProcDefName() {
		return procDefName;
	}

	public void setProcDefName(String procDefName) {
		this.procDefName = procDefName;
	}
	
	public void setTenantId(String tenantId) {
		this.tenantId = tenantId;
	}
	
	public String getTenantId() {
		return this.tenantId;
	}

	public String getTopProcInstId() {
		return topProcInstId;
	}

	public void setTopProcInstId(String topProcInstId) {
		this.topProcInstId = topProcInstId;
	}

	public List<String> getSubProcInstIds() {
		/*if(subProcInstIds==null){
			this.subProcInstIds=new ArrayList<String>();
		}*/
		return subProcInstIds;
	}

	public void setSubProcInstIds(List<String> subProcInstIds) {
		this.subProcInstIds = subProcInstIds;
	}

	@JsonIgnore
	public boolean isFinishProcess(){
		return this.finishTime!=null&&this.procState.equals(String.valueOf(SuspensionState.FINISH.getStateCode()));
	}
	public String toString(){
		return org.apache.commons.lang3.builder.ToStringBuilder.reflectionToString(this);
	}
	
}
