feat: add AI tree CRUD recipe

This commit is contained in:
2026-06-17 13:03:38 +08:00
parent 2b0b673ce9
commit 718c5a5b9b
17 changed files with 675 additions and 46 deletions

View File

@@ -9,5 +9,16 @@ public record AiFeatureRequest(
String entityName,
String templateId,
String menuName,
List<AiFeatureField> fields) {
List<AiFeatureField> fields,
String pageRecipe) {
public AiFeatureRequest(String objective,
String featureName,
String tableName,
String entityName,
String templateId,
String menuName,
List<AiFeatureField> fields) {
this(objective, featureName, tableName, entityName, templateId, menuName, fields, null);
}
}

View File

@@ -81,10 +81,12 @@ public class AiFeatureDraftGenerator {
private PageBlueprint pageBlueprint(FeatureSpec spec) {
List<PageBlueprintField> fields = new ArrayList<>();
for (FieldSpec field : spec.fields()) {
boolean treeParentField = PageBlueprint.RECIPE_TREE_CRUD.equals(spec.pageRecipe())
&& "PARENTID".equals(field.column());
fields.add(new PageBlueprintField(field.column(), field.label(), field.type(),
field.required(), true, true));
field.required(), !treeParentField, true));
}
return new PageBlueprint(PageBlueprint.VERSION, PageBlueprint.RECIPE_MAINTENANCE_CRUD,
return new PageBlueprint(PageBlueprint.VERSION, spec.pageRecipe(),
spec.templateId(), spec.featureName(), spec.tableName(), spec.entityName(),
spec.tableName() + "_ID", displayColumn(spec), fields,
true, true, true, 20);
@@ -129,7 +131,12 @@ public class AiFeatureDraftGenerator {
root.put("publishTarget", "local-smoke");
ArrayNode tests = root.putArray("smokeTests");
addSmoke(tests, "page-schema", "GET", "/api/page/schema/" + spec.templateId(), 200);
addSmoke(tests, "entity-list", "GET", "/api/entity/" + spec.tableName() + "/list?page=1&perPage=20", 200);
if (PageBlueprint.RECIPE_TREE_CRUD.equals(spec.pageRecipe())) {
addSmoke(tests, "entity-tree", "GET", "/api/entity/" + spec.tableName()
+ "/tree?parentKey=PARENTID&idKey=" + spec.tableName() + "_ID&perPage=2000", 200);
} else {
addSmoke(tests, "entity-list", "GET", "/api/entity/" + spec.tableName() + "/list?page=1&perPage=20", 200);
}
addSmoke(tests, "flow-search", "GET", "/api/flow/" + spec.tableName() + "_SEARCH?page=1&perPage=20", 200);
ObjectNode create = addSmoke(tests, "entity-create", "POST",
"/api/entity/" + spec.tableName() + "/save", 200);
@@ -152,7 +159,9 @@ public class AiFeatureDraftGenerator {
private ObjectNode sampleEntityBody(FeatureSpec spec) {
ObjectNode body = objectMapper.createObjectNode();
for (FieldSpec field : spec.fields()) {
switch (field.type()) {
if ("PARENTID".equals(field.column())) {
body.put(field.column(), "");
} else switch (field.type()) {
case "integer" -> body.put(field.column(), 1);
case "number" -> body.put(field.column(), 1.0);
case "date" -> body.put(field.column(), "2026-01-01");
@@ -173,11 +182,12 @@ public class AiFeatureDraftGenerator {
String entityName = firstText(request == null ? null : request.entityName(), feature, table);
String templateId = normalizeTemplateId(request == null ? null : request.templateId(), table);
String menuName = firstText(request == null ? null : request.menuName(), feature);
List<FieldSpec> fields = normalizeFields(request == null ? null : request.fields());
return new FeatureSpec(feature, table, entityName, templateId, menuName, fields);
String pageRecipe = normalizePageRecipe(request == null ? null : request.pageRecipe());
List<FieldSpec> fields = normalizeFields(request == null ? null : request.fields(), pageRecipe);
return new FeatureSpec(feature, table, entityName, templateId, menuName, pageRecipe, fields);
}
private List<FieldSpec> normalizeFields(List<AiFeatureField> input) {
private List<FieldSpec> normalizeFields(List<AiFeatureField> input, String pageRecipe) {
List<AiFeatureField> raw = input == null ? List.of() : input;
List<FieldSpec> out = new ArrayList<>();
Set<String> seen = new LinkedHashSet<>();
@@ -195,9 +205,24 @@ public class AiFeatureDraftGenerator {
out.add(new FieldSpec("NAME", "名称", "string", 100, true));
out.add(new FieldSpec("DESCRIPTION", "描述", "string", 500, false));
}
if (PageBlueprint.RECIPE_TREE_CRUD.equals(pageRecipe) && seen.add("PARENTID")) {
out.add(new FieldSpec("PARENTID", "上级", "string", 64, false));
}
return List.copyOf(out);
}
private static String normalizePageRecipe(String raw) {
String recipe = raw == null ? "" : raw.trim();
if (recipe.isBlank()) {
return PageBlueprint.RECIPE_MAINTENANCE_CRUD;
}
if (PageBlueprint.RECIPE_MAINTENANCE_CRUD.equals(recipe)
|| PageBlueprint.RECIPE_TREE_CRUD.equals(recipe)) {
return recipe;
}
throw new IllegalArgumentException("Unsupported pageRecipe: " + recipe);
}
private static String normalizeTable(String raw, String seed) {
String table = raw == null ? "" : raw.trim().toUpperCase(Locale.ROOT);
table = table.replaceAll("[^A-Z0-9_]", "_").replaceAll("_+", "_");
@@ -302,6 +327,7 @@ public class AiFeatureDraftGenerator {
String entityName,
String templateId,
String menuName,
String pageRecipe,
List<FieldSpec> fields) {
}

View File

@@ -6,35 +6,35 @@ compiler: com.eip4.platform.metadata.schema.blueprint.PageBlueprintCompiler
amis:
rootType: page
body:
- type: crud
syncLocation: false
perPage: 20
apiPattern: /api/entity/{tableName}/list?page=${page}&perPage=${perPage}
headerToolbar:
- addButton:
label: 新增
level: primary
icon: fa fa-plus
type: crud
syncLocation: false
perPage: 20
apiPattern: /api/entity/{tableName}/list?page=${page}&perPage=${perPage}
headerToolbar:
- addButton:
label: 新增
level: primary
icon: fa fa-plus
actionType: dialog
formApiPattern: post:/api/entity/{tableName}/save
- pagination
footerToolbar:
- statistics
- pagination
operationColumn:
label: 操作
width: 180
actions:
- edit:
level: link
actionType: dialog
formApiPattern: post:/api/entity/{tableName}/save
- pagination
footerToolbar:
- statistics
- pagination
operationColumn:
label: 操作
width: 180
actions:
- edit:
level: link
actionType: dialog
formApiPattern: post:/api/entity/{tableName}/save
- delete:
level: link
className: text-danger
actionType: ajax
confirmText: 确认删除该记录?
apiPattern: /api/entity/{tableName}/delete
- delete:
level: link
className: text-danger
actionType: ajax
confirmText: 确认删除该记录?
apiPattern: /api/entity/{tableName}/delete
fieldMappings:
string: input-text
integer: input-number

View File

@@ -0,0 +1,62 @@
schemaVersion: 1
recipe: tree-crud-v1
title: EIP4 tree CRUD page
purpose: 自引用层级实体维护页,覆盖树形列表、新增顶级、加子项、编辑、删除。
compiler: com.eip4.platform.metadata.schema.blueprint.PageBlueprintCompiler
requirements:
parentField: PARENTID
childrenColumnName: children
amis:
rootType: page
body:
type: crud
syncLocation: false
childrenColumnName: children
apiPattern: /api/entity/{tableName}/tree?parentKey=PARENTID&idKey={primaryKey}&perPage=2000
headerToolbar:
- addRootButton:
labelPattern: 新增顶级{entityName}
level: primary
icon: fa fa-plus
actionType: dialog
formApiPattern: post:/api/entity/{tableName}/save
- reloadButton:
label: 刷新
level: link
icon: fa fa-refresh
actionType: reload
targetPattern: "{crudName}"
footerToolbar: []
operationColumn:
label: 操作
width: 240
actions:
- addChild:
labelPattern: 加子{entityName}
level: link
actionType: dialog
parentBinding: PARENTID=${primaryKey}
formApiPattern: post:/api/entity/{tableName}/save
- edit:
level: link
actionType: dialog
formApiPattern: post:/api/entity/{tableName}/save
- delete:
level: link
className: text-danger
actionType: ajax
confirmText: 确认删除该记录?
apiPattern: /api/entity/{tableName}/delete
fieldMappings:
string: input-text
integer: input-number
number: input-number
date: input-date
datetime: input-datetime
boolean: switch
gates:
- page-recipe
- typed-page-schema
- amis-schema
- local-smoke
- playwright-render

View File

@@ -3,6 +3,7 @@ package com.eip4.platform.ai.service;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.List;
@@ -44,7 +45,7 @@ class AiFeatureDraftGeneratorTest {
.findFirst().orElseThrow();
assertEquals("maintenance-crud-v1", pageSchema.metadata().get("pageRecipe"));
assertEquals(1, pageSchema.metadata().get("pageBlueprintVersion"));
assertEquals("crud", pageSchema.payload().path("body").get(0).path("type").asText());
assertEquals("crud", pageSchema.payload().path("body").path("type").asText());
@SuppressWarnings("unchecked")
Map<String, Object> blueprint = (Map<String, Object>) pageSchema.metadata().get("pageBlueprint");
assertEquals("EIP4AI_CONTRACT_TYPE", blueprint.get("tableName"));
@@ -78,4 +79,78 @@ class AiFeatureDraftGeneratorTest {
assertNotNull(entity.payload().path("attributes").get(0));
assertEquals("NAME", entity.payload().path("attributes").get(0).path("column").asText());
}
@Test
@DisplayName("树形配方生成 tree-crud 页面和树接口 smoke")
void generatesTreeCrudFeatureDrafts() {
AiFeatureDraftGenerator generator = new AiFeatureDraftGenerator(MAPPER);
var drafts = generator.generate(new AiFeatureRequest(
"新增组织分类维护",
"组织分类维护",
"EIP4AI_ORG_CATEGORY",
"组织分类",
"eip4ai-org-category",
"组织分类维护",
List.of(
new AiFeatureField("NAME", "名称", "string", 100, true),
new AiFeatureField("CODE", "编码", "string", 64, true)),
"tree-crud-v1"));
var entity = drafts.stream().filter(d -> d.artifactType() == AiArtifactType.ENTITY)
.findFirst().orElseThrow();
assertTrue(hasAttribute(entity.payload().path("attributes"), "PARENTID"));
var pageSchema = drafts.stream().filter(d -> d.artifactType() == AiArtifactType.PAGE_SCHEMA)
.findFirst().orElseThrow();
assertEquals("tree-crud-v1", pageSchema.metadata().get("pageRecipe"));
assertEquals("crud", pageSchema.payload().path("body").path("type").asText());
assertEquals("children", pageSchema.payload().path("body").path("childrenColumnName").asText());
assertEquals("/api/entity/EIP4AI_ORG_CATEGORY/tree?parentKey=PARENTID&idKey=EIP4AI_ORG_CATEGORY_ID&perPage=2000",
pageSchema.payload().path("body").path("api").path("url").asText());
assertFalse(hasColumn(pageSchema.payload().path("body").path("columns"), "PARENTID"));
var test = drafts.stream().filter(d -> d.artifactType() == AiArtifactType.TEST)
.findFirst().orElseThrow();
assertEquals("entity-tree", test.payload().path("smokeTests").get(1).path("name").asText());
assertEquals("/api/entity/EIP4AI_ORG_CATEGORY/tree?parentKey=PARENTID&idKey=EIP4AI_ORG_CATEGORY_ID&perPage=2000",
test.payload().path("smokeTests").get(1).path("url").asText());
assertEquals("", test.payload().path("smokeTests").get(3).path("requestBody").path("PARENTID").asText());
}
@Test
@DisplayName("未知页面配方不静默降级")
void rejectsUnknownPageRecipe() {
AiFeatureDraftGenerator generator = new AiFeatureDraftGenerator(MAPPER);
assertThrows(IllegalArgumentException.class, () -> generator.generate(new AiFeatureRequest(
"新增客户等级维护",
"客户等级维护",
"EIP4AI_CUSTOMER_LEVEL",
"客户等级",
"eip4ai-customer-level",
"客户等级维护",
List.of(new AiFeatureField("NAME", "名称", "string", 100, true)),
"unknown-recipe")));
}
private static boolean hasAttribute(Iterable<?> attributes, String column) {
for (Object attribute : attributes) {
if (attribute instanceof com.fasterxml.jackson.databind.JsonNode node
&& column.equals(node.path("column").asText())) {
return true;
}
}
return false;
}
private static boolean hasColumn(Iterable<?> columns, String name) {
for (Object column : columns) {
if (column instanceof com.fasterxml.jackson.databind.JsonNode node
&& name.equals(node.path("name").asText())) {
return true;
}
}
return false;
}
}