服务流分派: amis ids 批量形态适配——按流 <input> 必填 *_ID 逐 id 扇出执行

修 F2-17 删除/提交审批/文件归档断链:bulkActions 只送 ids=主键逗号串,而老
服务流 <input> 声明单条必填主键(如 OA_NOTICEMANAGE_ID)→透传 ids 必填缺失
500、行未动(API 实证)。NAMED_WRITE 收到 ids 且流声明了缺失的唯一必填 *_ID
字符串入参时,拆逗号逐 id 执行(对齐老系统逐行跑流语义);无声明/歧义→原样
透传。实证:ids 形态 status 0 + isdel=1 落库;e2e f2-17 5/5 过。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-12 14:50:02 +08:00
parent a021f335f3
commit e2c5d43ee3
4 changed files with 225 additions and 0 deletions

View File

@@ -141,6 +141,92 @@ public class FlowInputCoercer {
}
}
/** flowId → 流 {@code <input>} 声明的必填 *_ID 入参名缓存({@code ""} = 无/歧义)。 */
private final Cache<String, String> requiredIdInputCache = Caffeine.newBuilder()
.maximumSize(5_000)
.build();
/** {@code <input>} 块提取。 */
private static final Pattern INPUT_BLOCK_PATTERN = Pattern.compile(
"<input>(.*?)</input>", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
/** input 块内单个 field 标签。 */
private static final Pattern INPUT_FIELD_PATTERN = Pattern.compile(
"<field\\b[^>]*>", Pattern.CASE_INSENSITIVE);
/**
* amis bulkActions 契约适配(F2-17 删除/提交审批/文件归档断链根因):批量按钮只送
* {@code ids}=选中行主键逗号串,而老服务流 {@code <input>} 声明的是单条主键入参
* (如 {@code OA_NOTICEMANAGE_ID}, required)——直接透传 ids 会因必填缺失 500。
*
* <p>返回该流声明的、调用方 map 里没给的必填 {@code *_ID} 字符串入参名;
* 无此入参 / 有多个(歧义不猜)/ map 已显式提供 → null。
*/
public String missingRequiredIdInput(String flowId, Map<String, Object> map) {
if (flowId == null || flowId.isBlank()) {
return null;
}
String field = requiredIdInputCache.get(flowId, this::resolveRequiredIdInput);
if (field.isEmpty() || (map != null && map.containsKey(field))) {
return null;
}
return field;
}
private String resolveRequiredIdInput(String flowId) {
try {
String xml = jdbcTemplate.queryForObject(
"SELECT contentxml::text FROM eip3.sys_serviceflow WHERE sys_serviceflow_id = ?",
String.class, flowId);
String field = extractRequiredIdInput(xml);
return field != null ? field : "";
} catch (org.springframework.dao.EmptyResultDataAccessException e) {
return ""; // 流不存在
} catch (RuntimeException e) {
log.warn("[FLOW-IDS] 查 contentxml 失败 flowId={}: {}", sanitize(flowId), e.getMessage());
return "";
}
}
/**
* 从 contentxml 的 {@code <input>} 块提取必填 {@code *_ID} 字符串入参名。
* 条件:{@code required="true"}、无 {@code initValue}(有初值的不缺)、name 以 _ID 结尾
* (大小写不敏感)。命中 0 个或 ≥2 个(歧义)→ null。
*/
static String extractRequiredIdInput(String contentXml) {
if (contentXml == null || contentXml.isBlank()) {
return null;
}
Matcher block = INPUT_BLOCK_PATTERN.matcher(contentXml);
if (!block.find()) {
return null;
}
String hit = null;
Matcher fields = INPUT_FIELD_PATTERN.matcher(block.group(1));
while (fields.find()) {
String tag = fields.group();
String name = attr(tag, "name");
if (name == null || !name.toUpperCase().endsWith("_ID")) {
continue;
}
if (!"true".equalsIgnoreCase(attr(tag, "required")) || attr(tag, "initValue") != null) {
continue;
}
if (hit != null) {
return null; // 多个必填 *_ID,歧义不猜
}
hit = name;
}
return hit;
}
/** 从单个标签文本里取属性值;无该属性 → null。 */
private static String attr(String tag, String attrName) {
Matcher m = Pattern.compile("\\b" + attrName + "\\s*=\\s*\"([^\"]*)\"", Pattern.CASE_INSENSITIVE)
.matcher(tag);
return m.find() ? m.group(1).trim() : null;
}
/**
* 按 flowId 从 sys_serviceflow 查 contentxml,提取 tableName。
* 找不到 → 返回 {@code ""}(空字符串,会被缓存以避免重查)。

View File

@@ -195,6 +195,31 @@ public class FlowController {
if (!isPost) {
yield forbiddenWriteViaGet(id);
}
// amis bulkActions 契约适配(F2-17 删除/提交审批/文件归档断链根因):
// 批量按钮只送 ids=选中行主键逗号串,而老服务流 <input> 声明的是单条必填主键
// (如 OA_NOTICEMANAGE_ID, required)——透传 ids 会因必填缺失 500。
// 这里拆 ids 逐 id 执行(对齐老系统逐行跑流的语义),其余 body 键原样带上。
// 注意:每次 excute 各自一个引擎事务,部分失败时已执行的 id 不回滚(与老系统逐行点击一致)。
String pkInput = (inputCoercer != null && map.get("ids") instanceof String s && !s.isBlank())
? inputCoercer.missingRequiredIdInput(id, map) : null;
if (pkInput != null) {
Properties fanoutCtx = currentCtx();
List<Object> results = new ArrayList<>();
for (String one : ((String) map.get("ids")).split(",")) {
if (one.isBlank()) {
continue;
}
Map<String, Object> per = new HashMap<>(map);
per.remove("ids");
per.put(pkInput, one.trim());
inputCoercer.coerceByFlowId(id, per);
results.add(flowExcuteService.excute(per, fanoutCtx, id));
}
Map<String, Object> agg = new LinkedHashMap<>();
agg.put("count", results.size());
agg.put("results", results);
yield ok(agg, null);
}
// amis JSON body 全为 String,引擎 GeneralPO.load 按 POJOInfo 列类型直接 cast:
// ENUMERATE/INTEGER/STATUS→Integer、BOOLEAN→Boolean、DECIMAL→BigDecimal。
// coerceByFlowId 按 SYS_ENTITYATTRIBUTE 就地转换,fail-safe 不阻断写路径。