潘志宝
9 天以前 6b13839488edcd06046e26a41fe897358176689c
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.bpm.convert.task;
H 2
bb2880 3 import cn.hutool.core.collection.CollUtil;
H 4 import cn.hutool.core.util.StrUtil;
5
e7c126 6 import com.iailab.framework.common.pojo.PageResult;
H 7 import com.iailab.framework.common.util.collection.MapUtils;
bb2880 8 import com.iailab.framework.common.util.collection.SetUtils;
e7c126 9 import com.iailab.framework.common.util.number.NumberUtils;
H 10 import com.iailab.framework.common.util.object.BeanUtils;
bb2880 11 import com.iailab.module.bpm.controller.admin.base.user.UserSimpleBaseVO;
H 12 import com.iailab.module.bpm.controller.admin.definition.vo.model.simple.BpmSimpleModelNodeVO;
e7c126 13 import com.iailab.module.bpm.controller.admin.definition.vo.process.BpmProcessDefinitionRespVO;
bb2880 14 import com.iailab.module.bpm.controller.admin.task.vo.instance.BpmApprovalDetailRespVO;
H 15 import com.iailab.module.bpm.controller.admin.task.vo.instance.BpmProcessInstanceBpmnModelViewRespVO;
e7c126 16 import com.iailab.module.bpm.controller.admin.task.vo.instance.BpmProcessInstanceRespVO;
bb2880 17 import com.iailab.module.bpm.controller.admin.task.vo.task.BpmTaskRespVO;
H 18 import com.iailab.module.bpm.convert.definition.BpmProcessDefinitionConvert;
e7c126 19 import com.iailab.module.bpm.dal.dataobject.definition.BpmCategoryDO;
H 20 import com.iailab.module.bpm.dal.dataobject.definition.BpmProcessDefinitionInfoDO;
21 import com.iailab.module.bpm.event.BpmProcessInstanceStatusEvent;
bb2880 22 import com.iailab.module.bpm.framework.flowable.core.util.BpmnModelUtils;
e7c126 23 import com.iailab.module.bpm.framework.flowable.core.util.FlowableUtils;
H 24 import com.iailab.module.bpm.service.message.dto.BpmMessageSendWhenProcessInstanceApproveReqDTO;
25 import com.iailab.module.bpm.service.message.dto.BpmMessageSendWhenProcessInstanceRejectReqDTO;
26 import com.iailab.module.system.api.dept.dto.DeptRespDTO;
27 import com.iailab.module.system.api.user.dto.AdminUserRespDTO;
bb2880 28 import org.flowable.bpmn.model.BpmnModel;
e7c126 29 import org.flowable.engine.history.HistoricProcessInstance;
H 30 import org.flowable.engine.repository.ProcessDefinition;
31 import org.flowable.engine.runtime.ProcessInstance;
32 import org.flowable.task.api.Task;
bb2880 33 import org.flowable.task.api.history.HistoricTaskInstance;
e7c126 34 import org.mapstruct.Mapper;
H 35 import org.mapstruct.Mapping;
36 import org.mapstruct.MappingTarget;
37 import org.mapstruct.factory.Mappers;
38
bb2880 39 import java.util.HashSet;
e7c126 40 import java.util.List;
H 41 import java.util.Map;
bb2880 42 import java.util.Set;
H 43
44 import static com.iailab.framework.common.util.collection.CollectionUtils.convertList;
45 import static com.iailab.framework.common.util.collection.CollectionUtils.convertSet;
46
e7c126 47
H 48 /**
49  * 流程实例 Convert
50  *
bb2880 51  * @author 芋道源码
e7c126 52  */
H 53 @Mapper
54 public interface BpmProcessInstanceConvert {
55
56     BpmProcessInstanceConvert INSTANCE = Mappers.getMapper(BpmProcessInstanceConvert.class);
57
58     default PageResult<BpmProcessInstanceRespVO> buildProcessInstancePage(PageResult<HistoricProcessInstance> pageResult,
59                                                                           Map<String, ProcessDefinition> processDefinitionMap,
60                                                                           Map<String, BpmCategoryDO> categoryMap,
61                                                                           Map<String, List<Task>> taskMap,
62                                                                           Map<Long, AdminUserRespDTO> userMap,
63                                                                           Map<Long, DeptRespDTO> deptMap) {
64         PageResult<BpmProcessInstanceRespVO> vpPageResult = BeanUtils.toBean(pageResult, BpmProcessInstanceRespVO.class);
65         for (int i = 0; i < pageResult.getList().size(); i++) {
66             BpmProcessInstanceRespVO respVO = vpPageResult.getList().get(i);
67             respVO.setStatus(FlowableUtils.getProcessInstanceStatus(pageResult.getList().get(i)));
68             MapUtils.findAndThen(processDefinitionMap, respVO.getProcessDefinitionId(),
4a47e4 69                     processDefinition -> respVO.setCategory(processDefinition.getCategory())
H 70                             .setProcessDefinition(BeanUtils.toBean(processDefinition, BpmProcessDefinitionRespVO.class)));
e7c126 71             MapUtils.findAndThen(categoryMap, respVO.getCategory(), category -> respVO.setCategoryName(category.getName()));
H 72             respVO.setTasks(BeanUtils.toBean(taskMap.get(respVO.getId()), BpmProcessInstanceRespVO.Task.class));
73             // user
74             if (userMap != null) {
75                 AdminUserRespDTO startUser = userMap.get(NumberUtils.parseLong(pageResult.getList().get(i).getStartUserId()));
76                 if (startUser != null) {
bb2880 77                     respVO.setStartUser(BeanUtils.toBean(startUser, UserSimpleBaseVO.class));
e7c126 78                     MapUtils.findAndThen(deptMap, startUser.getDeptId(), dept -> respVO.getStartUser().setDeptName(dept.getName()));
H 79                 }
80             }
81         }
82         return vpPageResult;
83     }
84
85     default BpmProcessInstanceRespVO buildProcessInstance(HistoricProcessInstance processInstance,
86                                                           ProcessDefinition processDefinition,
bb2880 87                                                           BpmProcessDefinitionInfoDO processDefinitionInfo,
e7c126 88                                                           AdminUserRespDTO startUser,
H 89                                                           DeptRespDTO dept) {
90         BpmProcessInstanceRespVO respVO = BeanUtils.toBean(processInstance, BpmProcessInstanceRespVO.class);
bb2880 91         respVO.setStatus(FlowableUtils.getProcessInstanceStatus(processInstance))
H 92                 .setFormVariables(FlowableUtils.getProcessInstanceFormVariable(processInstance));
e7c126 93         // definition
H 94         respVO.setProcessDefinition(BeanUtils.toBean(processDefinition, BpmProcessDefinitionRespVO.class));
bb2880 95         copyTo(processDefinitionInfo, respVO.getProcessDefinition());
e7c126 96         // user
H 97         if (startUser != null) {
bb2880 98             respVO.setStartUser(BeanUtils.toBean(startUser, UserSimpleBaseVO.class));
e7c126 99             if (dept != null) {
H 100                 respVO.getStartUser().setDeptName(dept.getName());
101             }
102         }
103         return respVO;
104     }
105
106     @Mapping(source = "from.id", target = "to.id", ignore = true)
107     void copyTo(BpmProcessDefinitionInfoDO from, @MappingTarget BpmProcessDefinitionRespVO to);
108
bb2880 109     default BpmProcessInstanceStatusEvent buildProcessInstanceStatusEvent(Object source, ProcessInstance instance, Integer status) {
e7c126 110         return new BpmProcessInstanceStatusEvent(source).setId(instance.getId()).setStatus(status)
H 111                 .setProcessDefinitionKey(instance.getProcessDefinitionKey()).setBusinessKey(instance.getBusinessKey());
112     }
113
114     default BpmMessageSendWhenProcessInstanceApproveReqDTO buildProcessInstanceApproveMessage(ProcessInstance instance) {
115         return new BpmMessageSendWhenProcessInstanceApproveReqDTO()
116                 .setStartUserId(NumberUtils.parseLong(instance.getStartUserId()))
117                 .setProcessInstanceId(instance.getId())
118                 .setProcessInstanceName(instance.getName());
119     }
120
121     default BpmMessageSendWhenProcessInstanceRejectReqDTO buildProcessInstanceRejectMessage(ProcessInstance instance, String reason) {
122         return new BpmMessageSendWhenProcessInstanceRejectReqDTO()
bb2880 123             .setProcessInstanceName(instance.getName())
H 124             .setProcessInstanceId(instance.getId())
125             .setReason(reason)
126             .setStartUserId(NumberUtils.parseLong(instance.getStartUserId()));
127     }
128
129     default BpmProcessInstanceBpmnModelViewRespVO buildProcessInstanceBpmnModelView(HistoricProcessInstance processInstance,
130                                                                                     List<HistoricTaskInstance> taskInstances,
131                                                                                     BpmnModel bpmnModel,
132                                                                                     BpmSimpleModelNodeVO simpleModel,
133                                                                                     Set<String> unfinishedTaskActivityIds,
134                                                                                     Set<String> finishedTaskActivityIds,
135                                                                                     Set<String> finishedSequenceFlowActivityIds,
136                                                                                     Set<String> rejectTaskActivityIds,
137                                                                                     Map<Long, AdminUserRespDTO> userMap,
138                                                                                     Map<Long, DeptRespDTO> deptMap) {
139         BpmProcessInstanceBpmnModelViewRespVO respVO = new BpmProcessInstanceBpmnModelViewRespVO();
140         // 基本信息
141         respVO.setProcessInstance(BeanUtils.toBean(processInstance, BpmProcessInstanceRespVO.class, o -> o
142                         .setStatus(FlowableUtils.getProcessInstanceStatus(processInstance)))
143                         .setStartUser(buildUser(processInstance.getStartUserId(), userMap, deptMap)));
144         respVO.setTasks(convertList(taskInstances, task -> BeanUtils.toBean(task, BpmTaskRespVO.class)
145                 .setStatus(FlowableUtils.getTaskStatus(task)).setReason(FlowableUtils.getTaskReason(task))
146                 .setAssigneeUser(buildUser(task.getAssignee(), userMap, deptMap))
147                 .setOwnerUser(buildUser(task.getOwner(), userMap, deptMap))));
148         respVO.setBpmnXml(BpmnModelUtils.getBpmnXml(bpmnModel));
149         respVO.setSimpleModel(simpleModel);
150         // 进度信息
151         respVO.setUnfinishedTaskActivityIds(unfinishedTaskActivityIds)
152                 .setFinishedTaskActivityIds(finishedTaskActivityIds)
153                 .setFinishedSequenceFlowActivityIds(finishedSequenceFlowActivityIds)
154                 .setRejectedTaskActivityIds(rejectTaskActivityIds);
155         return respVO;
156     }
157
158     default UserSimpleBaseVO buildUser(String userIdStr,
159                                        Map<Long, AdminUserRespDTO> userMap,
160                                        Map<Long, DeptRespDTO> deptMap) {
161         if (StrUtil.isEmpty(userIdStr)) {
162             return null;
163         }
164         Long userId = NumberUtils.parseLong(userIdStr);
165         return buildUser(userId, userMap, deptMap);
166     }
167
168     default UserSimpleBaseVO buildUser(Long userId,
169                                                     Map<Long, AdminUserRespDTO> userMap,
170                                                     Map<Long, DeptRespDTO> deptMap) {
171         if (userId == null) {
172             return null;
173         }
174         AdminUserRespDTO user = userMap.get(userId);
175         if (user == null) {
176             return null;
177         }
178         UserSimpleBaseVO userVO = BeanUtils.toBean(user, UserSimpleBaseVO.class);
179         DeptRespDTO dept = user.getDeptId() != null ? deptMap.get(user.getDeptId()) : null;
180         if (dept != null) {
181             userVO.setDeptName(dept.getName());
182         }
183         return userVO;
184     }
185
186     default BpmApprovalDetailRespVO.ActivityNodeTask buildApprovalTaskInfo(HistoricTaskInstance task) {
187         if (task == null) {
188             return null;
189         }
190         return BeanUtils.toBean(task, BpmApprovalDetailRespVO.ActivityNodeTask.class)
191                 .setStatus(FlowableUtils.getTaskStatus(task)).setReason(FlowableUtils.getTaskReason(task));
192     }
193
194     default Set<Long> parseUserIds(HistoricProcessInstance processInstance,
195                                    List<BpmApprovalDetailRespVO.ActivityNode> activityNodes,
196                                    BpmTaskRespVO todoTask) {
197         Set<Long> userIds = new HashSet<>();
198         if (processInstance != null) {
199             userIds.add(NumberUtils.parseLong(processInstance.getStartUserId()));
200         }
201         for (BpmApprovalDetailRespVO.ActivityNode activityNode : activityNodes) {
202             CollUtil.addAll(userIds, convertSet(activityNode.getTasks(), BpmApprovalDetailRespVO.ActivityNodeTask::getAssignee));
203             CollUtil.addAll(userIds, convertSet(activityNode.getTasks(), BpmApprovalDetailRespVO.ActivityNodeTask::getOwner));
204             CollUtil.addAll(userIds, activityNode.getCandidateUserIds());
205         }
206         if (todoTask != null) {
207             CollUtil.addIfAbsent(userIds, todoTask.getAssignee());
208             CollUtil.addIfAbsent(userIds, todoTask.getOwner());
209             if (CollUtil.isNotEmpty(todoTask.getChildren())) {
210                 CollUtil.addAll(userIds, convertSet(todoTask.getChildren(), BpmTaskRespVO::getAssignee));
211                 CollUtil.addAll(userIds, convertSet(todoTask.getChildren(), BpmTaskRespVO::getOwner));
212             }
213         }
214         return userIds;
215     }
216
217     default Set<Long> parseUserIds02(HistoricProcessInstance processInstance,
218                                      List<HistoricTaskInstance> tasks) {
219         Set<Long> userIds = SetUtils.asSet(Long.valueOf(processInstance.getStartUserId()));
220         tasks.forEach(task -> {
221             CollUtil.addIfAbsent(userIds, NumberUtils.parseLong((task.getAssignee())));
222             CollUtil.addIfAbsent(userIds, NumberUtils.parseLong((task.getOwner())));
223         });
224         return userIds;
225     }
226
227     default BpmApprovalDetailRespVO buildApprovalDetail(BpmnModel bpmnModel,
228                                                         ProcessDefinition processDefinition,
229                                                         BpmProcessDefinitionInfoDO processDefinitionInfo,
230                                                         HistoricProcessInstance processInstance,
231                                                         Integer processInstanceStatus,
232                                                         List<BpmApprovalDetailRespVO.ActivityNode> activityNodes,
233                                                         BpmTaskRespVO todoTask,
234                                                         Map<String, String> formFieldsPermission,
235                                                         Map<Long, AdminUserRespDTO> userMap,
236                                                         Map<Long, DeptRespDTO> deptMap) {
237         // 1.1 流程实例
238         BpmProcessInstanceRespVO processInstanceResp = null;
239         if (processInstance != null) {
240             AdminUserRespDTO startUser = userMap.get(NumberUtils.parseLong(processInstance.getStartUserId()));
241             DeptRespDTO dept = startUser != null ? deptMap.get(startUser.getDeptId()) : null;
242             processInstanceResp = buildProcessInstance(processInstance, null, null, startUser, dept);
243         }
244
245         // 1.2 流程定义
246         BpmProcessDefinitionRespVO definitionResp = BpmProcessDefinitionConvert.INSTANCE.buildProcessDefinition(
247                 processDefinition, null, processDefinitionInfo, null, null, bpmnModel);
248
249         // 1.3 流程节点
250         activityNodes.forEach(approveNode -> {
251             if (approveNode.getTasks() != null) {
252                 approveNode.getTasks().forEach(task -> {
253                     task.setAssigneeUser(buildUser(task.getAssignee(), userMap, deptMap));
254                     task.setOwnerUser(buildUser(task.getOwner(), userMap, deptMap));
255                 });
256             }
257             approveNode.setCandidateUsers(convertList(approveNode.getCandidateUserIds(), userId -> buildUser(userId, userMap, deptMap)));
258         });
259
260         // 1.4 待办任务
261         if (todoTask != null) {
262             todoTask.setAssigneeUser(buildUser(todoTask.getAssignee(), userMap, deptMap));
263             todoTask.setOwnerUser(buildUser(todoTask.getOwner(), userMap, deptMap));
264             if (CollUtil.isNotEmpty(todoTask.getChildren())) {
265                 todoTask.getChildren().forEach(childTask -> {
266                     childTask.setAssigneeUser(buildUser(childTask.getAssignee(), userMap, deptMap));
267                     childTask.setOwnerUser(buildUser(childTask.getOwner(), userMap, deptMap));
268                 });
269             }
270         }
271
272         // 2. 拼接起来
273         return new BpmApprovalDetailRespVO().setStatus(processInstanceStatus)
274                 .setProcessDefinition(definitionResp)
275                 .setProcessInstance(processInstanceResp)
276                 .setFormFieldsPermission(formFieldsPermission)
277                 .setTodoTask(todoTask)
278                 .setActivityNodes(activityNodes);
e7c126 279     }
H 280
281 }