package com.ycl.controller; import com.ycl.common.core.controller.BaseController; import com.ycl.common.core.domain.AjaxResult; import com.ycl.common.core.domain.entity.SysUser; import com.ycl.common.core.page.TableDataInfo; import com.ycl.common.utils.SecurityUtils; import com.ycl.common.utils.StringUtils; import com.ycl.system.domain.TaskInfo; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import org.flowable.engine.FormService; import org.flowable.engine.HistoryService; import org.flowable.engine.RuntimeService; import org.flowable.engine.TaskService; import org.flowable.engine.history.HistoricActivityInstance; import org.flowable.engine.runtime.ProcessInstance; import org.flowable.engine.task.Comment; import org.flowable.task.api.Task; import org.flowable.task.api.TaskQuery; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import java.util.Map; @Api(value = "待办任务接口") @Controller @RequiredArgsConstructor @RequestMapping("/task/manage") public class TaskController extends BaseController { private final RuntimeService runtimeService; private final TaskService taskService; private final FormService formService; private final HistoryService historyService; private static final String prefix = "flowable/task"; @GetMapping("/mytask") public String mytasks() { return prefix + "/mytasks"; } @GetMapping("/alltasks") public String alltasks() { return prefix + "/alltasks"; } /** * 查询我的待办任务列表 */ @ApiOperation("查询我的待办任务列表") @PostMapping("/mylist") @ResponseBody public TableDataInfo mylist(TaskInfo param) { SysUser user = SecurityUtils.getLoginUser().getUser(); String username = getUsername(); TaskQuery condition = taskService.createTaskQuery().taskAssignee(username); if (StringUtils.isNotEmpty(param.getTaskName())) { condition.taskName(param.getTaskName()); } if (StringUtils.isNotEmpty(param.getProcessName())) { condition.processDefinitionName(param.getProcessName()); } // 过滤掉流程挂起的待办任务 int total = condition.active().orderByTaskCreateTime().desc().list().size(); int start = (param.getPageNum()-1) * param.getPageSize(); List taskList = condition.active().orderByTaskCreateTime().desc().listPage(start, param.getPageSize()); List tasks = new ArrayList<>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); taskList.stream().forEach(a->{ ProcessInstance process = runtimeService.createProcessInstanceQuery().processInstanceId(a.getProcessInstanceId()).singleResult(); TaskInfo info = new TaskInfo(); info.setAssignee(a.getAssignee()); info.setBusinessKey(process.getBusinessKey()); info.setCreateTime(sdf.format(a.getCreateTime())); info.setTaskName(a.getName()); info.setExecutionId(a.getExecutionId()); info.setProcessInstanceId(a.getProcessInstanceId()); info.setProcessName(process.getProcessDefinitionName()); info.setStarter(process.getStartUserId()); info.setStartTime(sdf.format(process.getStartTime())); info.setTaskId(a.getId()); String formKey = formService.getTaskFormData(a.getId()).getFormKey(); info.setFormKey(formKey); tasks.add(info); }); TableDataInfo rspData = new TableDataInfo(); rspData.setCode(0); rspData.setRows(tasks); rspData.setTotal(total); return rspData; } /** * 查询所有待办任务列表 */ @ApiOperation("查询所有待办任务列表") @PostMapping("/alllist") @ResponseBody public TableDataInfo alllist(TaskInfo param) { TaskQuery condition = taskService.createTaskQuery(); if (StringUtils.isNotEmpty(param.getTaskName())) { condition.taskName(param.getTaskName()); } if (StringUtils.isNotEmpty(param.getProcessName())) { condition.processDefinitionName(param.getProcessName()); } int total = condition.active().orderByTaskCreateTime().desc().list().size(); int start = (param.getPageNum()-1) * param.getPageSize(); List taskList = condition.active().orderByTaskCreateTime().desc().listPage(start, param.getPageSize()); List tasks = new ArrayList<>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); taskList.stream().forEach(a->{ ProcessInstance process = runtimeService.createProcessInstanceQuery().processInstanceId(a.getProcessInstanceId()).singleResult(); TaskInfo info = new TaskInfo(); info.setAssignee(a.getAssignee()); info.setBusinessKey(process.getBusinessKey()); info.setCreateTime(sdf.format(a.getCreateTime())); info.setTaskName(a.getName()); info.setExecutionId(a.getExecutionId()); info.setProcessInstanceId(a.getProcessInstanceId()); info.setProcessName(process.getProcessDefinitionName()); info.setStarter(process.getStartUserId()); info.setStartTime(sdf.format(process.getStartTime())); info.setTaskId(a.getId()); String formKey = formService.getTaskFormData(a.getId()).getFormKey(); info.setFormKey(formKey); tasks.add(info); }); TableDataInfo rspData = new TableDataInfo(); rspData.setCode(0); rspData.setRows(tasks); rspData.setTotal(total); return rspData; } /** * 用taskid查询formkey **/ @ApiOperation("用taskid查询formkey") @PostMapping("/forminfo/{taskId}") @ResponseBody public AjaxResult alllist(@PathVariable String taskId) { String formKey = formService.getTaskFormData(taskId).getFormKey(); return AjaxResult.success(formKey); } @ApiOperation("办理一个用户任务") @RequestMapping(value = "/completeTask/{taskId}", method = RequestMethod.POST) @ResponseBody public AjaxResult completeTask(@PathVariable("taskId") String taskId, @RequestBody(required=false) Map variables) { SysUser user = SecurityUtils.getLoginUser().getUser(); String username = getUsername(); taskService.setAssignee(taskId, username); // 查出流程实例id String processInstanceId = taskService.createTaskQuery().taskId(taskId).singleResult().getProcessInstanceId(); if (variables == null) { taskService.complete(taskId); } else { // 添加审批意见 if (variables.get("comment") != null) { taskService.addComment(taskId, processInstanceId, (String) variables.get("comment")); variables.remove("comment"); } taskService.complete(taskId, variables); } return AjaxResult.success(); } @ApiOperation("任务办理时间轴") @RequestMapping(value = "/history/{taskId}", method = RequestMethod.GET) @ResponseBody public List history(@PathVariable String taskId) { String processInstanceId = taskService.createTaskQuery().taskId(taskId).singleResult().getProcessInstanceId(); List history = historyService.createHistoricActivityInstanceQuery().processInstanceId(processInstanceId).activityType("userTask").orderByHistoricActivityInstanceStartTime().asc().list(); List infos = new ArrayList<>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); history.stream().forEach(h->{ TaskInfo info = new TaskInfo(); info.setProcessInstanceId(h.getProcessInstanceId()); info.setStartTime(sdf.format(h.getStartTime())); if (h.getEndTime() != null) { info.setEndTime(sdf.format(h.getEndTime())); } info.setAssignee(h.getAssignee()); info.setTaskName(h.getActivityName()); List comments = taskService.getTaskComments(h.getTaskId()); if (comments.size() > 0) { info.setComment(comments.get(0).getFullMessage()); } infos.add(info); }); return infos; } }