feat(metadata): 实体加载按 pageId 把引用字段解析为 {_id,_label} 对象 + 保存拍回裸 id

EntityController.load 加可选 ?pageId=:读 view_page(pageId) 字段,把 uitype=SelectDialog/MultiSelectDialog(带 dialogid)字段值解析成 {_id,_label} 对象(multi→对象数组),label 由各字段 dialogid 经 PickerController.resolveValuesToOptions 同源解析(口径=老 DialogDisplayUtil);type=3 树 dialog(转 tree-select 吃裸id)按 isTreeDialog 跳过不转对象防 undefined;_id 去尾补白。pageId 空→行为不变(回归)。PickerController.resolveValuesToOptions 提包级可见供复用(端点行为不变)。FlowInputCoercer.flattenPickerObjects:写流入参里 {_id,...} 形态值拍回裸 _id(去尾补白)——amis picker「载入后不动直接保存」会把整个对象进 payload,后端统一扁平守住提交值=id(零前端改动)。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-16 17:58:42 +08:00
parent 4468616e88
commit a19bcdbf67
5 changed files with 370 additions and 14 deletions

View File

@@ -89,6 +89,11 @@ public class FlowInputCoercer {
if (flowId == null || flowId.isBlank() || map == null || map.isEmpty()) {
return;
}
// 引用 picker 对象值扁平化:master-detail/编辑表单经 form initApi 把引用字段载为 {_id,_label}
// 对象(picker 自带 label 即显名)。amis picker 仅在用户「重新选值」时按 valueField=_id 提取裸 id;
// 「载入后不动直接保存」时整个对象原样进 payload → 引擎收到 Map 而非裸 id 必坏。故写流前统一把
// {_id,...} 形态对象就地降为其 _id 裸值(与 valueField=_id 提交语义一致),与表名解析无关先行。
flattenPickerObjects(map);
// 先从缓存取 tableName;缓存未命中则查 contentxml
String tableName = flowTableCache.get(flowId, this::resolveTableNameByFlowId);
if (tableName.isEmpty()) {
@@ -114,6 +119,8 @@ public class FlowInputCoercer {
if (contentXml == null || contentXml.isBlank() || map == null || map.isEmpty()) {
return;
}
// 引用 picker {_id,_label} 对象 → 裸 _id(见 coerceByFlowId 注释;与表名解析无关先行)
flattenPickerObjects(map);
String tableName = extractTableName(contentXml);
if (tableName == null) {
return; // 无固定 tableName(如跨表流),不做 coerce
@@ -125,6 +132,26 @@ public class FlowInputCoercer {
applyCoerce(map, colTypes);
}
/**
* 引用 picker 对象值就地扁平化:把 {@code {_id,_label}} 形态的 Map 值降为其 {@code _id} 裸值
* (与 amis picker {@code valueField=_id} 提交语义一致)。仅处理「含 {@code _id} 键的 Map」,
* 其它结构化值(数组/普通 Map)不动;{@code _id} 为 String 时去尾补白(CHAR 主键对齐)。
*
* <p>背景:master-detail 右编辑表单经 {@code form initApi} 把引用字段载为对象(picker 自带 label
* 即显名,spike 实证只此可靠显名);「载入后不改直接保存」时 amis 不触发 valueField 提取 → 整个对象
* 进 payload。本扁平化是后端唯一收口点(零前端改动),保证引擎收到的引用列恒为裸 id。fail-safe:
* 任何异常都不抛、不阻断写。
*/
static void flattenPickerObjects(Map<String, Object> map) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
Object val = entry.getValue();
if (val instanceof Map<?, ?> obj && obj.containsKey("_id")) {
Object id = obj.get("_id");
entry.setValue(id instanceof String s ? s.stripTrailing() : id);
}
}
}
/** 遍历 map,按 colTypes 就地强制转换 String 值为目标 Java 类型。 */
private void applyCoerce(Map<String, Object> map, Map<String, String> colTypes) {
for (Map.Entry<String, Object> entry : map.entrySet()) {

View File

@@ -8,6 +8,11 @@ import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -37,11 +42,17 @@ public class EntityController {
private final POJOCRUDService pojoCRUDService;
private final IdentifierWhitelist identifierWhitelist;
private final JdbcTemplate jdbcTemplate;
private final PickerController pickerController;
public EntityController(POJOCRUDService pojoCRUDService,
IdentifierWhitelist identifierWhitelist) {
IdentifierWhitelist identifierWhitelist,
JdbcTemplate jdbcTemplate,
PickerController pickerController) {
this.pojoCRUDService = pojoCRUDService;
this.identifierWhitelist = identifierWhitelist;
this.jdbcTemplate = jdbcTemplate;
this.pickerController = pickerController;
}
/**
@@ -97,15 +108,35 @@ public class EntityController {
return resp;
}
/** 单条加载:GET /api/entity/{table}/{id} */
/**
* 单条加载:GET /api/entity/{table}/{id}[?pageId=]
*
* <p><b>{@code pageId} 引用字段对象解析(picker 加载即显名)</b>:amis picker(可编辑引用字段)
* 在「form initApi 动态载入已存值」时只显裸 id(不自动补拉 source 解析 label)。给定 {@code pageId}
* 时读其 {@code view_page} XML 收集 {@code SelectDialog/MultiSelectDialog}(带 {@code dialogid})字段,
* 把载入记录里这些字段的裸 id 值就地解析为 {@code {_id,_label}} 对象(多选→对象数组),自带 label,
* picker 经 valueField=_id 仍提交 _id 不变。{@code pageId} 空 → 行为不变(仅 CHAR trim)。
*/
@GetMapping("/{table}/{id}")
public Map<String, Object> load(@PathVariable String table, @PathVariable String id) {
public Map<String, Object> load(@PathVariable String table, @PathVariable String id,
@RequestParam(required = false) String pageId) {
Properties ctx = UserSession.getAdmin();
Object pojo = pojoCRUDService.loadPOJO(id, table.toUpperCase(), ctx, false);
String t = table.toUpperCase();
Object pojo = pojoCRUDService.loadPOJO(id, t, ctx, false);
Object data;
if (pojo instanceof GeneralPOJO p) {
Map<String, Object> row = toRow(p);
if (pageId != null && !pageId.isBlank()) {
resolveReferenceFields(row, pageId.trim());
}
data = row;
} else {
data = pojo;
}
Map<String, Object> resp = new HashMap<>();
resp.put("status", 0);
resp.put("msg", "");
resp.put("data", pojo instanceof GeneralPOJO p ? toRow(p) : pojo);
resp.put("data", data);
return resp;
}
@@ -210,4 +241,139 @@ public class EntityController {
private static Object trimChar(Object v) {
return v instanceof String s ? s.stripTrailing() : v;
}
/** view_page 里的可编辑引用字段:列名(大写) + dialogid(picker 数据源) + 是否多选。 */
private record RefField(String name, String dialogid, boolean multi) {}
/**
* dialogid(=view_template_id) 指向的模板是否 type=3(树形对话框,层级引用)。与转换器
* {@code TemplateToAmisConverter.isTreeDialog} 同口径:type=3 的 SelectDialog 被转换器改写为
* {@code tree-select}(吃裸 id 标量、自 source 拉树标签),而非 {@code picker}(吃 {_id,_label} 对象)。
* 故 tree-select 引用字段不可转对象(否则 amis 显「undefined」),本端点据此跳过。查询失败/非树 → false。
*/
private boolean isTreeDialog(String dialogId) {
if (dialogId == null || dialogId.isBlank()) {
return false;
}
try {
Integer type = jdbcTemplate.queryForObject(
"SELECT type FROM view_template WHERE view_template_id = ?",
Integer.class, dialogId);
return type != null && type == 3;
} catch (DataAccessException e) {
return false;
}
}
/**
* 把载入记录里的引用字段裸 id 就地替换为 {@code {_id,_label}} 对象(picker 自带 label 即显名):
* 读 {@code pageId} 的 view_page XML 收集 {@code SelectDialog/MultiSelectDialog}(带 {@code dialogid})
* 字段,对 row 里这些非空字段值经 {@link PickerController#resolveValuesToOptions}(口径同 /resolve)
* 解析为对象(多选→逗号拆分为对象数组),解析不到的回退 {@code {_id:value,_label:value}}(不丢值)。
* 引用字段对象的 {@code _id} 同样去尾补白对齐选项 value。XML 缺失/解析异常 → 原样返回(降级)。
*/
private void resolveReferenceFields(Map<String, Object> row, String pageId) {
List<RefField> refs = referenceFields(pageId);
if (refs.isEmpty()) {
return;
}
for (RefField rf : refs) {
Object raw = row.get(rf.name());
if (raw == null) {
continue;
}
String v = String.valueOf(raw).trim();
if (v.isBlank()) {
continue;
}
if (rf.multi()) {
List<Map<String, Object>> objs = new ArrayList<>();
for (String part : v.split(",")) {
String single = part.trim();
if (!single.isBlank()) {
objs.add(resolveRefValue(rf.dialogid(), single));
}
}
row.put(rf.name(), objs);
} else {
row.put(rf.name(), resolveRefValue(rf.dialogid(), v));
}
}
}
/**
* 单个引用值 → {@code {_id,_label}} 对象(复用 PickerController 的 dialog→显示名口径)。
* 解析不到(dialog 异常/查无)→ 回退 {@code {_id:value,_label:value}}(不丢值)。{@code _id} 去尾补白。
*/
private Map<String, Object> resolveRefValue(String dialogid, String value) {
try {
List<Map<String, Object>> resolved = pickerController.resolveValuesToOptions(
dialogid, value, null, null, null, null, "_label", "_id");
if (!resolved.isEmpty()) {
Map<String, Object> opt = resolved.get(0);
Object idVal = opt.get("_id");
Map<String, Object> obj = new LinkedHashMap<>();
obj.put("_id", idVal instanceof String s ? s.stripTrailing() : idVal);
obj.put("_label", opt.get("_label"));
return obj;
}
} catch (RuntimeException ex) {
// dialog 解析/查询异常 → 不丢值,回退裸 id(picker 至少显裸 id)
}
Map<String, Object> obj = new LinkedHashMap<>();
obj.put("_id", value);
obj.put("_label", value);
return obj;
}
/**
* 读 {@code pageId} 的 view_page XML,收集可编辑引用字段({@code uitype} ∈
* {@code {SelectDialog, MultiSelectDialog}} 且有 {@code dialogid})。XML 缺失/无 fields/异常 → 空列表。
*/
private List<RefField> referenceFields(String pageId) {
List<RefField> refs = new ArrayList<>();
String xml = queryViewPageXml(pageId);
if (xml == null || xml.isBlank()) {
return refs;
}
try {
Document doc = DocumentHelper.parseText(xml.trim());
Element fields = doc.getRootElement().element("fields");
if (fields == null) {
return refs;
}
for (Element f : fields.elements("field")) {
String name = f.attributeValue("name");
String uitype = f.attributeValue("uitype");
String dialogid = f.attributeValue("dialogid");
if (name == null || name.isBlank() || dialogid == null || dialogid.isBlank()) {
continue;
}
boolean multi = "MultiSelectDialog".equals(uitype);
if (!multi && !"SelectDialog".equals(uitype)) {
continue;
}
// type=3 树对话框 → 转换器渲为 tree-select(吃裸 id),不可转 {_id,_label} 对象,跳过。
if (isTreeDialog(dialogid)) {
continue;
}
refs.add(new RefField(name.toUpperCase(), dialogid, multi));
}
} catch (Exception e) {
return new ArrayList<>();
}
return refs;
}
/** 取 view_page XML(PG xml 物理类型须 {@code xml::text} 取出);缺失/异常 → null。 */
private String queryViewPageXml(String pageId) {
try {
List<String> xmls = jdbcTemplate.queryForList(
"SELECT xml::text FROM view_page WHERE view_page_id = ? AND xml IS NOT NULL",
String.class, pageId);
return xmls.isEmpty() ? null : xmls.get(0);
} catch (DataAccessException ex) {
return null;
}
}
}

View File

@@ -323,10 +323,10 @@ public class PickerController {
* ③ 返回按入参顺序回填;④ 查不到的 value 兜底 {@code {label:value,value:value}}(不丢值、显裸 id);
* ⑤ 数据权限拒绝(PrivilegeException)→ 全部兜底,不 500(与 {@link #list} 友好降级一致)。
*/
private List<Map<String, Object>> resolveValuesToOptions(String entityOrRef, String value,
String asUser, String roles,
String asDepart, String asOrg,
String labelKey, String valueKey) {
List<Map<String, Object>> resolveValuesToOptions(String entityOrRef, String value,
String asUser, String roles,
String asDepart, String asOrg,
String labelKey, String valueKey) {
// 入参拆分 + trim(CHAR 主键去补白后比对)+ 保序去重(重复值只查一次,回填时按原序复用)
List<String> wanted = new ArrayList<>();
if (value != null) {