package com.unis.flowable.server.controller;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
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.RestController;

import com.github.pagehelper.PageInfo;
import com.unis.common.utils.ResponseWrapper;
import com.unis.flowable.common.vo.DeploymentVO;
import com.unis.flowable.common.vo.FlowTodoQuerVO;
import com.unis.flowable.common.vo.FlowTodoVO;
import com.unis.flowable.common.vo.HistoryApprovalList;
import com.unis.flowable.common.vo.StartTodoVO;
import com.unis.flowable.server.config.PageResult;
import com.unis.flowable.server.service.FlowTodoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


@RestController
@Api(value= "全局待办管理", description = "待办管理")
@Slf4j
public class FlowTodoController {
	
	@Autowired
	private FlowTodoService flowTodoService;

	@RequestMapping(value = "todo", method = RequestMethod.POST)
	@ApiOperation(value="新待办", notes = "开始新待办")
	public ResponseWrapper startNewTask(@RequestBody StartTodoVO startTodoVO){
		return flowTodoService.startNewTodo(startTodoVO);
	}
	
	@RequestMapping(value = "complete", method = RequestMethod.POST)
	@ApiOperation(value="审批", notes = "审批（流程id与业务信息必传一个）")
	public  ResponseWrapper complete(
			@RequestBody FlowTodoVO completeVO
	) {
		int result= flowTodoService.complete(completeVO);
		Map<String, Object> modelMap = new HashMap<String, Object>();
		if(result==0) {
			return ResponseWrapper.error(500,"该流程已结束",null);
		} else if(result==-1) {
			return ResponseWrapper.error(500,"您不能审批此节点",null);
		} else {
			modelMap.put("message", "审批成功");
			modelMap.put("data", completeVO.getProcessId());
			return ResponseWrapper.success(200, "启动成功", modelMap);
		}
	}

	@RequestMapping(value = "todoList", method = RequestMethod.POST)
	@ApiOperation(value="获取待办任务列表---不分页", notes = "待办列表")
	public ResponseWrapper getUserTaskNoPage(@RequestBody FlowTodoQuerVO flowTodoQuerVO){
		List<DeploymentVO> deploymentListList= flowTodoService.getUserTaskNoPage(flowTodoQuerVO);
		return ResponseWrapper.success(200, "查询成功", deploymentListList);
	}
	
	
	
	@RequestMapping(value = "/todoList/{pageNum}/{pageSize}", method = RequestMethod.POST)
	@ApiOperation(value="获取待办任务列表---分页", notes = "待办列表")
	public ResponseWrapper getUserTaskNoPage(@RequestBody FlowTodoQuerVO flowTodoQuerVO,@PathVariable(value = "pageNum") Integer pageNum,
			  @PathVariable(value = "pageSize") Integer pageSize){
		try {
			PageResult<DeploymentVO> pageInfo = flowTodoService.getUserTaskPage(flowTodoQuerVO,pageNum,pageSize);
			return ResponseWrapper.success(200, "查询成功", pageInfo);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResponseWrapper.success(500, "查询失败，服务异常", e.getMessage());
		}
		
		
	}


	@RequestMapping(value="/historic/{pageNum}/{pageSize}", method=RequestMethod.POST)
	@ApiOperation(value="获取已办列表", notes = "已办列表")
	public ResponseWrapper  historic(@RequestBody FlowTodoQuerVO flowTodoQuerVO,@PathVariable(value = "pageNum") Integer pageNum,
			  @PathVariable(value = "pageSize") Integer pageSize) {
		try {
			PageResult<HistoryApprovalList> pageInfo = flowTodoService.historic(flowTodoQuerVO,pageNum,pageSize);
			return ResponseWrapper.success(200, "查询成功", pageInfo);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResponseWrapper.success(500, "查询失败，服务异常", e.getMessage());
		}
		
	}
}
