houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.bpm.convert.task;
H 2
3 import cn.hutool.core.map.MapUtil;
4 import cn.hutool.core.util.StrUtil;
5 import com.iailab.framework.common.pojo.PageResult;
6 import com.iailab.framework.common.util.collection.CollectionUtils;
7 import com.iailab.framework.common.util.number.NumberUtils;
8 import com.iailab.framework.common.util.object.BeanUtils;
9 import com.iailab.module.bpm.controller.admin.task.vo.instance.BpmProcessInstanceRespVO;
10 import com.iailab.module.bpm.controller.admin.task.vo.task.BpmTaskRespVO;
11 import com.iailab.module.bpm.dal.dataobject.definition.BpmFormDO;
12 import com.iailab.module.bpm.framework.flowable.core.util.FlowableUtils;
13 import com.iailab.module.bpm.service.message.dto.BpmMessageSendWhenTaskCreatedReqDTO;
14 import com.iailab.module.system.api.dept.dto.DeptRespDTO;
15 import com.iailab.module.system.api.user.dto.AdminUserRespDTO;
16 import org.flowable.engine.history.HistoricProcessInstance;
17 import org.flowable.engine.runtime.ProcessInstance;
18 import org.flowable.task.api.Task;
19 import org.flowable.task.api.history.HistoricTaskInstance;
20 import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;
21 import org.mapstruct.Mapper;
22 import org.mapstruct.factory.Mappers;
23
24 import java.util.Date;
25 import java.util.List;
26 import java.util.Map;
27
28 import static com.iailab.framework.common.util.collection.CollectionUtils.*;
29 import static com.iailab.framework.common.util.collection.MapUtils.findAndThen;
30
31 /**
32  * Bpm 任务 Convert
33  *
34  * @author iailab
35  */
36 @Mapper
37 public interface BpmTaskConvert {
38
39     BpmTaskConvert INSTANCE = Mappers.getMapper(BpmTaskConvert.class);
40
41     default PageResult<BpmTaskRespVO> buildTodoTaskPage(PageResult<Task> pageResult,
42                                                         Map<String, ProcessInstance> processInstanceMap,
43                                                         Map<Long, AdminUserRespDTO> userMap) {
44         return BeanUtils.toBean(pageResult, BpmTaskRespVO.class, taskVO -> {
45             ProcessInstance processInstance = processInstanceMap.get(taskVO.getProcessInstanceId());
46             if (processInstance == null) {
47                 return;
48             }
49             taskVO.setProcessInstance(BeanUtils.toBean(processInstance, BpmTaskRespVO.ProcessInstance.class));
50             AdminUserRespDTO startUser = userMap.get(NumberUtils.parseLong(processInstance.getStartUserId()));
51             taskVO.getProcessInstance().setStartUser(BeanUtils.toBean(startUser, BpmProcessInstanceRespVO.User.class));
52         });
53     }
54
55     default PageResult<BpmTaskRespVO> buildTaskPage(PageResult<HistoricTaskInstance> pageResult,
56                                                     Map<String, HistoricProcessInstance> processInstanceMap,
57                                                     Map<Long, AdminUserRespDTO> userMap,
58                                                     Map<Long, DeptRespDTO> deptMap) {
59         List<BpmTaskRespVO> taskVOList = CollectionUtils.convertList(pageResult.getList(), task -> {
60             BpmTaskRespVO taskVO = BeanUtils.toBean(task, BpmTaskRespVO.class);
61             taskVO.setStatus(FlowableUtils.getTaskStatus(task)).setReason(FlowableUtils.getTaskReason(task));
62             // 用户信息
63             AdminUserRespDTO assignUser = userMap.get(NumberUtils.parseLong(task.getAssignee()));
64             if (assignUser != null) {
65                 taskVO.setAssigneeUser(BeanUtils.toBean(assignUser, BpmProcessInstanceRespVO.User.class));
66                 findAndThen(deptMap, assignUser.getDeptId(), dept -> taskVO.getAssigneeUser().setDeptName(dept.getName()));
67             }
68             // 流程实例
69             HistoricProcessInstance processInstance = processInstanceMap.get(taskVO.getProcessInstanceId());
70             if (processInstance != null) {
71                 AdminUserRespDTO startUser = userMap.get(NumberUtils.parseLong(processInstance.getStartUserId()));
72                 taskVO.setProcessInstance(BeanUtils.toBean(processInstance, BpmTaskRespVO.ProcessInstance.class));
73                 taskVO.getProcessInstance().setStartUser(BeanUtils.toBean(startUser, BpmProcessInstanceRespVO.User.class));
74             }
75             return taskVO;
76         });
77         return new PageResult<>(taskVOList, pageResult.getTotal());
78     }
79
80     default List<BpmTaskRespVO> buildTaskListByProcessInstanceId(List<HistoricTaskInstance> taskList,
81                                                                  HistoricProcessInstance processInstance,
82                                                                  Map<Long, BpmFormDO> formMap,
83                                                                  Map<Long, AdminUserRespDTO> userMap,
84                                                                  Map<Long, DeptRespDTO> deptMap) {
85         List<BpmTaskRespVO> taskVOList = CollectionUtils.convertList(taskList, task -> {
86             BpmTaskRespVO taskVO = BeanUtils.toBean(task, BpmTaskRespVO.class);
87             taskVO.setStatus(FlowableUtils.getTaskStatus(task)).setReason(FlowableUtils.getTaskReason(task));
88             // 流程实例
89             AdminUserRespDTO startUser = userMap.get(NumberUtils.parseLong(processInstance.getStartUserId()));
90             taskVO.setProcessInstance(BeanUtils.toBean(processInstance, BpmTaskRespVO.ProcessInstance.class));
91             taskVO.getProcessInstance().setStartUser(BeanUtils.toBean(startUser, BpmProcessInstanceRespVO.User.class));
92             // 表单信息
93             BpmFormDO form = MapUtil.get(formMap, NumberUtils.parseLong(task.getFormKey()), BpmFormDO.class);
94             if (form != null) {
95                 taskVO.setFormId(form.getId()).setFormName(form.getName()).setFormConf(form.getConf())
96                         .setFormFields(form.getFields()).setFormVariables(FlowableUtils.getTaskFormVariable(task));
97             }
98             // 用户信息
99             AdminUserRespDTO assignUser = userMap.get(NumberUtils.parseLong(task.getAssignee()));
100             if (assignUser != null) {
101                 taskVO.setAssigneeUser(BeanUtils.toBean(assignUser, BpmProcessInstanceRespVO.User.class));
102                 findAndThen(deptMap, assignUser.getDeptId(), dept -> taskVO.getAssigneeUser().setDeptName(dept.getName()));
103             }
104             AdminUserRespDTO ownerUser = userMap.get(NumberUtils.parseLong(task.getOwner()));
105             if (ownerUser != null) {
106                 taskVO.setOwnerUser(BeanUtils.toBean(ownerUser, BpmProcessInstanceRespVO.User.class));
107                 findAndThen(deptMap, ownerUser.getDeptId(), dept -> taskVO.getOwnerUser().setDeptName(dept.getName()));
108             }
109             return taskVO;
110         });
111
112         // 拼接父子关系
113         Map<String, List<BpmTaskRespVO>> childrenTaskMap = convertMultiMap(
114                 filterList(taskVOList, r -> StrUtil.isNotEmpty(r.getParentTaskId())),
115                 BpmTaskRespVO::getParentTaskId);
116         for (BpmTaskRespVO taskVO : taskVOList) {
117             taskVO.setChildren(childrenTaskMap.get(taskVO.getId()));
118         }
119         return filterList(taskVOList, r -> StrUtil.isEmpty(r.getParentTaskId()));
120     }
121
122     default List<BpmTaskRespVO> buildTaskListByParentTaskId(List<Task> taskList,
123                                                             Map<Long, AdminUserRespDTO> userMap,
124                                                             Map<Long, DeptRespDTO> deptMap) {
125         return convertList(taskList, task -> BeanUtils.toBean(task, BpmTaskRespVO.class, taskVO -> {
126             AdminUserRespDTO assignUser = userMap.get(NumberUtils.parseLong(task.getAssignee()));
127             if (assignUser != null) {
128                 taskVO.setAssigneeUser(BeanUtils.toBean(assignUser, BpmProcessInstanceRespVO.User.class));
129                 DeptRespDTO dept = deptMap.get(assignUser.getDeptId());
130                 if (dept != null) {
131                     taskVO.getAssigneeUser().setDeptName(dept.getName());
132                 }
133             }
134             AdminUserRespDTO ownerUser = userMap.get(NumberUtils.parseLong(task.getOwner()));
135             if (ownerUser != null) {
136                 taskVO.setOwnerUser(BeanUtils.toBean(ownerUser, BpmProcessInstanceRespVO.User.class));
137                 findAndThen(deptMap, ownerUser.getDeptId(), dept -> taskVO.getOwnerUser().setDeptName(dept.getName()));
138             }
139         }));
140     }
141
142     default BpmMessageSendWhenTaskCreatedReqDTO convert(ProcessInstance processInstance, AdminUserRespDTO startUser,
143                                                         Task task) {
144         BpmMessageSendWhenTaskCreatedReqDTO reqDTO = new BpmMessageSendWhenTaskCreatedReqDTO();
145         reqDTO.setProcessInstanceId(processInstance.getProcessInstanceId())
146                 .setProcessInstanceName(processInstance.getName()).setStartUserId(startUser.getId())
147                 .setStartUserNickname(startUser.getNickname()).setTaskId(task.getId()).setTaskName(task.getName())
148                 .setAssigneeUserId(NumberUtils.parseLong(task.getAssignee()));
149         return reqDTO;
150     }
151
152     /**
153      * 将父任务的属性,拷贝到子任务(加签任务)
154      *
155      * 为什么不使用 mapstruct 映射?因为 TaskEntityImpl 还有很多其他属性,这里我们只设置我们需要的。
156      * 使用 mapstruct 会将里面嵌套的各个属性值都设置进去,会出现意想不到的问题。
157      *
158      * @param parentTask 父任务
159      * @param childTask 加签任务
160      */
161     default void copyTo(TaskEntityImpl parentTask, TaskEntityImpl childTask) {
162         childTask.setName(parentTask.getName());
163         childTask.setDescription(parentTask.getDescription());
164         childTask.setCategory(parentTask.getCategory());
165         childTask.setParentTaskId(parentTask.getId());
166         childTask.setProcessDefinitionId(parentTask.getProcessDefinitionId());
167         childTask.setProcessInstanceId(parentTask.getProcessInstanceId());
168 //        childTask.setExecutionId(parentTask.getExecutionId()); // TODO iailab:新加的,不太确定;尴尬,不加时,子任务不通过会失败(报错);加了,子任务审批通过会失败(报错)
169         childTask.setTaskDefinitionKey(parentTask.getTaskDefinitionKey());
170         childTask.setTaskDefinitionId(parentTask.getTaskDefinitionId());
171         childTask.setPriority(parentTask.getPriority());
172         childTask.setCreateTime(new Date());
173         childTask.setTenantId(parentTask.getTenantId());
174     }
175
176 }