dongyukun
6 天以前 ed8fc5f674544d3af63c6f68093ffc038385c493
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.bpm.convert.task;
H 2
bb2880 3 import cn.hutool.core.collection.CollUtil;
e7c126 4 import cn.hutool.core.map.MapUtil;
H 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;
bb2880 9 import com.iailab.module.bpm.controller.admin.base.user.UserSimpleBaseVO;
e7c126 10 import com.iailab.module.bpm.controller.admin.task.vo.task.BpmTaskRespVO;
H 11 import com.iailab.module.bpm.dal.dataobject.definition.BpmFormDO;
bb2880 12 import com.iailab.module.bpm.enums.task.BpmTaskStatusEnum;
e7c126 13 import com.iailab.module.bpm.framework.flowable.core.util.FlowableUtils;
H 14 import com.iailab.module.bpm.service.message.dto.BpmMessageSendWhenTaskCreatedReqDTO;
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.runtime.ProcessInstance;
19 import org.flowable.task.api.Task;
20 import org.flowable.task.api.history.HistoricTaskInstance;
21 import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;
22 import org.mapstruct.Mapper;
23 import org.mapstruct.factory.Mappers;
24
25 import java.util.Date;
26 import java.util.List;
27 import java.util.Map;
28
bb2880 29 import static com.iailab.framework.common.util.collection.CollectionUtils.convertList;
e7c126 30 import static com.iailab.framework.common.util.collection.MapUtils.findAndThen;
bb2880 31
e7c126 32
H 33 /**
34  * Bpm 任务 Convert
35  *
bb2880 36  * @author 芋道源码
e7c126 37  */
H 38 @Mapper
39 public interface BpmTaskConvert {
40
41     BpmTaskConvert INSTANCE = Mappers.getMapper(BpmTaskConvert.class);
42
43     default PageResult<BpmTaskRespVO> buildTodoTaskPage(PageResult<Task> pageResult,
44                                                         Map<String, ProcessInstance> processInstanceMap,
45                                                         Map<Long, AdminUserRespDTO> userMap) {
46         return BeanUtils.toBean(pageResult, BpmTaskRespVO.class, taskVO -> {
47             ProcessInstance processInstance = processInstanceMap.get(taskVO.getProcessInstanceId());
48             if (processInstance == null) {
49                 return;
50             }
51             taskVO.setProcessInstance(BeanUtils.toBean(processInstance, BpmTaskRespVO.ProcessInstance.class));
52             AdminUserRespDTO startUser = userMap.get(NumberUtils.parseLong(processInstance.getStartUserId()));
bb2880 53             taskVO.getProcessInstance().setStartUser(BeanUtils.toBean(startUser, UserSimpleBaseVO.class));
e7c126 54         });
H 55     }
56
57     default PageResult<BpmTaskRespVO> buildTaskPage(PageResult<HistoricTaskInstance> pageResult,
58                                                     Map<String, HistoricProcessInstance> processInstanceMap,
59                                                     Map<Long, AdminUserRespDTO> userMap,
60                                                     Map<Long, DeptRespDTO> deptMap) {
bb2880 61         List<BpmTaskRespVO> taskVOList = convertList(pageResult.getList(), task -> {
e7c126 62             BpmTaskRespVO taskVO = BeanUtils.toBean(task, BpmTaskRespVO.class);
H 63             taskVO.setStatus(FlowableUtils.getTaskStatus(task)).setReason(FlowableUtils.getTaskReason(task));
64             // 用户信息
65             AdminUserRespDTO assignUser = userMap.get(NumberUtils.parseLong(task.getAssignee()));
66             if (assignUser != null) {
bb2880 67                 taskVO.setAssigneeUser(BeanUtils.toBean(assignUser, UserSimpleBaseVO.class));
e7c126 68                 findAndThen(deptMap, assignUser.getDeptId(), dept -> taskVO.getAssigneeUser().setDeptName(dept.getName()));
H 69             }
70             // 流程实例
71             HistoricProcessInstance processInstance = processInstanceMap.get(taskVO.getProcessInstanceId());
72             if (processInstance != null) {
73                 AdminUserRespDTO startUser = userMap.get(NumberUtils.parseLong(processInstance.getStartUserId()));
74                 taskVO.setProcessInstance(BeanUtils.toBean(processInstance, BpmTaskRespVO.ProcessInstance.class));
bb2880 75                 taskVO.getProcessInstance().setStartUser(BeanUtils.toBean(startUser, UserSimpleBaseVO.class));
e7c126 76             }
H 77             return taskVO;
78         });
79         return new PageResult<>(taskVOList, pageResult.getTotal());
80     }
81
82     default List<BpmTaskRespVO> buildTaskListByProcessInstanceId(List<HistoricTaskInstance> taskList,
83                                                                  Map<Long, BpmFormDO> formMap,
84                                                                  Map<Long, AdminUserRespDTO> userMap,
85                                                                  Map<Long, DeptRespDTO> deptMap) {
bb2880 86         return convertList(taskList, task -> {
H 87             // 特殊:已取消的任务,不返回
e7c126 88             BpmTaskRespVO taskVO = BeanUtils.toBean(task, BpmTaskRespVO.class);
bb2880 89             Integer taskStatus = FlowableUtils.getTaskStatus(task);
H 90             if (BpmTaskStatusEnum.isCancelStatus(taskStatus)) {
91                 return null;
92             }
93             taskVO.setStatus(taskStatus).setReason(FlowableUtils.getTaskReason(task));
e7c126 94             // 表单信息
H 95             BpmFormDO form = MapUtil.get(formMap, NumberUtils.parseLong(task.getFormKey()), BpmFormDO.class);
96             if (form != null) {
97                 taskVO.setFormId(form.getId()).setFormName(form.getName()).setFormConf(form.getConf())
98                         .setFormFields(form.getFields()).setFormVariables(FlowableUtils.getTaskFormVariable(task));
99             }
100             // 用户信息
bb2880 101             buildTaskAssignee(taskVO, task.getAssignee(), userMap, deptMap);
H 102             buildTaskOwner(taskVO, task.getOwner(), userMap, deptMap);
e7c126 103             return taskVO;
H 104         });
105     }
106
107     default List<BpmTaskRespVO> buildTaskListByParentTaskId(List<Task> taskList,
108                                                             Map<Long, AdminUserRespDTO> userMap,
109                                                             Map<Long, DeptRespDTO> deptMap) {
110         return convertList(taskList, task -> BeanUtils.toBean(task, BpmTaskRespVO.class, taskVO -> {
111             AdminUserRespDTO assignUser = userMap.get(NumberUtils.parseLong(task.getAssignee()));
112             if (assignUser != null) {
bb2880 113                 taskVO.setAssigneeUser(BeanUtils.toBean(assignUser, UserSimpleBaseVO.class));
e7c126 114                 DeptRespDTO dept = deptMap.get(assignUser.getDeptId());
H 115                 if (dept != null) {
116                     taskVO.getAssigneeUser().setDeptName(dept.getName());
117                 }
118             }
119             AdminUserRespDTO ownerUser = userMap.get(NumberUtils.parseLong(task.getOwner()));
120             if (ownerUser != null) {
bb2880 121                 taskVO.setOwnerUser(BeanUtils.toBean(ownerUser, UserSimpleBaseVO.class));
e7c126 122                 findAndThen(deptMap, ownerUser.getDeptId(), dept -> taskVO.getOwnerUser().setDeptName(dept.getName()));
H 123             }
124         }));
bb2880 125     }
H 126
127     default BpmTaskRespVO buildTodoTask(Task todoTask, List<Task> childrenTasks,
128                                               Map<Integer, BpmTaskRespVO.OperationButtonSetting> buttonsSetting) {
129         return BeanUtils.toBean(todoTask, BpmTaskRespVO.class)
130                 .setStatus(FlowableUtils.getTaskStatus(todoTask)).setReason(FlowableUtils.getTaskReason(todoTask))
131                 .setButtonsSetting(buttonsSetting)
132                 .setChildren(convertList(childrenTasks, childTask -> BeanUtils.toBean(childTask, BpmTaskRespVO.class)
133                         .setStatus(FlowableUtils.getTaskStatus(childTask))));
e7c126 134     }
H 135
136     default BpmMessageSendWhenTaskCreatedReqDTO convert(ProcessInstance processInstance, AdminUserRespDTO startUser,
137                                                         Task task) {
138         BpmMessageSendWhenTaskCreatedReqDTO reqDTO = new BpmMessageSendWhenTaskCreatedReqDTO();
139         reqDTO.setProcessInstanceId(processInstance.getProcessInstanceId())
140                 .setProcessInstanceName(processInstance.getName()).setStartUserId(startUser.getId())
141                 .setStartUserNickname(startUser.getNickname()).setTaskId(task.getId()).setTaskName(task.getName())
142                 .setAssigneeUserId(NumberUtils.parseLong(task.getAssignee()));
143         return reqDTO;
144     }
145
bb2880 146     default void buildTaskOwner(BpmTaskRespVO task, String taskOwner,
H 147                                 Map<Long, AdminUserRespDTO> userMap,
148                                 Map<Long, DeptRespDTO> deptMap) {
149         AdminUserRespDTO ownerUser = userMap.get(NumberUtils.parseLong(taskOwner));
150         if (ownerUser != null) {
151             task.setOwnerUser(BeanUtils.toBean(ownerUser, UserSimpleBaseVO.class));
152             findAndThen(deptMap, ownerUser.getDeptId(), dept -> task.getOwnerUser().setDeptName(dept.getName()));
153         }
154     }
155
156     default void buildTaskChildren(BpmTaskRespVO task, Map<String, List<Task>> childrenTaskMap,
157                                    Map<Long, AdminUserRespDTO> userMap, Map<Long, DeptRespDTO> deptMap) {
158         List<Task> childTasks = childrenTaskMap.get(task.getId());
159         if (CollUtil.isNotEmpty(childTasks)) {
160             task.setChildren(
161                     convertList(childTasks, childTask -> {
162                         BpmTaskRespVO childTaskVO = BeanUtils.toBean(childTask, BpmTaskRespVO.class);
163                         childTaskVO.setStatus(FlowableUtils.getTaskStatus(childTask));
164                         buildTaskOwner(childTaskVO, childTask.getOwner(), userMap, deptMap);
165                         buildTaskAssignee(childTaskVO, childTask.getAssignee(), userMap, deptMap);
166                         return childTaskVO;
167                     })
168             );
169         }
170     }
171
172     default void buildTaskAssignee(BpmTaskRespVO task, String taskAssignee,
173                                    Map<Long, AdminUserRespDTO> userMap,
174                                    Map<Long, DeptRespDTO> deptMap) {
175         AdminUserRespDTO assignUser = userMap.get(NumberUtils.parseLong(taskAssignee));
176         if (assignUser != null) {
177             task.setAssigneeUser(BeanUtils.toBean(assignUser, UserSimpleBaseVO.class));
178             findAndThen(deptMap, assignUser.getDeptId(), dept -> task.getAssigneeUser().setDeptName(dept.getName()));
179         }
180     }
181
e7c126 182     /**
H 183      * 将父任务的属性,拷贝到子任务(加签任务)
bb2880 184      * <p>
e7c126 185      * 为什么不使用 mapstruct 映射?因为 TaskEntityImpl 还有很多其他属性,这里我们只设置我们需要的。
H 186      * 使用 mapstruct 会将里面嵌套的各个属性值都设置进去,会出现意想不到的问题。
187      *
188      * @param parentTask 父任务
bb2880 189      * @param childTask  加签任务
e7c126 190      */
H 191     default void copyTo(TaskEntityImpl parentTask, TaskEntityImpl childTask) {
192         childTask.setName(parentTask.getName());
193         childTask.setDescription(parentTask.getDescription());
194         childTask.setCategory(parentTask.getCategory());
195         childTask.setParentTaskId(parentTask.getId());
196         childTask.setProcessDefinitionId(parentTask.getProcessDefinitionId());
197         childTask.setProcessInstanceId(parentTask.getProcessInstanceId());
198         childTask.setTaskDefinitionKey(parentTask.getTaskDefinitionKey());
199         childTask.setTaskDefinitionId(parentTask.getTaskDefinitionId());
200         childTask.setPriority(parentTask.getPriority());
201         childTask.setCreateTime(new Date());
202         childTask.setTenantId(parentTask.getTenantId());
203     }
204
205 }