|
|
@@ -1,95 +1,687 @@
|
|
1
|
1
|
package com.water.bpm.service;
|
|
2
|
2
|
|
|
|
3
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
4
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
5
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
6
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
7
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
3
|
8
|
import com.water.bpm.entity.*;
|
|
|
9
|
+import com.water.bpm.mapper.*;
|
|
|
10
|
+import com.water.common.core.exception.BusinessException;
|
|
4
|
11
|
import lombok.RequiredArgsConstructor;
|
|
5
|
12
|
import lombok.extern.slf4j.Slf4j;
|
|
6
|
13
|
import org.springframework.stereotype.Service;
|
|
7
|
14
|
import org.springframework.transaction.annotation.Transactional;
|
|
8
|
15
|
|
|
|
16
|
+import java.time.LocalDateTime;
|
|
|
17
|
+import java.time.temporal.ChronoUnit;
|
|
9
|
18
|
import java.util.*;
|
|
10
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
19
|
+import java.util.stream.Collectors;
|
|
11
|
20
|
|
|
|
21
|
+/**
|
|
|
22
|
+ * 流程引擎核心服务
|
|
|
23
|
+ * 支持:启动流程、审批通过、驳回、转办、会签、待办/已办查询、流程状态流转
|
|
|
24
|
+ */
|
|
12
|
25
|
@Slf4j
|
|
13
|
26
|
@Service
|
|
14
|
27
|
@RequiredArgsConstructor
|
|
15
|
28
|
public class ProcessEngine {
|
|
16
|
29
|
|
|
17
|
|
- // 简化流程引擎:模拟 Camunda/Flowable 核心功能
|
|
18
|
|
- private final Map<String, BpmProcessInstance> instances = new ConcurrentHashMap<>();
|
|
19
|
|
- private final List<BpmApprovalRecord> approvalRecords = new ArrayList<>();
|
|
|
30
|
+ private final BpmProcessDefinitionMapper definitionMapper;
|
|
|
31
|
+ private final BpmProcessInstanceMapper instanceMapper;
|
|
|
32
|
+ private final BpmApprovalRecordMapper approvalRecordMapper;
|
|
|
33
|
+ private final BpmTodoTaskMapper todoTaskMapper;
|
|
|
34
|
+ private final BpmProcessNodeMapper processNodeMapper;
|
|
|
35
|
+ private final ObjectMapper objectMapper;
|
|
20
|
36
|
|
|
21
|
|
- /** 创建流程实例 */
|
|
|
37
|
+ // ==================== 常量 ====================
|
|
|
38
|
+
|
|
|
39
|
+ public static final String STATUS_DRAFT = "draft";
|
|
|
40
|
+ public static final String STATUS_RUNNING = "running";
|
|
|
41
|
+ public static final String STATUS_COMPLETED = "completed";
|
|
|
42
|
+ public static final String STATUS_REJECTED = "rejected";
|
|
|
43
|
+ public static final String STATUS_TERMINATED = "terminated";
|
|
|
44
|
+ public static final String STATUS_SUSPENDED = "suspended";
|
|
|
45
|
+
|
|
|
46
|
+ public static final String ACTION_APPROVE = "approve";
|
|
|
47
|
+ public static final String ACTION_REJECT = "reject";
|
|
|
48
|
+ public static final String ACTION_TRANSFER = "transfer";
|
|
|
49
|
+ public static final String ACTION_DELEGATE = "delegate";
|
|
|
50
|
+ public static final String ACTION_COUNTERSIGN = "countersign";
|
|
|
51
|
+ public static final String ACTION_BACK = "back";
|
|
|
52
|
+
|
|
|
53
|
+ public static final String TODO_PENDING = "pending";
|
|
|
54
|
+ public static final String TODO_COMPLETED = "completed";
|
|
|
55
|
+ public static final String TODO_TRANSFERRED = "transferred";
|
|
|
56
|
+ public static final String TODO_CANCELLED = "cancelled";
|
|
|
57
|
+
|
|
|
58
|
+ // ==================== 启动流程 ====================
|
|
|
59
|
+
|
|
|
60
|
+ /**
|
|
|
61
|
+ * 启动流程(向后兼容旧接口)
|
|
|
62
|
+ */
|
|
22
|
63
|
@Transactional
|
|
23
|
64
|
public BpmProcessInstance startProcess(BpmProcessDefinition definition, Long initiatorId,
|
|
24
|
65
|
String initiatorName, String businessKey,
|
|
25
|
66
|
Map<String, Object> formData) {
|
|
|
67
|
+ return startProcess(definition.getId(), initiatorId, initiatorName,
|
|
|
68
|
+ businessKey, null, null, formData, null, 0);
|
|
|
69
|
+ }
|
|
|
70
|
+
|
|
|
71
|
+ /**
|
|
|
72
|
+ * 启动流程(完整版)
|
|
|
73
|
+ *
|
|
|
74
|
+ * @param definitionId 流程定义ID
|
|
|
75
|
+ * @param initiatorId 发起人ID
|
|
|
76
|
+ * @param initiatorName 发起人姓名
|
|
|
77
|
+ * @param businessKey 业务主键
|
|
|
78
|
+ * @param businessType 业务类型
|
|
|
79
|
+ * @param title 流程标题
|
|
|
80
|
+ * @param formData 表单数据
|
|
|
81
|
+ * @param variables 流程变量
|
|
|
82
|
+ * @param priority 优先级
|
|
|
83
|
+ * @return 流程实例
|
|
|
84
|
+ */
|
|
|
85
|
+ @Transactional
|
|
|
86
|
+ public BpmProcessInstance startProcess(Long definitionId, Long initiatorId, String initiatorName,
|
|
|
87
|
+ String businessKey, String businessType, String title,
|
|
|
88
|
+ Map<String, Object> formData, Map<String, Object> variables,
|
|
|
89
|
+ Integer priority) {
|
|
|
90
|
+ // 1. 加载并校验流程定义
|
|
|
91
|
+ BpmProcessDefinition definition = definitionMapper.selectById(definitionId);
|
|
|
92
|
+ if (definition == null || definition.getStatus() != 1) {
|
|
|
93
|
+ throw new BusinessException("流程定义不存在或未发布");
|
|
|
94
|
+ }
|
|
|
95
|
+
|
|
|
96
|
+ // 2. 加载流程节点
|
|
|
97
|
+ List<BpmProcessNode> nodes = processNodeMapper.selectByDefinitionId(definitionId);
|
|
|
98
|
+ if (nodes == null || nodes.isEmpty()) {
|
|
|
99
|
+ throw new BusinessException("流程定义无节点配置");
|
|
|
100
|
+ }
|
|
|
101
|
+
|
|
|
102
|
+ // 3. 找到起始节点
|
|
|
103
|
+ BpmProcessNode startNode = findStartNode(nodes);
|
|
|
104
|
+
|
|
|
105
|
+ // 4. 创建流程实例
|
|
26
|
106
|
BpmProcessInstance instance = new BpmProcessInstance();
|
|
27
|
|
- instance.setId(System.currentTimeMillis());
|
|
28
|
107
|
instance.setInstanceId(UUID.randomUUID().toString());
|
|
29
|
|
- instance.setDefinitionId(definition.getId());
|
|
|
108
|
+ instance.setDefinitionId(definitionId);
|
|
30
|
109
|
instance.setProcessKey(definition.getProcessKey());
|
|
31
|
|
- instance.setTitle(definition.getProcessName());
|
|
|
110
|
+ instance.setProcessName(definition.getProcessName());
|
|
32
|
111
|
instance.setBusinessKey(businessKey);
|
|
|
112
|
+ instance.setBusinessType(businessType);
|
|
|
113
|
+ instance.setTitle(title != null ? title : definition.getProcessName() + " - " + initiatorName);
|
|
33
|
114
|
instance.setInitiatorId(initiatorId);
|
|
34
|
115
|
instance.setInitiatorName(initiatorName);
|
|
35
|
|
- instance.setStatus("running");
|
|
36
|
|
- instance.setCurrentNode("START");
|
|
37
|
|
- instance.setFormData(formData);
|
|
38
|
|
- instance.setStartedAt(java.time.LocalDateTime.now());
|
|
39
|
|
- instance.setCreatedAt(java.time.LocalDateTime.now());
|
|
40
|
|
- instances.put(instance.getInstanceId(), instance);
|
|
41
|
|
- log.info("Process started: {} - {}", instance.getProcessKey(), instance.getTitle());
|
|
|
116
|
+ instance.setStatus(STATUS_RUNNING);
|
|
|
117
|
+ instance.setCurrentNodeId(startNode.getNodeId());
|
|
|
118
|
+ instance.setCurrentNodeName(startNode.getNodeName());
|
|
|
119
|
+ instance.setPriority(priority != null ? priority : 0);
|
|
|
120
|
+ instance.setFormData(toJson(formData));
|
|
|
121
|
+ instance.setVariables(toJson(variables));
|
|
|
122
|
+ instance.setStartedAt(LocalDateTime.now());
|
|
|
123
|
+ instance.setCreatedAt(LocalDateTime.now());
|
|
|
124
|
+ instance.setUpdatedAt(LocalDateTime.now());
|
|
|
125
|
+ instanceMapper.insert(instance);
|
|
|
126
|
+
|
|
|
127
|
+ // 5. 确定起始节点审批人并创建待办
|
|
|
128
|
+ Long assigneeId = resolveAssignee(startNode, initiatorId);
|
|
|
129
|
+ String assigneeName = startNode.getAssigneeName();
|
|
|
130
|
+ createTodoTask(instance, startNode, assigneeId, assigneeName);
|
|
|
131
|
+
|
|
|
132
|
+ // 6. 更新实例当前处理人
|
|
|
133
|
+ instance.setCurrentAssigneeId(assigneeId);
|
|
|
134
|
+ instance.setCurrentAssigneeName(assigneeName);
|
|
|
135
|
+ instanceMapper.updateById(instance);
|
|
|
136
|
+
|
|
|
137
|
+ // 7. 记录启动审批记录
|
|
|
138
|
+ BpmApprovalRecord startRecord = new BpmApprovalRecord();
|
|
|
139
|
+ startRecord.setInstanceId(instance.getId());
|
|
|
140
|
+ startRecord.setInstanceUuid(instance.getInstanceId());
|
|
|
141
|
+ startRecord.setNodeId(startNode.getNodeId());
|
|
|
142
|
+ startRecord.setNodeName(startNode.getNodeName());
|
|
|
143
|
+ startRecord.setApproverId(initiatorId);
|
|
|
144
|
+ startRecord.setApproverName(initiatorName);
|
|
|
145
|
+ startRecord.setAction("start");
|
|
|
146
|
+ startRecord.setComment("发起流程");
|
|
|
147
|
+ startRecord.setApprovedAt(LocalDateTime.now());
|
|
|
148
|
+ startRecord.setCreatedAt(LocalDateTime.now());
|
|
|
149
|
+ approvalRecordMapper.insert(startRecord);
|
|
|
150
|
+
|
|
|
151
|
+ log.info("流程已启动: key={}, instanceId={}, initiator={}",
|
|
|
152
|
+ definition.getProcessKey(), instance.getInstanceId(), initiatorName);
|
|
42
|
153
|
return instance;
|
|
43
|
154
|
}
|
|
44
|
155
|
|
|
45
|
|
- /** 审批节点 */
|
|
|
156
|
+ // ==================== 审批通过 ====================
|
|
|
157
|
+
|
|
|
158
|
+ /**
|
|
|
159
|
+ * 审批(向后兼容旧接口)
|
|
|
160
|
+ */
|
|
|
161
|
+ @Transactional
|
|
|
162
|
+ public BpmProcessInstance approve(String instanceUuid, Long approverId, String approverName,
|
|
|
163
|
+ String nodeId, String nodeName,
|
|
|
164
|
+ String action, String comment) {
|
|
|
165
|
+ switch (action) {
|
|
|
166
|
+ case ACTION_APPROVE:
|
|
|
167
|
+ return approveTask(instanceUuid, approverId, approverName, nodeId, comment);
|
|
|
168
|
+ case ACTION_REJECT:
|
|
|
169
|
+ return rejectTask(instanceUuid, approverId, approverName, nodeId, comment);
|
|
|
170
|
+ case ACTION_TRANSFER:
|
|
|
171
|
+ // 旧接口不支持转办目标人,使用默认
|
|
|
172
|
+ return approveTask(instanceUuid, approverId, approverName, nodeId, comment);
|
|
|
173
|
+ default:
|
|
|
174
|
+ return approveTask(instanceUuid, approverId, approverName, nodeId, comment);
|
|
|
175
|
+ }
|
|
|
176
|
+ }
|
|
|
177
|
+
|
|
|
178
|
+ /**
|
|
|
179
|
+ * 审批通过
|
|
|
180
|
+ */
|
|
46
|
181
|
@Transactional
|
|
47
|
|
- public BpmProcessInstance approve(String instanceId, Long approverId, String approverName,
|
|
48
|
|
- String nodeId, String nodeName,
|
|
49
|
|
- String action, String comment) {
|
|
50
|
|
- BpmProcessInstance instance = instances.get(instanceId);
|
|
51
|
|
- if (instance == null) throw new RuntimeException("流程实例不存在");
|
|
|
182
|
+ public BpmProcessInstance approveTask(String instanceUuid, Long approverId, String approverName,
|
|
|
183
|
+ String nodeId, String comment) {
|
|
|
184
|
+ BpmProcessInstance instance = getInstanceByUuid(instanceUuid);
|
|
|
185
|
+ validateRunning(instance);
|
|
|
186
|
+ validateAssignee(instance, approverId);
|
|
52
|
187
|
|
|
53
|
|
- BpmApprovalRecord record = new BpmApprovalRecord();
|
|
54
|
|
- record.setInstanceId(instance.getId());
|
|
55
|
|
- record.setNodeId(nodeId);
|
|
56
|
|
- record.setNodeName(nodeName);
|
|
57
|
|
- record.setApproverId(approverId);
|
|
58
|
|
- record.setApproverName(approverName);
|
|
59
|
|
- record.setAction(action);
|
|
60
|
|
- record.setComment(comment);
|
|
61
|
|
- record.setApprovedAt(java.time.LocalDateTime.now());
|
|
62
|
|
- approvalRecords.add(record);
|
|
|
188
|
+ // 记录审批
|
|
|
189
|
+ BpmApprovalRecord record = buildApprovalRecord(instance, nodeId, approverId, approverName,
|
|
|
190
|
+ ACTION_APPROVE, comment);
|
|
|
191
|
+ approvalRecordMapper.insert(record);
|
|
63
|
192
|
|
|
64
|
|
- instance.setCurrentNode(nodeName);
|
|
65
|
|
- switch (action) {
|
|
66
|
|
- case "approve": instance.setStatus("running"); break;
|
|
67
|
|
- case "reject": instance.setStatus("rejected"); instance.setCompletedAt(java.time.LocalDateTime.now()); break;
|
|
68
|
|
- default: instance.setStatus("running");
|
|
|
193
|
+ // 完成当前待办
|
|
|
194
|
+ completeTodoTasks(instance.getId(), approverId);
|
|
|
195
|
+
|
|
|
196
|
+ // 流转到下一节点
|
|
|
197
|
+ List<BpmProcessNode> nodes = processNodeMapper.selectByDefinitionId(instance.getDefinitionId());
|
|
|
198
|
+ BpmProcessNode currentNode = findNodeById(nodes, nodeId != null ? nodeId : instance.getCurrentNodeId());
|
|
|
199
|
+ BpmProcessNode nextNode = findNextNode(nodes, currentNode);
|
|
|
200
|
+
|
|
|
201
|
+ if (nextNode == null || "end".equals(nextNode.getNodeType())) {
|
|
|
202
|
+ // 流程结束
|
|
|
203
|
+ completeInstance(instance);
|
|
|
204
|
+ } else {
|
|
|
205
|
+ // 流转到下一节点
|
|
|
206
|
+ Long nextAssigneeId = resolveAssignee(nextNode, instance.getInitiatorId());
|
|
|
207
|
+ String nextAssigneeName = nextNode.getAssigneeName();
|
|
|
208
|
+
|
|
|
209
|
+ instance.setCurrentNodeId(nextNode.getNodeId());
|
|
|
210
|
+ instance.setCurrentNodeName(nextNode.getNodeName());
|
|
|
211
|
+ instance.setCurrentAssigneeId(nextAssigneeId);
|
|
|
212
|
+ instance.setCurrentAssigneeName(nextAssigneeName);
|
|
|
213
|
+ instance.setUpdatedAt(LocalDateTime.now());
|
|
|
214
|
+ instanceMapper.updateById(instance);
|
|
|
215
|
+
|
|
|
216
|
+ createTodoTask(instance, nextNode, nextAssigneeId, nextAssigneeName);
|
|
|
217
|
+ }
|
|
|
218
|
+
|
|
|
219
|
+ log.info("审批通过: instanceUuid={}, approver={}, nodeId={}", instanceUuid, approverName, nodeId);
|
|
|
220
|
+ return instance;
|
|
|
221
|
+ }
|
|
|
222
|
+
|
|
|
223
|
+ // ==================== 驳回 ====================
|
|
|
224
|
+
|
|
|
225
|
+ /**
|
|
|
226
|
+ * 驳回
|
|
|
227
|
+ */
|
|
|
228
|
+ @Transactional
|
|
|
229
|
+ public BpmProcessInstance rejectTask(String instanceUuid, Long approverId, String approverName,
|
|
|
230
|
+ String nodeId, String comment) {
|
|
|
231
|
+ BpmProcessInstance instance = getInstanceByUuid(instanceUuid);
|
|
|
232
|
+ validateRunning(instance);
|
|
|
233
|
+ validateAssignee(instance, approverId);
|
|
|
234
|
+
|
|
|
235
|
+ // 记录驳回
|
|
|
236
|
+ BpmApprovalRecord record = buildApprovalRecord(instance, nodeId, approverId, approverName,
|
|
|
237
|
+ ACTION_REJECT, comment);
|
|
|
238
|
+ approvalRecordMapper.insert(record);
|
|
|
239
|
+
|
|
|
240
|
+ // 取消所有待办
|
|
|
241
|
+ cancelTodoTasks(instance.getId());
|
|
|
242
|
+
|
|
|
243
|
+ // 更新实例状态
|
|
|
244
|
+ instance.setStatus(STATUS_REJECTED);
|
|
|
245
|
+ instance.setCompletedAt(LocalDateTime.now());
|
|
|
246
|
+ instance.setDurationSeconds(ChronoUnit.SECONDS.between(instance.getStartedAt(), LocalDateTime.now()));
|
|
|
247
|
+ instance.setUpdatedAt(LocalDateTime.now());
|
|
|
248
|
+ instanceMapper.updateById(instance);
|
|
|
249
|
+
|
|
|
250
|
+ log.info("审批驳回: instanceUuid={}, approver={}, reason={}", instanceUuid, approverName, comment);
|
|
|
251
|
+ return instance;
|
|
|
252
|
+ }
|
|
|
253
|
+
|
|
|
254
|
+ // ==================== 转办 ====================
|
|
|
255
|
+
|
|
|
256
|
+ /**
|
|
|
257
|
+ * 转办:将审批权转给其他人,原审批人不再审批
|
|
|
258
|
+ */
|
|
|
259
|
+ @Transactional
|
|
|
260
|
+ public BpmProcessInstance transferTask(String instanceUuid, Long currentApproverId, String currentApproverName,
|
|
|
261
|
+ Long targetAssigneeId, String targetAssigneeName,
|
|
|
262
|
+ String nodeId, String comment) {
|
|
|
263
|
+ BpmProcessInstance instance = getInstanceByUuid(instanceUuid);
|
|
|
264
|
+ validateRunning(instance);
|
|
|
265
|
+ validateAssignee(instance, currentApproverId);
|
|
|
266
|
+
|
|
|
267
|
+ if (targetAssigneeId == null || targetAssigneeId.equals(currentApproverId)) {
|
|
|
268
|
+ throw new BusinessException("转办目标人不能为空或与当前审批人相同");
|
|
|
269
|
+ }
|
|
|
270
|
+
|
|
|
271
|
+ // 记录转办
|
|
|
272
|
+ BpmApprovalRecord record = buildApprovalRecord(instance, nodeId, currentApproverId, currentApproverName,
|
|
|
273
|
+ ACTION_TRANSFER, comment);
|
|
|
274
|
+ record.setTargetAssigneeId(targetAssigneeId);
|
|
|
275
|
+ record.setTargetAssigneeName(targetAssigneeName);
|
|
|
276
|
+ approvalRecordMapper.insert(record);
|
|
|
277
|
+
|
|
|
278
|
+ // 关闭当前待办(标记为转办)
|
|
|
279
|
+ List<BpmTodoTask> currentTodos = todoTaskMapper.selectPendingByUserId(currentApproverId);
|
|
|
280
|
+ for (BpmTodoTask todo : currentTodos) {
|
|
|
281
|
+ if (todo.getInstanceId().equals(instance.getId())) {
|
|
|
282
|
+ todo.setStatus(TODO_TRANSFERRED);
|
|
|
283
|
+ todo.setCompletedAt(LocalDateTime.now());
|
|
|
284
|
+ todo.setUpdatedAt(LocalDateTime.now());
|
|
|
285
|
+ todoTaskMapper.updateById(todo);
|
|
|
286
|
+ }
|
|
|
287
|
+ }
|
|
|
288
|
+
|
|
|
289
|
+ // 创建新的待办给目标人
|
|
|
290
|
+ List<BpmProcessNode> nodes = processNodeMapper.selectByDefinitionId(instance.getDefinitionId());
|
|
|
291
|
+ BpmProcessNode currentNode = findNodeById(nodes, nodeId != null ? nodeId : instance.getCurrentNodeId());
|
|
|
292
|
+ createTodoTask(instance, currentNode, targetAssigneeId, targetAssigneeName);
|
|
|
293
|
+
|
|
|
294
|
+ // 更新实例当前处理人
|
|
|
295
|
+ instance.setCurrentAssigneeId(targetAssigneeId);
|
|
|
296
|
+ instance.setCurrentAssigneeName(targetAssigneeName);
|
|
|
297
|
+ instance.setUpdatedAt(LocalDateTime.now());
|
|
|
298
|
+ instanceMapper.updateById(instance);
|
|
|
299
|
+
|
|
|
300
|
+ log.info("任务转办: instanceUuid={}, from={} to={}",
|
|
|
301
|
+ instanceUuid, currentApproverName, targetAssigneeName);
|
|
|
302
|
+ return instance;
|
|
|
303
|
+ }
|
|
|
304
|
+
|
|
|
305
|
+ // ==================== 会签 ====================
|
|
|
306
|
+
|
|
|
307
|
+ /**
|
|
|
308
|
+ * 发起会签:多人审批,全部通过才继续流转
|
|
|
309
|
+ */
|
|
|
310
|
+ @Transactional
|
|
|
311
|
+ public BpmProcessInstance initiateCountersign(String instanceUuid, Long initiatorId,
|
|
|
312
|
+ List<Long> signerIds, List<String> signerNames,
|
|
|
313
|
+ String nodeId, String comment) {
|
|
|
314
|
+ BpmProcessInstance instance = getInstanceByUuid(instanceUuid);
|
|
|
315
|
+ validateRunning(instance);
|
|
|
316
|
+
|
|
|
317
|
+ if (signerIds == null || signerIds.size() < 2) {
|
|
|
318
|
+ throw new BusinessException("会签至少需要2个审批人");
|
|
|
319
|
+ }
|
|
|
320
|
+
|
|
|
321
|
+ // 记录会签发起
|
|
|
322
|
+ BpmApprovalRecord record = buildApprovalRecord(instance, nodeId, initiatorId,
|
|
|
323
|
+ null, ACTION_COUNTERSIGN, comment);
|
|
|
324
|
+ record.setCountersignTotal(signerIds.size());
|
|
|
325
|
+ record.setCountersignApproved(0);
|
|
|
326
|
+ record.setCountersignResult("all");
|
|
|
327
|
+ approvalRecordMapper.insert(record);
|
|
|
328
|
+
|
|
|
329
|
+ // 关闭当前待办
|
|
|
330
|
+ cancelTodoTasks(instance.getId());
|
|
|
331
|
+
|
|
|
332
|
+ // 为每个会签人创建待办
|
|
|
333
|
+ List<BpmProcessNode> nodes = processNodeMapper.selectByDefinitionId(instance.getDefinitionId());
|
|
|
334
|
+ BpmProcessNode currentNode = findNodeById(nodes, nodeId != null ? nodeId : instance.getCurrentNodeId());
|
|
|
335
|
+
|
|
|
336
|
+ for (int i = 0; i < signerIds.size(); i++) {
|
|
|
337
|
+ Long signerId = signerIds.get(i);
|
|
|
338
|
+ String signerName = (signerNames != null && i < signerNames.size()) ? signerNames.get(i) : "审批人" + (i + 1);
|
|
|
339
|
+ createTodoTask(instance, currentNode, signerId, signerName);
|
|
|
340
|
+ }
|
|
|
341
|
+
|
|
|
342
|
+ // 更新实例(当前处理人设为第一个会签人作为代表)
|
|
|
343
|
+ instance.setCurrentAssigneeId(signerIds.get(0));
|
|
|
344
|
+ instance.setCurrentAssigneeName(signerNames != null && !signerNames.isEmpty() ? signerNames.get(0) : "会签中");
|
|
|
345
|
+ instance.setUpdatedAt(LocalDateTime.now());
|
|
|
346
|
+ instanceMapper.updateById(instance);
|
|
|
347
|
+
|
|
|
348
|
+ log.info("会签发起: instanceUuid={}, signers={}", instanceUuid, signerNames);
|
|
|
349
|
+ return instance;
|
|
|
350
|
+ }
|
|
|
351
|
+
|
|
|
352
|
+ /**
|
|
|
353
|
+ * 会签审批(单个会签人审批通过)
|
|
|
354
|
+ */
|
|
|
355
|
+ @Transactional
|
|
|
356
|
+ public BpmProcessInstance countersignApprove(String instanceUuid, Long signerId, String signerName,
|
|
|
357
|
+ String nodeId, String comment) {
|
|
|
358
|
+ BpmProcessInstance instance = getInstanceByUuid(instanceUuid);
|
|
|
359
|
+ validateRunning(instance);
|
|
|
360
|
+
|
|
|
361
|
+ // 校验该会签人是否有待办
|
|
|
362
|
+ List<BpmTodoTask> signerTodos = todoTaskMapper.selectPendingByUserId(signerId);
|
|
|
363
|
+ boolean hasTodo = signerTodos.stream()
|
|
|
364
|
+ .anyMatch(t -> t.getInstanceId().equals(instance.getId()));
|
|
|
365
|
+ if (!hasTodo) {
|
|
|
366
|
+ throw new BusinessException("该审批人无此任务的审批权限");
|
|
|
367
|
+ }
|
|
|
368
|
+
|
|
|
369
|
+ // 记录会签审批
|
|
|
370
|
+ BpmApprovalRecord record = buildApprovalRecord(instance, nodeId, signerId, signerName,
|
|
|
371
|
+ ACTION_APPROVE, comment);
|
|
|
372
|
+ approvalRecordMapper.insert(record);
|
|
|
373
|
+
|
|
|
374
|
+ // 完成该会签人的待办
|
|
|
375
|
+ completeTodoTasks(instance.getId(), signerId);
|
|
|
376
|
+
|
|
|
377
|
+ // 检查是否所有会签人都已审批
|
|
|
378
|
+ List<BpmTodoTask> remainingTodos = todoTaskMapper.selectPendingByInstanceId(instance.getId());
|
|
|
379
|
+ if (remainingTodos.isEmpty()) {
|
|
|
380
|
+ // 所有会签人完成,流转到下一节点
|
|
|
381
|
+ List<BpmProcessNode> nodes = processNodeMapper.selectByDefinitionId(instance.getDefinitionId());
|
|
|
382
|
+ BpmProcessNode currentNode = findNodeById(nodes, nodeId != null ? nodeId : instance.getCurrentNodeId());
|
|
|
383
|
+ BpmProcessNode nextNode = findNextNode(nodes, currentNode);
|
|
|
384
|
+
|
|
|
385
|
+ if (nextNode == null || "end".equals(nextNode.getNodeType())) {
|
|
|
386
|
+ completeInstance(instance);
|
|
|
387
|
+ } else {
|
|
|
388
|
+ Long nextAssigneeId = resolveAssignee(nextNode, instance.getInitiatorId());
|
|
|
389
|
+ String nextAssigneeName = nextNode.getAssigneeName();
|
|
|
390
|
+
|
|
|
391
|
+ instance.setCurrentNodeId(nextNode.getNodeId());
|
|
|
392
|
+ instance.setCurrentNodeName(nextNode.getNodeName());
|
|
|
393
|
+ instance.setCurrentAssigneeId(nextAssigneeId);
|
|
|
394
|
+ instance.setCurrentAssigneeName(nextAssigneeName);
|
|
|
395
|
+ instance.setUpdatedAt(LocalDateTime.now());
|
|
|
396
|
+ instanceMapper.updateById(instance);
|
|
|
397
|
+
|
|
|
398
|
+ createTodoTask(instance, nextNode, nextAssigneeId, nextAssigneeName);
|
|
|
399
|
+ }
|
|
|
400
|
+
|
|
|
401
|
+ log.info("会签完成: instanceUuid={}", instanceUuid);
|
|
|
402
|
+ } else {
|
|
|
403
|
+ log.info("会签进行中: instanceUuid={}, remaining={}", instanceUuid, remainingTodos.size());
|
|
69
|
404
|
}
|
|
70
|
|
- log.info("Approval: {} - {}: {}", instanceId, action, comment);
|
|
|
405
|
+
|
|
71
|
406
|
return instance;
|
|
72
|
407
|
}
|
|
73
|
408
|
|
|
74
|
|
- /** 完成流程 */
|
|
|
409
|
+ // ==================== 完成流程(向后兼容) ====================
|
|
|
410
|
+
|
|
|
411
|
+ /**
|
|
|
412
|
+ * 完成流程
|
|
|
413
|
+ */
|
|
75
|
414
|
@Transactional
|
|
76
|
|
- public void completeProcess(String instanceId) {
|
|
77
|
|
- BpmProcessInstance instance = instances.get(instanceId);
|
|
|
415
|
+ public void completeProcess(String instanceUuid) {
|
|
|
416
|
+ BpmProcessInstance instance = getInstanceByUuid(instanceUuid);
|
|
78
|
417
|
if (instance != null) {
|
|
79
|
|
- instance.setStatus("completed");
|
|
80
|
|
- instance.setCompletedAt(java.time.LocalDateTime.now());
|
|
|
418
|
+ completeInstance(instance);
|
|
81
|
419
|
}
|
|
82
|
420
|
}
|
|
83
|
421
|
|
|
84
|
|
- /** 查询待办 */
|
|
|
422
|
+ // ==================== 待办/已办查询 ====================
|
|
|
423
|
+
|
|
|
424
|
+ /**
|
|
|
425
|
+ * 查询待办(向后兼容旧接口)
|
|
|
426
|
+ */
|
|
85
|
427
|
public List<BpmProcessInstance> getTodoList(Long userId) {
|
|
86
|
|
- return instances.values().stream()
|
|
87
|
|
- .filter(i -> "running".equals(i.getStatus()) && i.getInitiatorId().equals(userId))
|
|
88
|
|
- .toList();
|
|
|
428
|
+ List<BpmTodoTask> todos = todoTaskMapper.selectPendingByUserId(userId);
|
|
|
429
|
+ if (todos.isEmpty()) {
|
|
|
430
|
+ return Collections.emptyList();
|
|
|
431
|
+ }
|
|
|
432
|
+ List<Long> instanceIds = todos.stream()
|
|
|
433
|
+ .map(BpmTodoTask::getInstanceId)
|
|
|
434
|
+ .distinct()
|
|
|
435
|
+ .collect(Collectors.toList());
|
|
|
436
|
+ if (instanceIds.isEmpty()) {
|
|
|
437
|
+ return Collections.emptyList();
|
|
|
438
|
+ }
|
|
|
439
|
+ return instanceMapper.selectBatchIds(instanceIds);
|
|
|
440
|
+ }
|
|
|
441
|
+
|
|
|
442
|
+ /**
|
|
|
443
|
+ * 查询待办任务列表(返回任务详情)
|
|
|
444
|
+ */
|
|
|
445
|
+ public List<BpmTodoTask> getTodoTasks(Long userId) {
|
|
|
446
|
+ return todoTaskMapper.selectPendingByUserId(userId);
|
|
|
447
|
+ }
|
|
|
448
|
+
|
|
|
449
|
+ /**
|
|
|
450
|
+ * 查询已办任务列表
|
|
|
451
|
+ */
|
|
|
452
|
+ public List<BpmTodoTask> getDoneTasks(Long userId) {
|
|
|
453
|
+ return todoTaskMapper.selectDoneByUserId(userId);
|
|
|
454
|
+ }
|
|
|
455
|
+
|
|
|
456
|
+ /**
|
|
|
457
|
+ * 查询待办数量
|
|
|
458
|
+ */
|
|
|
459
|
+ public Integer getTodoCount(Long userId) {
|
|
|
460
|
+ return todoTaskMapper.countPendingByUserId(userId);
|
|
|
461
|
+ }
|
|
|
462
|
+
|
|
|
463
|
+ /**
|
|
|
464
|
+ * 查询流程实例(通过UUID)
|
|
|
465
|
+ */
|
|
|
466
|
+ public BpmProcessInstance getInstance(String instanceUuid) {
|
|
|
467
|
+ return getInstanceByUuid(instanceUuid);
|
|
|
468
|
+ }
|
|
|
469
|
+
|
|
|
470
|
+ /**
|
|
|
471
|
+ * 查询流程实例(通过数据库ID)
|
|
|
472
|
+ */
|
|
|
473
|
+ public BpmProcessInstance getInstanceById(Long id) {
|
|
|
474
|
+ BpmProcessInstance instance = instanceMapper.selectById(id);
|
|
|
475
|
+ if (instance == null) {
|
|
|
476
|
+ throw new BusinessException("流程实例不存在");
|
|
|
477
|
+ }
|
|
|
478
|
+ return instance;
|
|
|
479
|
+ }
|
|
|
480
|
+
|
|
|
481
|
+ /**
|
|
|
482
|
+ * 查询我发起的流程
|
|
|
483
|
+ */
|
|
|
484
|
+ public List<BpmProcessInstance> getMyInitiated(Long userId) {
|
|
|
485
|
+ return instanceMapper.selectMyInitiated(userId);
|
|
|
486
|
+ }
|
|
|
487
|
+
|
|
|
488
|
+ /**
|
|
|
489
|
+ * 查询流程审批记录
|
|
|
490
|
+ */
|
|
|
491
|
+ public List<BpmApprovalRecord> getApprovalRecords(Long instanceId) {
|
|
|
492
|
+ return approvalRecordMapper.selectByInstanceId(instanceId);
|
|
|
493
|
+ }
|
|
|
494
|
+
|
|
|
495
|
+ /**
|
|
|
496
|
+ * 查询流程审批记录(通过UUID)
|
|
|
497
|
+ */
|
|
|
498
|
+ public List<BpmApprovalRecord> getApprovalRecordsByUuid(String instanceUuid) {
|
|
|
499
|
+ return approvalRecordMapper.selectByInstanceUuid(instanceUuid);
|
|
|
500
|
+ }
|
|
|
501
|
+
|
|
|
502
|
+ /**
|
|
|
503
|
+ * 条件查询流程实例
|
|
|
504
|
+ */
|
|
|
505
|
+ public List<BpmProcessInstance> queryInstances(String processKey, String status,
|
|
|
506
|
+ Long initiatorId, String businessKey,
|
|
|
507
|
+ Integer pageNum, Integer pageSize) {
|
|
|
508
|
+ LambdaQueryWrapper<BpmProcessInstance> wrapper = new LambdaQueryWrapper<>();
|
|
|
509
|
+ wrapper.eq(processKey != null, BpmProcessInstance::getProcessKey, processKey)
|
|
|
510
|
+ .eq(status != null, BpmProcessInstance::getStatus, status)
|
|
|
511
|
+ .eq(initiatorId != null, BpmProcessInstance::getInitiatorId, initiatorId)
|
|
|
512
|
+ .eq(businessKey != null, BpmProcessInstance::getBusinessKey, businessKey)
|
|
|
513
|
+ .orderByDesc(BpmProcessInstance::getCreatedAt);
|
|
|
514
|
+
|
|
|
515
|
+ if (pageNum != null && pageSize != null) {
|
|
|
516
|
+ wrapper.last("LIMIT " + pageSize + " OFFSET " + (pageNum - 1) * pageSize);
|
|
|
517
|
+ }
|
|
|
518
|
+ return instanceMapper.selectList(wrapper);
|
|
|
519
|
+ }
|
|
|
520
|
+
|
|
|
521
|
+ // ==================== 内部辅助方法 ====================
|
|
|
522
|
+
|
|
|
523
|
+ private BpmProcessInstance getInstanceByUuid(String instanceUuid) {
|
|
|
524
|
+ LambdaQueryWrapper<BpmProcessInstance> wrapper = new LambdaQueryWrapper<>();
|
|
|
525
|
+ wrapper.eq(BpmProcessInstance::getInstanceId, instanceUuid);
|
|
|
526
|
+ BpmProcessInstance instance = instanceMapper.selectOne(wrapper);
|
|
|
527
|
+ if (instance == null) {
|
|
|
528
|
+ throw new BusinessException("流程实例不存在");
|
|
|
529
|
+ }
|
|
|
530
|
+ return instance;
|
|
89
|
531
|
}
|
|
90
|
532
|
|
|
91
|
|
- /** 查询流程实例 */
|
|
92
|
|
- public BpmProcessInstance getInstance(String instanceId) {
|
|
93
|
|
- return instances.get(instanceId);
|
|
|
533
|
+ private void validateRunning(BpmProcessInstance instance) {
|
|
|
534
|
+ if (!STATUS_RUNNING.equals(instance.getStatus())) {
|
|
|
535
|
+ throw new BusinessException("流程实例不在运行中,当前状态: " + instance.getStatus());
|
|
|
536
|
+ }
|
|
|
537
|
+ }
|
|
|
538
|
+
|
|
|
539
|
+ private void validateAssignee(BpmProcessInstance instance, Long approverId) {
|
|
|
540
|
+ if (instance.getCurrentAssigneeId() != null &&
|
|
|
541
|
+ !instance.getCurrentAssigneeId().equals(approverId)) {
|
|
|
542
|
+ throw new BusinessException("当前审批人无权操作此任务");
|
|
|
543
|
+ }
|
|
|
544
|
+ }
|
|
|
545
|
+
|
|
|
546
|
+ private void completeInstance(BpmProcessInstance instance) {
|
|
|
547
|
+ instance.setStatus(STATUS_COMPLETED);
|
|
|
548
|
+ instance.setCompletedAt(LocalDateTime.now());
|
|
|
549
|
+ if (instance.getStartedAt() != null) {
|
|
|
550
|
+ instance.setDurationSeconds(ChronoUnit.SECONDS.between(instance.getStartedAt(), LocalDateTime.now()));
|
|
|
551
|
+ }
|
|
|
552
|
+ instance.setUpdatedAt(LocalDateTime.now());
|
|
|
553
|
+ instanceMapper.updateById(instance);
|
|
|
554
|
+
|
|
|
555
|
+ // 关闭所有待办
|
|
|
556
|
+ cancelTodoTasks(instance.getId());
|
|
|
557
|
+ }
|
|
|
558
|
+
|
|
|
559
|
+ private BpmProcessNode findStartNode(List<BpmProcessNode> nodes) {
|
|
|
560
|
+ return nodes.stream()
|
|
|
561
|
+ .filter(n -> "start".equals(n.getNodeType()))
|
|
|
562
|
+ .findFirst()
|
|
|
563
|
+ .orElse(nodes.stream()
|
|
|
564
|
+ .min(Comparator.comparingInt(n -> n.getSortOrder() != null ? n.getSortOrder() : 0))
|
|
|
565
|
+ .orElseThrow(() -> new BusinessException("无法找到起始节点")));
|
|
|
566
|
+ }
|
|
|
567
|
+
|
|
|
568
|
+ private BpmProcessNode findNextNode(List<BpmProcessNode> nodes, BpmProcessNode currentNode) {
|
|
|
569
|
+ if (currentNode == null) return null;
|
|
|
570
|
+ int currentSort = currentNode.getSortOrder() != null ? currentNode.getSortOrder() : 0;
|
|
|
571
|
+ return nodes.stream()
|
|
|
572
|
+ .filter(n -> {
|
|
|
573
|
+ int sort = n.getSortOrder() != null ? n.getSortOrder() : 0;
|
|
|
574
|
+ return sort > currentSort && !"start".equals(n.getNodeType());
|
|
|
575
|
+ })
|
|
|
576
|
+ .min(Comparator.comparingInt(n -> n.getSortOrder() != null ? n.getSortOrder() : 0))
|
|
|
577
|
+ .orElse(null);
|
|
|
578
|
+ }
|
|
|
579
|
+
|
|
|
580
|
+ private BpmProcessNode findNodeById(List<BpmProcessNode> nodes, String nodeId) {
|
|
|
581
|
+ if (nodeId == null) return null;
|
|
|
582
|
+ return nodes.stream()
|
|
|
583
|
+ .filter(n -> nodeId.equals(n.getNodeId()))
|
|
|
584
|
+ .findFirst()
|
|
|
585
|
+ .orElse(null);
|
|
|
586
|
+ }
|
|
|
587
|
+
|
|
|
588
|
+ private Long resolveAssignee(BpmProcessNode node, Long fallbackUserId) {
|
|
|
589
|
+ if (node == null || node.getAssigneeValue() == null || node.getAssigneeValue().isEmpty()) {
|
|
|
590
|
+ return fallbackUserId;
|
|
|
591
|
+ }
|
|
|
592
|
+ try {
|
|
|
593
|
+ return Long.parseLong(node.getAssigneeValue());
|
|
|
594
|
+ } catch (NumberFormatException e) {
|
|
|
595
|
+ return fallbackUserId;
|
|
|
596
|
+ }
|
|
|
597
|
+ }
|
|
|
598
|
+
|
|
|
599
|
+ private void createTodoTask(BpmProcessInstance instance, BpmProcessNode node,
|
|
|
600
|
+ Long assigneeId, String assigneeName) {
|
|
|
601
|
+ BpmTodoTask todo = new BpmTodoTask();
|
|
|
602
|
+ todo.setInstanceId(instance.getId());
|
|
|
603
|
+ todo.setInstanceUuid(instance.getInstanceId());
|
|
|
604
|
+ todo.setTitle(instance.getTitle());
|
|
|
605
|
+ todo.setProcessKey(instance.getProcessKey());
|
|
|
606
|
+ todo.setProcessName(instance.getProcessName());
|
|
|
607
|
+ todo.setNodeId(node != null ? node.getNodeId() : "unknown");
|
|
|
608
|
+ todo.setNodeName(node != null ? node.getNodeName() : "未知节点");
|
|
|
609
|
+ todo.setAssigneeId(assigneeId);
|
|
|
610
|
+ todo.setAssigneeName(assigneeName);
|
|
|
611
|
+ todo.setInitiatorId(instance.getInitiatorId());
|
|
|
612
|
+ todo.setInitiatorName(instance.getInitiatorName());
|
|
|
613
|
+ todo.setBusinessKey(instance.getBusinessKey());
|
|
|
614
|
+ todo.setStatus(TODO_PENDING);
|
|
|
615
|
+ todo.setPriority(instance.getPriority() != null ? instance.getPriority() : 0);
|
|
|
616
|
+ todo.setReceivedAt(LocalDateTime.now());
|
|
|
617
|
+ todo.setIsRead(false);
|
|
|
618
|
+ todo.setCreatedAt(LocalDateTime.now());
|
|
|
619
|
+ todo.setUpdatedAt(LocalDateTime.now());
|
|
|
620
|
+
|
|
|
621
|
+ // 设置超时时间
|
|
|
622
|
+ if (node != null && node.getTimeoutHours() != null && node.getTimeoutHours() > 0) {
|
|
|
623
|
+ todo.setDeadlineAt(LocalDateTime.now().plusHours(node.getTimeoutHours()));
|
|
|
624
|
+ }
|
|
|
625
|
+
|
|
|
626
|
+ todoTaskMapper.insert(todo);
|
|
|
627
|
+ }
|
|
|
628
|
+
|
|
|
629
|
+ private void completeTodoTasks(Long instanceId, Long userId) {
|
|
|
630
|
+ LambdaUpdateWrapper<BpmTodoTask> wrapper = new LambdaUpdateWrapper<>();
|
|
|
631
|
+ wrapper.eq(BpmTodoTask::getInstanceId, instanceId)
|
|
|
632
|
+ .eq(BpmTodoTask::getAssigneeId, userId)
|
|
|
633
|
+ .eq(BpmTodoTask::getStatus, TODO_PENDING)
|
|
|
634
|
+ .set(BpmTodoTask::getStatus, TODO_COMPLETED)
|
|
|
635
|
+ .set(BpmTodoTask::getCompletedAt, LocalDateTime.now())
|
|
|
636
|
+ .set(BpmTodoTask::getUpdatedAt, LocalDateTime.now());
|
|
|
637
|
+ todoTaskMapper.update(null, wrapper);
|
|
|
638
|
+ }
|
|
|
639
|
+
|
|
|
640
|
+ private void cancelTodoTasks(Long instanceId) {
|
|
|
641
|
+ LambdaUpdateWrapper<BpmTodoTask> wrapper = new LambdaUpdateWrapper<>();
|
|
|
642
|
+ wrapper.eq(BpmTodoTask::getInstanceId, instanceId)
|
|
|
643
|
+ .eq(BpmTodoTask::getStatus, TODO_PENDING)
|
|
|
644
|
+ .set(BpmTodoTask::getStatus, TODO_CANCELLED)
|
|
|
645
|
+ .set(BpmTodoTask::getCompletedAt, LocalDateTime.now())
|
|
|
646
|
+ .set(BpmTodoTask::getUpdatedAt, LocalDateTime.now());
|
|
|
647
|
+ todoTaskMapper.update(null, wrapper);
|
|
|
648
|
+ }
|
|
|
649
|
+
|
|
|
650
|
+ private BpmApprovalRecord buildApprovalRecord(BpmProcessInstance instance, String nodeId,
|
|
|
651
|
+ Long approverId, String approverName,
|
|
|
652
|
+ String action, String comment) {
|
|
|
653
|
+ BpmApprovalRecord record = new BpmApprovalRecord();
|
|
|
654
|
+ record.setInstanceId(instance.getId());
|
|
|
655
|
+ record.setInstanceUuid(instance.getInstanceId());
|
|
|
656
|
+ record.setNodeId(nodeId != null ? nodeId : instance.getCurrentNodeId());
|
|
|
657
|
+ record.setNodeName(instance.getCurrentNodeName());
|
|
|
658
|
+ record.setApproverId(approverId);
|
|
|
659
|
+ record.setApproverName(approverName);
|
|
|
660
|
+ record.setAction(action);
|
|
|
661
|
+ record.setComment(comment);
|
|
|
662
|
+ record.setApprovedAt(LocalDateTime.now());
|
|
|
663
|
+ record.setCreatedAt(LocalDateTime.now());
|
|
|
664
|
+ return record;
|
|
|
665
|
+ }
|
|
|
666
|
+
|
|
|
667
|
+ private String toJson(Object obj) {
|
|
|
668
|
+ if (obj == null) return null;
|
|
|
669
|
+ try {
|
|
|
670
|
+ return objectMapper.writeValueAsString(obj);
|
|
|
671
|
+ } catch (JsonProcessingException e) {
|
|
|
672
|
+ log.warn("JSON序列化失败: {}", e.getMessage());
|
|
|
673
|
+ return null;
|
|
|
674
|
+ }
|
|
|
675
|
+ }
|
|
|
676
|
+
|
|
|
677
|
+ @SuppressWarnings("unchecked")
|
|
|
678
|
+ private Map<String, Object> fromJson(String json) {
|
|
|
679
|
+ if (json == null || json.isEmpty()) return Collections.emptyMap();
|
|
|
680
|
+ try {
|
|
|
681
|
+ return objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
|
|
|
682
|
+ } catch (JsonProcessingException e) {
|
|
|
683
|
+ log.warn("JSON反序列化失败: {}", e.getMessage());
|
|
|
684
|
+ return Collections.emptyMap();
|
|
|
685
|
+ }
|
|
94
|
686
|
}
|
|
95
|
|
-}
|
|
|
687
|
+}
|