From 687ade7513a1b901c2b2967b8834ffc808cf4c31 Mon Sep 17 00:00:00 2001 From: zhengwr Date: Sat, 20 Jun 2026 09:52:13 +0800 Subject: [PATCH] =?UTF-8?q?feat(design):=20RecipePageComposer=20=E5=85=B1?= =?UTF-8?q?=E4=BA=AB=E8=93=9D=E5=9B=BE=E8=A3=85=E9=85=8D=E5=99=A8=20+=20Pa?= =?UTF-8?q?geBlueprintField=20=E5=BC=95=E7=94=A8=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E6=89=A9=E5=B1=95(=E5=90=91=E5=90=8E=E5=85=BC=E5=AE=B9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.8 (1M context) --- .../schema/blueprint/PageBlueprintField.java | 10 +- .../schema/blueprint/RecipePageComposer.java | 94 +++++++++++++++++++ .../blueprint/RecipePageComposerTest.java | 80 ++++++++++++++++ 3 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/RecipePageComposer.java create mode 100644 platform-metadata/src/test/java/com/eip4/platform/metadata/schema/blueprint/RecipePageComposerTest.java diff --git a/platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprintField.java b/platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprintField.java index 759731f..73f2e57 100644 --- a/platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprintField.java +++ b/platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprintField.java @@ -6,5 +6,13 @@ public record PageBlueprintField( String type, boolean required, boolean listVisible, - boolean formVisible) { + boolean formVisible, + String referenceTable, + String displayColumn) { + + /** 6-arg 兼容构造:非引用字段(referenceTable/displayColumn 置空)。保持既有调用方/测试零改。 */ + public PageBlueprintField(String name, String label, String type, + boolean required, boolean listVisible, boolean formVisible) { + this(name, label, type, required, listVisible, formVisible, null, null); + } } diff --git a/platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/RecipePageComposer.java b/platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/RecipePageComposer.java new file mode 100644 index 0000000..4782540 --- /dev/null +++ b/platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/RecipePageComposer.java @@ -0,0 +1,94 @@ +package com.eip4.platform.metadata.schema.blueprint; + +import java.util.ArrayList; +import java.util.List; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; + +/** + * 共享蓝图装配器:实体属性 → 受控 {@link PageBlueprint} → {@link PageBlueprintCompiler#compile}。 + * + *

人工单步端点(DesignFeatureController)与未来 AI 路径共用同一装配口径(DRY),UI/UE 规则集中在 + * recipe/compiler 而非散落在调用方。本类不写库、不校验白名单(严格 typed 闸在端点层),只负责装配+编译。 + * + *

装配口径镜像 {@code AiFeatureDraftGenerator.pageBlueprint}:tree-crud 的 PARENTID 列 listVisible=false; + * displayField 缺省回退首个 NAME/required 列。引用字段(type=reference + referenceTable/displayColumn) + * 透传到 {@link PageBlueprintField},由 compiler 渲染显名列(见 applyColumnRender)。 + */ +public final class RecipePageComposer { + + private final PageBlueprintCompiler compiler; + + public RecipePageComposer(ObjectMapper objectMapper) { + this.compiler = new PageBlueprintCompiler(objectMapper); + } + + public ObjectNode compose(Spec spec) { + if (spec == null) { + throw new IllegalArgumentException("compose spec 不能为空"); + } + PageBlueprint blueprint = toBlueprint(spec); + return compiler.compile(blueprint); + } + + PageBlueprint toBlueprint(Spec spec) { + List fields = new ArrayList<>(); + for (AttrSpec attr : spec.attributes()) { + boolean treeParentField = PageBlueprint.RECIPE_TREE_CRUD.equals(spec.recipe()) + && "PARENTID".equals(attr.column()); + fields.add(new PageBlueprintField( + attr.column(), attr.display(), attr.type(), + attr.required(), !treeParentField, true, + attr.referenceTable(), attr.displayColumn())); + } + String displayField = firstNonBlank(spec.displayField(), deriveDisplayField(spec.attributes())); + int pageSize = spec.pageSize() < 1 ? 20 : spec.pageSize(); + // templateId 由端点层服务端生成;compose 阶段用占位(compiler.validate 只要求非空)。 + return new PageBlueprint( + PageBlueprint.VERSION, spec.recipe(), + "compose-placeholder", spec.title(), spec.tableName(), spec.entityName(), + spec.tableName() + "_ID", displayField, fields, + true, true, true, pageSize); + } + + private static String deriveDisplayField(List attrs) { + for (AttrSpec a : attrs) { + if ("NAME".equals(a.column()) || a.required()) { + return a.column(); + } + } + return attrs.isEmpty() ? "NAME" : attrs.get(0).column(); + } + + private static String firstNonBlank(String preferred, String fallback) { + if (preferred != null && !preferred.isBlank()) { + return preferred.trim(); + } + return fallback; + } + + /** 装配输入:等价于 GET /api/meta/entity/{table}/attributes 的读取结果 + recipe/title/displayField/pageSize。 */ + public record Spec( + String tableName, + String entityName, + String recipe, + String title, + String displayField, + int pageSize, + List attributes) { + public Spec { + attributes = attributes == null ? List.of() : List.copyOf(attributes); + } + } + + /** 单个实体属性。referenceTable/displayColumn 仅引用列非空(type=reference)。 */ + public record AttrSpec( + String column, + String display, + String type, + boolean required, + String referenceTable, + String displayColumn) { + } +} diff --git a/platform-metadata/src/test/java/com/eip4/platform/metadata/schema/blueprint/RecipePageComposerTest.java b/platform-metadata/src/test/java/com/eip4/platform/metadata/schema/blueprint/RecipePageComposerTest.java new file mode 100644 index 0000000..9564be0 --- /dev/null +++ b/platform-metadata/src/test/java/com/eip4/platform/metadata/schema/blueprint/RecipePageComposerTest.java @@ -0,0 +1,80 @@ +package com.eip4.platform.metadata.schema.blueprint; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; + +class RecipePageComposerTest { + + private static final ObjectMapper MAPPER = new ObjectMapper(); + private final RecipePageComposer composer = new RecipePageComposer(MAPPER); + + @Test + @DisplayName("1. compose maintenance-crud → crud.api == /api/flow/_SEARCH,列= listVisible 业务列") + void composesMaintenanceCrud() { + RecipePageComposer.Spec spec = new RecipePageComposer.Spec( + "EIP4AI_CUSTOMER", "客户", PageBlueprint.RECIPE_MAINTENANCE_CRUD, + "客户维护", "NAME", 20, + List.of( + new RecipePageComposer.AttrSpec("NAME", "名称", "string", true, null, null), + new RecipePageComposer.AttrSpec("LEVEL", "等级", "integer", false, null, null))); + + ObjectNode page = composer.compose(spec); + + assertEquals("page", page.path("type").asText()); + assertEquals("客户维护", page.path("title").asText()); + ObjectNode crud = (ObjectNode) page.path("body"); + assertEquals("/api/flow/EIP4AI_CUSTOMER_SEARCH", crud.path("api").asText()); + ArrayNode columns = (ArrayNode) crud.path("columns"); + assertEquals(2, columns.size()); + assertEquals("NAME", columns.get(0).path("name").asText()); + assertEquals("LEVEL", columns.get(1).path("name").asText()); + } + + @Test + @DisplayName("2. compose tree-crud → 注入 PARENTID 字段且列表不显示该列") + void composesTreeCrudWithParent() { + RecipePageComposer.Spec spec = new RecipePageComposer.Spec( + "EIP4AI_CATEGORY", "分类", PageBlueprint.RECIPE_TREE_CRUD, + "分类维护", "NAME", 20, + List.of( + new RecipePageComposer.AttrSpec("NAME", "名称", "string", true, null, null), + new RecipePageComposer.AttrSpec("PARENTID", "上级", "string", false, null, null))); + + ObjectNode page = composer.compose(spec); + ObjectNode crud = (ObjectNode) page.path("body"); + assertEquals("eip4aiCategoryTreeCrud", crud.path("name").asText()); + // 业务列里不含 PARENTID(compiler.treeColumns 跳过 PARENTID),但保留「操作」列。 + ArrayNode columns = (ArrayNode) crud.path("columns"); + boolean hasParentColumn = false; + for (int i = 0; i < columns.size(); i++) { + if ("PARENTID".equals(columns.get(i).path("name").asText())) { + hasParentColumn = true; + } + } + assertTrue(!hasParentColumn); + } + + @Test + @DisplayName("3. displayField 省略时回退首个 NAME/required 列") + void derivesDisplayFieldWhenBlank() { + RecipePageComposer.Spec spec = new RecipePageComposer.Spec( + "EIP4AI_ITEM", "条目", PageBlueprint.RECIPE_MAINTENANCE_CRUD, + "条目维护", null, 20, + List.of( + new RecipePageComposer.AttrSpec("CODE", "编码", "string", false, null, null), + new RecipePageComposer.AttrSpec("NAME", "名称", "string", true, null, null))); + + ObjectNode page = composer.compose(spec); + // displayField 回退到 NAME,filter 兜底用它(此处只需 compose 成功不抛)。 + assertEquals("page", page.path("type").asText()); + } +}