潘志宝
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.instance.BpmProcessInstanceCancelReqVO;
8 import com.iailab.module.bpm.controller.admin.task.vo.instance.BpmProcessInstanceCreateReqVO;
9 import com.iailab.module.bpm.controller.admin.task.vo.instance.BpmProcessInstancePageReqVO;
10 import com.iailab.module.bpm.controller.admin.task.vo.instance.BpmProcessInstanceRespVO;
11 import com.iailab.module.bpm.convert.task.BpmProcessInstanceConvert;
12 import com.iailab.module.bpm.dal.dataobject.definition.BpmCategoryDO;
13 import com.iailab.module.bpm.dal.dataobject.definition.BpmProcessDefinitionInfoDO;
14 import com.iailab.module.bpm.framework.flowable.core.util.BpmnModelUtils;
15 import com.iailab.module.bpm.service.definition.BpmCategoryService;
16 import com.iailab.module.bpm.service.definition.BpmProcessDefinitionService;
17 import com.iailab.module.bpm.service.task.BpmProcessInstanceService;
18 import com.iailab.module.bpm.service.task.BpmTaskService;
19 import com.iailab.module.system.api.dept.DeptApi;
20 import com.iailab.module.system.api.dept.dto.DeptRespDTO;
21 import com.iailab.module.system.api.user.AdminUserApi;
22 import com.iailab.module.system.api.user.dto.AdminUserRespDTO;
23 import io.swagger.v3.oas.annotations.Operation;
24 import io.swagger.v3.oas.annotations.Parameter;
25 import io.swagger.v3.oas.annotations.tags.Tag;
26 import org.flowable.engine.history.HistoricProcessInstance;
27 import org.flowable.engine.repository.ProcessDefinition;
28 import org.flowable.task.api.Task;
29 import org.springframework.security.access.prepost.PreAuthorize;
30 import org.springframework.validation.annotation.Validated;
31 import org.springframework.web.bind.annotation.*;
32
33 import javax.annotation.Resource;
34 import javax.validation.Valid;
35 import java.util.List;
36 import java.util.Map;
37
38 import static com.iailab.framework.common.pojo.CommonResult.success;
39 import static com.iailab.framework.common.util.collection.CollectionUtils.convertList;
40 import static com.iailab.framework.common.util.collection.CollectionUtils.convertSet;
41 import static com.iailab.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
42
43 @Tag(name = "管理后台 - 流程实例") // 流程实例,通过流程定义创建的一次“申请”
44 @RestController
45 @RequestMapping("/bpm/process-instance")
46 @Validated
47 public class BpmProcessInstanceController {
48
49     @Resource
50     private BpmProcessInstanceService processInstanceService;
51     @Resource
52     private BpmTaskService taskService;
53     @Resource
54     private BpmProcessDefinitionService processDefinitionService;
55     @Resource
56     private BpmCategoryService categoryService;
57
58     @Resource
59     private AdminUserApi adminUserApi;
60     @Resource
61     private DeptApi deptApi;
62
63     @GetMapping("/my-page")
64     @Operation(summary = "获得我的实例分页列表", description = "在【我的流程】菜单中,进行调用")
65     @PreAuthorize("@ss.hasPermission('bpm:process-instance:query')")
66     public CommonResult<PageResult<BpmProcessInstanceRespVO>> getProcessInstanceMyPage(
67             @Valid BpmProcessInstancePageReqVO pageReqVO) {
68         PageResult<HistoricProcessInstance> pageResult = processInstanceService.getProcessInstancePage(
69                 getLoginUserId(), pageReqVO);
70         if (CollUtil.isEmpty(pageResult.getList())) {
71             return success(PageResult.empty(pageResult.getTotal()));
72         }
73
74         // 拼接返回
75         Map<String, List<Task>> taskMap = taskService.getTaskMapByProcessInstanceIds(
76                 convertList(pageResult.getList(), HistoricProcessInstance::getId));
77         Map<String, ProcessDefinition> processDefinitionMap = processDefinitionService.getProcessDefinitionMap(
78                 convertSet(pageResult.getList(), HistoricProcessInstance::getProcessDefinitionId));
79         Map<String, BpmCategoryDO> categoryMap = categoryService.getCategoryMap(
80                 convertSet(processDefinitionMap.values(), ProcessDefinition::getCategory));
81         return success(BpmProcessInstanceConvert.INSTANCE.buildProcessInstancePage(pageResult,
82                 processDefinitionMap, categoryMap, taskMap, null, null));
83     }
84
85     @GetMapping("/manager-page")
86     @Operation(summary = "获得管理流程实例的分页列表", description = "在【流程实例】菜单中,进行调用")
87     @PreAuthorize("@ss.hasPermission('bpm:process-instance:manager-query')")
88     public CommonResult<PageResult<BpmProcessInstanceRespVO>> getProcessInstanceManagerPage(
89             @Valid BpmProcessInstancePageReqVO pageReqVO) {
90         PageResult<HistoricProcessInstance> pageResult = processInstanceService.getProcessInstancePage(
91                 null, pageReqVO);
92         if (CollUtil.isEmpty(pageResult.getList())) {
93             return success(PageResult.empty(pageResult.getTotal()));
94         }
95
96         // 拼接返回
97         Map<String, List<Task>> taskMap = taskService.getTaskMapByProcessInstanceIds(
98                 convertList(pageResult.getList(), HistoricProcessInstance::getId));
99         Map<String, ProcessDefinition> processDefinitionMap = processDefinitionService.getProcessDefinitionMap(
100                 convertSet(pageResult.getList(), HistoricProcessInstance::getProcessDefinitionId));
101         Map<String, BpmCategoryDO> categoryMap = categoryService.getCategoryMap(
102                 convertSet(processDefinitionMap.values(), ProcessDefinition::getCategory));
103         // 发起人信息
104         Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(
105                 convertSet(pageResult.getList(), processInstance -> NumberUtils.parseLong(processInstance.getStartUserId())));
106         Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(
107                 convertSet(userMap.values(), AdminUserRespDTO::getDeptId));
108         return success(BpmProcessInstanceConvert.INSTANCE.buildProcessInstancePage(pageResult,
109                 processDefinitionMap, categoryMap, taskMap, userMap, deptMap));
110     }
111
112     @PostMapping("/create")
113     @Operation(summary = "新建流程实例")
114     @PreAuthorize("@ss.hasPermission('bpm:process-instance:query')")
115     public CommonResult<String> createProcessInstance(@Valid @RequestBody BpmProcessInstanceCreateReqVO createReqVO) {
116         return success(processInstanceService.createProcessInstance(getLoginUserId(), createReqVO));
117     }
118
119     @GetMapping("/get")
120     @Operation(summary = "获得指定流程实例", description = "在【流程详细】界面中,进行调用")
121     @Parameter(name = "id", description = "流程实例的编号", required = true)
122     @PreAuthorize("@ss.hasPermission('bpm:process-instance:query')")
123     public CommonResult<BpmProcessInstanceRespVO> getProcessInstance(@RequestParam("id") String id) {
124         HistoricProcessInstance processInstance = processInstanceService.getHistoricProcessInstance(id);
125         if (processInstance == null) {
126             return success(null);
127         }
128
129         // 拼接返回
130         ProcessDefinition processDefinition = processDefinitionService.getProcessDefinition(
131                 processInstance.getProcessDefinitionId());
132         BpmProcessDefinitionInfoDO processDefinitionInfo = processDefinitionService.getProcessDefinitionInfo(
133                 processInstance.getProcessDefinitionId());
134         String bpmnXml = BpmnModelUtils.getBpmnXml(
135                 processDefinitionService.getProcessDefinitionBpmnModel(processInstance.getProcessDefinitionId()));
136         AdminUserRespDTO startUser = adminUserApi.getUser(NumberUtils.parseLong(processInstance.getStartUserId())).getCheckedData();
137         DeptRespDTO dept = null;
138         if (startUser != null) {
139             dept = deptApi.getDept(startUser.getDeptId()).getCheckedData();
140         }
141         return success(BpmProcessInstanceConvert.INSTANCE.buildProcessInstance(processInstance,
142                 processDefinition, processDefinitionInfo, bpmnXml, startUser, dept));
143     }
144
145     @DeleteMapping("/cancel-by-start-user")
146     @Operation(summary = "用户取消流程实例", description = "取消发起的流程")
147     @PreAuthorize("@ss.hasPermission('bpm:process-instance:cancel')")
148     public CommonResult<Boolean> cancelProcessInstanceByStartUser(
149             @Valid @RequestBody BpmProcessInstanceCancelReqVO cancelReqVO) {
150         processInstanceService.cancelProcessInstanceByStartUser(getLoginUserId(), cancelReqVO);
151         return success(true);
152     }
153
154     @DeleteMapping("/cancel-by-admin")
155     @Operation(summary = "管理员取消流程实例", description = "管理员撤回流程")
156     @PreAuthorize("@ss.hasPermission('bpm:process-instance:cancel-by-admin')")
157     public CommonResult<Boolean> cancelProcessInstanceByManager(
158             @Valid @RequestBody BpmProcessInstanceCancelReqVO cancelReqVO) {
159         processInstanceService.cancelProcessInstanceByAdmin(getLoginUserId(), cancelReqVO);
160         return success(true);
161     }
162
163 }