feat(design): /recipe/compile 支持 master-detail-v1(先接线再编译)

DesignFeatureController 注入 MasterDetailLinker(生产+包测构造同步);
CompileRequest 加 childTable/linkColumn/listColumn(默认 DETAILS)/listLabel;
recipe 门禁放行 master-detail-v1。compile() master-detail 分支:先
masterDetailLinker.ensureLink(主表,子表,外键,listColumn,listLabel) 接线
元数据,再 attributeReader.read(子表) 读子列(剔除指回主表的外键列),
装配带 child 的 Spec→compose→严格 typed 校验→upsertStandard 落标准层。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-20 15:20:17 +08:00
parent 25494efadf
commit c9be9bc125
2 changed files with 120 additions and 4 deletions

View File

@@ -60,6 +60,7 @@ public class DesignFeatureController {
private final boolean skipAdminCheck; // 仅单测置 true
private final com.eip4.platform.metadata.publish.DefaultFlowPreviewService flowPreviewService; // Task 10
private final com.eip4.platform.metadata.publish.PublishOrchestrator publishOrchestrator; // Task 11-13
private final com.eip4.platform.metadata.publish.MasterDetailLinker masterDetailLinker; // 主子表接线
/** 生产构造(Spring 注入)。 */
@Autowired
@@ -67,12 +68,14 @@ public class DesignFeatureController {
PageSchemaWriter pageSchemaWriter,
EntityAttributeReader attributeReader,
com.eip4.platform.metadata.publish.DefaultFlowPreviewService flowPreviewService,
com.eip4.platform.metadata.publish.PublishOrchestrator publishOrchestrator) {
com.eip4.platform.metadata.publish.PublishOrchestrator publishOrchestrator,
com.eip4.platform.metadata.publish.MasterDetailLinker masterDetailLinker) {
this.jdbcTemplate = jdbcTemplate;
this.pageSchemaWriter = pageSchemaWriter;
this.attributeReader = attributeReader;
this.flowPreviewService = flowPreviewService;
this.publishOrchestrator = publishOrchestrator;
this.masterDetailLinker = masterDetailLinker;
this.composer = new RecipePageComposer(MAPPER);
this.strictValidator = TypedPageSchemaValidator.defaults();
this.skipAdminCheck = false;
@@ -81,12 +84,14 @@ public class DesignFeatureController {
/** 包级测试构造:脱库 + 可旁路 admin 门禁(发布编排依赖在 compile 单测中置 null)。 */
DesignFeatureController(PageSchemaWriter pageSchemaWriter,
EntityAttributeReader attributeReader,
com.eip4.platform.metadata.publish.MasterDetailLinker masterDetailLinker,
boolean skipAdminCheck) {
this.jdbcTemplate = null;
this.pageSchemaWriter = pageSchemaWriter;
this.attributeReader = attributeReader;
this.flowPreviewService = null;
this.publishOrchestrator = null;
this.masterDetailLinker = masterDetailLinker;
this.composer = new RecipePageComposer(MAPPER);
this.strictValidator = TypedPageSchemaValidator.defaults();
this.skipAdminCheck = skipAdminCheck;
@@ -103,9 +108,14 @@ public class DesignFeatureController {
}
String recipe = isBlank(req.recipe) ? PageBlueprint.RECIPE_MAINTENANCE_CRUD : req.recipe.trim();
if (!PageBlueprint.RECIPE_MAINTENANCE_CRUD.equals(recipe)
&& !PageBlueprint.RECIPE_TREE_CRUD.equals(recipe)) {
&& !PageBlueprint.RECIPE_TREE_CRUD.equals(recipe)
&& !PageBlueprint.RECIPE_MASTER_DETAIL.equals(recipe)) {
return ResponseEntity.badRequest().body(fail("不支持的 recipe: " + recipe));
}
boolean masterDetail = PageBlueprint.RECIPE_MASTER_DETAIL.equals(recipe);
if (masterDetail && (isBlank(req.childTable) || isBlank(req.linkColumn))) {
return ResponseEntity.badRequest().body(fail("master-detail-v1 需指定 childTable 和 linkColumn"));
}
int pageSize = req.pageSize == null || req.pageSize < 1 ? 20 : req.pageSize;
// 1. 读实体属性
@@ -126,9 +136,44 @@ public class DesignFeatureController {
a.column(), a.display(), a.type(), a.required(), a.referenceTable(), null));
}
String displayField = isBlank(req.displayField) ? attrs.displayColumn() : req.displayField.trim();
// 2b. master-detail:先接线元数据(幂等),再读子表列装配子表 Spec。
String childTable = null;
String listColumn = null;
List<RecipePageComposer.AttrSpec> childSpecAttrs = new ArrayList<>();
if (masterDetail) {
childTable = req.childTable.trim();
String linkColumn = req.linkColumn.trim();
listColumn = isBlank(req.listColumn) ? "DETAILS" : req.listColumn.trim();
String listLabel = isBlank(req.listLabel) ? childTable : req.listLabel.trim();
try {
masterDetailLinker.ensureLink(req.tableName.trim(), childTable, linkColumn,
listColumn, listLabel, UserSession.getAdmin());
} catch (RuntimeException ex) {
return ResponseEntity.badRequest().body(fail("主子表接线失败:" + ex.getMessage()));
}
EntityAttributes childAttrs;
try {
childAttrs = attributeReader.read(childTable);
} catch (DataAccessException ex) {
return ResponseEntity.badRequest().body(fail("读取子表属性失败:" + ex.getMessage()));
}
if (childAttrs == null || childAttrs.columns().isEmpty()) {
return ResponseEntity.badRequest().body(fail("子表无可用属性: " + childTable));
}
for (EntityAttribute a : childAttrs.columns()) {
// 子表的外键引用列(指回主表)不作为可编辑子列展示。
if (linkColumn.equalsIgnoreCase(a.column())) {
continue;
}
childSpecAttrs.add(new RecipePageComposer.AttrSpec(
a.column(), a.display(), a.type(), a.required(), a.referenceTable(), null));
}
}
ObjectNode schema = composer.compose(new RecipePageComposer.Spec(
req.tableName.trim(), entityNameOf(req), recipe, req.title.trim(),
displayField, pageSize, specAttrs));
displayField, pageSize, specAttrs, childTable, listColumn, childSpecAttrs));
String schemaJson;
try {
@@ -316,5 +361,10 @@ public class DesignFeatureController {
public String entityName;
public String displayField;
public Integer pageSize;
// master-detail-v1 专用:子表表名 / 子表指向主表的外键列 / 主表 LIST 列名(默认 DETAILS) / LIST 显示名
public String childTable;
public String linkColumn;
public String listColumn;
public String listLabel;
}
}

