fix(flow): restore workflow task converted pages

This commit is contained in:
2026-06-17 20:44:52 +08:00
parent 5946afa6e6
commit 57c3412aff
6 changed files with 689 additions and 15 deletions

View File

@@ -1,10 +1,23 @@
package com.eip4.platform.flowgateway.web;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.springframework.dao.DataAccessException;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -45,15 +58,22 @@ import io.swagger.v3.oas.annotations.tags.Tag;
@RestController
public class TodoController {
private static final int DEFAULT_AMIS_PER_PAGE = 10;
private static final int LEGACY_TASK_FETCH_LIMIT = 1_000;
private static final DateTimeFormatter LEGACY_DATE_TIME =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private final TodoAggregator todoAggregator;
private final FlowableClient flowableClient;
private final StatusWriteback statusWriteback;
private final JdbcTemplate jdbcTemplate;
public TodoController(TodoAggregator todoAggregator, FlowableClient flowableClient,
StatusWriteback statusWriteback) {
StatusWriteback statusWriteback, JdbcTemplate jdbcTemplate) {
this.todoAggregator = todoAggregator;
this.flowableClient = flowableClient;
this.statusWriteback = statusWriteback;
this.jdbcTemplate = jdbcTemplate;
}
@Operation(summary = "当前用户待办(含候选组待办)",
@@ -76,6 +96,42 @@ public class TodoController {
return todoAggregator.done(userId, firstResult, maxResults);
}
/**
* 转换页「流程管理/待办任务」兼容端点。
*
* <p>老模板列绑定的是 {@code VIEW_ACT_RU_TASK_*} 视图字段,但当前有效待办数据源是
* {@link TodoAggregator}。这里保持老列名与查询字段不变,底层改走 Flowable 聚合,供 amis CRUD 直接消费。
*/
@Operation(summary = "转换页待办任务数据(amis)", description = "返回老 VIEW_ACT_RU_TASK 字段形态,底层走 /api/todo 聚合口径")
@GetMapping("/api/workflow/tasks/todo-amis")
public Map<String, Object> todoAmis(@AuthenticationPrincipal AuthPrincipal principal,
@RequestParam Map<String, String> params) {
String userId = principal == null ? null : principal.userId();
List<String> candidateGroups = principal == null ? List.of() : principal.roles();
List<TodoItem> raw = todoAggregator.todo(userId, candidateGroups, 0, LEGACY_TASK_FETCH_LIMIT);
List<Map<String, Object>> rows = raw.stream()
.map(item -> legacyTaskRow(item, false))
.filter(row -> matchesLegacyFilters(row, params, false))
.toList();
resolveInitiatorDisplayNames(rows);
return amisCrud(rows, params);
}
/** 转换页「流程管理/已办任务」兼容端点;列名对齐 VIEW_ACT_TASK_COMPLETE_SEARCH/VIEW_ACT_FTASK_COMPLETE_SEARCH。 */
@Operation(summary = "转换页已办任务数据(amis)", description = "返回老 VIEW_ACT_TASK 字段形态,底层走 /api/done 聚合口径")
@GetMapping("/api/workflow/tasks/done-amis")
public Map<String, Object> doneAmis(@AuthenticationPrincipal AuthPrincipal principal,
@RequestParam Map<String, String> params) {
String userId = principal == null ? null : principal.userId();
List<TodoItem> raw = todoAggregator.done(userId, 0, LEGACY_TASK_FETCH_LIMIT);
List<Map<String, Object>> rows = raw.stream()
.map(item -> legacyTaskRow(item, true))
.filter(row -> matchesLegacyFilters(row, params, true))
.toList();
resolveInitiatorDisplayNames(rows);
return amisCrud(rows, params);
}
@Operation(summary = "办理任务 + 回写业务状态",
description = "complete 后,若请求带 businessKey/processInstanceId,按老系统 ProcessCoreServiceImpl 语义回写 SYS_DATASTATUS(结束→3 通过,未结束→2 处理中,守卫单调递增)。挂起任务被引擎拒为 409(透传)")
@PostMapping("/api/task/{id}/complete")
@@ -143,6 +199,258 @@ public class TodoController {
return pi == null || pi.ended;
}
private static Map<String, Object> amisCrud(List<Map<String, Object>> rows, Map<String, String> params) {
int page = parsePositive(params.get("page"), 1);
int perPage = parsePositive(params.get("perPage"), DEFAULT_AMIS_PER_PAGE);
int from = Math.min((page - 1) * perPage, rows.size());
int to = Math.min(from + perPage, rows.size());
Map<String, Object> data = new LinkedHashMap<>();
data.put("items", rows.subList(from, to));
data.put("total", rows.size());
Map<String, Object> resp = new LinkedHashMap<>();
resp.put("status", 0);
resp.put("msg", "");
resp.put("data", data);
return resp;
}
private static Map<String, Object> legacyTaskRow(TodoItem item, boolean done) {
Map<String, Object> row = new LinkedHashMap<>();
String taskId = trim(item.taskId);
if (done) {
row.put("VIEW_ACT_TASK_ID", taskId);
row.put("VIEW_ACT_FTASK_ID", taskId);
} else {
row.put("VIEW_ACT_RU_TASK_ID", taskId);
}
row.put("TASKID", taskId);
row.put("TITLE", titleOf(item));
row.put("PROCESSNAME", processName(item.processDefinitionId));
row.put("TASKNAME", item.taskName);
row.put("STARTTIME", formatMillis(item.createTimeMillis));
row.put("ENDTIME", formatMillis(item.endTimeMillis));
row.put("WFSTATUS", done ? 3 : 2);
String initUser = summaryString(item, "USER_ID", "USERID", "INITUSERID");
row.put("INITUSERID", initUser);
row.put("INITUSERID_DISPLAY__", initUser);
String initOrg = summaryString(item, "USERORGID", "INITUSERORGID", "ORGID");
row.put("INITUSERORGID", initOrg);
row.put("INITUSERORGID_DISPLAY__", initOrg);
row.put("processInstanceId", item.processInstanceId);
row.put("businessKey", item.businessKey);
row.put("businessTable", item.businessTable);
row.put("businessSummary", item.businessSummary == null ? Map.of() : item.businessSummary);
row.put("pageTemplateId", item.pageTemplateId);
row.put("pageUrl", item.pageUrl);
row.put("pageUrlReason", item.pageUrlReason);
return row;
}
private void resolveInitiatorDisplayNames(List<Map<String, Object>> rows) {
Set<String> userIds = new LinkedHashSet<>();
for (Map<String, Object> row : rows) {
String userId = trim(Objects.toString(row.get("INITUSERID"), ""));
if (userId != null && !userId.isBlank()) {
userIds.add(userId);
}
}
if (userIds.isEmpty()) {
return;
}
Map<String, String> displayById = new HashMap<>();
List<String> ids = new ArrayList<>(userIds);
for (int from = 0; from < ids.size(); from += 200) {
List<String> batch = ids.subList(from, Math.min(from + 200, ids.size()));
String placeholders = String.join(",", Collections.nCopies(batch.size(), "?"));
try {
List<Map<String, Object>> matches = jdbcTemplate.queryForList(
"SELECT TRIM(VIEW_SYS_USER_ID) AS USER_ID, DISPLAYNAME AS DISPLAY_NAME "
+ "FROM VIEW_SYS_USER WHERE TRIM(VIEW_SYS_USER_ID) IN (" + placeholders + ")",
batch.toArray());
if (matches == null) {
continue;
}
for (Map<String, Object> match : matches) {
String userId = rowString(match, "USER_ID", "user_id", "ID", "id");
String displayName = rowString(match, "DISPLAY_NAME", "display_name", "DISPLAYNAME", "displayname");
if (userId != null && !userId.isBlank() && displayName != null && !displayName.isBlank()) {
displayById.put(userId, displayName);
}
}
} catch (DataAccessException ignored) {
return;
}
}
for (Map<String, Object> row : rows) {
String userId = trim(Objects.toString(row.get("INITUSERID"), ""));
String displayName = displayById.get(userId);
if (displayName != null && !displayName.isBlank()) {
row.put("INITUSERID_DISPLAY__", displayName);
}
}
}
private static boolean matchesLegacyFilters(Map<String, Object> row, Map<String, String> params, boolean done) {
return contains(row, params, "TITLE")
&& contains(row, params, "PROCESSNAME")
&& contains(row, params, "TASKNAME")
&& equalsIfPresent(row, params, "INITUSERID")
&& equalsIfPresent(row, params, "INITUSERORGID")
&& inDateRange(row.get("STARTTIME"), params.get("STARTTIME"))
&& (!done || inDateRange(row.get("ENDTIME"), params.get("ENDTIME")))
&& (!done || equalsIfPresent(row, params, "WFSTATUS"));
}
private static boolean contains(Map<String, Object> row, Map<String, String> params, String key) {
String needle = trim(params.get(key));
if (needle == null || needle.isBlank()) {
return true;
}
String value = trim(Objects.toString(row.get(key), ""));
return value != null && value.toLowerCase().contains(needle.toLowerCase());
}
private static boolean equalsIfPresent(Map<String, Object> row, Map<String, String> params, String key) {
String expected = trim(params.get(key));
if (expected == null || expected.isBlank()) {
return true;
}
return expected.equals(trim(Objects.toString(row.get(key), "")));
}
private static boolean inDateRange(Object raw, String range) {
String value = trim(Objects.toString(raw, ""));
String trimmedRange = trim(range);
if (trimmedRange == null || trimmedRange.isBlank() || value == null || value.isBlank()) {
return true;
}
if (trimmedRange.startsWith("[") && trimmedRange.endsWith("]")) {
trimmedRange = trimmedRange.substring(1, trimmedRange.length() - 1);
}
LocalDateTime actual = parseDateTime(value);
if (actual == null) {
return true;
}
String[] parts = trimmedRange.split(",", -1);
LocalDateTime start = parts.length > 0 ? parseDateTime(parts[0]) : null;
LocalDateTime end = parts.length > 1 ? parseDateTime(parts[1]) : null;
return (start == null || !actual.isBefore(start)) && (end == null || !actual.isAfter(end));
}
private static LocalDateTime parseDateTime(String raw) {
String s = trim(raw);
if (s == null || s.isBlank()) {
return null;
}
s = stripDateToken(s);
if (s.matches("-?\\d+")) {
try {
long epoch = Long.parseLong(s);
long millis = Math.abs(epoch) < 10_000_000_000L ? epoch * 1_000L : epoch;
return LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneId.systemDefault());
} catch (NumberFormatException ignored) {
return null;
}
}
try {
return LocalDateTime.parse(s.replace('T', ' ').replaceAll("Z$", ""), LEGACY_DATE_TIME);
} catch (DateTimeParseException ignored) {
try {
return LocalDateTime.parse(s);
} catch (DateTimeParseException ignoredAgain) {
return null;
}
}
}
private static String stripDateToken(String raw) {
String s = trim(raw);
if (s == null || s.length() < 2) {
return s;
}
if ((s.startsWith("\"") && s.endsWith("\"")) || (s.startsWith("'") && s.endsWith("'"))) {
return trim(s.substring(1, s.length() - 1));
}
return s;
}
private static String titleOf(TodoItem item) {
if (item.businessSummary != null) {
for (String key : List.of("TITLE", "NAME", "SUBJECT", "NO", "SERIALNO", "FORMNO")) {
String value = summaryString(item, key);
if (value != null && !value.isBlank()) {
return value;
}
}
}
if (item.businessKey != null && !item.businessKey.isBlank()) {
return item.businessKey;
}
return item.taskName;
}
private static String summaryString(TodoItem item, String... keys) {
if (item.businessSummary == null || item.businessSummary.isEmpty()) {
return null;
}
for (String key : keys) {
Object value = item.businessSummary.get(key);
if (value == null) {
value = item.businessSummary.get(key.toLowerCase());
}
String s = trim(Objects.toString(value, ""));
if (s != null && !s.isBlank()) {
return s;
}
}
return null;
}
private static String rowString(Map<String, Object> row, String... keys) {
for (String key : keys) {
Object value = row.get(key);
String s = trim(Objects.toString(value, ""));
if (s != null && !s.isBlank()) {
return s;
}
}
return null;
}
private static String processName(String processDefinitionId) {
String s = trim(processDefinitionId);
if (s == null || s.isBlank()) {
return "";
}
int colon = s.indexOf(':');
return colon > 0 ? s.substring(0, colon) : s;
}
private static String formatMillis(Long millis) {
if (millis == null) {
return null;
}
return LEGACY_DATE_TIME.format(Instant.ofEpochMilli(millis).atZone(ZoneId.systemDefault()));
}
private static int parsePositive(String raw, int fallback) {
try {
int value = Integer.parseInt(Objects.toString(raw, "").trim());
return value > 0 ? value : fallback;
} catch (NumberFormatException e) {
return fallback;
}
}
private static String trim(String value) {
return value == null ? null : value.strip();
}
private static boolean notBlank(String s) {
return s != null && !s.isBlank();
}