xm
2024-06-14 722af26bc6fec32bb289b1df51a9016a4935610f
提交 | 用户 | 时间
722af2 1 package com.dl.workflow.service.impl;
X 2
3
4 import cn.hutool.core.collection.CollUtil;
5 import cn.hutool.core.date.BetweenFormatter;
6 import cn.hutool.core.date.DateUtil;
7 import cn.hutool.core.util.ObjectUtil;
8 import com.dl.common.core.domain.entity.SysDept;
9 import com.dl.common.core.domain.entity.SysRole;
10 import com.dl.common.core.service.UserService;
11 import com.dl.common.exception.ServiceException;
12 import com.dl.common.utils.JsonUtils;
13 import com.dl.common.utils.StringUtils;
14 import com.dl.flowable.common.constant.TaskConstants;
15 import com.dl.flowable.factory.FlowServiceFactory;
16 import com.dl.system.service.ISysDeptService;
17 import com.dl.system.service.ISysRoleService;
18 import com.dl.workflow.domain.bo.WfTaskBo;
19 import com.dl.workflow.domain.vo.WfFormVo;
20 import com.dl.workflow.domain.vo.WfTaskVo;
21 import com.dl.workflow.service.IWfDeployFormService;
22 import com.dl.workflow.service.IWfInstanceService;
23 import lombok.RequiredArgsConstructor;
24 import lombok.extern.slf4j.Slf4j;
25 import org.flowable.common.engine.api.FlowableObjectNotFoundException;
26 import org.flowable.engine.history.HistoricProcessInstance;
27 import org.flowable.engine.task.Comment;
28 import org.flowable.identitylink.api.history.HistoricIdentityLink;
29 import org.flowable.task.api.history.HistoricTaskInstance;
30 import org.springframework.stereotype.Service;
31 import org.springframework.transaction.annotation.Transactional;
32
33 import java.util.*;
34
35 /**
36  * 工作流流程实例管理
37  *
38  * @author KonBAI
39  * @createTime 2022/3/10 00:12
40  */
41 @RequiredArgsConstructor
42 @Service
43 @Slf4j
44 public class WfInstanceServiceImpl extends FlowServiceFactory implements IWfInstanceService {
45
46     private final IWfDeployFormService deployFormService;
47     private final UserService userService;
48     private final ISysRoleService roleService;
49     private final ISysDeptService deptService;
50
51     /**
52      * 结束流程实例
53      *
54      * @param vo
55      */
56     @Override
57     public void stopProcessInstance(WfTaskBo vo) {
58         String taskId = vo.getTaskId();
59
60     }
61
62     /**
63      * 激活或挂起流程实例
64      *
65      * @param state      状态
66      * @param instanceId 流程实例ID
67      */
68     @Override
69     public void updateState(Integer state, String instanceId) {
70
71         // 激活
72         if (state == 1) {
73             runtimeService.activateProcessInstanceById(instanceId);
74         }
75         // 挂起
76         if (state == 2) {
77             runtimeService.suspendProcessInstanceById(instanceId);
78         }
79     }
80
81     /**
82      * 删除流程实例ID
83      *
84      * @param instanceId   流程实例ID
85      * @param deleteReason 删除原因
86      */
87     @Override
88     @Transactional(rollbackFor = Exception.class)
89     public void delete(String instanceId, String deleteReason) {
90
91         // 查询历史数据
92         HistoricProcessInstance historicProcessInstance = getHistoricProcessInstanceById(instanceId);
93         if (historicProcessInstance.getEndTime() != null) {
94             historyService.deleteHistoricProcessInstance(historicProcessInstance.getId());
95             return;
96         }
97         // 删除流程实例
98         runtimeService.deleteProcessInstance(instanceId, deleteReason);
99         // 删除历史流程实例
100         historyService.deleteHistoricProcessInstance(instanceId);
101     }
102
103     /**
104      * 根据实例ID查询历史实例数据
105      *
106      * @param processInstanceId
107      * @return
108      */
109     @Override
110     public HistoricProcessInstance getHistoricProcessInstanceById(String processInstanceId) {
111         HistoricProcessInstance historicProcessInstance =
112                 historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
113         if (Objects.isNull(historicProcessInstance)) {
114             throw new FlowableObjectNotFoundException("流程实例不存在: " + processInstanceId);
115         }
116         return historicProcessInstance;
117     }
118
119
120     /**
121      * 流程历史流转记录
122      *
123      * @param procInsId 流程实例Id
124      * @return
125      */
126     @Override
127     public Map<String, Object> queryDetailProcess(String procInsId, String deployId) {
128         Map<String, Object> map = new HashMap<>();
129         if (StringUtils.isNotBlank(procInsId)) {
130             List<HistoricTaskInstance> taskInstanceList = historyService.createHistoricTaskInstanceQuery()
131                 .processInstanceId(procInsId)
132                 .orderByHistoricTaskInstanceStartTime().desc()
133                 .list();
134             List<Comment> commentList = taskService.getProcessInstanceComments(procInsId);
135             List<WfTaskVo> taskVoList = new ArrayList<>(taskInstanceList.size());
136             taskInstanceList.forEach(taskInstance -> {
137                 WfTaskVo taskVo = new WfTaskVo();
138                 taskVo.setProcDefId(taskInstance.getProcessDefinitionId());
139                 taskVo.setTaskId(taskInstance.getId());
140                 taskVo.setTaskDefKey(taskInstance.getTaskDefinitionKey());
141                 taskVo.setTaskName(taskInstance.getName());
142                 taskVo.setCreateTime(taskInstance.getStartTime());
143                 taskVo.setFinishTime(taskInstance.getEndTime());
144                 if (StringUtils.isNotBlank(taskInstance.getAssignee())) {
145                     String userId = taskInstance.getAssignee();
146                     String nickName = userService.selectNickNameById(userId);
147                     taskVo.setAssigneeId(userId);
148                     taskVo.setAssigneeName(nickName);
149                 }
150                 // 展示审批人员
151                 List<HistoricIdentityLink> linksForTask = historyService.getHistoricIdentityLinksForTask(taskInstance.getId());
152                 StringBuilder stringBuilder = new StringBuilder();
153                 for (HistoricIdentityLink identityLink : linksForTask) {
154                     if ("candidate".equals(identityLink.getType())) {
155                         if (StringUtils.isNotBlank(identityLink.getUserId())) {
156                             String userId = identityLink.getUserId();
157                             String nickName = userService.selectNickNameById(userId);
158                             stringBuilder.append(nickName).append(",");
159                         }
160                         if (StringUtils.isNotBlank(identityLink.getGroupId())) {
161                             if (identityLink.getGroupId().startsWith(TaskConstants.ROLE_GROUP_PREFIX)) {
162                                 String roleId = StringUtils.stripStart(identityLink.getGroupId(), TaskConstants.ROLE_GROUP_PREFIX);
163 //                                Long roleId = Long.parseLong();
164                                 SysRole role = roleService.selectRoleById(roleId);
165                                 stringBuilder.append(role.getRoleName()).append(",");
166                             } else if (identityLink.getGroupId().startsWith(TaskConstants.DEPT_GROUP_PREFIX)) {
167                                 Long deptId = Long.parseLong(StringUtils.stripStart(identityLink.getGroupId(), TaskConstants.DEPT_GROUP_PREFIX));
168                                 SysDept dept = deptService.selectDeptById(deptId);
169                                 stringBuilder.append(dept.getDeptName()).append(",");
170                             }
171                         }
172                     }
173                 }
174                 if (StringUtils.isNotBlank(stringBuilder)) {
175                     taskVo.setCandidate(stringBuilder.substring(0, stringBuilder.length() - 1));
176                 }
177                 if (ObjectUtil.isNotNull(taskInstance.getDurationInMillis())) {
178                     taskVo.setDuration(DateUtil.formatBetween(taskInstance.getDurationInMillis(), BetweenFormatter.Level.SECOND));
179                 }
180                 // 获取意见评论内容
181                 if (CollUtil.isNotEmpty(commentList)) {
182                     List<Comment> comments = new ArrayList<>();
183                     // commentList.stream().filter(comment -> taskInstance.getId().equals(comment.getTaskId())).collect(Collectors.toList());
184                     for (Comment comment : commentList) {
185                         if (comment.getTaskId().equals(taskInstance.getId())) {
186                             comments.add(comment);
187                             // taskVo.setComment(WfCommentDto.builder().type(comment.getType()).comment(comment.getFullMessage()).build());
188                         }
189                     }
190                     taskVo.setCommentList(comments);
191                 }
192                 taskVoList.add(taskVo);
193             });
194             map.put("flowList", taskVoList);
195 //            // 查询当前任务是否完成
196 //            List<Task> taskList = taskService.createTaskQuery().processInstanceId(procInsId).list();
197 //            if (CollectionUtils.isNotEmpty(taskList)) {
198 //                map.put("finished", true);
199 //            } else {
200 //                map.put("finished", false);
201 //            }
202         }
203         // 第一次申请获取初始化表单
204         if (StringUtils.isNotBlank(deployId)) {
205             WfFormVo formVo = deployFormService.selectDeployFormByDeployId(deployId);
206             if (Objects.isNull(formVo)) {
207                 throw new ServiceException("请先配置流程表单");
208             }
209             map.put("formData", JsonUtils.parseObject(formVo.getContent(), Map.class));
210         }
211         return map;
212     }
213 }