View File

@@ -16,6 +16,7 @@ import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.http.ResponseEntity;
import com.eip4.platform.metadata.publish.MasterDetailLinker;
import com.eip4.platform.metadata.schema.PageSchemaWriter;
import com.eip4.platform.metadata.web.EntityAttributeReader.EntityAttributes;
import com.eip4.platform.metadata.web.EntityAttributeReader.EntityAttribute;
@@ -23,8 +24,13 @@ import com.eip4.platform.metadata.web.EntityAttributeReader.EntityAttribute;
class DesignFeatureControllerTest {
private DesignFeatureController controller(PageSchemaWriter writer, EntityAttributeReader reader) {
return controller(writer, reader, mock(MasterDetailLinker.class));
}
private DesignFeatureController controller(PageSchemaWriter writer, EntityAttributeReader reader,
MasterDetailLinker linker) {
// 包级测试构造:跳过 admin 门禁(生产构造恒走 requireAdmin)。
return new DesignFeatureController(writer, reader, true);
return new DesignFeatureController(writer, reader, linker, true);
}
@Test
@@ -75,6 +81,66 @@ class DesignFeatureControllerTest {
assertEquals(400, resp.getStatusCode().value());
}
@Test
@DisplayName("4. master-detail compile: 先 ensureLink 接线再编译,落标准层;产物含 name=DETAILS 的 input-table")
void compileMasterDetailLinksThenCompiles() {
PageSchemaWriter writer = mock(PageSchemaWriter.class);
when(writer.upsertStandard(anyString(), anyString(), anyString(), anyString()))
.thenReturn("etag-md");
EntityAttributeReader reader = mock(EntityAttributeReader.class);
when(reader.read("EIP4AI_PURCHASE")).thenReturn(new EntityAttributes(
"EIP4AI_PURCHASE", "TITLE", List.of(
new EntityAttribute("TITLE", "标题", "string", true, null),
new EntityAttribute("AMOUNT", "金额", "number", false, null))));
when(reader.read("EIP4AI_PURCHASE_ITEM")).thenReturn(new EntityAttributes(
"EIP4AI_PURCHASE_ITEM", "ITEMNAME", List.of(
new EntityAttribute("ITEMNAME", "物料", "string", true, null),
new EntityAttribute("QTY", "数量", "integer", false, null),
new EntityAttribute("EIP4AI_PURCHASE_ID", "主表", "reference", false, "EIP4AI_PURCHASE"))));
MasterDetailLinker linker = mock(MasterDetailLinker.class);
var body = new DesignFeatureController.CompileRequest();
body.tableName = "EIP4AI_PURCHASE";
body.recipe = "master-detail-v1";
body.title = "采购申请";
body.childTable = "EIP4AI_PURCHASE_ITEM";
body.linkColumn = "EIP4AI_PURCHASE_ID";
body.listColumn = "DETAILS";
body.pageSize = 20;
ResponseEntity<Map<String, Object>> resp = controller(writer, reader, linker).compile(body);
assertEquals(200, resp.getStatusCode().value());
// 接线先于编译:ensureLink 以主表/子表/外键/列表列调用一次。
verify(linker).ensureLink(eq("EIP4AI_PURCHASE"), eq("EIP4AI_PURCHASE_ITEM"),
eq("EIP4AI_PURCHASE_ID"), eq("DETAILS"), anyString(),
org.mockito.ArgumentMatchers.any());
// 标准层落一次,产物含 input-table 子表 name=DETAILS。
ArgumentCaptor<String> schemaCap = ArgumentCaptor.forClass(String.class);
verify(writer).upsertStandard(anyString(), eq("采购申请"), schemaCap.capture(), anyString());
String schema = schemaCap.getValue();
assertTrue(schema.contains("\"input-table\""), schema);
assertTrue(schema.contains("\"DETAILS\""), schema);
}
@Test
@DisplayName("5. master-detail 缺 childTable → 400,不接线不编译")
void compileMasterDetailRequiresChildTable() {
PageSchemaWriter writer = mock(PageSchemaWriter.class);
EntityAttributeReader reader = mock(EntityAttributeReader.class);
MasterDetailLinker linker = mock(MasterDetailLinker.class);
var body = new DesignFeatureController.CompileRequest();
body.tableName = "EIP4AI_PURCHASE";
body.recipe = "master-detail-v1";
body.title = "采购申请";
// 缺 childTable/linkColumn
ResponseEntity<Map<String, Object>> resp = controller(writer, reader, linker).compile(body);
assertEquals(400, resp.getStatusCode().value());
org.mockito.Mockito.verifyNoInteractions(linker);
}
@Test
@DisplayName("3. 装配回归:有两个构造器时,生产构造器必须 @Autowired,否则 Spring 回退无参构造启动崩(live e2e 揪出)")
void productionConstructorIsAutowired() {