package com.iailab.module.bpm.controller.admin.task; import cn.hutool.core.collection.CollUtil; import com.iailab.framework.common.pojo.CommonResult; import com.iailab.framework.common.pojo.PageResult; import com.iailab.framework.common.util.number.NumberUtils; import com.iailab.module.bpm.controller.admin.task.vo.task.*; import com.iailab.module.bpm.convert.task.BpmTaskConvert; import com.iailab.module.bpm.dal.dataobject.definition.BpmFormDO; import com.iailab.module.bpm.service.definition.BpmFormService; import com.iailab.module.bpm.service.task.BpmProcessInstanceService; import com.iailab.module.bpm.service.task.BpmTaskService; import com.iailab.module.system.api.dept.DeptApi; import com.iailab.module.system.api.dept.dto.DeptRespDTO; import com.iailab.module.system.api.user.AdminUserApi; import com.iailab.module.system.api.user.dto.AdminUserRespDTO; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import org.flowable.bpmn.model.UserTask; import org.flowable.engine.history.HistoricProcessInstance; import org.flowable.engine.runtime.ProcessInstance; import org.flowable.task.api.Task; import org.flowable.task.api.history.HistoricTaskInstance; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.validation.Valid; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Stream; import static com.iailab.framework.common.pojo.CommonResult.success; import static com.iailab.framework.common.util.collection.CollectionUtils.*; import static com.iailab.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; @Tag(name = "管理后台 - 流程任务实例") @RestController @RequestMapping("/bpm/task") @Validated public class BpmTaskController { @Resource private BpmTaskService taskService; @Resource private BpmProcessInstanceService processInstanceService; @Resource private BpmFormService formService; @Resource private AdminUserApi adminUserApi; @Resource private DeptApi deptApi; @GetMapping("todo-page") @Operation(summary = "获取 Todo 待办任务分页") @PreAuthorize("@ss.hasPermission('bpm:task:query')") public CommonResult> getTaskTodoPage(@Valid BpmTaskPageReqVO pageVO) { PageResult pageResult = taskService.getTaskTodoPage(getLoginUserId(), pageVO); if (CollUtil.isEmpty(pageResult.getList())) { return success(PageResult.empty()); } // 拼接数据 Map processInstanceMap = processInstanceService.getProcessInstanceMap( convertSet(pageResult.getList(), Task::getProcessInstanceId)); Map userMap = adminUserApi.getUserMap( convertSet(processInstanceMap.values(), instance -> Long.valueOf(instance.getStartUserId()))); return success(BpmTaskConvert.INSTANCE.buildTodoTaskPage(pageResult, processInstanceMap, userMap)); } @GetMapping("done-page") @Operation(summary = "获取 Done 已办任务分页") @PreAuthorize("@ss.hasPermission('bpm:task:query')") public CommonResult> getTaskDonePage(@Valid BpmTaskPageReqVO pageVO) { PageResult pageResult = taskService.getTaskDonePage(getLoginUserId(), pageVO); if (CollUtil.isEmpty(pageResult.getList())) { return success(PageResult.empty()); } // 拼接数据 Map processInstanceMap = processInstanceService.getHistoricProcessInstanceMap( convertSet(pageResult.getList(), HistoricTaskInstance::getProcessInstanceId)); Map userMap = adminUserApi.getUserMap( convertSet(processInstanceMap.values(), instance -> Long.valueOf(instance.getStartUserId()))); return success(BpmTaskConvert.INSTANCE.buildTaskPage(pageResult, processInstanceMap, userMap, null)); } @GetMapping("manager-page") @Operation(summary = "获取全部任务的分页", description = "用于【流程任务】菜单") @PreAuthorize("@ss.hasPermission('bpm:task:mananger-query')") public CommonResult> getTaskManagerPage(@Valid BpmTaskPageReqVO pageVO) { PageResult pageResult = taskService.getTaskPage(getLoginUserId(), pageVO); if (CollUtil.isEmpty(pageResult.getList())) { return success(PageResult.empty()); } // 拼接数据 Map processInstanceMap = processInstanceService.getHistoricProcessInstanceMap( convertSet(pageResult.getList(), HistoricTaskInstance::getProcessInstanceId)); // 获得 User 和 Dept Map Set userIds = convertSet(processInstanceMap.values(), instance -> Long.valueOf(instance.getStartUserId())); userIds.addAll(convertSet(pageResult.getList(), task -> NumberUtils.parseLong(task.getAssignee()))); Map userMap = adminUserApi.getUserMap(userIds); Map deptMap = deptApi.getDeptMap( convertSet(userMap.values(), AdminUserRespDTO::getDeptId)); return success(BpmTaskConvert.INSTANCE.buildTaskPage(pageResult, processInstanceMap, userMap, deptMap)); } @GetMapping("/list-by-process-instance-id") @Operation(summary = "获得指定流程实例的任务列表", description = "包括完成的、未完成的") @Parameter(name = "processInstanceId", description = "流程实例的编号", required = true) @PreAuthorize("@ss.hasPermission('bpm:task:query')") public CommonResult> getTaskListByProcessInstanceId( @RequestParam("processInstanceId") String processInstanceId) { List taskList = taskService.getTaskListByProcessInstanceId(processInstanceId, true); if (CollUtil.isEmpty(taskList)) { return success(Collections.emptyList()); } // 拼接数据 Set userIds = convertSetByFlatMap(taskList, task -> Stream.of(NumberUtils.parseLong(task.getAssignee()), NumberUtils.parseLong(task.getOwner()))); Map userMap = adminUserApi.getUserMap(userIds); Map deptMap = deptApi.getDeptMap( convertSet(userMap.values(), AdminUserRespDTO::getDeptId)); // 获得 Form Map Map formMap = formService.getFormMap( convertSet(taskList, task -> NumberUtils.parseLong(task.getFormKey()))); return success(BpmTaskConvert.INSTANCE.buildTaskListByProcessInstanceId(taskList, formMap, userMap, deptMap)); } @PutMapping("/approve") @Operation(summary = "通过任务") @PreAuthorize("@ss.hasPermission('bpm:task:update')") public CommonResult approveTask(@Valid @RequestBody BpmTaskApproveReqVO reqVO) { taskService.approveTask(getLoginUserId(), reqVO); return success(true); } @PutMapping("/reject") @Operation(summary = "不通过任务") @PreAuthorize("@ss.hasPermission('bpm:task:update')") public CommonResult rejectTask(@Valid @RequestBody BpmTaskRejectReqVO reqVO) { taskService.rejectTask(getLoginUserId(), reqVO); return success(true); } @GetMapping("/list-by-return") @Operation(summary = "获取所有可退回的节点", description = "用于【流程详情】的【退回】按钮") @Parameter(name = "taskId", description = "当前任务ID", required = true) @PreAuthorize("@ss.hasPermission('bpm:task:update')") public CommonResult> getTaskListByReturn(@RequestParam("id") String id) { List userTaskList = taskService.getUserTaskListByReturn(id); return success(convertList(userTaskList, userTask -> // 只返回 id 和 name new BpmTaskRespVO().setName(userTask.getName()).setTaskDefinitionKey(userTask.getId()))); } @PutMapping("/return") @Operation(summary = "退回任务", description = "用于【流程详情】的【退回】按钮") @PreAuthorize("@ss.hasPermission('bpm:task:update')") public CommonResult returnTask(@Valid @RequestBody BpmTaskReturnReqVO reqVO) { taskService.returnTask(getLoginUserId(), reqVO); return success(true); } @PutMapping("/delegate") @Operation(summary = "委派任务", description = "用于【流程详情】的【委派】按钮") @PreAuthorize("@ss.hasPermission('bpm:task:update')") public CommonResult delegateTask(@Valid @RequestBody BpmTaskDelegateReqVO reqVO) { taskService.delegateTask(getLoginUserId(), reqVO); return success(true); } @PutMapping("/transfer") @Operation(summary = "转派任务", description = "用于【流程详情】的【转派】按钮") @PreAuthorize("@ss.hasPermission('bpm:task:update')") public CommonResult transferTask(@Valid @RequestBody BpmTaskTransferReqVO reqVO) { taskService.transferTask(getLoginUserId(), reqVO); return success(true); } @PutMapping("/create-sign") @Operation(summary = "加签", description = "before 前加签,after 后加签") @PreAuthorize("@ss.hasPermission('bpm:task:update')") public CommonResult createSignTask(@Valid @RequestBody BpmTaskSignCreateReqVO reqVO) { taskService.createSignTask(getLoginUserId(), reqVO); return success(true); } @DeleteMapping("/delete-sign") @Operation(summary = "减签") @PreAuthorize("@ss.hasPermission('bpm:task:update')") public CommonResult deleteSignTask(@Valid @RequestBody BpmTaskSignDeleteReqVO reqVO) { taskService.deleteSignTask(getLoginUserId(), reqVO); return success(true); } @PutMapping("/copy") @Operation(summary = "抄送任务") @PreAuthorize("@ss.hasPermission('bpm:task:update')") public CommonResult copyTask(@Valid @RequestBody BpmTaskCopyReqVO reqVO) { taskService.copyTask(getLoginUserId(), reqVO); return success(true); } @GetMapping("/list-by-parent-task-id") @Operation(summary = "获得指定父级任务的子任务列表") // 目前用于,减签的时候,获得子任务列表 @Parameter(name = "parentTaskId", description = "父级任务编号", required = true) @PreAuthorize("@ss.hasPermission('bpm:task:query')") public CommonResult> getTaskListByParentTaskId(@RequestParam("parentTaskId") String parentTaskId) { List taskList = taskService.getTaskListByParentTaskId(parentTaskId); if (CollUtil.isEmpty(taskList)) { return success(Collections.emptyList()); } // 拼接数据 Map userMap = adminUserApi.getUserMap(convertSetByFlatMap(taskList, user -> Stream.of(NumberUtils.parseLong(user.getAssignee()), NumberUtils.parseLong(user.getOwner())))); Map deptMap = deptApi.getDeptMap( convertSet(userMap.values(), AdminUserRespDTO::getDeptId)); return success(BpmTaskConvert.INSTANCE.buildTaskListByParentTaskId(taskList, userMap, deptMap)); } }