houzhongjian
2024-08-02 4a47e4b93f62b5e636ac0e76f3e4ee98e2b83154
提交 | 用户 | 时间
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(),
4a47e4 50                     processDefinition -> respVO.setCategory(processDefinition.getCategory())
H 51                             .setProcessDefinition(BeanUtils.toBean(processDefinition, BpmProcessDefinitionRespVO.class)));
e7c126 52             MapUtils.findAndThen(categoryMap, respVO.getCategory(), category -> respVO.setCategoryName(category.getName()));
H 53             respVO.setTasks(BeanUtils.toBean(taskMap.get(respVO.getId()), BpmProcessInstanceRespVO.Task.class));
54             // user
55             if (userMap != null) {
56                 AdminUserRespDTO startUser = userMap.get(NumberUtils.parseLong(pageResult.getList().get(i).getStartUserId()));
57                 if (startUser != null) {
58                     respVO.setStartUser(BeanUtils.toBean(startUser, BpmProcessInstanceRespVO.User.class));
59                     MapUtils.findAndThen(deptMap, startUser.getDeptId(), dept -> respVO.getStartUser().setDeptName(dept.getName()));
60                 }
61             }
62         }
63         return vpPageResult;
64     }
65
66     default BpmProcessInstanceRespVO buildProcessInstance(HistoricProcessInstance processInstance,
67                                                           ProcessDefinition processDefinition,
68                                                           BpmProcessDefinitionInfoDO processDefinitionExt,
69                                                           String bpmnXml,
70                                                           AdminUserRespDTO startUser,
71                                                           DeptRespDTO dept) {
72         BpmProcessInstanceRespVO respVO = BeanUtils.toBean(processInstance, BpmProcessInstanceRespVO.class);
73         respVO.setStatus(FlowableUtils.getProcessInstanceStatus(processInstance));
74         respVO.setFormVariables(FlowableUtils.getProcessInstanceFormVariable(processInstance));
75         // definition
76         respVO.setProcessDefinition(BeanUtils.toBean(processDefinition, BpmProcessDefinitionRespVO.class));
77         copyTo(processDefinitionExt, respVO.getProcessDefinition());
78         respVO.getProcessDefinition().setBpmnXml(bpmnXml);
79         // user
80         if (startUser != null) {
81             respVO.setStartUser(BeanUtils.toBean(startUser, BpmProcessInstanceRespVO.User.class));
82             if (dept != null) {
83                 respVO.getStartUser().setDeptName(dept.getName());
84             }
85         }
86         return respVO;
87     }
88
89     @Mapping(source = "from.id", target = "to.id", ignore = true)
90     void copyTo(BpmProcessDefinitionInfoDO from, @MappingTarget BpmProcessDefinitionRespVO to);
91
92     default BpmProcessInstanceStatusEvent buildProcessInstanceStatusEvent(Object source, HistoricProcessInstance instance, Integer status) {
93         return new BpmProcessInstanceStatusEvent(source).setId(instance.getId()).setStatus(status)
94                 .setProcessDefinitionKey(instance.getProcessDefinitionKey()).setBusinessKey(instance.getBusinessKey());
95     }
96
97     default BpmProcessInstanceStatusEvent buildProcessInstanceStatusEvent(Object source, ProcessInstance instance, Integer status) {;
98         return new BpmProcessInstanceStatusEvent(source).setId(instance.getId()).setStatus(status)
99                 .setProcessDefinitionKey(instance.getProcessDefinitionKey()).setBusinessKey(instance.getBusinessKey());
100     }
101
102     default BpmMessageSendWhenProcessInstanceApproveReqDTO buildProcessInstanceApproveMessage(ProcessInstance instance) {
103         return new BpmMessageSendWhenProcessInstanceApproveReqDTO()
104                 .setStartUserId(NumberUtils.parseLong(instance.getStartUserId()))
105                 .setProcessInstanceId(instance.getId())
106                 .setProcessInstanceName(instance.getName());
107     }
108
109     default BpmMessageSendWhenProcessInstanceRejectReqDTO buildProcessInstanceRejectMessage(ProcessInstance instance, String reason) {
110         return new BpmMessageSendWhenProcessInstanceRejectReqDTO()
111                 .setProcessInstanceName(instance.getName())
112                 .setProcessInstanceId(instance.getId())
113                 .setReason(reason)
114                 .setStartUserId(NumberUtils.parseLong(instance.getStartUserId()));
115     }
116
117 }