列表引用列 id→显示名:补提交 converter 绑定 + resolver 通用化实现(收尾 662828c)
662828c 仅提交了通用化测试(UserColumnResolverTest),其消息所述的 resolver 实现
与 converter 绑定改动当时漏入暂存区,致 HEAD 处于"新测试 vs 旧实现"不一致态:
committed resolver 仍是"只认 SYS_USER"的 211 行旧版,通用化测试本应红。本次补齐。
resolver(UserColumnResolver,211→341 行):
- 从"只认 SYS_USER + 覆盖写"推广为"任意引用实体"。按 referenceid→SYS_REFERENCE→
目标实体解析目标表/键/显示列(isentitydisplay→isidentifier→物理名称列兜底→主键);
SYS_USER 重定向 VIEW_SYS_USER.DISPLAYNAME。
- 产出 row[col+'_DISPLAY__']=显示名,**保留原始 col=id 不动**(编辑弹窗 data=
selectedItems[0],覆盖原始列会把名当 select 值写回 id 列致数据损坏)。
- 物理列守卫:元数据声明的显示列可能是虚拟列(如 SYS_DEPART.FULLNAME 由 COLUMNSQL
算出,物理表无此列,直接 SELECT 报错)→ 兜底物理 NAME/FULLNAME/TITLE。
- 修正 3 处 NUL 字节污损(prior edit 把空格写成 \0,含 cacheKey 分隔符字面量 '\0'),
git 因之将该文件判为 binary;现复原为空格,文件回到 UTF-8 文本。
converter(applyColumnRenderType + buildColumns):
- default 分支识别真引用列(referenceid 是 36 位 UUID,与引擎 GeneralPO.
get_DisplayValue 的 dt.length()==36 同口径)→ 列表列绑 <COL>_DISPLAY__。
- 源 <COL>_DISPLAY__ 字段保留绑定,不再剥成原始列(那会显示 id)。
- 新增 2 测试:引用列绑定到 displayField、源 _DISPLAY__ 列保留不剥离。
- 分级清单.csv 随新转换器重批(mapping_notes 计数因每引用列多一条绑定 note 上浮)。
验证:resolver 9 测 + converter 54 测全过;运行时 OA_OFFICE_COLLARAPPLY_SEARCH 实证
APPLYNAME=系统管理员[admin]、DEPART_ID=法务合约部(虚拟列兜底)、STORE_ID=lll办公用品0412,
原始 id 全保留(APPLYNAME=ADMIN/DEPART_ID=402881.../STORE_ID=162c2061...)。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -188,9 +188,10 @@ public class TemplateToAmisConverter {
|
||||
String name = f.attributeValue("name", "");
|
||||
Map<String, Object> col = new LinkedHashMap<>();
|
||||
if (name.endsWith("_DISPLAY__")) {
|
||||
String raw = name.substring(0, name.length() - "_DISPLAY__".length());
|
||||
col.put("name", raw);
|
||||
result.note("display@field " + name + " → 原始列 " + raw + "(翻译列,REST 层待补显示值)");
|
||||
// 老模板已显式声明显示列:保留 _DISPLAY__ 绑定,REST 读层(UserColumnResolver)填充显示名。
|
||||
// 不再剥成原始列(那会显示 id)。label 取原始列名去后缀。
|
||||
col.put("name", name);
|
||||
result.note("display@field " + name + " → 保留显示列绑定(REST 层 UserColumnResolver 填显示名)");
|
||||
} else {
|
||||
col.put("name", name);
|
||||
}
|
||||
@@ -302,7 +303,17 @@ public class TemplateToAmisConverter {
|
||||
col.put("map", boolMap);
|
||||
result.note("F2-16 display@field " + name + " referenceid=BOOLEAN → type=mapping(否/是)");
|
||||
}
|
||||
default -> { /* NVARCHAR/CHAR/INTEGER 等普通类型,不注入特殊渲染 */ }
|
||||
default -> {
|
||||
// 真引用列:referenceid 是 36 位 UUID(经 SYS_REFERENCE 指向另一实体),与引擎
|
||||
// GeneralPO.get_DisplayValue 的 dt.length()==36 判定同口径。列表列绑到 <COL>_DISPLAY__,
|
||||
// 由 REST 读层 UserColumnResolver 填目标实体显示名;原始 id 列保留供编辑表单 select。
|
||||
// 其余(NVARCHAR/CHAR/INTEGER/DECIMAL 等类型字面)不注入特殊渲染。
|
||||
if (refId.length() == 36 && refId.indexOf('-') >= 0 && !name.endsWith("_DISPLAY__")) {
|
||||
col.put("name", name + "_DISPLAY__");
|
||||
result.note("display@field " + name + " referenceid=引用(" + refId
|
||||
+ ") → 列绑 " + name + "_DISPLAY__(REST 层 UserColumnResolver 填显示名)");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -650,6 +650,44 @@ class TemplateToAmisConverterTest {
|
||||
assertTrue(r.getMappingNotes().stream().anyMatch(n -> n.contains("F2-16") && n.contains("STATUS")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void 引用列绑定到displayField字段() throws Exception {
|
||||
// 真引用列(referenceid=36 位 UUID)→ 列表列绑 <COL>_DISPLAY__,由 REST 层 UserColumnResolver 填显示名;
|
||||
// 类型字面(NVARCHAR 等)的普通列不动。与引擎 GeneralPO.get_DisplayValue 的 dt.length()==36 同口径。
|
||||
String refUuid = "12345678-1234-1234-1234-123456789abc";
|
||||
String[][] attrRows = {
|
||||
{"DEPART_ID", refUuid, null}, // 真引用列
|
||||
{"TITLE", "NVARCHAR", null}, // 普通类型字面
|
||||
};
|
||||
TemplateToAmisConverter c = converterWithMeta(attrRows, new String[0][]);
|
||||
String xml = "<crud entityid='e1'><display>"
|
||||
+ "<field name='DEPART_ID' text='申请部门'/>"
|
||||
+ "<field name='TITLE' text='标题'/>"
|
||||
+ "</display></crud>";
|
||||
ConversionResult r = c.convertGraded(xml, new ArrayList<>(), noPage);
|
||||
List<Map<String, Object>> cols = (List<Map<String, Object>>) body(r.getSchema()).get("columns");
|
||||
|
||||
assertEquals("DEPART_ID_DISPLAY__", cols.get(0).get("name"), "引用列应绑到 _DISPLAY__ 显示字段");
|
||||
assertEquals("申请部门", cols.get(0).get("label"), "label 不变");
|
||||
assertEquals("TITLE", cols.get(1).get("name"), "普通类型列不动");
|
||||
assertTrue(r.getMappingNotes().stream().anyMatch(n -> n.contains("DEPART_ID_DISPLAY__")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void 源DISPLAY后缀列保留绑定不剥离() throws Exception {
|
||||
// 老模板已显式声明 <COL>_DISPLAY__ 显示列 → 保留绑定(不再剥成原始列致显示 id)
|
||||
TemplateToAmisConverter c = converterWithMeta(new String[0][], new String[0][]);
|
||||
String xml = "<crud entityid='e1'><display>"
|
||||
+ "<field name='USER_ID_DISPLAY__' text='拟稿人'/>"
|
||||
+ "</display></crud>";
|
||||
ConversionResult r = c.convertGraded(xml, new ArrayList<>(), noPage);
|
||||
List<Map<String, Object>> cols = (List<Map<String, Object>>) body(r.getSchema()).get("columns");
|
||||
assertEquals("USER_ID_DISPLAY__", cols.get(0).get("name"), "源 _DISPLAY__ 列应保留绑定");
|
||||
assertEquals("拟稿人", cols.get(0).get("label"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void f2_16_布尔列注入mapping否是() throws Exception {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -2,7 +2,9 @@ package com.eip4.platform.metadata.flow;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -16,196 +18,324 @@ import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
|
||||
/**
|
||||
* 用户引用列后处理器(read-path issue D1=603:列表/_SEARCH 人员列显示 id/工号而非姓名)。
|
||||
* 引用列显示值后处理器(read-path:列表/_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>历史名 UserColumnResolver</b>:最初只解析人员引用列(D1=603 人员列显示工号/UUID)。现已推广为
|
||||
* <b>解析任意引用列</b>(项目/部门/单位/任意业务实体引用),类名与公开 API 为接线稳定保留不改。
|
||||
*
|
||||
* <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>:合成搜索流 {@code <TABLE>_SEARCH} 与命名查询流只把裸列值平展到 amis 行;引用列存的是
|
||||
* 目标实体主键 id,前端遂显示 id。老引擎本有 {@code <COL>_DISPLAY__} 显示值机制({@code GeneralPO.toMap}
|
||||
* 给每列额外算 {@code get_DisplayValue}),但合成搜索流序列化只吐裸值、转换器又把 {@code _DISPLAY__} 列剥成
|
||||
* 原始列——两半从未接通。本处在 REST 读响应层补回:为每个引用列产出 <b>平行字段</b>
|
||||
* {@code <COL>_DISPLAY__}=目标实体显示名,<b>保留原始 {@code <COL>}=id 不动</b>(关键:编辑弹窗用
|
||||
* {@code data="${selectedItems[0]}"} 从列表行取值,若覆盖原始列会把"名"当 select 值写回 id 列致数据损坏)。
|
||||
* 转换器把引用列的列表列绑到 {@code <COL>_DISPLAY__}(见 TemplateToAmisConverter.applyColumnRenderType)。
|
||||
*
|
||||
* <p><b>缓存</b>(与 FlowNameResolver 同 Caffeine 策略):
|
||||
* <p><b>元数据驱动,零误伤</b>:仅当 {@code SYS_ENTITYATTRIBUTE.referenceid} 经 {@code SYS_REFERENCE} 解析到
|
||||
* 真实体表时才视为引用列(伪类型 DATE/TIME/ENUMERATE/STATUS/BOOLEAN 的 referenceid 不是 UUID,join 不到
|
||||
* sys_reference,天然排除)。{@code SYS_USER}/{@code VIEW_SYS_USER} 引用重定向到 {@code VIEW_SYS_USER.DISPLAYNAME}
|
||||
* (形如「罗来伟[130218]」,与 PickerController 同口径);其它实体的显示列按 {@code isentitydisplay=1 →
|
||||
* isidentifier=1 → 回落主键(显示 id)} 解析。
|
||||
*
|
||||
* <p><b>缓存</b>(与 FlowNameResolver 同 Caffeine 策略):
|
||||
* <ul>
|
||||
* <li>{@code userColumnsByTable}:表名 → 用户引用列名集合(避免每次请求查元数据)。</li>
|
||||
* <li>{@code idToDisplay}:用户 id(大写)→ DISPLAYNAME(避免每行查库;批量补缓存)。</li>
|
||||
* <li>{@code refColsByTable}:源表 → {列名 → 目标(表/键/显示列)}。</li>
|
||||
* <li>{@code targetMetaCache}:目标表 → 目标(表/键/显示列)。</li>
|
||||
* <li>{@code displayCache}:{@code 目标表 id(大写)} → 显示名(批量补缓存)。</li>
|
||||
* </ul>
|
||||
* 失效跟 {@link FlowNameResolver#evict} 的 {@code /api/meta/evict} 钩子对齐口径(见 {@link #evictAll})。
|
||||
* 失效跟 {@code /api/meta/evict} 钩子对齐(见 {@link #evictAll})。
|
||||
*/
|
||||
@Component
|
||||
public class UserColumnResolver {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(UserColumnResolver.class);
|
||||
|
||||
/** 引用列在 amis 行里的显示值平行字段后缀(与老引擎 GeneralPOJO.DISPLAY 同名)。 */
|
||||
public static final String DISPLAY_SUFFIX = "_DISPLAY__";
|
||||
|
||||
/** 合法标识符(表/列名)守卫:元数据来源,仍校验防御性拼接逃逸。 */
|
||||
private static final java.util.regex.Pattern IDENT = java.util.regex.Pattern.compile("^[A-Za-z0-9_]+$");
|
||||
|
||||
/** 单批 IN 子句最大占位符数。 */
|
||||
private static final int MAX_IN_BATCH = 1_000;
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
/** 表名(大写)→ 用户引用列名集合(大写)。空集合表示该表无用户引用列(亦缓存,避免重复查元数据)。 */
|
||||
private final Cache<String, Set<String>> userColumnsByTable = Caffeine.newBuilder()
|
||||
/** 源表(大写)→ {引用列名(大写) → 目标}。空 map 亦缓存,避免重复查元数据。 */
|
||||
private final Cache<String, Map<String, RefTarget>> refColsByTable = Caffeine.newBuilder()
|
||||
.maximumSize(2_000)
|
||||
.build();
|
||||
|
||||
/** 用户 id(大写、已去空白)→ DISPLAYNAME(姓名[工号])。未命中的 id 不入缓存,回退原值。 */
|
||||
private final Cache<String, String> idToDisplay = Caffeine.newBuilder()
|
||||
.maximumSize(50_000)
|
||||
/** 目标表(大写)→ 目标元信息(表/键/显示列)。 */
|
||||
private final Cache<String, RefTarget> targetMetaCache = Caffeine.newBuilder()
|
||||
.maximumSize(2_000)
|
||||
.build();
|
||||
|
||||
/** {@code 目标表 id(大写、去空白)} → 显示名。未命中不入缓存,回落原始 id。 */
|
||||
private final Cache<String, String> displayCache = Caffeine.newBuilder()
|
||||
.maximumSize(100_000)
|
||||
.build();
|
||||
|
||||
/** 目标表(大写)→ 物理列名集合(大写)。守卫元数据声明的显示列是否真存在(防虚拟列)。 */
|
||||
private final Cache<String, Set<String>> physColsCache = Caffeine.newBuilder()
|
||||
.maximumSize(2_000)
|
||||
.build();
|
||||
|
||||
/** 显示列兜底:元数据显示列若为虚拟列(物理不存在)时,按此优先级在物理列里找"名称列"。 */
|
||||
private static final String[] NAME_FALLBACKS = {"NAME", "FULLNAME", "TITLE", "DISPLAYNAME", "MC"};
|
||||
|
||||
public UserColumnResolver(JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
/** 引用目标:用于显示值查询的表、主键列、显示列。 */
|
||||
private record RefTarget(String table, String keyColumn, String displayColumn) {}
|
||||
|
||||
/**
|
||||
* 就地解析一批行里「元数据标记为用户引用的列」的 id → 姓名。
|
||||
* 就地为一批行的引用列补显示值字段 {@code <COL>_DISPLAY__}(保留原始 {@code <COL>}=id 不动)。
|
||||
*
|
||||
* @param table 实体表名(任意大小写;null/空 → 直接跳过)
|
||||
* @param rows amis 平面行列表({@code List<Map<String,Object>>},会被就地修改)
|
||||
* @param table 实体表名(任意大小写;null/空 → 跳过)
|
||||
* @param rows amis 平面行列表(就地修改)
|
||||
*/
|
||||
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()) {
|
||||
Map<String, RefTarget> refCols = refColumnsForTable(table);
|
||||
if (refCols.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 1) 收集本批所有待解析 id(大写、去空白),批量补缓存,避免每行查库
|
||||
Set<String> needIds = new HashSet<>();
|
||||
// 1) 按目标表归集本批待解析 id(去缓存命中),批量补缓存避免每行查库
|
||||
Map<String, Set<String>> needByTarget = new HashMap<>();
|
||||
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));
|
||||
for (Map.Entry<String, RefTarget> e : refCols.entrySet()) {
|
||||
String id = normalizeId(row.get(e.getKey()));
|
||||
if (id == null) {
|
||||
continue;
|
||||
}
|
||||
String name = idToDisplay.getIfPresent(id);
|
||||
if (name != null && !name.isBlank()) {
|
||||
row.put(col, name);
|
||||
RefTarget t = e.getValue();
|
||||
if (displayCache.getIfPresent(cacheKey(t.table(), id)) == null) {
|
||||
needByTarget.computeIfAbsent(t.table(), k -> new HashSet<>()).add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Map.Entry<String, Set<String>> e : needByTarget.entrySet()) {
|
||||
RefTarget t = anyTargetFor(refCols, e.getKey());
|
||||
if (t != null) {
|
||||
loadDisplayNames(t, e.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
// 2) 就地补字段:命中 → 显示名;未命中 → 原始 id(列永不空白,优雅降级)
|
||||
for (Map<String, Object> row : rows) {
|
||||
for (Map.Entry<String, RefTarget> e : refCols.entrySet()) {
|
||||
String col = e.getKey();
|
||||
if (!row.containsKey(col)) {
|
||||
continue;
|
||||
}
|
||||
Object raw = row.get(col);
|
||||
String id = normalizeId(raw);
|
||||
if (id == null) {
|
||||
continue;
|
||||
}
|
||||
String name = displayCache.getIfPresent(cacheKey(e.getValue().table(), id));
|
||||
row.put(col + DISPLAY_SUFFIX,
|
||||
name != null && !name.isBlank() ? name : String.valueOf(raw).trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 该表的用户引用列名集合(大写)。按 {@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);
|
||||
/** 取 refCols 中目标表为 targetTable 的任一 RefTarget(同表的 key/display 一致)。 */
|
||||
private RefTarget anyTargetFor(Map<String, RefTarget> refCols, String targetTable) {
|
||||
for (RefTarget t : refCols.values()) {
|
||||
if (t.table().equals(targetTable)) {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Set<String> queryUserColumns(String upperTable) {
|
||||
private static String cacheKey(String targetTable, String idUpper) {
|
||||
return targetTable + ' ' + idUpper;
|
||||
}
|
||||
|
||||
/** 源表 → {引用列 → 目标}。referenceid 经 SYS_REFERENCE 解析到真实体的列才入选。 */
|
||||
private Map<String, RefTarget> refColumnsForTable(String table) {
|
||||
return refColsByTable.get(table.toUpperCase(), this::queryRefColumns);
|
||||
}
|
||||
|
||||
private Map<String, RefTarget> queryRefColumns(String upperTable) {
|
||||
try {
|
||||
List<String> cols = jdbcTemplate.queryForList(
|
||||
"SELECT UPPER(ea.columnname) AS col "
|
||||
List<Map<String, Object>> rows = jdbcTemplate.queryForList(
|
||||
"SELECT UPPER(ea.columnname) AS col, UPPER(e2.tablename) AS target "
|
||||
+ "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();
|
||||
+ "WHERE UPPER(e1.tablename) = ?",
|
||||
upperTable);
|
||||
if (rows.isEmpty()) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
Set<String> set = new HashSet<>(cols);
|
||||
log.debug("[USER-COL] table={} userRefColumns={}", upperTable, set);
|
||||
return set;
|
||||
Map<String, RefTarget> map = new LinkedHashMap<>();
|
||||
for (Map<String, Object> r : rows) {
|
||||
String col = String.valueOf(r.get("col"));
|
||||
String target = String.valueOf(r.get("target"));
|
||||
RefTarget t = targetMetaCache.get(target, this::resolveTargetMeta);
|
||||
if (t != null) {
|
||||
map.put(col, t);
|
||||
}
|
||||
}
|
||||
log.debug("[REF-COL] table={} refColumns={}", upperTable, map.keySet());
|
||||
return map;
|
||||
} catch (org.springframework.dao.DataAccessException ex) {
|
||||
// 元数据查询异常(库不可用等)→ 视作无用户引用列,绝不影响主读路径
|
||||
log.warn("[USER-COL] 解析用户引用列失败 table={}: {}", upperTable, ex.getMessage());
|
||||
// 元数据查询异常 → 视作无引用列,绝不影响主读路径
|
||||
log.warn("[REF-COL] 解析引用列失败 table={}: {}", upperTable, ex.getMessage());
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
}
|
||||
|
||||
/** 目标表 → (表/键/显示列)。SYS_USER 重定向 VIEW_SYS_USER;否则显示列 isentitydisplay→isidentifier→回落主键。 */
|
||||
private RefTarget resolveTargetMeta(String upperTarget) {
|
||||
// 人员引用统一走 VIEW_SYS_USER.DISPLAYNAME(姓名[工号]),与 PickerController 一致
|
||||
if ("SYS_USER".equals(upperTarget) || "VIEW_SYS_USER".equals(upperTarget)) {
|
||||
return new RefTarget("VIEW_SYS_USER", "VIEW_SYS_USER_ID", "DISPLAYNAME");
|
||||
}
|
||||
if (!IDENT.matcher(upperTarget).matches()) {
|
||||
return null;
|
||||
}
|
||||
String keyCol = upperTarget + "_ID";
|
||||
Set<String> phys = physColsCache.get(upperTarget, this::queryPhysicalColumns);
|
||||
String entityDisplayCol = null;
|
||||
String identCol = null;
|
||||
try {
|
||||
List<Map<String, Object>> rows = jdbcTemplate.queryForList(
|
||||
"SELECT UPPER(ea.columnname) AS col, ea.isentitydisplay AS disp, ea.isidentifier AS ident "
|
||||
+ "FROM eip3.sys_entityattribute ea "
|
||||
+ "JOIN eip3.sys_entity e ON UPPER(e.sys_entity_id) = UPPER(ea.entityid) "
|
||||
+ "WHERE UPPER(e.tablename) = ? AND (ea.isentitydisplay = 1 OR ea.isidentifier = 1)",
|
||||
upperTarget);
|
||||
for (Map<String, Object> r : rows) {
|
||||
String col = String.valueOf(r.get("col"));
|
||||
if (entityDisplayCol == null && isOne(r.get("disp"))) {
|
||||
entityDisplayCol = col;
|
||||
}
|
||||
if (identCol == null && isOne(r.get("ident"))) {
|
||||
identCol = col;
|
||||
}
|
||||
}
|
||||
} catch (org.springframework.dao.DataAccessException ex) {
|
||||
log.warn("[REF-COL] 解析目标显示列失败 target={}: {} → 回落物理名称列/主键", upperTarget, ex.getMessage());
|
||||
}
|
||||
|
||||
// 显示列须物理存在(元数据声明的可能是虚拟列,如 SYS_DEPART.FULLNAME 由 COLUMNSQL 算出,
|
||||
// 物理表无此列,直接 SELECT 会报错)。优先级:元数据显示列(物理) → 常见名称列(物理) →
|
||||
// 标识列(物理,非主键) → 主键(显示 id,优雅降级)。phys 为空(查不到物理列)时不守卫,信元数据。
|
||||
String displayCol = keyCol;
|
||||
if (entityDisplayCol != null && physExists(phys, entityDisplayCol)) {
|
||||
displayCol = entityDisplayCol;
|
||||
} else {
|
||||
String nameCol = null;
|
||||
for (String cand : NAME_FALLBACKS) {
|
||||
if (physExists(phys, cand)) {
|
||||
nameCol = cand;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (nameCol != null) {
|
||||
displayCol = nameCol;
|
||||
} else if (identCol != null && !identCol.equalsIgnoreCase(keyCol) && physExists(phys, identCol)) {
|
||||
displayCol = identCol;
|
||||
}
|
||||
}
|
||||
return new RefTarget(upperTarget, keyCol, displayCol);
|
||||
}
|
||||
|
||||
/** phys 为空集(物理列查询失败)→ 视为存在(不守卫,信元数据);否则按集合判定。 */
|
||||
private static boolean physExists(Set<String> phys, String colUpper) {
|
||||
return phys.isEmpty() || phys.contains(colUpper);
|
||||
}
|
||||
|
||||
/** 查目标表物理列名(大写)。失败 → 空集(上层据此跳过物理守卫)。 */
|
||||
private Set<String> queryPhysicalColumns(String upperTable) {
|
||||
try {
|
||||
List<String> cols = jdbcTemplate.queryForList(
|
||||
"SELECT UPPER(column_name) FROM information_schema.columns "
|
||||
+ "WHERE table_schema='eip3' AND UPPER(table_name)=?",
|
||||
String.class, upperTable);
|
||||
return cols.isEmpty() ? Collections.emptySet() : new HashSet<>(cols);
|
||||
} catch (org.springframework.dao.DataAccessException ex) {
|
||||
log.warn("[REF-COL] 查物理列失败 target={}: {} → 不守卫", 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) {
|
||||
/** 批量加载某目标表的 id → 显示名进缓存。表/列名经 IDENT 守卫,id 走绑定参数。失败静默跳过。 */
|
||||
private void loadDisplayNames(RefTarget t, Set<String> ids) {
|
||||
if (!IDENT.matcher(t.table()).matches() || !IDENT.matcher(t.keyColumn()).matches()
|
||||
|| !IDENT.matcher(t.displayColumn()).matches()) {
|
||||
return;
|
||||
}
|
||||
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);
|
||||
loadDisplayBatch(t, all.subList(start, Math.min(start + MAX_IN_BATCH, all.size())));
|
||||
}
|
||||
}
|
||||
|
||||
private void loadDisplayNameBatch(List<String> idBatch) {
|
||||
StringBuilder placeholders = new StringBuilder(idBatch.size() * 2);
|
||||
private void loadDisplayBatch(RefTarget t, List<String> idBatch) {
|
||||
StringBuilder ph = new StringBuilder(idBatch.size() * 2);
|
||||
for (int i = 0; i < idBatch.size(); i++) {
|
||||
if (i > 0) {
|
||||
placeholders.append(',');
|
||||
}
|
||||
placeholders.append('?');
|
||||
ph.append(i > 0 ? ",?" : "?");
|
||||
}
|
||||
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 + ")";
|
||||
String sql = "SELECT UPPER(" + t.keyColumn() + ") AS id, " + t.displayColumn() + " AS disp "
|
||||
+ "FROM eip3." + t.table() + " WHERE UPPER(" + t.keyColumn() + ") IN (" + ph + ")";
|
||||
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);
|
||||
Object idObj = r.get("id");
|
||||
Object disp = r.get("disp");
|
||||
if (idObj == null || disp == null) {
|
||||
continue;
|
||||
}
|
||||
String label = String.valueOf(disp).trim();
|
||||
if (!label.isBlank()) {
|
||||
displayCache.put(cacheKey(t.table(), String.valueOf(idObj).trim().toUpperCase()), label);
|
||||
}
|
||||
}
|
||||
} catch (org.springframework.dao.DataAccessException ex) {
|
||||
log.warn("[USER-COL] 批量加载用户姓名失败: {}", ex.getMessage());
|
||||
log.warn("[REF-COL] 批量加载显示名失败 target={}: {}", t.table(), ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/** id 归一化:null/空白 → null;否则去首尾空白(char 列定长补空格)并大写(与 view_sys_user_id 比较口径一致)。 */
|
||||
private static boolean isOne(Object v) {
|
||||
if (v instanceof Number n) {
|
||||
return n.intValue() == 1;
|
||||
}
|
||||
return v != null && "1".equals(String.valueOf(v).trim());
|
||||
}
|
||||
|
||||
/** id 归一化:null/空白 → null;否则去首尾空白(char 列定长补空格)并大写。 */
|
||||
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();
|
||||
return s.isEmpty() ? null : s.toUpperCase();
|
||||
}
|
||||
|
||||
/** 失效全部缓存(与 /api/meta/evict 钩子对齐:元数据/用户改动后重建)。 */
|
||||
/** 失效全部缓存(与 /api/meta/evict 钩子对齐)。 */
|
||||
public void evictAll() {
|
||||
userColumnsByTable.invalidateAll();
|
||||
idToDisplay.invalidateAll();
|
||||
refColsByTable.invalidateAll();
|
||||
targetMetaCache.invalidateAll();
|
||||
displayCache.invalidateAll();
|
||||
physColsCache.invalidateAll();
|
||||
}
|
||||
|
||||
/** 失效某表的用户引用列缓存(表结构/引用关系变更时)。 */
|
||||
/** 失效某源表的引用列缓存(表结构/引用关系变更时)。 */
|
||||
public void evictTable(String table) {
|
||||
if (table != null && !table.isBlank()) {
|
||||
userColumnsByTable.invalidate(table.toUpperCase());
|
||||
refColsByTable.invalidate(table.toUpperCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user