package org.springblade.flow.engine.controller;

import java.util.LinkedList;
import java.util.List;

import org.springblade.flow.base.ActionResult;
import org.springblade.flow.base.vo.PageListVO;
import org.springblade.flow.base.vo.PaginationVO;
import org.springblade.flow.engine.entity.FlowTaskEntity;
import org.springblade.flow.engine.model.flowmonitor.FlowMonitorListVO;
import org.springblade.flow.engine.model.flowtask.FlowDeleteModel;
import org.springblade.flow.engine.model.flowtask.PaginationFlowTask;
import org.springblade.flow.engine.service.FlowTaskService;
import org.springblade.flow.exception.WorkFlowException;
import org.springblade.flow.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 流程监控
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2019年9月27日 上午9:18
 */
@Api(tags = "流程监控", value = "FlowMonitor")
@RestController
@RequestMapping("/paas/workflow/Engine/FlowMonitor")
public class FlowMonitorController {

	@Autowired
	private FlowTaskService flowTaskService;

	/**
	 * 获取流程监控列表
	 *
	 * @param paginationFlowTask
	 * @return
	 */
	@ApiOperation("获取流程监控列表")
	@GetMapping
	public ActionResult<PageListVO<FlowMonitorListVO>> list(PaginationFlowTask paginationFlowTask) {
		List<FlowTaskEntity> list = flowTaskService.getMonitorList(paginationFlowTask);
		List<FlowMonitorListVO> listVO = new LinkedList<>();
		for (FlowTaskEntity taskEntity : list) {
			// 用户名称赋值
			FlowMonitorListVO vo = JsonUtil.getJsonToBean(taskEntity, FlowMonitorListVO.class);
			vo.setUserName(taskEntity.getCreatorUserName());
			vo.setFormData(taskEntity.getFlowForm());
			vo.setFormType(Integer.valueOf(taskEntity.getFlowFormType()));
			listVO.add(vo);
		}
		PaginationVO paginationVO = JsonUtil.getJsonToBean(paginationFlowTask, PaginationVO.class);
		return ActionResult.page(listVO, paginationVO);
	}

	/**
	 * 批量删除流程监控
	 *
	 * @param deleteModel 主键
	 * @return
	 */
	@ApiOperation("批量删除流程监控")
	@DeleteMapping
	public ActionResult delete(@RequestBody FlowDeleteModel deleteModel) throws WorkFlowException {
		String[] taskId = deleteModel.getIds().split(",");
		flowTaskService.delete(taskId);
		return ActionResult.success("删除成功");
	}

}
