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