fix(flow): 搜索时间段过滤——amis date-range 串转 DateTime[] 接通引擎区间查询
Some checks failed
ci / build-test-parity (push) Has been cancelled

操作日志等"时间段查不动"(设了起止仍返回全部)根因:amis input-date(time)-range
把时间段提交为 "起,止" 逗号串,但引擎 GeneralPO 的搜索 where 构建只对 DateTime[]
值生成区间条件 (col>=起 AND col<=止)——收到 String 识别不了,整条时间段过滤被忽略。
且保真层的 coerceByFlowId 只在写流(NAMED_WRITE)调用,读流(SEARCH)根本不 coerce。

修:
- FlowInputCoercer.coerceValue DATE/TIME 支持区间:含逗号 → 拆两段各 parse → DateTime[]
  (空段→null,引擎该侧不加约束);解析失败保留原值(fail-safe)。
- coerceDate 增 10-11 位 unix 秒识别(amis input-date(time)-range 默认 format=X 提交秒),
  原仅认 13 位毫秒;ISO/yyyy-MM-dd[ HH:mm:ss] 等格式不变。
- 新增 coerceSearchDatesByFlowId / coerceSearchDatesByTable:只转 DATE/TIME 列
  (其它过滤参一概不动 → 对已工作的文本/枚举/picker 搜索零回归)。
- FlowController 在 NAMED_READ / SYNTHESIZED(读流)路径调用上述方法。

注:单值日期 → DateTime(引擎 = 精确匹配,忠实老引擎);区间才是常见用法。

live 实证 SYS_DATA_LOG_SEARCH:无过滤 748179,远古区间 45643,2026-06 区间 535,
2026-06-15~18 格式化 53——不同区间返回不同结果(秒/毫秒/格式化字符串均识别)。
FlowInputCoercerTest 43 绿(+6 日期/区间用例)。已重建 platform-app + 重启 8093。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-17 17:57:46 +08:00
parent 3276774037
commit fb8a37ddc5
3 changed files with 151 additions and 3 deletions

View File

