fix(metadata): 引用列 id→显名一致化(业务+审批表单)

UserColumnResolver 新增 resolveFormRecord(主+子表 _DISPLAY__,子表名按子行主键侦测)与 joinUserDisplayNames(多选人员列如接收人显名);单记录表单加载经 FlowController.resolveFormRecord 解析引用列,修「查看页全显 id」。PickerController/FormDefaultsController 对 CHAR 主键去尾空格补白,使 select 默认值与 picker option value 精确匹配显名而非裸 id。PickerController 弹窗按 <display> 忠实复刻老系统多列。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-16 14:08:12 +08:00
parent 4c02019da0
commit cd133aa11d
4 changed files with 215 additions and 3 deletions

View File

@@ -146,6 +146,60 @@ public class UserColumnResolver {
} }
} }
/**
* 单记录表单加载(_LOAD)引用列解析:主表 {@link #resolveRows} + 各 List&lt;Map&gt; 子表行
* (按子行主键列 {@code <子表>_ID} 侦测真实子表名后解析)。修「查看页子表用品名显 id」。
*
* <p>为何不能用约定 {@code <主表>_<字段>} 推子表名:部分主子表不成立——领用申请主表
* {@code OA_OFFICE_COLLARAPPLY}、字段 {@code GOODSDETAIL},约定得 {@code OA_OFFICE_COLLARAPPLY_GOODSDETAIL},
* 实际子表却是 {@code OA_OFFICE_COLLAR_GOODSDETAIL}。改由子行主键列 {@code <子表>_ID} 反解,通用可靠。
*/
@SuppressWarnings("unchecked")
public void resolveFormRecord(String mainTable, Map<String, Object> record) {
if (mainTable == null || mainTable.isBlank() || record == null) {
return;
}
List<Map<String, Object>> mainList = new ArrayList<>();
mainList.add(record);
resolveRows(mainTable, mainList);
for (Object value : record.values()) {
if (value instanceof List<?> sub && !sub.isEmpty() && sub.get(0) instanceof Map) {
List<Map<String, Object>> subRows = new ArrayList<>();
for (Object o : sub) {
if (o instanceof Map<?, ?> r) {
subRows.add((Map<String, Object>) r);
}
}
String subTable = detectSubTable(subRows.get(0));
if (subTable != null) {
resolveRows(subTable, subRows);
}
}
}
}
/**
* 由子行主键列 {@code <子表>_ID} 侦测真实子表名:取 strip {@code _ID} 后「确有引用列可解析」
* ({@link #refColumnsForTable} 非空)的最长候选(最具体=子表本身,非父表/裸引用)。无则 null。
*/
private String detectSubTable(Map<String, Object> sampleRow) {
String best = null;
for (String key : sampleRow.keySet()) {
if (key == null) {
continue;
}
String up = key.toUpperCase();
if (up.endsWith("_ID") && up.length() > "_ID".length()) {
String cand = up.substring(0, up.length() - "_ID".length());
if ((best == null || cand.length() > best.length())
&& !refColumnsForTable(cand).isEmpty()) {
best = cand;
}
}
}
return best;
}
/** 取 refCols 中目标表为 targetTable 的任一 RefTarget(同表的 key/display 一致)。 */ /** 取 refCols 中目标表为 targetTable 的任一 RefTarget(同表的 key/display 一致)。 */
private RefTarget anyTargetFor(Map<String, RefTarget> refCols, String targetTable) { private RefTarget anyTargetFor(Map<String, RefTarget> refCols, String targetTable) {
for (RefTarget t : refCols.values()) { for (RefTarget t : refCols.values()) {
@@ -352,6 +406,52 @@ public class UserColumnResolver {
return s.isEmpty() ? null : s.toUpperCase(); return s.isEmpty() ? null : s.toUpperCase();
} }
/**
* 把一批用户 id 解析为「姓名[工号]」并以「、」连接(审批表单多选人员列如接收人 NOTICEDRAFTPEOPLES 显名用)。
* 该类列存的是用户 id 列表,元数据未必登记为引用列(NVARCHAR 存 id 串),故单值 {@link #resolveRows} 漏掉;
* 这里走 VIEW_SYS_USER.DISPLAYNAME(与人员引用同口径)逐元素解析,未命中元素回落其原始 id(永不空白)。
*/
public String joinUserDisplayNames(java.util.Collection<?> ids) {
if (ids == null || ids.isEmpty()) {
return "";
}
RefTarget userTarget = targetMetaCache.get("SYS_USER", this::resolveTargetMeta); // → VIEW_SYS_USER.DISPLAYNAME
if (userTarget == null) {
return joinRaw(ids);
}
Set<String> need = new HashSet<>();
for (Object o : ids) {
String id = normalizeId(o);
if (id != null && displayCache.getIfPresent(cacheKey(userTarget.table(), id)) == null) {
need.add(id);
}
}
if (!need.isEmpty()) {
loadDisplayNames(userTarget, need);
}
StringBuilder sb = new StringBuilder();
for (Object o : ids) {
String id = normalizeId(o);
String name = id == null ? null : displayCache.getIfPresent(cacheKey(userTarget.table(), id));
if (sb.length() > 0) {
sb.append('、');
}
sb.append(name != null && !name.isBlank() ? name : String.valueOf(o).trim());
}
return sb.toString();
}
private static String joinRaw(java.util.Collection<?> ids) {
StringBuilder sb = new StringBuilder();
for (Object o : ids) {
if (sb.length() > 0) {
sb.append('、');
}
sb.append(String.valueOf(o).trim());
}
return sb.toString();
}
/** 失效全部缓存(与 /api/meta/evict 钩子对齐)。 */ /** 失效全部缓存(与 /api/meta/evict 钩子对齐)。 */
public void evictAll() { public void evictAll() {
refColsByTable.invalidateAll(); refColsByTable.invalidateAll();

View File

@@ -411,7 +411,12 @@ public class FlowController {
if (userColumnResolver == null || table == null) { if (userColumnResolver == null || table == null) {
return; return;
} }
if (data instanceof Map<?, ?> m && m.get("items") instanceof List<?> items) { if (!(data instanceof Map<?, ?> m0)) {
return;
}
Map<String, Object> map = (Map<String, Object>) m0;
// ① 列表/crud 形态 {items:[...]} —— 列表页引用列解析(原逻辑)
if (map.get("items") instanceof List<?> items) {
List<Map<String, Object>> rows = new ArrayList<>(); List<Map<String, Object>> rows = new ArrayList<>();
for (Object o : items) { for (Object o : items) {
if (o instanceof Map<?, ?> row) { if (o instanceof Map<?, ?> row) {
@@ -419,6 +424,39 @@ public class FlowController {
} }
} }
userColumnResolver.resolveRows(table, rows); userColumnResolver.resolveRows(table, rows);
return;
}
// ② 单记录表单加载(查看/编辑 dialog initApi = _LOAD,返回 {result:{主列..., 子表:[行]}} 或裸记录):
// 主表 + 各子表行引用列补 {COL}_DISPLAY__(子表 GOODS_ID 等只读 tpl 列显名),并 trim 引用 id 去 CHAR 补白
// (主表部门/库等 select 默认值与 picker option 精确匹配显名)。修「查看页全显 id」。
Object resultObj = map.get("result");
Map<String, Object> record = resultObj instanceof Map<?, ?> r ? (Map<String, Object>) r : map;
resolveFormRecord(record, table);
}
/** 单记录表单加载引用列解析:委托 {@link UserColumnResolver#resolveFormRecord}(主+子表 _DISPLAY__,
* 子表名按子行主键侦测)+ trim 主记录及各子表行的 CHAR 补白 id(select 默认值匹配 picker option)。 */
@SuppressWarnings("unchecked")
private void resolveFormRecord(Map<String, Object> record, String mainTable) {
userColumnResolver.resolveFormRecord(mainTable, record);
trimCharPadding(record);
for (Object v : record.values()) {
if (v instanceof List<?> sub) {
for (Object o : sub) {
if (o instanceof Map<?, ?> r) {
trimCharPadding((Map<String, Object>) r);
}
}
}
}
}
/** 就地去 Map 内 String 值的尾空格补白(CHAR(36) 主键 PG 以尾空格返回,致 select/比对失配)。 */
private static void trimCharPadding(Map<String, Object> row) {
for (Map.Entry<String, Object> e : row.entrySet()) {
if (e.getValue() instanceof String s && s.length() != s.stripTrailing().length()) {
e.setValue(s.stripTrailing());
}
} }
} }

View File

@@ -91,6 +91,11 @@ public class FormDefaultsController {
continue; continue;
} }
Object resolved = resolveInitValue(t, name, iv, f.attributeValue("format")); Object resolved = resolveInitValue(t, name, iv, f.attributeValue("format"));
// 引用 id({CURRENTDEPART}/{CURRENTUSER} 等解析出的 CHAR 主键)去尾空格补白,
// 与 picker option value(PickerController 已 trim)精确匹配 → 部门/用户 select 显名而非裸 id。
if (resolved instanceof String s) {
resolved = s.trim();
}
if (resolved != null) { if (resolved != null) {
data.put(name.toUpperCase(), resolved); data.put(name.toUpperCase(), resolved);
} }

View File

@@ -169,10 +169,15 @@ public class PickerController {
options.add(opt); options.add(opt);
} }
// 选择窗多列:优先用 dialog(view_template)<display> 里显式声明的列(忠实复刻老系统弹窗列序,
// 如用品选择「用品类别/用品名称/规格/单位/重要性/库存数量」);无模板 display(孤儿模板/直连实体)
// 才回落实体元数据启发式 gridColumns。修「多选弹窗列不对(仅显 名称+裸类别 id)」。
List<Map<String, Object>> displayCols = displayColumnsFromTemplate(entityOrRef);
Map<String, Object> data = new HashMap<>(); Map<String, Object> data = new HashMap<>();
data.put("items", rows); // amis crud/picker 内嵌 CRUD 直接消费 data.put("items", rows); // amis crud/picker 内嵌 CRUD 直接消费
data.put("options", options); // amis select 的 source 直接消费 data.put("options", options); // amis select 的 source 直接消费
data.put("columns", gridColumns(table, info, keyColumn, labelColumn)); // amis picker 内嵌 CRUD 按响应 columns 渲染多列 data.put("columns", displayCols != null ? displayCols
: gridColumns(table, info, keyColumn, labelColumn)); // amis picker 内嵌 CRUD 按响应 columns 渲染多列
data.put("total", result.get(POJOCRUDService.PAGE_COUNT)); data.put("total", result.get(POJOCRUDService.PAGE_COUNT));
Map<String, Object> resp = new HashMap<>(); Map<String, Object> resp = new HashMap<>();
@@ -318,6 +323,62 @@ public class PickerController {
return tableName != null ? tableName.toString() : null; return tableName != null ? tableName.toString() : null;
} }
/**
* 把 {@code dialogid}(= view_template_id)解析为选择窗的「对照列」定义:取该模板 XML 第一个
* {@code <display>}(crud 直接子 / tree 的 gridtemplate 内)下非隐藏 {@code <field>},按
* {@code text}→列标签、{@code name}→列名(与 picker 返回行键一致,大写)忠实复刻老系统弹窗列。
*
* <p>这是「列知识落运行时、转换器零跨模板耦合」契约的落点:转换器只产 source=/api/picker/{dialogid}/list,
* 弹窗到底显哪些列由本端点按 dialog 模板的 {@code <display>} 决定(老系统弹窗列序的权威来源)。
* 非模板 id / 无 display / 无可见 field → 返回 null,调用方回落 {@link #gridColumns} 实体元数据启发式。
*/
private List<Map<String, Object>> displayColumnsFromTemplate(String maybeTemplateId) {
String xml;
try {
List<String> xmls = jdbcTemplate.queryForList(
"SELECT xml::text FROM VIEW_TEMPLATE WHERE VIEW_TEMPLATE_ID = ? AND xml IS NOT NULL",
String.class, maybeTemplateId);
if (xmls.isEmpty()) {
return null;
}
xml = xmls.get(0);
} catch (DataAccessException ex) {
return null;
}
if (xml == null || xml.isBlank()) {
return null;
}
try {
Document doc = DocumentHelper.parseText(xml.trim());
org.dom4j.Element display = (org.dom4j.Element) doc.getRootElement().selectSingleNode(".//display");
if (display == null) {
return null;
}
List<Map<String, Object>> cols = new ArrayList<>();
for (Object o : display.elements("field")) {
org.dom4j.Element fld = (org.dom4j.Element) o;
if ("true".equalsIgnoreCase(fld.attributeValue("hidden"))) {
continue; // 隐藏列(主键等)不作对照列
}
String name = fld.attributeValue("name");
if (name == null || name.isBlank()) {
continue;
}
String text = fld.attributeValue("text");
if (text == null || text.isBlank()) {
text = fld.attributeValue("label");
}
if (text == null || text.isBlank()) {
text = name;
}
cols.add(col(name.toUpperCase(), text));
}
return cols.isEmpty() ? null : cols;
} catch (Exception e) {
return null;
}
}
/** 取模板 XML 里第一个 {@code <tag entityid="...">} 的 entityid(descendant 搜索,含嵌套);无则 null。 */ /** 取模板 XML 里第一个 {@code <tag entityid="...">} 的 entityid(descendant 搜索,含嵌套);无则 null。 */
private static String firstDescendantEntityId(org.dom4j.Element root, String tag) { private static String firstDescendantEntityId(org.dom4j.Element root, String tag) {
for (Object n : root.selectNodes(".//" + tag + "[@entityid]")) { for (Object n : root.selectNodes(".//" + tag + "[@entityid]")) {
@@ -570,10 +631,18 @@ public class PickerController {
private Map<String, Object> toRow(GeneralPOJO p) { private Map<String, Object> toRow(GeneralPOJO p) {
Map<String, Object> row = new LinkedHashMap<>(); Map<String, Object> row = new LinkedHashMap<>();
row.put("_id", p.get_ID()); // CHAR(36) 主键在 PG 里以尾空格补白返回(如 "402881...be1 ")。picker 选项 value/_id 必须去补白,
// 否则与表单默认值(如 {CURRENTDEPART} 解析出的 trim 后部门 id)精确比对失败 → select 显裸 id 不显名。
// 通用修复所有 CHAR 主键引用 select(部门/用户/机构等)。
row.put("_id", trimId(p.get_ID()));
for (int i = 0; i < p.get_ColumnCount(); i++) { for (int i = 0; i < p.get_ColumnCount(); i++) {
row.put(p.get_ColumnName(i), p.get_Value(i)); row.put(p.get_ColumnName(i), p.get_Value(i));
} }
return row; return row;
} }
/** 去 CHAR 列尾空格补白(仅 String 生效;非 String 原样返回)。 */
private static Object trimId(Object v) {
return v instanceof String s ? s.trim() : v;
}
} }