fix(platform): resolve picker load defaults
Some checks failed
ci / build-test-parity (push) Has been cancelled

This commit is contained in:
2026-06-18 01:51:59 +08:00
parent cfb7451e62
commit 278b0da422
5 changed files with 255 additions and 19 deletions

View File

@@ -9,10 +9,15 @@ import java.util.Properties;
import java.util.Set;
import java.util.function.Supplier;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.PostMapping;
@@ -75,22 +80,36 @@ public class FlowController {
private final FlowInputCoercer inputCoercer;
/** A1 compute-on-save:写流执行前为 editgrid 子表补算派生字段(金额/总额)。可空(老单测构造)。 */
private final EditGridDerivedFieldComputer editGridComputer;
/** 编辑弹窗 load:把 picker 默认值裸 id 解析成 {_id,_label}。可空(老单测构造)。 */
private final JdbcTemplate jdbcTemplate;
private final PickerController pickerController;
/** Spring 注入入口(6 参:读写分类器 + 类型强制器 + editgrid 派生字段补算器)。
/** Spring 注入入口(8 参:读写分类器 + 类型强制器 + editgrid 派生字段补算器 + picker 默认值解析)。
* 多构造器场景须显式 @Autowired 指明注入构造,否则 Spring 回退无参构造致启动失败。 */
@org.springframework.beans.factory.annotation.Autowired
public FlowController(FlowExcuteService flowExcuteService, FlowNameResolver flowNameResolver,
UserColumnResolver userColumnResolver, FlowReadWriteClassifier classifier,
FlowInputCoercer inputCoercer, EditGridDerivedFieldComputer editGridComputer) {
FlowInputCoercer inputCoercer, EditGridDerivedFieldComputer editGridComputer,
JdbcTemplate jdbcTemplate, PickerController pickerController) {
this.flowExcuteService = flowExcuteService;
this.flowNameResolver = flowNameResolver;
this.userColumnResolver = userColumnResolver;
this.classifier = classifier;
this.inputCoercer = inputCoercer;
this.editGridComputer = editGridComputer;
this.jdbcTemplate = jdbcTemplate;
this.pickerController = pickerController;
}
/** 兼容老单测的五参构造(无 editgrid 补算器)。生产由 Spring 走六参构造。 */
/** 兼容老单测的六参构造(无 picker 默认值解析)。生产由 Spring 走八参构造。 */
public FlowController(FlowExcuteService flowExcuteService, FlowNameResolver flowNameResolver,
UserColumnResolver userColumnResolver, FlowReadWriteClassifier classifier,
FlowInputCoercer inputCoercer, EditGridDerivedFieldComputer editGridComputer) {
this(flowExcuteService, flowNameResolver, userColumnResolver, classifier, inputCoercer,
editGridComputer, null, null);
}
/** 兼容老单测的五参构造(无 editgrid 补算器)。生产由 Spring 走八参构造。 */
public FlowController(FlowExcuteService flowExcuteService, FlowNameResolver flowNameResolver,
UserColumnResolver userColumnResolver, FlowReadWriteClassifier classifier,
FlowInputCoercer inputCoercer) {
@@ -121,16 +140,23 @@ public class FlowController {
@GetMapping("/{id}")
@Operation(summary = "执行服务流(GET):query 参数作为入参 map,返回流输出 JSON。报表 D 类查询入口")
public ResponseEntity<Map<String, Object>> executeGet(@PathVariable String id,
@RequestParam(value = "__egtpl", required = false) String egTpl,
@RequestParam Map<String, String> allParams) {
Map<String, Object> map = new HashMap<>(allParams);
map.remove("__egtpl");
applyPaging(map);
try {
return run(id, map, false);
return run(id, map, false, egTpl);
} catch (RuntimeException ex) {
return gracefulFlowError(id, ex);
}
}
/** 兼容老单测直接调用的两参入口;HTTP 入口走带 __egtpl 的三参方法。 */
ResponseEntity<Map<String, Object>> executeGet(String id, Map<String, String> allParams) {
return executeGet(id, null, allParams);
}
/** POST 执行:JSON body 透传为服务流入参 map。body 为空时按空 map 执行。 */
@PostMapping("/{id}")
@Operation(summary = "执行服务流(POST):JSON body 作为入参 map,返回流输出 JSON")
@@ -145,7 +171,7 @@ public class FlowController {
editGridComputer.enrich(map, egTpl);
}
try {
return run(id, map, true);
return run(id, map, true, egTpl);
} catch (RuntimeException ex) {
return gracefulFlowError(id, ex);
}
@@ -274,7 +300,7 @@ public class FlowController {
*
* @param isPost 是否 POST 请求(GET=false)。写流仅 POST 放行,防 URL 触发写。
*/
private ResponseEntity<Map<String, Object>> run(String id, Map<String, Object> map, boolean isPost) {
private ResponseEntity<Map<String, Object>> run(String id, Map<String, Object> map, boolean isPost, String egTpl) {
ResolvedFlow resolved = flowNameResolver.resolve(id);
return switch (resolved.type()) {
@@ -286,7 +312,7 @@ public class FlowController {
}
Properties ctx = UserSession.getAdmin();
Object output = withEngineContext(ctx, () -> flowExcuteService.excute(map, ctx, id));
yield ok(output, null);
yield ok(output, null, egTpl);
}
case NAMED_READ -> {
// 命名读流:GET/POST 皆可。WS-D D.3:命名读流(如查看新闻查询 OA_NEWSMANAGE_SEARCH_LOOK)按其
@@ -299,7 +325,7 @@ public class FlowController {
}
Properties ctx = UserSession.getAdmin();
Object output = withEngineContext(ctx, () -> flowExcuteService.excute(map, ctx, id));
yield ok(output, flowNameResolver.entityTableForNamedFlow(id));
yield ok(output, flowNameResolver.entityTableForNamedFlow(id), egTpl);
}
case NAMED_WRITE -> {
// 命名写流:GET 拒绝(防 URL 触发写);POST 用真实 JWT 用户上下文执行
@@ -334,7 +360,7 @@ public class FlowController {
Map<String, Object> agg = new LinkedHashMap<>();
agg.put("count", results.size());
agg.put("results", results);
yield ok(agg, null);
yield ok(agg, null, egTpl);
}
// amis JSON body 全为 String,引擎 GeneralPO.load 按 POJOInfo 列类型直接 cast:
// ENUMERATE/INTEGER/STATUS→Integer、BOOLEAN→Boolean、DECIMAL→BigDecimal。
@@ -344,7 +370,7 @@ public class FlowController {
}
Properties ctx = currentCtx();
Object output = withEngineContext(ctx, () -> flowExcuteService.excute(map, ctx, id));
yield ok(output, null);
yield ok(output, null, egTpl);
}
case SYNTHESIZED -> {
// _SEARCH 合成:合成流已注册到 SynthesizedFlowRegistry,
@@ -358,7 +384,7 @@ public class FlowController {
}
Object output = withEngineContext(ctx, () -> flowExcuteService.excute(map, ctx, flowKey));
// 合成搜索流的 key = <TABLE>_SEARCH,剥后缀即数据源实体表名,用于用户引用列 id→姓名解析
yield ok(output, tableFromSearchKey(flowKey));
yield ok(output, tableFromSearchKey(flowKey), egTpl);
}
case ORPHAN -> {
Map<String, Object> err = new LinkedHashMap<>();
@@ -409,10 +435,11 @@ public class FlowController {
}
}
private ResponseEntity<Map<String, Object>> ok(Object output, String table) {
private ResponseEntity<Map<String, Object>> ok(Object output, String table, String egTpl) {
Object data = unwrapCrudResult(normalize(output));
// read-path issue D1=603:把元数据标记为用户引用的列 id/工号 → 姓名(仅在已知数据源实体的合成搜索流上)
resolveUserColumns(data, table);
resolveReferenceFields(data, egTpl);
Map<String, Object> resp = new LinkedHashMap<>();
resp.put("status", 0);
resp.put("msg", "");
@@ -493,6 +520,162 @@ public class FlowController {
}
}
/** view_page 里的可编辑引用字段:列名(大写) + dialogid(picker 数据源) + 是否多选。 */
private record RefField(String name, String dialogid, boolean multi) {}
/**
* 编辑弹窗 service.api load 后,标准 picker 的默认值必须是 {@code {_id,_label}} 对象(多选为对象数组)。
* 老服务流只回裸 id/裸 id 数组;带 {@code __egtpl=<编辑表单 view_page_id>} 时,按该模板 XML 解析
* {@code SelectDialog/MultiSelectDialog} 字段并补显示名,避免控件展开成长串 ID。
*/
@SuppressWarnings("unchecked")
private void resolveReferenceFields(Object data, String pageId) {
if (pageId == null || pageId.isBlank() || jdbcTemplate == null || pickerController == null) {
return;
}
if (!(data instanceof Map<?, ?> m0)) {
return;
}
Map<String, Object> map = (Map<String, Object>) m0;
if (map.get("items") instanceof List<?>) {
return;
}
Object resultObj = map.get("result");
Map<String, Object> record = resultObj instanceof Map<?, ?> r ? (Map<String, Object>) r : map;
List<RefField> refs = referenceFields(pageId);
if (refs.isEmpty()) {
return;
}
for (RefField rf : refs) {
Object raw = record.get(rf.name());
if (raw == null) {
continue;
}
if (rf.multi()) {
if (raw instanceof List<?> rawList && rawList.stream().allMatch(Map.class::isInstance)) {
continue;
}
List<Map<String, Object>> resolved = new ArrayList<>();
for (String value : splitRefValues(raw)) {
resolved.add(resolveRefValue(rf.dialogid(), value));
}
if (!resolved.isEmpty()) {
record.put(rf.name(), resolved);
}
} else if (!(raw instanceof Map<?, ?>)) {
String value = String.valueOf(raw).trim();
if (!value.isBlank()) {
record.put(rf.name(), resolveRefValue(rf.dialogid(), value));
}
}
}
}
/** 多选引用字段可能来自逗号串,也可能来自 JSON 数组;统一拆为非空 id 列表。 */
private static List<String> splitRefValues(Object raw) {
List<String> values = new ArrayList<>();
if (raw instanceof List<?> list) {
for (Object item : list) {
String v = item == null ? "" : String.valueOf(item).trim();
if (!v.isBlank()) {
values.add(v);
}
}
return values;
}
String s = String.valueOf(raw).trim();
if (s.startsWith("[") && s.endsWith("]")) {
s = s.substring(1, s.length() - 1);
}
for (String part : s.split(",")) {
String v = part.trim();
if (!v.isBlank()) {
values.add(v);
}
}
return values;
}
/** 单个引用值 → {@code {_id,_label}};查不到则回退原值,保证不丢数据。 */
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 解析/查询异常不应阻断 load;picker 至少保留原始 id。
}
Map<String, Object> obj = new LinkedHashMap<>();
obj.put("_id", value);
obj.put("_label", value);
return obj;
}
/** 读编辑表单 XML,收集非树形 SelectDialog/MultiSelectDialog 字段。 */
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;
}
if (!isTreeDialog(dialogid)) {
refs.add(new RefField(name.toUpperCase(), dialogid, multi));
}
}
} catch (Exception ex) {
return new ArrayList<>();
}
return refs;
}
/** type=3 树形对话框由 tree-select 自己拉 source 显名,不转成 picker 对象。 */
private boolean isTreeDialog(String dialogId) {
try {
Integer type = jdbcTemplate.queryForObject(
"SELECT type FROM view_template WHERE view_template_id = ?",
Integer.class, dialogId);
return type != null && type == 3;
} catch (DataAccessException ex) {
return false;
}
}
/** 取 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;
}
}
/**
* F2-14b:流输出变量名为 result(DefaultFlowHelper 默认搜索流口径),查询结果被包成
* {result:{items,total}},而 amis crud 契约读 data.items/data.total——一层之差导致