package com.leanwo.activiti.rest.base;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.activiti.engine.runtime.Execution;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.leanwo.activiti.dto.BaseObjectResponse;
import com.leanwo.activiti.dto.BaseRangeResponse;
import com.leanwo.activiti.dto.BaseResponse;
import com.leanwo.activiti.dto.base.ChooseTask;
import com.leanwo.activiti.dto.base.DeploymentDto;
import com.leanwo.activiti.dto.base.Form;
import com.leanwo.activiti.dto.base.FormRequest;
import com.leanwo.activiti.dto.base.HistoricProcessInstanceInfos;
import com.leanwo.activiti.dto.base.HistoricTaskInfos;
import com.leanwo.activiti.dto.base.NeedReceiveCountQueryParam;
import com.leanwo.activiti.dto.base.PreviousUserSelectCalculateRequest;
import com.leanwo.activiti.dto.base.PreviousUserSelectCalculateResponse;
import com.leanwo.activiti.dto.base.ProcessInstanceDescription;
import com.leanwo.activiti.dto.base.TaskCountResponse;
import com.leanwo.activiti.dto.base.TaskDto;
import com.leanwo.activiti.dto.base.TaskInfo;
import com.leanwo.activiti.dto.base.TaskInfos;
import com.leanwo.activiti.dto.base.TaskQueryParam;
import com.leanwo.activiti.dto.base.TransferTaskDto;
import com.leanwo.activiti.dto.base.UserSelectCalculateRequest;
import com.leanwo.activiti.dto.base.UserSelectDefine;
import com.leanwo.activiti.dto.base.UserTaskDto;
import com.leanwo.activiti.dto.base.WorkflowInfo;
import com.leanwo.activiti.exception.ServerException;
import com.leanwo.activiti.service.base.WorkflowService;

/**
 * 
 * 工作流服务
 * 
 * @author YangZhiJie
 *
 */
@RestController
@RequestMapping("/workflow-prodog/workflowResource")
public class WorkflowResource {
	
	@Inject
	private WorkflowService workflowService;
	

	/**
	 * 获取当前时间的毫秒数
	 * @return
	 */
	@RequestMapping(value = "/monitor", method = RequestMethod.GET)
	public Long getCurrentTime(){
		Date date = new Date();
		return date.getTime();
	}
	
	
	@RequestMapping(value = "/version", method = RequestMethod.GET)
	public String version() {
		return "2019";
	}

