feat(meta): POST /api/meta/entity 支持 reference 列(find-or-create SYS_REFERENCE 指向目标实体)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ import java.util.LinkedHashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
@@ -87,7 +88,7 @@ public class MetaController {
|
|||||||
entityMap.remove("SYS_ENTITY_ID");
|
entityMap.remove("SYS_ENTITY_ID");
|
||||||
entityMap.put("TABLENAME", table);
|
entityMap.put("TABLENAME", table);
|
||||||
entityMap.put("NAME", String.valueOf(req.getOrDefault("name", table)));
|
entityMap.put("NAME", String.valueOf(req.getOrDefault("name", table)));
|
||||||
entityMap.put("DESCRIPTION", "AI 闭环创建 (eip4-poc WS5)");
|
entityMap.put("DESCRIPTION", resolveDescription(req));
|
||||||
GeneralPOJO newEntity = (GeneralPOJO) dataDicService.save(entityMap, ctx);
|
GeneralPOJO newEntity = (GeneralPOJO) dataDicService.save(entityMap, ctx);
|
||||||
String entityId = newEntity.get_ID();
|
String entityId = newEntity.get_ID();
|
||||||
|
|
||||||
@@ -104,13 +105,29 @@ public class MetaController {
|
|||||||
created.add(saveAttr(attrTpl, entityId, "UPDATEBY", "更新人", "NVARCHAR", 36, true, false, ctx));
|
created.add(saveAttr(attrTpl, entityId, "UPDATEBY", "更新人", "NVARCHAR", 36, true, false, ctx));
|
||||||
created.add(saveAttr(attrTpl, entityId, "ISACTIVE", "是否有效", "BOOLEAN", null, true, false, ctx));
|
created.add(saveAttr(attrTpl, entityId, "ISACTIVE", "是否有效", "BOOLEAN", null, true, false, ctx));
|
||||||
|
|
||||||
// 业务列
|
// 业务列。reference 列的目标表 → DDL 外键指向目标表(Task 4 透传)。
|
||||||
|
Map<String, String> refTargets = new HashMap<>();
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
List<Map<String, Object>> attrs = (List<Map<String, Object>>) req.get("attributes");
|
List<Map<String, Object>> attrs = (List<Map<String, Object>>) req.get("attributes");
|
||||||
if (attrs != null) {
|
if (attrs != null) {
|
||||||
for (Map<String, Object> a : attrs) {
|
for (Map<String, Object> a : attrs) {
|
||||||
String col = String.valueOf(a.get("column")).toUpperCase();
|
String col = String.valueOf(a.get("column")).toUpperCase();
|
||||||
String type = String.valueOf(a.getOrDefault("type", "string"));
|
String type = String.valueOf(a.getOrDefault("type", "string"));
|
||||||
|
boolean mandatory = Boolean.TRUE.equals(a.get("mandatory"));
|
||||||
|
String display = String.valueOf(a.getOrDefault("display", col));
|
||||||
|
|
||||||
|
if ("reference".equals(type)) {
|
||||||
|
String refTable = a.get("referenceTable") == null
|
||||||
|
? null : String.valueOf(a.get("referenceTable")).toUpperCase();
|
||||||
|
if (refTable == null || dataDicService.getSYSEntityByTableName(refTable) == null) {
|
||||||
|
return err("引用目标实体不存在: " + refTable);
|
||||||
|
}
|
||||||
|
String referenceId = findOrCreateReference(refTable, col, ctx);
|
||||||
|
created.add(saveAttr(attrTpl, entityId, col, display, referenceId, 36, mandatory, false, ctx));
|
||||||
|
refTargets.put(col, refTable);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
String ref = switch (type) {
|
String ref = switch (type) {
|
||||||
case "integer" -> "INTEGER";
|
case "integer" -> "INTEGER";
|
||||||
case "number" -> "DECIMAL";
|
case "number" -> "DECIMAL";
|
||||||
@@ -121,9 +138,7 @@ public class MetaController {
|
|||||||
};
|
};
|
||||||
Integer len = "NVARCHAR".equals(ref)
|
Integer len = "NVARCHAR".equals(ref)
|
||||||
? (a.get("length") instanceof Number n ? n.intValue() : 100) : null;
|
? (a.get("length") instanceof Number n ? n.intValue() : 100) : null;
|
||||||
boolean mandatory = Boolean.TRUE.equals(a.get("mandatory"));
|
created.add(saveAttr(attrTpl, entityId, col, display, ref, len, mandatory, false, ctx));
|
||||||
created.add(saveAttr(attrTpl, entityId,
|
|
||||||
col, String.valueOf(a.getOrDefault("display", col)), ref, len, mandatory, false, ctx));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -294,6 +309,29 @@ public class MetaController {
|
|||||||
return pojoCRUDService.save(m, "SYS_ENTITYATTRIBUTE", ctx);
|
return pojoCRUDService.save(m, "SYS_ENTITYATTRIBUTE", ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为引用列 find-or-create 一行 SYS_REFERENCE(ENTITYID=目标实体),返回其 36 位 SYS_REFERENCE_ID。
|
||||||
|
* 幂等:命中既有(同目标实体且 ISACTIVE=1)则复用。列与 ParitySeeder.createReference 一致。
|
||||||
|
*/
|
||||||
|
String findOrCreateReference(String targetTable, String column, Properties ctx) {
|
||||||
|
String targetEntityId = jdbcTemplate.queryForObject(
|
||||||
|
"SELECT SYS_ENTITY_ID FROM SYS_ENTITY WHERE ISACTIVE=1 AND TABLENAME=?",
|
||||||
|
String.class, targetTable.toUpperCase());
|
||||||
|
List<String> existing = jdbcTemplate.queryForList(
|
||||||
|
"SELECT SYS_REFERENCE_ID FROM SYS_REFERENCE WHERE ISACTIVE=1 AND ENTITYID=?",
|
||||||
|
String.class, targetEntityId);
|
||||||
|
if (!existing.isEmpty()) {
|
||||||
|
return existing.get(0);
|
||||||
|
}
|
||||||
|
String refId = UUID.randomUUID().toString();
|
||||||
|
jdbcTemplate.update(
|
||||||
|
"INSERT INTO SYS_REFERENCE"
|
||||||
|
+ "(SYS_REFERENCE_ID,ENTITYID,NAME,DESCRIPTION,ISACTIVE,CREATETIME,UPDATETIME,CREATEBY,UPDATEBY,VERSION)"
|
||||||
|
+ " VALUES (?,?,?,?,1,0,0,'ADMIN','ADMIN',0)",
|
||||||
|
refId, targetEntityId, column + "->" + targetTable.toUpperCase(), "设计中心创建");
|
||||||
|
return refId;
|
||||||
|
}
|
||||||
|
|
||||||
static String createTableDdl(String table, List<GeneralPOJO> attrs) {
|
static String createTableDdl(String table, List<GeneralPOJO> attrs) {
|
||||||
if (attrs == null || attrs.isEmpty()) {
|
if (attrs == null || attrs.isEmpty()) {
|
||||||
throw new IllegalStateException("实体属性为空,无法建表: " + table);
|
throw new IllegalStateException("实体属性为空,无法建表: " + table);
|
||||||
@@ -416,6 +454,14 @@ public class MetaController {
|
|||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static String resolveDescription(Map<String, Object> req) {
|
||||||
|
Object d = req.get("description");
|
||||||
|
if (d != null && !String.valueOf(d).isBlank()) {
|
||||||
|
return String.valueOf(d);
|
||||||
|
}
|
||||||
|
return "设计中心创建";
|
||||||
|
}
|
||||||
|
|
||||||
private static Map<String, Object> err(String msg) {
|
private static Map<String, Object> err(String msg) {
|
||||||
Map<String, Object> r = new LinkedHashMap<>();
|
Map<String, Object> r = new LinkedHashMap<>();
|
||||||
r.put("status", 1);
|
r.put("status", 1);
|
||||||
|
|||||||
@@ -0,0 +1,74 @@
|
|||||||
|
package com.eip4.platform.metadata.web;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.ArgumentMatchers.contains;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
|
||||||
|
import com.dsasp.server.common.dataDic.service.DataDicService;
|
||||||
|
import com.dsasp.server.common.dataDic.service.POJOCRUDService;
|
||||||
|
import com.eip4.platform.metadata.perf.BacklogIndexScriptGenerator;
|
||||||
|
import com.eip4.platform.metadata.perf.IndexEnhancer;
|
||||||
|
import com.eip4.platform.metadata.perf.PoInfoCacheInvalidator;
|
||||||
|
|
||||||
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
|
class MetaControllerReferenceTest {
|
||||||
|
|
||||||
|
private MetaController newController(JdbcTemplate jdbc) {
|
||||||
|
return new MetaController(
|
||||||
|
mock(DataDicService.class),
|
||||||
|
mock(POJOCRUDService.class),
|
||||||
|
jdbc,
|
||||||
|
mock(IndexEnhancer.class),
|
||||||
|
mock(PoInfoCacheInvalidator.class),
|
||||||
|
mock(BacklogIndexScriptGenerator.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("findOrCreateReference 命中既有 SYS_REFERENCE → 复用,不再 INSERT")
|
||||||
|
void findOrCreateReuses() {
|
||||||
|
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||||
|
// 目标实体 id
|
||||||
|
when(jdbc.queryForObject(contains("FROM SYS_ENTITY"), eq(String.class), anyString()))
|
||||||
|
.thenReturn("target-entity-id");
|
||||||
|
// 既有引用行
|
||||||
|
when(jdbc.queryForList(contains("FROM SYS_REFERENCE"), eq(String.class), anyString()))
|
||||||
|
.thenReturn(List.of("existing-ref-uuid"));
|
||||||
|
|
||||||
|
String refId = newController(jdbc).findOrCreateReference("EIP4_CUSTOMER", "CUSTOMERID", new Properties());
|
||||||
|
|
||||||
|
assertEquals("existing-ref-uuid", refId);
|
||||||
|
verify(jdbc, never()).update(contains("INSERT INTO SYS_REFERENCE"),
|
||||||
|
(Object[]) any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("findOrCreateReference 无既有行 → INSERT 一行并返回新 uuid(36位,ENTITYID=目标实体)")
|
||||||
|
void findOrCreateInserts() {
|
||||||
|
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||||
|
when(jdbc.queryForObject(contains("FROM SYS_ENTITY"), eq(String.class), anyString()))
|
||||||
|
.thenReturn("target-entity-id");
|
||||||
|
when(jdbc.queryForList(contains("FROM SYS_REFERENCE"), eq(String.class), anyString()))
|
||||||
|
.thenReturn(List.of());
|
||||||
|
|
||||||
|
String refId = newController(jdbc).findOrCreateReference("EIP4_CUSTOMER", "CUSTOMERID", new Properties());
|
||||||
|
|
||||||
|
assertNotNull(refId);
|
||||||
|
assertEquals(36, refId.length());
|
||||||
|
verify(jdbc).update(contains("INSERT INTO SYS_REFERENCE"),
|
||||||
|
eq(refId), eq("target-entity-id"), anyString(), anyString());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user