dengzedong
2024-12-24 aa0382e44311f9f7e62a688c8fcaa9c69a512e0f
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.bpm.controller.admin.task;
H 2
3 import cn.hutool.core.collection.CollUtil;
4 import com.iailab.framework.common.pojo.CommonResult;
5 import com.iailab.framework.common.pojo.PageResult;
6 import com.iailab.framework.common.util.number.NumberUtils;
49b4b6 7 import com.iailab.framework.datapermission.core.annotation.DataPermission;
e7c126 8 import com.iailab.module.bpm.controller.admin.task.vo.task.*;
H 9 import com.iailab.module.bpm.convert.task.BpmTaskConvert;
10 import com.iailab.module.bpm.dal.dataobject.definition.BpmFormDO;
11 import com.iailab.module.bpm.service.definition.BpmFormService;
12 import com.iailab.module.bpm.service.task.BpmProcessInstanceService;
13 import com.iailab.module.bpm.service.task.BpmTaskService;
14 import com.iailab.module.system.api.dept.DeptApi;
15 import com.iailab.module.system.api.dept.dto.DeptRespDTO;
16 import com.iailab.module.system.api.user.AdminUserApi;
17 import com.iailab.module.system.api.user.dto.AdminUserRespDTO;
18 import io.swagger.v3.oas.annotations.Operation;
19 import io.swagger.v3.oas.annotations.Parameter;
20 import io.swagger.v3.oas.annotations.tags.Tag;
21 import org.flowable.bpmn.model.UserTask;
22 import org.flowable.engine.history.HistoricProcessInstance;
23 import org.flowable.engine.runtime.ProcessInstance;
24 import org.flowable.task.api.Task;
25 import org.flowable.task.api.history.HistoricTaskInstance;
26 import org.springframework.security.access.prepost.PreAuthorize;
27 import org.springframework.validation.annotation.Validated;
28 import org.springframework.web.bind.annotation.*;
29
30 import javax.annotation.Resource;
31 import javax.validation.Valid;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.stream.Stream;
37
38 import static com.iailab.framework.common.pojo.CommonResult.success;
39 import static com.iailab.framework.common.util.collection.CollectionUtils.*;
bb2880 40 import static com.iailab.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
H 41
e7c126 42
H 43 @Tag(name = "管理后台 - 流程任务实例")
44 @RestController
45 @RequestMapping("/bpm/task")
46 @Validated
47 public class BpmTaskController {
48
49     @Resource
50     private BpmTaskService taskService;
51     @Resource
52     private BpmProcessInstanceService processInstanceService;
53     @Resource
54     private BpmFormService formService;
55
56     @Resource
57     private AdminUserApi adminUserApi;
58     @Resource
59     private DeptApi deptApi;
60
61     @GetMapping("todo-page")
62     @Operation(summary = "获取 Todo 待办任务分页")
63     @PreAuthorize("@ss.hasPermission('bpm:task:query')")
49b4b6 64     @DataPermission(enable = false) // 关闭数据权限,避免只查看自己时,查询不到部门。
e7c126 65     public CommonResult<PageResult<BpmTaskRespVO>> getTaskTodoPage(@Valid BpmTaskPageReqVO pageVO) {
H 66         PageResult<Task> pageResult = taskService.getTaskTodoPage(getLoginUserId(), pageVO);
67         if (CollUtil.isEmpty(pageResult.getList())) {
68             return success(PageResult.empty());
69         }
70
71         // 拼接数据
72         Map<String, ProcessInstance> processInstanceMap = processInstanceService.getProcessInstanceMap(
73                 convertSet(pageResult.getList(), Task::getProcessInstanceId));
49b4b6 74         // TODO 此处有bug
e7c126 75         Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(
H 76                 convertSet(processInstanceMap.values(), instance -> Long.valueOf(instance.getStartUserId())));
49b4b6 77         PageResult<BpmTaskRespVO> bpmTaskRespVOPageResult = BpmTaskConvert.INSTANCE.buildTodoTaskPage(pageResult, processInstanceMap, userMap);
H 78         return success(bpmTaskRespVOPageResult);
e7c126 79     }
H 80
81     @GetMapping("done-page")
82     @Operation(summary = "获取 Done 已办任务分页")
83     @PreAuthorize("@ss.hasPermission('bpm:task:query')")
84     public CommonResult<PageResult<BpmTaskRespVO>> getTaskDonePage(@Valid BpmTaskPageReqVO pageVO) {
85         PageResult<HistoricTaskInstance> pageResult = taskService.getTaskDonePage(getLoginUserId(), pageVO);
86         if (CollUtil.isEmpty(pageResult.getList())) {
87             return success(PageResult.empty());
88         }
89
90         // 拼接数据
91         Map<String, HistoricProcessInstance> processInstanceMap = processInstanceService.getHistoricProcessInstanceMap(
92                 convertSet(pageResult.getList(), HistoricTaskInstance::getProcessInstanceId));
93         Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(
94                 convertSet(processInstanceMap.values(), instance -> Long.valueOf(instance.getStartUserId())));
95         return success(BpmTaskConvert.INSTANCE.buildTaskPage(pageResult, processInstanceMap, userMap, null));
96     }
97
98     @GetMapping("manager-page")
99     @Operation(summary = "获取全部任务的分页", description = "用于【流程任务】菜单")
100     @PreAuthorize("@ss.hasPermission('bpm:task:mananger-query')")
101     public CommonResult<PageResult<BpmTaskRespVO>> getTaskManagerPage(@Valid BpmTaskPageReqVO pageVO) {
102         PageResult<HistoricTaskInstance> pageResult = taskService.getTaskPage(getLoginUserId(), pageVO);
103         if (CollUtil.isEmpty(pageResult.getList())) {
104             return success(PageResult.empty());
105         }
106
107         // 拼接数据
108         Map<String, HistoricProcessInstance> processInstanceMap = processInstanceService.getHistoricProcessInstanceMap(
109                 convertSet(pageResult.getList(), HistoricTaskInstance::getProcessInstanceId));
110         // 获得 User 和 Dept Map
111         Set<Long> userIds = convertSet(processInstanceMap.values(), instance -> Long.valueOf(instance.getStartUserId()));
112         userIds.addAll(convertSet(pageResult.getList(), task -> NumberUtils.parseLong(task.getAssignee())));
113         Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(userIds);
114         Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(
115                 convertSet(userMap.values(), AdminUserRespDTO::getDeptId));
116         return success(BpmTaskConvert.INSTANCE.buildTaskPage(pageResult, processInstanceMap, userMap, deptMap));
117     }
118
119     @GetMapping("/list-by-process-instance-id")
120     @Operation(summary = "获得指定流程实例的任务列表", description = "包括完成的、未完成的")
121     @Parameter(name = "processInstanceId", description = "流程实例的编号", required = true)
122     @PreAuthorize("@ss.hasPermission('bpm:task:query')")
123     public CommonResult<List<BpmTaskRespVO>> getTaskListByProcessInstanceId(
124             @RequestParam("processInstanceId") String processInstanceId) {
bb2880 125         List<HistoricTaskInstance> taskList = taskService.getTaskListByProcessInstanceId(processInstanceId, true);
e7c126 126         if (CollUtil.isEmpty(taskList)) {
H 127             return success(Collections.emptyList());
128         }
129
130         // 拼接数据
131         Set<Long> userIds = convertSetByFlatMap(taskList, task ->
132                 Stream.of(NumberUtils.parseLong(task.getAssignee()), NumberUtils.parseLong(task.getOwner())));
133         Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(userIds);
134         Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(
135                 convertSet(userMap.values(), AdminUserRespDTO::getDeptId));
136         // 获得 Form Map
137         Map<Long, BpmFormDO> formMap = formService.getFormMap(
138                 convertSet(taskList, task -> NumberUtils.parseLong(task.getFormKey())));
bb2880 139         return success(BpmTaskConvert.INSTANCE.buildTaskListByProcessInstanceId(taskList,
e7c126 140                 formMap, userMap, deptMap));
H 141     }
142
143     @PutMapping("/approve")
144     @Operation(summary = "通过任务")
145     @PreAuthorize("@ss.hasPermission('bpm:task:update')")
146     public CommonResult<Boolean> approveTask(@Valid @RequestBody BpmTaskApproveReqVO reqVO) {
147         taskService.approveTask(getLoginUserId(), reqVO);
148         return success(true);
149     }
150
151     @PutMapping("/reject")
152     @Operation(summary = "不通过任务")
153     @PreAuthorize("@ss.hasPermission('bpm:task:update')")
154     public CommonResult<Boolean> rejectTask(@Valid @RequestBody BpmTaskRejectReqVO reqVO) {
155         taskService.rejectTask(getLoginUserId(), reqVO);
156         return success(true);
157     }
158
159     @GetMapping("/list-by-return")
bb2880 160     @Operation(summary = "获取所有可退回的节点", description = "用于【流程详情】的【退回】按钮")
e7c126 161     @Parameter(name = "taskId", description = "当前任务ID", required = true)
H 162     @PreAuthorize("@ss.hasPermission('bpm:task:update')")
163     public CommonResult<List<BpmTaskRespVO>> getTaskListByReturn(@RequestParam("id") String id) {
164         List<UserTask> userTaskList = taskService.getUserTaskListByReturn(id);
165         return success(convertList(userTaskList, userTask -> // 只返回 id 和 name
166                 new BpmTaskRespVO().setName(userTask.getName()).setTaskDefinitionKey(userTask.getId())));
167     }
168
169     @PutMapping("/return")
bb2880 170     @Operation(summary = "退回任务", description = "用于【流程详情】的【退回】按钮")
e7c126 171     @PreAuthorize("@ss.hasPermission('bpm:task:update')")
H 172     public CommonResult<Boolean> returnTask(@Valid @RequestBody BpmTaskReturnReqVO reqVO) {
173         taskService.returnTask(getLoginUserId(), reqVO);
174         return success(true);
175     }
176
177     @PutMapping("/delegate")
178     @Operation(summary = "委派任务", description = "用于【流程详情】的【委派】按钮")
179     @PreAuthorize("@ss.hasPermission('bpm:task:update')")
180     public CommonResult<Boolean> delegateTask(@Valid @RequestBody BpmTaskDelegateReqVO reqVO) {
181         taskService.delegateTask(getLoginUserId(), reqVO);
182         return success(true);
183     }
184
185     @PutMapping("/transfer")
186     @Operation(summary = "转派任务", description = "用于【流程详情】的【转派】按钮")
187     @PreAuthorize("@ss.hasPermission('bpm:task:update')")
188     public CommonResult<Boolean> transferTask(@Valid @RequestBody BpmTaskTransferReqVO reqVO) {
189         taskService.transferTask(getLoginUserId(), reqVO);
190         return success(true);
191     }
192
193     @PutMapping("/create-sign")
194     @Operation(summary = "加签", description = "before 前加签,after 后加签")
195     @PreAuthorize("@ss.hasPermission('bpm:task:update')")
196     public CommonResult<Boolean> createSignTask(@Valid @RequestBody BpmTaskSignCreateReqVO reqVO) {
197         taskService.createSignTask(getLoginUserId(), reqVO);
198         return success(true);
199     }
200
201     @DeleteMapping("/delete-sign")
202     @Operation(summary = "减签")
203     @PreAuthorize("@ss.hasPermission('bpm:task:update')")
204     public CommonResult<Boolean> deleteSignTask(@Valid @RequestBody BpmTaskSignDeleteReqVO reqVO) {
205         taskService.deleteSignTask(getLoginUserId(), reqVO);
206         return success(true);
207     }
208
bb2880 209     @PutMapping("/copy")
H 210     @Operation(summary = "抄送任务")
211     @PreAuthorize("@ss.hasPermission('bpm:task:update')")
212     public CommonResult<Boolean> copyTask(@Valid @RequestBody BpmTaskCopyReqVO reqVO) {
213         taskService.copyTask(getLoginUserId(), reqVO);
214         return success(true);
215     }
216
e7c126 217     @GetMapping("/list-by-parent-task-id")
H 218     @Operation(summary = "获得指定父级任务的子任务列表") // 目前用于,减签的时候,获得子任务列表
219     @Parameter(name = "parentTaskId", description = "父级任务编号", required = true)
220     @PreAuthorize("@ss.hasPermission('bpm:task:query')")
221     public CommonResult<List<BpmTaskRespVO>> getTaskListByParentTaskId(@RequestParam("parentTaskId") String parentTaskId) {
222         List<Task> taskList = taskService.getTaskListByParentTaskId(parentTaskId);
223         if (CollUtil.isEmpty(taskList)) {
224             return success(Collections.emptyList());
225         }
226         // 拼接数据
227         Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(convertSetByFlatMap(taskList,
228                 user -> Stream.of(NumberUtils.parseLong(user.getAssignee()), NumberUtils.parseLong(user.getOwner()))));
229         Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(
230                 convertSet(userMap.values(), AdminUserRespDTO::getDeptId));
231         return success(BpmTaskConvert.INSTANCE.buildTaskListByParentTaskId(taskList, userMap, deptMap));
232     }
233
234 }