	@RequestMapping(value = "/startWorkflow", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public String startWorkflow(@RequestBody WorkflowInfo workflowInfo) {
		return workflowService.startWorkflow(workflowInfo);
	}

	@RequestMapping(value = "/deleteAllDeployment", method = RequestMethod.DELETE, consumes = "application/json", produces = "application/json")
	public void deleteAllDeployment() {
		workflowService.deleteAllDeployment();
	}

	@RequestMapping(value = "/countProcessInstance", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public Integer getProcessInstanceCount(@RequestBody String deploymentId) {
		return workflowService.getProcessInstanceCount(deploymentId);
	}

	/**
	 * 删除工作流部署文件
	 * 
	 * @param deploymentId
	 * @author YangZhiJie 20171115
	 */
	@RequestMapping(value = "/deleteDeployment", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public void deleteDeployment(@RequestBody String deploymentId) {
		workflowService.deleteDeployment(deploymentId);
	}

	/**
	 * 部署工作流文件
	 * 
	 * @param fileName bpmn文件名
	 * @param category bpmn文件类别
	 * @return
	 * @author YangZhiJie 20171115
	 */
	@RequestMapping(value = "/deploy", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public String deploy(@RequestBody String fileName) {
		return workflowService.deploy(fileName);
	}

	/**
	 * 查询待审批的任务
	 * 
	 * @param taskQueryParam
	 * @return
	 */
	@RequestMapping(value = "/listNeedApproveTaskInfo", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public TaskInfos listNeedApproveTaskInfo(@RequestBody TaskQueryParam taskQueryParam) {
		return workflowService.queryNeedApproveTaskInfo(taskQueryParam);
	}

	@RequestMapping(value = "/getTaskInfoByProcessInstanceId", method = RequestMethod.GET, produces = "application/json")
	public TaskInfo getTaskInfoByProcessInstanceId(@RequestParam("processInstanceId") String processInstanceId, @RequestParam("userId") String userId) {
		return workflowService.getTaskInfoByProcessInstanceId(processInstanceId, userId);
	}

	@RequestMapping(value = "/getProcessInstanceIdTaskInfo", method = RequestMethod.GET, produces = "application/json")
	public TaskInfo getProcessInstanceIdTaskInfo(@RequestParam("processInstanceId") String processInstanceId) {
		return workflowService.getProcessInstanceIdTaskInfo(processInstanceId);
	}

	/**
	 * 根据TaskId查询TaskInfo
	 * 
	 * @param taskId
	 * @return
	 */
	@RequestMapping(value = "/queryTaskInfo", method = RequestMethod.GET, produces = "application/json")
	public TaskInfo queryTaskInfo(@RequestParam("taskId") String taskId) {
		return workflowService.queryTaskInfo(taskId);
	}
	
	/**
	 * 根据任务Id和用户Id查询任务信息
	 * @param taskId 任务Id
	 * @param userId 用户Id
	 * @return 任务信息
	 */
	@RequestMapping(value = "/uniqueTaskInfoByTaskIdUserId", method = RequestMethod.GET, produces = "application/json")
	public TaskInfo uniqueTaskInfoByTaskIdUserId(@RequestParam("taskId") String taskId, @RequestParam("userId") String userId) {
		return workflowService.uniqueTaskInfoByTaskIdUserId(taskId, userId);
	}
	
	

	/**
	 * 查询已审批的任务信息
	 * 
	 * @param taskQueryParam
	 * @return
	 */
	@RequestMapping(value = "/listApprovedTask", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public HistoricTaskInfos listApprovedTask(@RequestBody TaskQueryParam taskQueryParam) {
		return workflowService.queryApprovedTask(taskQueryParam);
	}

	/**
	 * 查询已拒绝的任务信息
	 * 
	 * @param taskQueryParam
	 * @return
	 */
	@RequestMapping(value = "/listNotApprovedTask", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public HistoricTaskInfos listNotApprovedTask(@RequestBody TaskQueryParam taskQueryParam) {
		return workflowService.queryNotApprovedTask(taskQueryParam);
	}

	/**
	 * 查询我发起的审批
	 * 
	 * @param taskQueryParam
	 * @return
	 */
	@RequestMapping(value = "/listMyApplyProcessInfo", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public HistoricProcessInstanceInfos listMyApplyProcessInfo(@RequestBody TaskQueryParam taskQueryParam) {
		return workflowService.queryMyApplyProcessInfo(taskQueryParam);
	}

	/**
	 * 查询工作流实例对应有效实例的数量
	 * 
	 * @param processInstanceIds
	 * @return
	 */
	@RequestMapping(value = "/countActiveByProcessInstanceId", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public List<Long> countActiveByProcessInstanceId(@RequestBody List<String> processInstanceIds) {
		return workflowService.countActiveByProcessInstanceId(processInstanceIds);
	}

	@RequestMapping(value = "/countTaskInfoNeedReceive", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public Long countTaskInfoNeedReceive(@RequestBody NeedReceiveCountQueryParam param) {
		return workflowService.getTaskInfoNeedReceiveCount(param);
	}

	/**
	 * 获取待审批的用户任务的总数
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/countTaskInfoNeedApprove", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public Long countTaskInfoNeedApprove(@RequestBody Long userId) {
		return workflowService.getTaskInfoNeedApproveCount(userId);
	}

	/**
	 * 根据用户查询已审批的数量
	 * 
	 * @param taskQueryParam
	 * @return
	 */
	@RequestMapping(value = "/countTaskInfoApproved", method = RequestMethod.GET, produces = "application/json")
	public Long countTaskInfoApproved(@RequestParam("userId") Long userId) {
		return workflowService.countTaskInfoApproved(userId);
	}

	/**
	 * 根据用户查询我发起的数量
	 * 
	 * @param taskQueryParam
	 * @return
	 */
	@RequestMapping(value = "/countTaskInfoMyApply", method = RequestMethod.GET, produces = "application/json")
	public Long countTaskInfoMyApply(@RequestParam("userId") Long userId) {
		return workflowService.countTaskInfoMyApply(userId);
	}

	/**
	 * 根据用户查询已拒绝的数量
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/countTaskInfoRefuse", method = RequestMethod.GET, produces = "application/json")
	public Long countTaskInfoRefuse(@RequestParam("userId") Long userId) {
		return workflowService.countTaskInfoRefuse(userId);
	}

	/**
	 * 获取工作流备注
	 * 
	 * @param processInstanceId
	 * @return
	 */

	@RequestMapping(value = "/processInstanceDescription", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public ProcessInstanceDescription getProcessInstanceDescription(@RequestBody String processInstanceId) {
		return workflowService.getProcessInstanceDescription(processInstanceId);
	}

	@RequestMapping(value = "/suspend", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public void suspendProcessInstance(@RequestBody String processInstanceId) {
		workflowService.suspendProcessInstance(processInstanceId);
	}

	@RequestMapping(value = "/activate", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public void activateProcessInstance(@RequestBody String processInstanceId) {
		workflowService.activateProcessInstance(processInstanceId);
	}


	/**
	 * 提交任务表单属性
	 * 
	 * @param chooseTask
	 * @throws ServerException
	 */
	@RequestMapping(value = "/setChoice", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public void setChoice(@RequestBody ChooseTask chooseTask) {
		workflowService.setChoice(chooseTask);
	}

	/**
	 * 添加评论
	 * 
	 * @param chooseTask
	 */
	@RequestMapping(value = "/addComment", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public void addComment(@RequestBody ChooseTask chooseTask) {
		workflowService.addComment(chooseTask);
	}

	/**
	 * 获取工作流解析数据
	 * 
	 * @param taskId
	 * @return
	 */
	@RequestMapping(value = "/listUserTask", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public List<UserTaskDto> listUserTask(@RequestBody String deploymentId) {
		return workflowService.listUserTask(deploymentId);
	}

	/**
	 * 获取工作流解析数据
	 * 
	 * @param taskId
	 * @return
	 */
	@RequestMapping(value = "/getUserTask", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public UserTaskDto getUserTask(@RequestBody ChooseTask chooseTask) throws Exception {
		return workflowService.getUserTask(chooseTask);
	}

	/**
	 * 设置流程变量
	 * 
	 * @param processInstanceId
	 * @param variableName
	 * @param variableValue
	 * @author GuoZhiBo 20180911
	 */
	@RequestMapping(value = "/setBooleanVariable", method = RequestMethod.GET, produces = "application/json")
	public void setBooleanVariable(@RequestParam("processInstanceId") String processInstanceId, @RequestParam("variableName") String variableName,
			@RequestParam("variableValue") Boolean variableValue) {
		workflowService.setBooleanVariable(processInstanceId, variableName, variableValue);
	}

	/**
	 * 查询所有待审批的任务
	 * 
	 * @param taskQueryParam
	 * @return
	 * @author GuoZhiBo 20181015
	 */
	@RequestMapping(value = "/listAllNeedApproveTaskInfo", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public TaskInfos listAllNeedApproveTaskInfo(@RequestBody TaskQueryParam taskQueryParam) {
		return workflowService.queryAllNeedApproveTaskInfo(taskQueryParam);
	}


	/**
	 * 根据任务Id删除流程实例
	 * 
	 * @param taskId
	 * @return
	 */
	@RequestMapping(value = "/deleteProcessInstanceByProcessInstanceId", method = RequestMethod.GET)
	public Boolean deleteProcessInstanceByProcessInstanceId(@RequestParam(value = "processInstanceId") String processInstanceId) {
		Boolean result = workflowService.deleteProcessInstanceByProcessInstanceId(processInstanceId);
		return result;
	}
	
	/**
	 * 查询所有部署的工作流
	 * @return
	 */
	@RequestMapping(value = "/queryDeploymentIds", method = RequestMethod.GET, produces = "application/json")
	public List<DeploymentDto> queryDeploymentIds() {
		return workflowService.queryDeploymentIds();
	}
	
	/**
	 * 转批
	 * 
	 * @param chooseTask
	 * @throws ServerException
	 */
	@RequestMapping(value = "/transfer", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public void transfer(@RequestBody TransferTaskDto transferTaskDto) {
		workflowService.transfer(transferTaskDto);
	}
	
	/**
	 * 设置modelData的流程变量
	 * @param processInstanceId
	 * @param variableName
	 * @param object
	 * @author GuoZhiBo 20210324
	 */
	@RequestMapping(value = "/setVariableModelData", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public void setVariableModelData(@RequestBody WorkflowInfo workflowInfo) {
		workflowService.setVariableModelData(workflowInfo);
	}
	
	/**
	 * 计算哪些用户选择项可以执行
	 * @param userSelectCalculateRequest
	 */
	@RequestMapping(value = "/calculateUserSelect", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
	public List<UserSelectDefine> calculateUserSelect(@RequestBody UserSelectCalculateRequest userSelectCalculateRequest) {
		return workflowService.calculateUserSelect(userSelectCalculateRequest);
	}

	/**
     * 
     * 在审批的过程中(用户任务)判断(选择下一个节点选择用户)是否显示。
     * @param userSelectCalculateRequest
     */
	@RequestMapping(value = "/reCalculatePreviousUserSelect", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
    public List<PreviousUserSelectCalculateResponse> reCalculatePreviousUserSelect(@RequestBody PreviousUserSelectCalculateRequest previousUserSelectCalculateRequest) {
        return workflowService.reCalculatePreviousUserSelect(previousUserSelectCalculateRequest);
    }
    
    
	/**
	 * 根据表单请求(流程任务Id)查询表单数据
	 * @param formRequest 表单请求(流程任务Id)
	 * @return 表单数据
	 */
	@RequestMapping(value = "/findForm", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
	public Form findForm(@RequestBody FormRequest formRequest) {
		return workflowService.findForm(formRequest);
	}
	
	
	/**
	 * 在审批的过程中判断是否下一个节点是否需要选择用户。
	 * @param userSelectCalculateRequest
	 */
	@RequestMapping(value = "/calculatePreviousUserSelect", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
	public List<UserSelectDefine> calculatePreviousUserSelect(@RequestBody PreviousUserSelectCalculateRequest previousUserSelectCalculateRequest) {
		return workflowService.calculatePreviousUserSelect(previousUserSelectCalculateRequest);
	}
	
	
	/**
	 * 根据任务Id获取审批节点定义Id
	 * @param taskId 任务Id
	 * @return activityId  审批节点定义Id
	 */
	@RequestMapping(value = "/getActivityId", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
	public String getActivityId(@RequestBody String taskId) {
		Execution execution = workflowService.getExecution(taskId);
		if(execution == null) {
			return null;
		}else {
			return execution.getActivityId();
		}
	}
	

	/**
	 * 根据流程实例Id获取部署Id
	 * @param processInstanceId 流程实例Id
	 * @return 部署Id
	 */
	@RequestMapping(value = "/getDeploymentId", method = RequestMethod.GET, produces = "text/plain")
	public String getDeploymentId(@RequestParam(value = "processInstanceId") String processInstanceId) {
		return workflowService.getDeploymentId(processInstanceId);
	}
	
	/**
	 * 根据用户Id和数据记录id查询审批任务的数量 和 历史审批任务数量
	 * @param userId 用户Id
	 * @param recordId 数据记录id
	 * @return 审批任务的数量 和 历史审批任务数量
	 * @author YangZhiJie 2022-05-05
	 */
	@RequestMapping(value = "/countTaskByUserIdRecordId", method = RequestMethod.GET, produces = "application/json")
    public BaseObjectResponse<TaskCountResponse> countTaskByUserIdRecordId(@RequestParam(value = "userId") Long userId, @RequestParam(value = "recordId") Long recordId) {
	    return workflowService.countTaskByUserIdRecordId(userId, recordId);
	}
    

	
	
}