@@ -106,6 +106,57 @@ public class FlowInputCoercer {
applyCoerce(map, colTypes); applyCoerce(map, colTypes);
} }
/**
* 读流(SEARCH)日期 coerce:<b>仅</b>把 DATE/TIME 列的 String 值转 {@code DateTime}/{@code DateTime[]}。
*
* <p>amis {@code input-date(time)-range} 把时间段提交为 "起,止" 逗号串,但引擎 GeneralPO 的搜索 where
* 构建只对 {@code DateTime[]} 值生成区间条件 {@code (col>=起 AND col<=止)}——收到 String 则识别不了,
* 整条时间段过滤被忽略(操作日志等"时间段查不动"根因)。本方法把日期列值转成引擎期望形态。
*
* <p><b>只碰 DATE/TIME 列</b>,其它过滤参(文本/枚举/picker)一概不动 → 对已工作的搜索零回归。fail-safe:
* 无表名/查元数据失败/parse 失败 → 原值保留。供 {@code FlowController} 命名读流(NAMED_READ)路径调用。
*/
public void coerceSearchDatesByFlowId(String flowId, Map<String, Object> map) {
if (flowId == null || flowId.isBlank() || map == null || map.isEmpty()) {
return;
}
coerceDatesOnly(map, flowTableCache.get(flowId, this::resolveTableNameByFlowId));
}
/**
* 同 {@link #coerceSearchDatesByFlowId},但表名直接给定——合成 {@code _SEARCH} 流无 contentxml,
* 表名由 key 剥后缀推得({@code FlowController} SYNTHESIZED 路径用)。
*/
public void coerceSearchDatesByTable(String tableName, Map<String, Object> map) {
if (map == null || map.isEmpty()) {
return;
}
coerceDatesOnly(map, tableName);
}
/** 就地:只对 {@code tableName} 里 DATE/TIME 列的 String 值做日期/区间 coerce,其余键不动。 */
private void coerceDatesOnly(Map<String, Object> map, String tableName) {
if (tableName == null || tableName.isEmpty()) {
return;
}
Map<String, String> colTypes = columnTypeCache.get(tableName, this::queryColumnTypes);
if (colTypes.isEmpty()) {
return;
}
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (!(entry.getValue() instanceof String s) || s.isEmpty()) {
continue;
}
String refId = colTypes.get(entry.getKey().toUpperCase());
if ("DATE".equals(refId) || "TIME".equals(refId)) {
Object coerced = coerceValue(s, refId, entry.getKey());
if (coerced != entry.getValue()) {
entry.setValue(coerced);
}
}
}
}
/** /**
* 就地 coerce 写流入参 map:按流 contentxml 里的 tableName 查元数据,把 String 值 * 就地 coerce 写流入参 map:按流 contentxml 里的 tableName 查元数据,把 String 值
* 强制转成引擎期望的 Java 类型(Integer/Boolean/BigDecimal),其余类型不动。 * 强制转成引擎期望的 Java 类型(Integer/Boolean/BigDecimal),其余类型不动。
@@ -426,7 +477,9 @@ public class FlowInputCoercer {
// DATE/TIME 列:引擎 GeneralPO 期望 joda DateTime,amis input-date/datetime 提交字符串 // DATE/TIME 列:引擎 GeneralPO 期望 joda DateTime,amis input-date/datetime 提交字符串
// (ISO/时间戳/yyyy-MM-dd[ HH:mm:ss])。原默认不转 → 引擎直接 cast String→DateTime 抛 // (ISO/时间戳/yyyy-MM-dd[ HH:mm:ss])。原默认不转 → 引擎直接 cast String→DateTime 抛
// ClassCastException,致一切含日期列的表单写流崩。此处 String→joda DateTime(parse 失败保留原值)。 // ClassCastException,致一切含日期列的表单写流崩。此处 String→joda DateTime(parse 失败保留原值)。
return coerceDate(val.trim(), colName); // amis input-date(time)-range 提交 "起,止"(逗号串) → joda DateTime[],引擎 GeneralPO 据此
// 构建区间查询 (col>=起millis AND col<=止millis);单值 → DateTime。
return coerceDateMaybeRange(val.trim(), colName);
} }
default -> { default -> {
// NVARCHAR/CHAR/LIST/BLOB/CODE/UUID-ref 等 → 不动 // NVARCHAR/CHAR/LIST/BLOB/CODE/UUID-ref 等 → 不动
@@ -435,12 +488,44 @@ public class FlowInputCoercer {
} }
} }
/** DATE/TIME 字符串 → joda {@link DateTime}:依次试 毫秒时间戳 → ISO → {@code yyyy-MM-dd HH:mm:ss} → {@code yyyy-MM-dd};都失败保留原值。 */ /**
* DATE/TIME 值 coerce,识别 amis 区间:"起,止"(逗号串) → {@code DateTime[]}(引擎 GeneralPO 构建
* {@code (col>=起 AND col<=止)} 区间查询);单值 → {@code DateTime}。空段 → null(引擎该侧不加约束);
* 两段都解析失败 → 原 String 保留(fail-safe)。
*/
private Object coerceDateMaybeRange(String v, String colName) {
if (v.isEmpty()) {
return v;
}
int comma = v.indexOf(',');
if (comma < 0) {
return coerceDate(v, colName); // 单值
}
String startStr = v.substring(0, comma).trim();
String endStr = v.substring(comma + 1).trim();
Object start = startStr.isEmpty() ? null : coerceDate(startStr, colName);
Object end = endStr.isEmpty() ? null : coerceDate(endStr, colName);
boolean startOk = start == null || start instanceof DateTime;
boolean endOk = end == null || end instanceof DateTime;
if (startOk && endOk && (start != null || end != null)) {
return new DateTime[] { (DateTime) start, (DateTime) end };
}
return v; // 解析失败 → 保留原值
}
/** DATE/TIME 字符串 → joda {@link DateTime}:依次试 秒/毫秒时间戳 → ISO → {@code yyyy-MM-dd HH:mm:ss} → {@code yyyy-MM-dd};都失败保留原值。 */
private Object coerceDate(String v, String colName) { private Object coerceDate(String v, String colName) {
if (v.isEmpty()) { if (v.isEmpty()) {
return v; return v;
} }
if (v.matches("-?\\d{12,}")) { // 13 位左右毫秒时间戳 if (v.matches("-?\\d{10,11}")) { // 10-11 位 → unix 秒(amis input-date(time)-range 默认 format=X)
try {
return new DateTime(Long.parseLong(v) * 1000L);
} catch (RuntimeException ignore) {
// 继续试
}
}
if (v.matches("-?\\d{12,}")) { // 12+ 位 → 毫秒时间戳
try { try {
return new DateTime(Long.parseLong(v)); return new DateTime(Long.parseLong(v));
} catch (RuntimeException ignore) { } catch (RuntimeException ignore) {

View File

@@ -291,6 +291,11 @@ public class FlowController {
// 命名读流:GET/POST 皆可。WS-D D.3:命名读流(如查看新闻查询 OA_NEWSMANAGE_SEARCH_LOOK)按其 // 命名读流:GET/POST 皆可。WS-D D.3:命名读流(如查看新闻查询 OA_NEWSMANAGE_SEARCH_LOOK)按其
// sys_serviceflow.entityid → sys_entity.tablename 解析数据源实体表,使引用列 id→姓名解析生效 // sys_serviceflow.entityid → sys_entity.tablename 解析数据源实体表,使引用列 id→姓名解析生效
// (发布人 USERSEND_ID 等显「-」根因);解析不到 → null,跳过解析(现状行为不变)。 // (发布人 USERSEND_ID 等显「-」根因);解析不到 → null,跳过解析(现状行为不变)。
// 时间段过滤:amis input-date(time)-range 提交 "起,止" 逗号串,引擎需 DateTime[] 才构建区间
// 查询。仅转 DATE/TIME 列(其它过滤参不动,零回归),修「操作日志等时间段查不动」。
if (inputCoercer != null) {
inputCoercer.coerceSearchDatesByFlowId(id, map);
}
Properties ctx = UserSession.getAdmin(); Properties ctx = UserSession.getAdmin();
Object output = flowExcuteService.excute(map, ctx, id); Object output = flowExcuteService.excute(map, ctx, id);
yield ok(output, flowNameResolver.entityTableForNamedFlow(id)); yield ok(output, flowNameResolver.entityTableForNamedFlow(id));
@@ -343,6 +348,10 @@ public class FlowController {
// 直接用原始 flowId(已大写 = TABLE_SEARCH)调引擎。读流,GET/POST 皆可。 // 直接用原始 flowId(已大写 = TABLE_SEARCH)调引擎。读流,GET/POST 皆可。
Properties ctx = UserSession.getAdmin(); Properties ctx = UserSession.getAdmin();
String flowKey = resolved.definition().getKey(); String flowKey = resolved.definition().getKey();
// 时间段过滤同 NAMED_READ:合成流无 contentxml,表名由 key 剥 _SEARCH 后缀得(仅转 DATE/TIME 列)。
if (inputCoercer != null) {
inputCoercer.coerceSearchDatesByTable(tableFromSearchKey(flowKey), map);
}
Object output = flowExcuteService.excute(map, ctx, flowKey); Object output = flowExcuteService.excute(map, ctx, flowKey);
// 合成搜索流的 key = <TABLE>_SEARCH,剥后缀即数据源实体表名,用于用户引用列 id→姓名解析 // 合成搜索流的 key = <TABLE>_SEARCH,剥后缀即数据源实体表名,用于用户引用列 id→姓名解析
yield ok(output, tableFromSearchKey(flowKey)); yield ok(output, tableFromSearchKey(flowKey));

View File

@@ -187,6 +187,60 @@ class FlowInputCoercerTest {
assertEquals("maybe", result); assertEquals("maybe", result);
} }
// ── coerceValue DATE/TIME(单值 + amis 区间 input-date(time)-range)──────────
@Test
@DisplayName("coerceValue: TIME 'yyyy-MM-dd HH:mm:ss' → joda DateTime")
void coerceTimeFormatted() {
Object r = coercer.coerceValue("2026-06-15 17:41:00", "TIME", "UPDATETIME");
org.junit.jupiter.api.Assertions.assertInstanceOf(org.joda.time.DateTime.class, r);
}
@Test
@DisplayName("coerceValue: TIME unix 秒(10位) → DateTime(×1000,amis range 默认 format=X)")
void coerceTimeUnixSeconds() {
Object r = coercer.coerceValue("1749978060", "TIME", "UPDATETIME");
org.junit.jupiter.api.Assertions.assertInstanceOf(org.joda.time.DateTime.class, r);
assertEquals(1749978060_000L, ((org.joda.time.DateTime) r).getMillis(), "秒应 ×1000 转毫秒");
}
@Test
@DisplayName("coerceValue: TIME 区间 '起秒,止秒' → DateTime[](引擎据此构建区间查询)")
void coerceTimeRangeSeconds() {
Object r = coercer.coerceValue("1749978060,1750237260", "TIME", "UPDATETIME");
org.junit.jupiter.api.Assertions.assertInstanceOf(org.joda.time.DateTime[].class, r);
org.joda.time.DateTime[] arr = (org.joda.time.DateTime[]) r;
assertEquals(2, arr.length);
assertEquals(1749978060_000L, arr[0].getMillis());
assertEquals(1750237260_000L, arr[1].getMillis());
}
@Test
@DisplayName("coerceValue: TIME 区间格式化字符串 '起,止' → DateTime[]")
void coerceTimeRangeFormatted() {
Object r = coercer.coerceValue("2026-06-15 00:00:00,2026-06-18 23:59:59", "TIME", "UPDATETIME");
org.junit.jupiter.api.Assertions.assertInstanceOf(org.joda.time.DateTime[].class, r);
org.joda.time.DateTime[] arr = (org.joda.time.DateTime[]) r;
org.junit.jupiter.api.Assertions.assertTrue(arr[0].isBefore(arr[1]));
}
@Test
@DisplayName("coerceValue: TIME 区间一侧空 '起,' → DateTime[]{起, null}(引擎只加下界)")
void coerceTimeRangeOpenEnd() {
Object r = coercer.coerceValue("1749978060,", "TIME", "UPDATETIME");
org.junit.jupiter.api.Assertions.assertInstanceOf(org.joda.time.DateTime[].class, r);
org.joda.time.DateTime[] arr = (org.joda.time.DateTime[]) r;
assertEquals(1749978060_000L, arr[0].getMillis());
assertNull(arr[1], "止侧空 → null(不加上界约束)");
}
@Test
@DisplayName("coerceValue: TIME 区间无法解析 → 原 String 保留(fail-safe)")
void coerceTimeRangeUnparseable() {
Object r = coercer.coerceValue("foo,bar", "TIME", "UPDATETIME");
assertEquals("foo,bar", r);
}
// ── coerce(contentXml, map) ─────────────────────────────────────────────── // ── coerce(contentXml, map) ───────────────────────────────────────────────
@Test @Test