feat(blueprint): master-detail-v1 编译器分支 + PageBlueprintChild

PageBlueprint 加 RECIPE_MASTER_DETAIL 常量与可空 child 组件(14-arg,
保留 13-arg 兼容构造);新增 PageBlueprintChild record。
PageBlueprintCompiler.masterDetail 复用 maintenance 列表骨架,
新增/编辑表单内联 house-style input-table 子表(name=listColumn,
quickEdit 控件),编辑表单 initApi 走 _LOAD 回显主+子行。
validate 接受 master-detail 并要求 child 非空/childTable/listColumn
非空/fields 非空。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-20 15:16:30 +08:00
parent d71ad6f461
commit 8fe377b0c8
2691 changed files with 552833 additions and 1696 deletions

View File

@@ -21,13 +21,24 @@ public record PageBlueprint(
boolean allowCreate,
boolean allowEdit,
boolean allowDelete,
int pageSize) {
int pageSize,
PageBlueprintChild child) {
public static final int VERSION = 1;
public static final String RECIPE_MAINTENANCE_CRUD = "maintenance-crud-v1";
public static final String RECIPE_TREE_CRUD = "tree-crud-v1";
public static final String RECIPE_MASTER_DETAIL = "master-detail-v1";
public PageBlueprint {
fields = fields == null ? List.of() : List.copyOf(fields);
}
/** 13-arg 兼容构造:单表/树 recipe 无子表(child=null)。保持既有调用方/测试零改。 */
public PageBlueprint(int schemaVersion, String recipe, String templateId, String title,
String tableName, String entityName, String primaryKey, String displayField,
List<PageBlueprintField> fields, boolean allowCreate, boolean allowEdit,
boolean allowDelete, int pageSize) {
this(schemaVersion, recipe, templateId, title, tableName, entityName, primaryKey, displayField,
fields, allowCreate, allowEdit, allowDelete, pageSize, null);
}
}

View File

@@ -0,0 +1,27 @@
package com.eip4.platform.metadata.schema.blueprint;
import java.util.List;
/**
* 主子表 recipe(master-detail-v1)的子表侧装配描述。
*
* <p>主表表单内联一个 amis input-table(name={@link #listColumn}),其列由 {@link #fields} 经
* controlType 渲染成可编辑单元格。子行作为主表单同名数组属性随主表单一并提交(无独立 source/api),
* 保真层默认流 _ADD/_UPDATE 经 LIST 属性级联落子表(已实测)。编辑态经 _LOAD 回显
* data.result.{@code listColumn} 的子行。
*
* @param childTable 子表表名(普通实体 tabletype=1)
* @param listColumn 主表 LIST 属性列名(amis input-table 的 name,如 DETAILS)
* @param displayField 子表显示字段(预留;当前仅作元信息)
* @param fields 子表可编辑列(每列经 controlType 映射为 quickEdit 控件)
*/
public record PageBlueprintChild(
String childTable,
String listColumn,
String displayField,
List<PageBlueprintField> fields) {
public PageBlueprintChild {
fields = fields == null ? List.of() : List.copyOf(fields);
}
}

View File

