feat: wire process monitor actions

This commit is contained in:
2026-06-18 09:02:54 +08:00
parent 7192c2aaa6
commit a019af8e3a
8 changed files with 267 additions and 0 deletions

View File

@@ -382,6 +382,50 @@ public class FlowController {
return ResponseEntity.ok(resp);
}
@PostMapping("/process/{processInstanceId}/terminate")
@Operation(summary = "结束运行中流程实例",
description = "对齐老 ProcessMonitorComponentNew:流程监控页按实例结束活动流程。仅运行中且有活动任务的流程可结束;"
+ "操作会记录流程评论、设置 approveResult=invalid,并 deleteProcessInstance。")
public ResponseEntity<Map<String, Object>> terminateProcessInstance(@PathVariable String processInstanceId,
@RequestBody(required = false) Requests.ActionRequest req) {
String pid = trimToNull(processInstanceId);
if (pid == null) {
return ResponseEntity.badRequest().body(errorBody("processInstanceId 不能为空"));
}
ProcessInstance pi = runtimeService.createProcessInstanceQuery()
.processInstanceId(pid).singleResult();
if (pi == null) {
return ResponseEntity.status(org.springframework.http.HttpStatus.CONFLICT)
.body(errorBody("该流程已结束"));
}
List<org.flowable.task.api.Task> activeTasks = taskService.createTaskQuery()
.processInstanceId(pid).active().list();
if (activeTasks == null || activeTasks.isEmpty()) {
return ResponseEntity.status(org.springframework.http.HttpStatus.CONFLICT)
.body(errorBody("该流程已结束"));
}
String comment = firstNonBlank(req == null ? null : req.comment, "流程有误作废处理");
for (org.flowable.task.api.Task task : activeTasks) {
if (task.getId() != null) {
taskService.addComment(task.getId(), pid, comment);
}
}
try {
runtimeService.setVariable(pid, "approveResult", "invalid");
} catch (RuntimeException ignore) {
/* 变量写入失败不阻断终止 */
}
String businessKey = pi.getBusinessKey();
runtimeService.deleteProcessInstance(pid, comment);
Map<String, Object> resp = new java.util.LinkedHashMap<>();
resp.put("terminated", true);
resp.put("processEnded", true);
resp.put("processInstanceId", pid);
resp.put("businessKey", businessKey);
resp.put("activeTaskCount", activeTasks.size());
return ResponseEntity.ok(resp);
}
@PostMapping("/tasks/{taskId}/delegate")
@Operation(summary = "委托任务", description = "委托给指定用户:assignee 变为受托人,owner 记原办理人")
public ResponseEntity<TaskDto> delegate(@PathVariable String taskId, @RequestBody Requests.DelegateRequest req) {

View File

@@ -156,6 +156,49 @@ class FlowControllerTest {
verify(historyService, never()).deleteHistoricProcessInstance(anyString());
}
@Test
void 结束运行中流程实例_记录评论并删除实例() throws Exception {
ProcessInstance pi = org.mockito.Mockito.mock(ProcessInstance.class);
when(pi.getBusinessKey()).thenReturn("BK-END");
when(runtimeService.createProcessInstanceQuery()).thenReturn(processInstanceQuery);
when(processInstanceQuery.processInstanceId("PI-END")).thenReturn(processInstanceQuery);
when(processInstanceQuery.singleResult()).thenReturn(pi);
Task task = org.mockito.Mockito.mock(Task.class);
when(task.getId()).thenReturn("T-END");
when(taskService.createTaskQuery()).thenReturn(taskQuery);
when(taskQuery.processInstanceId("PI-END")).thenReturn(taskQuery);
when(taskQuery.active()).thenReturn(taskQuery);
when(taskQuery.list()).thenReturn(java.util.List.of(task));
mvc.perform(post("/api/flow/process/PI-END/terminate")
.contentType("application/json")
.content("{}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.terminated").value(true))
.andExpect(jsonPath("$.businessKey").value("BK-END"))
.andExpect(jsonPath("$.activeTaskCount").value(1));
verify(taskService).addComment("T-END", "PI-END", "流程有误作废处理");
verify(runtimeService).setVariable("PI-END", "approveResult", "invalid");
verify(runtimeService).deleteProcessInstance("PI-END", "流程有误作废处理");
}
@Test
void 结束已结束流程实例_返回409且不删除() throws Exception {
when(runtimeService.createProcessInstanceQuery()).thenReturn(processInstanceQuery);
when(processInstanceQuery.processInstanceId("PI-DONE")).thenReturn(processInstanceQuery);
when(processInstanceQuery.singleResult()).thenReturn(null);
mvc.perform(post("/api/flow/process/PI-DONE/terminate")
.contentType("application/json")
.content("{}"))
.andExpect(status().isConflict())
.andExpect(jsonPath("$.msg").value("该流程已结束"));
verify(runtimeService, never()).deleteProcessInstance(anyString(), anyString());
}
@Test
void 撤回流程_移动回起始节点并挂起实例() throws Exception {
Task task = org.mockito.Mockito.mock(Task.class);