houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.bpm.convert.task;
H 2
3 import com.iailab.framework.common.pojo.PageResult;
4 import com.iailab.framework.common.util.collection.MapUtils;
5 import com.iailab.framework.common.util.number.NumberUtils;
6 import com.iailab.framework.common.util.object.BeanUtils;
7 import com.iailab.module.bpm.controller.admin.definition.vo.process.BpmProcessDefinitionRespVO;
8 import com.iailab.module.bpm.controller.admin.task.vo.instance.BpmProcessInstanceRespVO;
9 import com.iailab.module.bpm.dal.dataobject.definition.BpmCategoryDO;
10 import com.iailab.module.bpm.dal.dataobject.definition.BpmProcessDefinitionInfoDO;
11 import com.iailab.module.bpm.event.BpmProcessInstanceStatusEvent;
12 import com.iailab.module.bpm.framework.flowable.core.util.FlowableUtils;
13 import com.iailab.module.bpm.service.message.dto.BpmMessageSendWhenProcessInstanceApproveReqDTO;
14 import com.iailab.module.bpm.service.message.dto.BpmMessageSendWhenProcessInstanceRejectReqDTO;
15 import com.iailab.module.system.api.dept.dto.DeptRespDTO;
16 import com.iailab.module.system.api.user.dto.AdminUserRespDTO;
17 import org.flowable.engine.history.HistoricProcessInstance;
18 import org.flowable.engine.repository.ProcessDefinition;
19 import org.flowable.engine.runtime.ProcessInstance;
20 import org.flowable.task.api.Task;
21 import org.mapstruct.Mapper;
22 import org.mapstruct.Mapping;
23 import org.mapstruct.MappingTarget;
24 import org.mapstruct.factory.Mappers;
25
26 import java.util.List;
27 import java.util.Map;
28
29 /**
30  * 流程实例 Convert
31  *
32  * @author iailab
33  */
34 @Mapper
35 public interface BpmProcessInstanceConvert {
36
37     BpmProcessInstanceConvert INSTANCE = Mappers.getMapper(BpmProcessInstanceConvert.class);
38
39     default PageResult<BpmProcessInstanceRespVO> buildProcessInstancePage(PageResult<HistoricProcessInstance> pageResult,
40                                                                           Map<String, ProcessDefinition> processDefinitionMap,
41                                                                           Map<String, BpmCategoryDO> categoryMap,
42                                                                           Map<String, List<Task>> taskMap,
43                                                                           Map<Long, AdminUserRespDTO> userMap,
44                                                                           Map<Long, DeptRespDTO> deptMap) {
45         PageResult<BpmProcessInstanceRespVO> vpPageResult = BeanUtils.toBean(pageResult, BpmProcessInstanceRespVO.class);
46         for (int i = 0; i < pageResult.getList().size(); i++) {
47             BpmProcessInstanceRespVO respVO = vpPageResult.getList().get(i);
48             respVO.setStatus(FlowableUtils.getProcessInstanceStatus(pageResult.getList().get(i)));
49             MapUtils.findAndThen(processDefinitionMap, respVO.getProcessDefinitionId(),
50                     processDefinition -> respVO.setCategory(processDefinition.getCategory()));
51             MapUtils.findAndThen(categoryMap, respVO.getCategory(), category -> respVO.setCategoryName(category.getName()));
52             respVO.setTasks(BeanUtils.toBean(taskMap.get(respVO.getId()), BpmProcessInstanceRespVO.Task.class));
53             // user
54             if (userMap != null) {
55                 AdminUserRespDTO startUser = userMap.get(NumberUtils.parseLong(pageResult.getList().get(i).getStartUserId()));
56                 if (startUser != null) {
57                     respVO.setStartUser(BeanUtils.toBean(startUser, BpmProcessInstanceRespVO.User.class));
58                     MapUtils.findAndThen(deptMap, startUser.getDeptId(), dept -> respVO.getStartUser().setDeptName(dept.getName()));
59                 }
60             }
61         }
62         return vpPageResult;
63     }
64
65     default BpmProcessInstanceRespVO buildProcessInstance(HistoricProcessInstance processInstance,
66                                                           ProcessDefinition processDefinition,
67                                                           BpmProcessDefinitionInfoDO processDefinitionExt,
68                                                           String bpmnXml,
69                                                           AdminUserRespDTO startUser,
70                                                           DeptRespDTO dept) {
71         BpmProcessInstanceRespVO respVO = BeanUtils.toBean(processInstance, BpmProcessInstanceRespVO.class);
72         respVO.setStatus(FlowableUtils.getProcessInstanceStatus(processInstance));
73         respVO.setFormVariables(FlowableUtils.getProcessInstanceFormVariable(processInstance));
74         // definition
75         respVO.setProcessDefinition(BeanUtils.toBean(processDefinition, BpmProcessDefinitionRespVO.class));
76         copyTo(processDefinitionExt, respVO.getProcessDefinition());
77         respVO.getProcessDefinition().setBpmnXml(bpmnXml);
78         // user
79         if (startUser != null) {
80             respVO.setStartUser(BeanUtils.toBean(startUser, BpmProcessInstanceRespVO.User.class));
81             if (dept != null) {
82                 respVO.getStartUser().setDeptName(dept.getName());
83             }
84         }
85         return respVO;
86     }
87
88     @Mapping(source = "from.id", target = "to.id", ignore = true)
89     void copyTo(BpmProcessDefinitionInfoDO from, @MappingTarget BpmProcessDefinitionRespVO to);
90
91     default BpmProcessInstanceStatusEvent buildProcessInstanceStatusEvent(Object source, HistoricProcessInstance instance, Integer status) {
92         return new BpmProcessInstanceStatusEvent(source).setId(instance.getId()).setStatus(status)
93                 .setProcessDefinitionKey(instance.getProcessDefinitionKey()).setBusinessKey(instance.getBusinessKey());
94     }
95
96     default BpmProcessInstanceStatusEvent buildProcessInstanceStatusEvent(Object source, ProcessInstance instance, Integer status) {;
97         return new BpmProcessInstanceStatusEvent(source).setId(instance.getId()).setStatus(status)
98                 .setProcessDefinitionKey(instance.getProcessDefinitionKey()).setBusinessKey(instance.getBusinessKey());
99     }
100
101     default BpmMessageSendWhenProcessInstanceApproveReqDTO buildProcessInstanceApproveMessage(ProcessInstance instance) {
102         return new BpmMessageSendWhenProcessInstanceApproveReqDTO()
103                 .setStartUserId(NumberUtils.parseLong(instance.getStartUserId()))
104                 .setProcessInstanceId(instance.getId())
105                 .setProcessInstanceName(instance.getName());
106     }
107
108     default BpmMessageSendWhenProcessInstanceRejectReqDTO buildProcessInstanceRejectMessage(ProcessInstance instance, String reason) {
109         return new BpmMessageSendWhenProcessInstanceRejectReqDTO()
110                 .setProcessInstanceName(instance.getName())
111                 .setProcessInstanceId(instance.getId())
112                 .setReason(reason)
113                 .setStartUserId(NumberUtils.parseLong(instance.getStartUserId()));
114     }
115
116 }