@@ -25,9 +25,132 @@ public final class PageBlueprintCompiler {
if (PageBlueprint.RECIPE_TREE_CRUD.equals(blueprint.recipe())) {
return treeCrud(blueprint);
}
if (PageBlueprint.RECIPE_MASTER_DETAIL.equals(blueprint.recipe())) {
return masterDetail(blueprint);
}
throw new IllegalArgumentException("Unsupported page recipe: " + blueprint.recipe());
}
/**
* 主子表页(master-detail-v1):复用 maintenance CRUD 列表骨架,但新增/编辑表单在主表控件之后
* 内联一个 amis input-table 子表(house-style,name={@code child.listColumn})。子行随主表单同名数组
* 一并提交(无独立 source/api),保真层默认流经 LIST 属性级联落子表(已实测)。
*
* <p>编辑表单 initApi 走 {@code /api/flow/<TABLE>_LOAD}(data.{@code <TABLE>_ID}=主键),_LOAD
* 返回 data.result 含 LIST 列即子行,amis 据此回显主+子(已实测)。
*/
private ObjectNode masterDetail(PageBlueprint blueprint) {
ObjectNode page = objectMapper.createObjectNode();
page.put("type", "page");
page.put("title", blueprint.title());
ObjectNode crud = page.putObject("body");
String crudName = lowerCamel(blueprint.tableName()) + "Crud";
crud.put("type", "crud");
crud.put("name", crudName);
crud.put("syncLocation", false);
crud.put("api", "/api/flow/" + blueprint.tableName() + "_SEARCH");
crud.put("selectable", true);
crud.put("checkOnItemClick", true);
crud.put("primaryField", blueprint.primaryKey());
crud.set("filter", searchFilter(blueprint));
ArrayNode headerToolbar = crud.putArray("headerToolbar");
if (blueprint.allowCreate()) {
headerToolbar.add(masterDetailAddButton(blueprint, crudName));
}
if (blueprint.allowEdit()) {
headerToolbar.add(masterDetailEditButton(blueprint, crudName));
}
if (blueprint.allowDelete()) {
headerToolbar.add(maintenanceDeleteButton(blueprint, crudName));
}
headerToolbar.add("pagination");
ArrayNode footerToolbar = crud.putArray("footerToolbar");
footerToolbar.add("statistics");
footerToolbar.add("pagination");
crud.put("perPage", blueprint.pageSize());
crud.set("columns", maintenanceColumns(blueprint));
return page;
}
private ObjectNode masterDetailAddButton(PageBlueprint blueprint, String crudName) {
ObjectNode addButton = objectMapper.createObjectNode();
addButton.put("type", "button");
addButton.put("id", "新增");
addButton.put("label", "新增");
addButton.put("level", "primary");
addButton.put("icon", "fa fa-plus");
addButton.put("actionType", "dialog");
ObjectNode addDialog = addButton.putObject("dialog");
addDialog.put("title", "新增" + blueprint.entityName());
addDialog.put("size", "lg");
ObjectNode addForm = addDialog.putObject("body");
addForm.put("type", "form");
addForm.put("api", "post:/api/flow/" + blueprint.tableName() + "_ADD");
addForm.put("reload", crudName);
ArrayNode controls = formControls(blueprint, false);
controls.add(childInputTable(blueprint.child()));
addForm.set("body", controls);
return addButton;
}
private ObjectNode masterDetailEditButton(PageBlueprint blueprint, String crudName) {
ObjectNode edit = objectMapper.createObjectNode();
edit.put("type", "button");
edit.put("id", "编辑");
edit.put("label", "编辑");
edit.put("level", "primary");
edit.put("actionType", "dialog");
edit.put("disabledOn", "${!selectedItems || selectedItems.length === 0}");
ObjectNode dialog = edit.putObject("dialog");
dialog.put("title", "编辑" + blueprint.entityName());
dialog.put("size", "lg");
ObjectNode editForm = dialog.putObject("body");
editForm.put("type", "form");
editForm.put("api", "post:/api/flow/" + blueprint.tableName() + "_UPDATE");
editForm.put("reload", crudName);
// 经 _LOAD 回显主+子行(data.result.<listColumn>=子行,已实测)。
ObjectNode initApi = editForm.putObject("initApi");
initApi.put("method", "post");
initApi.put("url", "/api/flow/" + blueprint.tableName() + "_LOAD");
initApi.putObject("data").put(blueprint.primaryKey(),
"${" + blueprint.primaryKey() + "}");
// 选中行铺进对话框作用域,主键经 initApi 拉 _LOAD(主+子)。
dialog.putObject("data").put("&", "${selectedItems[0]}");
ArrayNode editControls = formControls(blueprint, false);
editControls.add(childInputTable(blueprint.child()));
editControls.add(hidden(blueprint.primaryKey(), "${" + blueprint.primaryKey() + "}"));
editForm.set("body", editControls);
return edit;
}
/**
* 子表 input-table(house-style,对齐转换器 UiTypeMapper EditGrid 形态):name={@code child.listColumn},
* 列由 {@code child.fields} 经 controlType 渲染为 quickEdit 可编辑单元格。子行随主表单同名数组提交。
*/
private ObjectNode childInputTable(PageBlueprintChild child) {
ObjectNode table = objectMapper.createObjectNode();
table.put("type", "input-table");
table.put("name", child.listColumn());
table.put("id", "eg-" + child.listColumn());
table.put("label", child.displayField() == null ? child.listColumn() : child.displayField());
table.put("addable", true);
table.put("removable", true);
table.put("needConfirm", false);
table.put("staticOn", "${__readonly}");
ArrayNode columns = table.putArray("columns");
for (PageBlueprintField field : child.fields()) {
ObjectNode col = columns.addObject();
col.put("name", field.name());
col.put("label", field.label());
ObjectNode quickEdit = col.putObject("quickEdit");
quickEdit.put("type", controlType(field.type()));
quickEdit.put("name", field.name());
}
return table;
}
/**
* 单表维护页,对齐转换器 house-style:
* <ul>
@@ -399,7 +522,8 @@ public final class PageBlueprintCompiler {
throw new IllegalArgumentException("Unsupported page blueprint version: " + blueprint.schemaVersion());
}
if (!PageBlueprint.RECIPE_MAINTENANCE_CRUD.equals(blueprint.recipe())
&& !PageBlueprint.RECIPE_TREE_CRUD.equals(blueprint.recipe())) {
&& !PageBlueprint.RECIPE_TREE_CRUD.equals(blueprint.recipe())
&& !PageBlueprint.RECIPE_MASTER_DETAIL.equals(blueprint.recipe())) {
throw new IllegalArgumentException("Unsupported page recipe: " + blueprint.recipe());
}
required(blueprint.templateId(), "templateId");
@@ -427,6 +551,17 @@ public final class PageBlueprintCompiler {
&& blueprint.fields().stream().noneMatch(field -> "PARENTID".equals(field.name()))) {
throw new IllegalArgumentException("tree-crud-v1 requires PARENTID field");
}
if (PageBlueprint.RECIPE_MASTER_DETAIL.equals(blueprint.recipe())) {
PageBlueprintChild child = blueprint.child();
if (child == null) {
throw new IllegalArgumentException("master-detail-v1 requires child");
}
required(child.childTable(), "child.childTable");
required(child.listColumn(), "child.listColumn");
if (child.fields() == null || child.fields().isEmpty()) {
throw new IllegalArgumentException("child.fields must not be empty");
}
}
if (blueprint.pageSize() < 1 || blueprint.pageSize() > 200) {
throw new IllegalArgumentException("pageSize must be between 1 and 200");
}

View File

@@ -192,6 +192,80 @@ class PageBlueprintCompilerTest {
assertThrows(IllegalArgumentException.class, () -> new PageBlueprintCompiler(MAPPER).compile(blueprint));
}
@Test
@DisplayName("master-detail-v1 编译: 编辑表单含 name=<listColumn> 的 input-table 子表,且 initApi 走 _LOAD 回显子行")
void compilesMasterDetailRecipe() {
PageBlueprint blueprint = new PageBlueprint(
PageBlueprint.VERSION,
PageBlueprint.RECIPE_MASTER_DETAIL,
"eip4ai-purchase",
"采购申请",
"EIP4AI_PURCHASE",
"采购申请",
"EIP4AI_PURCHASE_ID",
"TITLE",
List.of(
new PageBlueprintField("TITLE", "标题", "string", true, true, true),
new PageBlueprintField("AMOUNT", "金额", "number", false, true, true)),
true, true, true, 20,
new PageBlueprintChild("EIP4AI_PURCHASE_ITEM", "DETAILS", "ITEMNAME",
List.of(
new PageBlueprintField("ITEMNAME", "物料", "string", true, false, true),
new PageBlueprintField("QTY", "数量", "integer", false, false, true))));
ObjectNode schema = new PageBlueprintCompiler(MAPPER).compile(blueprint);
// 主表 CRUD 列表骨架与 maintenance 一致。
ObjectNode crud = (ObjectNode) schema.path("body");
assertEquals("crud", crud.path("type").asText());
assertEquals("/api/flow/EIP4AI_PURCHASE_SEARCH", crud.path("api").asText());
ArrayNode header = (ArrayNode) crud.path("headerToolbar");
// 新增表单内联 input-table 子表(name=DETAILS)。
ObjectNode addForm = (ObjectNode) header.get(0).path("dialog").path("body");
ArrayNode addBody = (ArrayNode) addForm.path("body");
ObjectNode addChild = findByType(addBody, "input-table");
assertEquals("DETAILS", addChild.path("name").asText());
assertTrue(addChild.path("addable").asBoolean());
assertTrue(addChild.path("removable").asBoolean());
// 子列经 quickEdit 控件渲染(QTY=integer → input-number)。
ArrayNode childCols = (ArrayNode) addChild.path("columns");
assertEquals(2, childCols.size());
assertEquals("ITEMNAME", childCols.get(0).path("name").asText());
assertEquals("input-text", childCols.get(0).path("quickEdit").path("type").asText());
assertEquals("input-number", childCols.get(1).path("quickEdit").path("type").asText());
// 编辑表单:input-table 子表 name=DETAILS,且 initApi 走 _LOAD 回显主+子行。
ObjectNode editForm = (ObjectNode) header.get(1).path("dialog").path("body");
assertEquals("/api/flow/EIP4AI_PURCHASE_LOAD", editForm.path("initApi").path("url").asText());
assertEquals("${EIP4AI_PURCHASE_ID}",
editForm.path("initApi").path("data").path("EIP4AI_PURCHASE_ID").asText());
ArrayNode editBody = (ArrayNode) editForm.path("body");
ObjectNode editChild = findByType(editBody, "input-table");
assertEquals("DETAILS", editChild.path("name").asText());
}
private static ObjectNode findByType(ArrayNode arr, String type) {
for (int i = 0; i < arr.size(); i++) {
if (type.equals(arr.get(i).path("type").asText())) {
return (ObjectNode) arr.get(i);
}
}
throw new AssertionError("no node with type=" + type);
}
@Test
@DisplayName("master-detail-v1 缺 child 校验失败")
void rejectsMasterDetailWithoutChild() {
PageBlueprint blueprint = new PageBlueprint(
PageBlueprint.VERSION,
PageBlueprint.RECIPE_MASTER_DETAIL,
"tpl", "标题", "EIP4AI_M", "实体", "EIP4AI_M_ID", "NAME",
List.of(new PageBlueprintField("NAME", "名称", "string", true, true, true)),
true, true, true, 20, null);
assertThrows(IllegalArgumentException.class, () -> new PageBlueprintCompiler(MAPPER).compile(blueprint));
}
@Test
@DisplayName("引用列渲染显名: list 列 name 指向 <COL>_DISPLAY__(运行时 _SEARCH 注入显名),非裸 id")
void referenceColumnRendersDisplayName() {