F2-17根因: 列表人员列 id→姓名解析(UserColumnResolver,568列/385实体)
按 SYS_ENTITYATTRIBUTE.referenceid→SYS_REFERENCE→SYS_USER 判定用户引用列, 读路径(_SEARCH)把 id→DISPLAYNAME(Caffeine 双缓存,/api/meta/evict 联动失效); FlowController 3参构造补 @Autowired(多构造器歧义致启动崩溃,运行时实测捕获)。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,211 @@
|
||||
package com.eip4.platform.metadata.flow;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
|
||||
/**
|
||||
* 用户引用列后处理器(read-path issue D1=603:列表/_SEARCH 人员列显示 id/工号而非姓名)。
|
||||
*
|
||||
* <p><b>问题</b>:合成搜索流 {@code <TABLE>_SEARCH} 与命名查询流只把裸列值(用户引用列存的是
|
||||
* {@code SYS_USER} 主键 = {@code view_sys_user_id},如 {@code 402881...} 或字面 {@code ADMIN})
|
||||
* 平展到 amis 行里;前端列表遂显示 id/工号而非姓名。元数据里 {@code _DISPLAY__} 翻译列在转换器
|
||||
* 阶段已被剥离(TemplateToAmisConverter:189,注「REST 层待补显示值」),合成搜索流不返回 display 值,
|
||||
* 故必须在 REST 读响应层把「元数据标记为用户引用的列」的 id → 姓名(口径与 PickerController 一致:
|
||||
* {@code VIEW_SYS_USER.DISPLAYNAME} 形如「罗来伟[130218]」)。
|
||||
*
|
||||
* <p><b>元数据驱动,零误伤</b>:仅当列的 {@code SYS_ENTITYATTRIBUTE.referenceid} 经
|
||||
* {@code SYS_REFERENCE} 解析到 {@code SYS_USER}/{@code VIEW_SYS_USER} 实体时才视为用户引用列
|
||||
* (与引擎 {@code POInfo.getReferenceTable} 同口径,但走自包含元数据查询,便于单测、不依赖 OSGi bean)。
|
||||
* 形如 {@code CREATEBY}/{@code UPDATEBY}(多数实体 referenceid=NVARCHAR,非引用)一律不动。
|
||||
* 全库实测 568 列 / 385 实体属用户引用列,目标实体 OA_NOTICEMANAGE 仅 USER_ID 命中。
|
||||
*
|
||||
* <p><b>缓存</b>(与 FlowNameResolver 同 Caffeine 策略):
|
||||
* <ul>
|
||||
* <li>{@code userColumnsByTable}:表名 → 用户引用列名集合(避免每次请求查元数据)。</li>
|
||||
* <li>{@code idToDisplay}:用户 id(大写)→ DISPLAYNAME(避免每行查库;批量补缓存)。</li>
|
||||
* </ul>
|
||||
* 失效跟 {@link FlowNameResolver#evict} 的 {@code /api/meta/evict} 钩子对齐口径(见 {@link #evictAll})。
|
||||
*/
|
||||
@Component
|
||||
public class UserColumnResolver {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(UserColumnResolver.class);
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
/** 表名(大写)→ 用户引用列名集合(大写)。空集合表示该表无用户引用列(亦缓存,避免重复查元数据)。 */
|
||||
private final Cache<String, Set<String>> userColumnsByTable = Caffeine.newBuilder()
|
||||
.maximumSize(2_000)
|
||||
.build();
|
||||
|
||||
/** 用户 id(大写、已去空白)→ DISPLAYNAME(姓名[工号])。未命中的 id 不入缓存,回退原值。 */
|
||||
private final Cache<String, String> idToDisplay = Caffeine.newBuilder()
|
||||
.maximumSize(50_000)
|
||||
.build();
|
||||
|
||||
public UserColumnResolver(JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 就地解析一批行里「元数据标记为用户引用的列」的 id → 姓名。
|
||||
*
|
||||
* @param table 实体表名(任意大小写;null/空 → 直接跳过)
|
||||
* @param rows amis 平面行列表({@code List<Map<String,Object>>},会被就地修改)
|
||||
*/
|
||||
public void resolveRows(String table, List<Map<String, Object>> rows) {
|
||||
if (table == null || table.isBlank() || rows == null || rows.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Set<String> userCols = userColumnsForTable(table);
|
||||
if (userCols.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 1) 收集本批所有待解析 id(大写、去空白),批量补缓存,避免每行查库
|
||||
Set<String> needIds = new HashSet<>();
|
||||
for (Map<String, Object> row : rows) {
|
||||
for (String col : userCols) {
|
||||
String id = normalizeId(row.get(col));
|
||||
if (id != null && idToDisplay.getIfPresent(id) == null) {
|
||||
needIds.add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!needIds.isEmpty()) {
|
||||
loadDisplayNames(needIds);
|
||||
}
|
||||
|
||||
// 2) 就地替换:命中缓存 → 姓名;未命中 → 保留原值(不破坏数据)
|
||||
for (Map<String, Object> row : rows) {
|
||||
for (String col : userCols) {
|
||||
if (!row.containsKey(col)) {
|
||||
continue;
|
||||
}
|
||||
String id = normalizeId(row.get(col));
|
||||
if (id == null) {
|
||||
continue;
|
||||
}
|
||||
String name = idToDisplay.getIfPresent(id);
|
||||
if (name != null && !name.isBlank()) {
|
||||
row.put(col, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 该表的用户引用列名集合(大写)。按 {@code SYS_ENTITYATTRIBUTE.referenceid → SYS_REFERENCE.entityid →
|
||||
* SYS_ENTITY} 解析,仅保留目标实体为 {@code SYS_USER}/{@code VIEW_SYS_USER} 的列。结果缓存(含空集合)。
|
||||
*/
|
||||
private Set<String> userColumnsForTable(String table) {
|
||||
String key = table.toUpperCase();
|
||||
return userColumnsByTable.get(key, this::queryUserColumns);
|
||||
}
|
||||
|
||||
private Set<String> queryUserColumns(String upperTable) {
|
||||
try {
|
||||
List<String> cols = jdbcTemplate.queryForList(
|
||||
"SELECT UPPER(ea.columnname) AS col "
|
||||
+ "FROM eip3.sys_entityattribute ea "
|
||||
+ "JOIN eip3.sys_entity e1 ON UPPER(e1.sys_entity_id) = UPPER(ea.entityid) "
|
||||
+ "JOIN eip3.sys_reference r ON UPPER(r.sys_reference_id) = UPPER(ea.referenceid) "
|
||||
+ "JOIN eip3.sys_entity e2 ON UPPER(e2.sys_entity_id) = UPPER(r.entityid) "
|
||||
+ "WHERE UPPER(e1.tablename) = ? "
|
||||
+ "AND UPPER(e2.tablename) IN ('SYS_USER', 'VIEW_SYS_USER')",
|
||||
String.class, upperTable);
|
||||
if (cols.isEmpty()) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
Set<String> set = new HashSet<>(cols);
|
||||
log.debug("[USER-COL] table={} userRefColumns={}", upperTable, set);
|
||||
return set;
|
||||
} catch (org.springframework.dao.DataAccessException ex) {
|
||||
// 元数据查询异常(库不可用等)→ 视作无用户引用列,绝不影响主读路径
|
||||
log.warn("[USER-COL] 解析用户引用列失败 table={}: {}", upperTable, ex.getMessage());
|
||||
return Collections.emptySet();
|
||||
}
|
||||
}
|
||||
|
||||
/** 单批 IN 子句最大占位符数(PG 参数上限 65535,留足余量;超出则分批)。 */
|
||||
private static final int MAX_IN_BATCH = 1_000;
|
||||
|
||||
/**
|
||||
* 批量加载 id → DISPLAYNAME 进缓存(按 {@code VIEW_SYS_USER},大小写不敏感匹配 view_sys_user_id)。
|
||||
* 用动态 {@code IN (?,?,...)} 占位符全参数化(id 来自库内数据,仍走绑定不拼值);超过单批上限自动分批。
|
||||
* 失败时静默跳过(保留原 id 值,不破坏读路径)。
|
||||
*/
|
||||
private void loadDisplayNames(Set<String> ids) {
|
||||
List<String> all = new ArrayList<>(ids);
|
||||
for (int start = 0; start < all.size(); start += MAX_IN_BATCH) {
|
||||
List<String> batch = all.subList(start, Math.min(start + MAX_IN_BATCH, all.size()));
|
||||
loadDisplayNameBatch(batch);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadDisplayNameBatch(List<String> idBatch) {
|
||||
StringBuilder placeholders = new StringBuilder(idBatch.size() * 2);
|
||||
for (int i = 0; i < idBatch.size(); i++) {
|
||||
if (i > 0) {
|
||||
placeholders.append(',');
|
||||
}
|
||||
placeholders.append('?');
|
||||
}
|
||||
String sql = "SELECT UPPER(view_sys_user_id) AS id, displayname, name "
|
||||
+ "FROM eip3.view_sys_user "
|
||||
+ "WHERE UPPER(view_sys_user_id) IN (" + placeholders + ")";
|
||||
try {
|
||||
List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql, idBatch.toArray());
|
||||
for (Map<String, Object> r : rows) {
|
||||
String id = r.get("id") != null ? String.valueOf(r.get("id")) : null;
|
||||
Object display = r.get("displayname");
|
||||
Object name = r.get("name");
|
||||
String label = display != null && !String.valueOf(display).isBlank()
|
||||
? String.valueOf(display)
|
||||
: (name != null ? String.valueOf(name) : null);
|
||||
if (id != null && label != null && !label.isBlank()) {
|
||||
idToDisplay.put(id, label);
|
||||
}
|
||||
}
|
||||
} catch (org.springframework.dao.DataAccessException ex) {
|
||||
log.warn("[USER-COL] 批量加载用户姓名失败: {}", ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/** id 归一化:null/空白 → null;否则去首尾空白(char 列定长补空格)并大写(与 view_sys_user_id 比较口径一致)。 */
|
||||
private static String normalizeId(Object raw) {
|
||||
if (raw == null) {
|
||||
return null;
|
||||
}
|
||||
String s = String.valueOf(raw).trim();
|
||||
if (s.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return s.toUpperCase();
|
||||
}
|
||||
|
||||
/** 失效全部缓存(与 /api/meta/evict 钩子对齐:元数据/用户改动后重建)。 */
|
||||
public void evictAll() {
|
||||
userColumnsByTable.invalidateAll();
|
||||
idToDisplay.invalidateAll();
|
||||
}
|
||||
|
||||
/** 失效某表的用户引用列缓存(表结构/引用关系变更时)。 */
|
||||
public void evictTable(String table) {
|
||||
if (table != null && !table.isBlank()) {
|
||||
userColumnsByTable.invalidate(table.toUpperCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import org.springframework.stereotype.Component;
|
||||
import com.dsasp.server.common.dataDic.bean.AbstractPOJOInfo;
|
||||
import com.dsasp.server.common.dataDic.bean.POJOInfo;
|
||||
import com.dsasp.server.dataDic.bean.POInfo;
|
||||
import com.eip4.platform.metadata.flow.UserColumnResolver;
|
||||
import com.eip4.platform.metadata.security.IdentifierWhitelist;
|
||||
|
||||
/**
|
||||
@@ -23,9 +24,12 @@ import com.eip4.platform.metadata.security.IdentifierWhitelist;
|
||||
public class PoInfoCacheInvalidator {
|
||||
|
||||
private final IdentifierWhitelist identifierWhitelist;
|
||||
private final UserColumnResolver userColumnResolver;
|
||||
|
||||
public PoInfoCacheInvalidator(IdentifierWhitelist identifierWhitelist) {
|
||||
public PoInfoCacheInvalidator(IdentifierWhitelist identifierWhitelist,
|
||||
UserColumnResolver userColumnResolver) {
|
||||
this.identifierWhitelist = identifierWhitelist;
|
||||
this.userColumnResolver = userColumnResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,6 +57,8 @@ public class PoInfoCacheInvalidator {
|
||||
// 白名单缓存仍按表名失效(下行),不让引擎侧异常阻断现代层流程。
|
||||
}
|
||||
identifierWhitelist.evict(upper);
|
||||
// read-path D1=603:属性变更可能改动列的 referenceid(用户引用与否),同步失效用户引用列缓存
|
||||
userColumnResolver.evictTable(upper);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,8 +74,11 @@ public class PoInfoCacheInvalidator {
|
||||
}
|
||||
if (table != null && !table.isBlank()) {
|
||||
identifierWhitelist.evict(table);
|
||||
userColumnResolver.evictTable(table);
|
||||
} else {
|
||||
identifierWhitelist.evictAll();
|
||||
// read-path D1=603:表名不可查的粗粒度变更 → 全量失效用户引用列缓存
|
||||
userColumnResolver.evictAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import com.dsasp.server.common.dataDic.service.POJOCRUDService;
|
||||
import com.dsasp.server.common.util.UserSession;
|
||||
import com.eip4.platform.metadata.flow.FlowNameResolver;
|
||||
import com.eip4.platform.metadata.flow.FlowNameResolver.ResolvedFlow;
|
||||
import com.eip4.platform.metadata.flow.UserColumnResolver;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
|
||||
@@ -56,10 +57,21 @@ public class FlowController {
|
||||
|
||||
private final FlowExcuteService flowExcuteService;
|
||||
private final FlowNameResolver flowNameResolver;
|
||||
private final UserColumnResolver userColumnResolver;
|
||||
|
||||
public FlowController(FlowExcuteService flowExcuteService, FlowNameResolver flowNameResolver) {
|
||||
/** Spring 注入入口:带用户引用列解析器(read-path issue D1=603 修复)。
|
||||
* 多构造器场景须显式 @Autowired 指明注入构造,否则 Spring 回退无参构造致启动失败。 */
|
||||
@org.springframework.beans.factory.annotation.Autowired
|
||||
public FlowController(FlowExcuteService flowExcuteService, FlowNameResolver flowNameResolver,
|
||||
UserColumnResolver userColumnResolver) {
|
||||
this.flowExcuteService = flowExcuteService;
|
||||
this.flowNameResolver = flowNameResolver;
|
||||
this.userColumnResolver = userColumnResolver;
|
||||
}
|
||||
|
||||
/** 兼容老单测的两参构造(无用户列解析,行为同未修复前)。生产由 Spring 走三参构造。 */
|
||||
public FlowController(FlowExcuteService flowExcuteService, FlowNameResolver flowNameResolver) {
|
||||
this(flowExcuteService, flowNameResolver, null);
|
||||
}
|
||||
|
||||
/** amis crud 默认每页条数(与前端 amis 默认 perPage 一致)。 */
|
||||
@@ -136,18 +148,21 @@ public class FlowController {
|
||||
|
||||
return switch (resolved.type()) {
|
||||
case UUID_PASSTHROUGH, NAMED_HIT -> {
|
||||
// UUID 或命名命中:直接走引擎(引擎按 PK 查 sys_serviceflow)
|
||||
// UUID 或命名命中:直接走引擎(引擎按 PK 查 sys_serviceflow)。
|
||||
// 非合成路径无法稳妥推断实体表,故不做用户列解析(table=null)。
|
||||
Properties ctx = UserSession.getAdmin();
|
||||
Object output = flowExcuteService.excute(map, ctx, id);
|
||||
yield ok(output);
|
||||
yield ok(output, null);
|
||||
}
|
||||
case SYNTHESIZED -> {
|
||||
// _SEARCH 合成:合成流已注册到 SynthesizedFlowRegistry,
|
||||
// FlowServiceWithSynthesis.getFlowDefinition 会按 key 命中,
|
||||
// 直接用原始 flowId(已大写 = TABLE_SEARCH)调引擎。
|
||||
Properties ctx = UserSession.getAdmin();
|
||||
Object output = flowExcuteService.excute(map, ctx, resolved.definition().getKey());
|
||||
yield ok(output);
|
||||
String flowKey = resolved.definition().getKey();
|
||||
Object output = flowExcuteService.excute(map, ctx, flowKey);
|
||||
// 合成搜索流的 key = <TABLE>_SEARCH,剥后缀即数据源实体表名,用于用户引用列 id→姓名解析
|
||||
yield ok(output, tableFromSearchKey(flowKey));
|
||||
}
|
||||
case WRITE_REDIRECT -> {
|
||||
Map<String, Object> err = new LinkedHashMap<>();
|
||||
@@ -164,14 +179,52 @@ public class FlowController {
|
||||
};
|
||||
}
|
||||
|
||||
private ResponseEntity<Map<String, Object>> ok(Object output) {
|
||||
private ResponseEntity<Map<String, Object>> ok(Object output, String table) {
|
||||
Object data = unwrapCrudResult(normalize(output));
|
||||
// read-path issue D1=603:把元数据标记为用户引用的列 id/工号 → 姓名(仅在已知数据源实体的合成搜索流上)
|
||||
resolveUserColumns(data, table);
|
||||
Map<String, Object> resp = new LinkedHashMap<>();
|
||||
resp.put("status", 0);
|
||||
resp.put("msg", "");
|
||||
resp.put("data", unwrapCrudResult(normalize(output)));
|
||||
resp.put("data", data);
|
||||
return ResponseEntity.ok(resp);
|
||||
}
|
||||
|
||||
/**
|
||||
* 合成搜索流 key({@code <TABLE>_SEARCH})→ 数据源实体表名(剥 {@code _SEARCH} 后缀)。
|
||||
* 非 {@code _SEARCH} 形态(不应发生在 SYNTHESIZED 路径)→ null,跳过用户列解析。
|
||||
*/
|
||||
private static String tableFromSearchKey(String flowKey) {
|
||||
if (flowKey == null) {
|
||||
return null;
|
||||
}
|
||||
String upper = flowKey.toUpperCase();
|
||||
if (upper.endsWith("_SEARCH")) {
|
||||
return upper.substring(0, upper.length() - "_SEARCH".length());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对归一化后的 amis data 应用用户引用列解析(id→姓名)。仅处理 crud 形态 {@code {items:[...]}}
|
||||
* 的行集;解析器为空(两参构造的老单测)或 table 缺省时静默跳过。对非用户列零影响。
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void resolveUserColumns(Object data, String table) {
|
||||
if (userColumnResolver == null || table == null) {
|
||||
return;
|
||||
}
|
||||
if (data instanceof Map<?, ?> m && m.get("items") instanceof List<?> items) {
|
||||
List<Map<String, Object>> rows = new ArrayList<>();
|
||||
for (Object o : items) {
|
||||
if (o instanceof Map<?, ?> row) {
|
||||
rows.add((Map<String, Object>) row);
|
||||
}
|
||||
}
|
||||
userColumnResolver.resolveRows(table, rows);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* F2-14b:流输出变量名为 result(DefaultFlowHelper 默认搜索流口径),查询结果被包成
|
||||
* {result:{items,total}},而 amis crud 契约读 data.items/data.total——一层之差导致
|
||||
|
||||
Reference in New Issue
Block a user