feat(meta): GET /api/meta/entity/{table}/attributes 列清单+displayColumn+引用列反解
抽出可复用 @Component EntityAttributesService(records Result/Column,读真实表 SYS_ENTITYATTRIBUTE⋈SYS_ENTITY,引用目标经 DataDicService.getReferenceEntityTableName 反解),MetaController.attributes() 委托它(供 Section 2 同源消费)。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,126 @@
|
|||||||
|
package com.eip4.platform.metadata.meta;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import com.dsasp.server.common.dataDic.service.DataDicService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取某实体的列清单(类型/显示名/引用目标表)并推断显示列。
|
||||||
|
*
|
||||||
|
* <p>真实表 {@code SYS_ENTITYATTRIBUTE}(无 table_name 列,靠 {@code entityid ⋈ SYS_ENTITY} 过滤);
|
||||||
|
* 引用列(REFERENCEID 为 36 位 uuid)的目标表名经 {@link DataDicService#getReferenceEntityTableName(String)} 反解。
|
||||||
|
*
|
||||||
|
* <p>本 service 是建模器后端(MetaController.attributes)与 recipe 编译(Section 2 EntityAttributeReader)的
|
||||||
|
* 同源读取面,二者委托它保证同结果。
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class EntityAttributesService {
|
||||||
|
|
||||||
|
public record Column(String column, String display, String type, boolean mandatory, String referenceTable) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public record Result(String tablename, String displayColumn, List<Column> columns) {
|
||||||
|
}
|
||||||
|
|
||||||
|
private final DataDicService dataDicService;
|
||||||
|
private final JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
public EntityAttributesService(DataDicService dataDicService, JdbcTemplate jdbcTemplate) {
|
||||||
|
this.dataDicService = dataDicService;
|
||||||
|
this.jdbcTemplate = jdbcTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 读取某表的列清单 + 推断显示列;表名大小写无关。 */
|
||||||
|
public Result read(String table) {
|
||||||
|
String t = table == null ? "" : table.toUpperCase();
|
||||||
|
List<Map<String, Object>> raw = jdbcTemplate.query(
|
||||||
|
"SELECT A.COLUMNNAME, A.DISPLAYNAME, A.REFERENCEID, A.ISMANDATORY, A.ISKEY, "
|
||||||
|
+ "A.ISENTITYDISPLAY, A.ENTITYDISPLAYSEQ "
|
||||||
|
+ "FROM SYS_ENTITYATTRIBUTE A INNER JOIN SYS_ENTITY E ON A.ENTITYID=E.SYS_ENTITY_ID "
|
||||||
|
+ "WHERE E.TABLENAME=? AND E.ISACTIVE=1 AND A.ISACTIVE=1 "
|
||||||
|
+ "ORDER BY A.ENTITYDISPLAYSEQ ASC",
|
||||||
|
(rs, i) -> {
|
||||||
|
Map<String, Object> m = new LinkedHashMap<>();
|
||||||
|
m.put("COLUMNNAME", rs.getString("COLUMNNAME"));
|
||||||
|
m.put("DISPLAYNAME", rs.getString("DISPLAYNAME"));
|
||||||
|
m.put("REFERENCEID", rs.getString("REFERENCEID"));
|
||||||
|
m.put("ISMANDATORY", rs.getInt("ISMANDATORY"));
|
||||||
|
m.put("ISKEY", rs.getInt("ISKEY"));
|
||||||
|
m.put("ISENTITYDISPLAY", rs.getInt("ISENTITYDISPLAY"));
|
||||||
|
return m;
|
||||||
|
},
|
||||||
|
t);
|
||||||
|
|
||||||
|
List<Column> columns = new ArrayList<>();
|
||||||
|
String displayByFlag = null;
|
||||||
|
String displayByString = null;
|
||||||
|
String keyColumn = null;
|
||||||
|
for (Map<String, Object> r : raw) {
|
||||||
|
String col = (String) r.get("COLUMNNAME");
|
||||||
|
String refId = (String) r.get("REFERENCEID");
|
||||||
|
boolean isKey = asInt(r.get("ISKEY")) == 1;
|
||||||
|
boolean isEntityDisplay = asInt(r.get("ISENTITYDISPLAY")) == 1;
|
||||||
|
boolean mandatory = asInt(r.get("ISMANDATORY")) == 1;
|
||||||
|
String apiType = refToApiType(refId);
|
||||||
|
String referenceTable = "reference".equals(apiType)
|
||||||
|
? dataDicService.getReferenceEntityTableName(refId) : null;
|
||||||
|
|
||||||
|
columns.add(new Column(col, (String) r.get("DISPLAYNAME"), apiType, mandatory, referenceTable));
|
||||||
|
|
||||||
|
if (isKey) {
|
||||||
|
keyColumn = col;
|
||||||
|
}
|
||||||
|
if (isEntityDisplay && displayByFlag == null) {
|
||||||
|
displayByFlag = col;
|
||||||
|
}
|
||||||
|
if (displayByString == null && !isKey && "string".equals(apiType) && !isAuditColumn(col)) {
|
||||||
|
displayByString = col;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String displayColumn = displayByFlag != null ? displayByFlag
|
||||||
|
: (displayByString != null ? displayByString : keyColumn);
|
||||||
|
return new Result(t, displayColumn, columns);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** REFERENCEID 反映射成 API 语义类型;36 位 uuid(外键引用) → reference。 */
|
||||||
|
public static String refToApiType(String referenceId) {
|
||||||
|
if (referenceId == null) {
|
||||||
|
return "string";
|
||||||
|
}
|
||||||
|
return switch (referenceId) {
|
||||||
|
case "NVARCHAR", "CODE", "CHAR" -> "string";
|
||||||
|
case "INTEGER", "ENUMERATE" -> "integer";
|
||||||
|
case "DECIMAL", "MONEY" -> "number";
|
||||||
|
case "DATE" -> "date";
|
||||||
|
case "TIME" -> "datetime";
|
||||||
|
case "BOOLEAN" -> "boolean";
|
||||||
|
default -> referenceId.length() >= 32 ? "reference" : "string";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isAuditColumn(String col) {
|
||||||
|
return "VERSION".equals(col) || "CREATETIME".equals(col) || "UPDATETIME".equals(col)
|
||||||
|
|| "CREATEBY".equals(col) || "UPDATEBY".equals(col) || "ISACTIVE".equals(col)
|
||||||
|
|| "ORGID".equals(col) || "DEPARTID".equals(col);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int asInt(Object v) {
|
||||||
|
if (v instanceof Number n) {
|
||||||
|
return n.intValue();
|
||||||
|
}
|
||||||
|
if (v != null) {
|
||||||
|
try {
|
||||||
|
return Integer.parseInt(v.toString());
|
||||||
|
} catch (NumberFormatException ignore) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,6 +21,7 @@ import com.dsasp.server.common.dataDic.service.DataDicService;
|
|||||||
import com.dsasp.server.common.dataDic.service.POJOCRUDService;
|
import com.dsasp.server.common.dataDic.service.POJOCRUDService;
|
||||||
import com.dsasp.server.common.util.UserSession;
|
import com.dsasp.server.common.util.UserSession;
|
||||||
import com.dsasp.server.dataDic.bean.Entity;
|
import com.dsasp.server.dataDic.bean.Entity;
|
||||||
|
import com.eip4.platform.metadata.meta.EntityAttributesService;
|
||||||
import com.eip4.platform.metadata.perf.BacklogIndexScriptGenerator;
|
import com.eip4.platform.metadata.perf.BacklogIndexScriptGenerator;
|
||||||
import com.eip4.platform.metadata.perf.IndexEnhancer;
|
import com.eip4.platform.metadata.perf.IndexEnhancer;
|
||||||
import com.eip4.platform.metadata.perf.PoInfoCacheInvalidator;
|
import com.eip4.platform.metadata.perf.PoInfoCacheInvalidator;
|
||||||
@@ -167,6 +168,33 @@ public class MetaController {
|
|||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "列出某表的列(类型/显示名)并标出引用列目标表与显示列(建模器/recipe 用)")
|
||||||
|
@GetMapping("/api/meta/entity/{table}/attributes")
|
||||||
|
public Map<String, Object> attributes(@PathVariable String table) {
|
||||||
|
EntityAttributesService.Result result =
|
||||||
|
new EntityAttributesService(dataDicService, jdbcTemplate).read(table);
|
||||||
|
List<Map<String, Object>> columns = new ArrayList<>();
|
||||||
|
for (EntityAttributesService.Column c : result.columns()) {
|
||||||
|
Map<String, Object> m = new LinkedHashMap<>();
|
||||||
|
m.put("column", c.column());
|
||||||
|
m.put("display", c.display());
|
||||||
|
m.put("type", c.type());
|
||||||
|
m.put("mandatory", c.mandatory());
|
||||||
|
if (c.referenceTable() != null) {
|
||||||
|
m.put("referenceTable", c.referenceTable());
|
||||||
|
}
|
||||||
|
columns.add(m);
|
||||||
|
}
|
||||||
|
Map<String, Object> data = new LinkedHashMap<>();
|
||||||
|
data.put("tablename", result.tablename());
|
||||||
|
data.put("displayColumn", result.displayColumn());
|
||||||
|
data.put("columns", columns);
|
||||||
|
Map<String, Object> resp = new LinkedHashMap<>();
|
||||||
|
resp.put("status", 0);
|
||||||
|
resp.put("data", data);
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
|
||||||
@Operation(summary = "删除实体:drop 物理表并清除元数据(仅限 EIP4AI_ 前缀,防误删)")
|
@Operation(summary = "删除实体:drop 物理表并清除元数据(仅限 EIP4AI_ 前缀,防误删)")
|
||||||
@DeleteMapping("/api/meta/entity/{table}")
|
@DeleteMapping("/api/meta/entity/{table}")
|
||||||
public Map<String, Object> dropEntity(@PathVariable String table) {
|
public Map<String, Object> dropEntity(@PathVariable String table) {
|
||||||
|
|||||||
@@ -35,6 +35,27 @@ class MetaCatalogControllerTest {
|
|||||||
mock(BacklogIndexScriptGenerator.class));
|
mock(BacklogIndexScriptGenerator.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private MetaController newController(JdbcTemplate jdbc, DataDicService dataDic) {
|
||||||
|
return new MetaController(
|
||||||
|
dataDic,
|
||||||
|
mock(POJOCRUDService.class),
|
||||||
|
jdbc,
|
||||||
|
mock(IndexEnhancer.class),
|
||||||
|
mock(PoInfoCacheInvalidator.class),
|
||||||
|
mock(BacklogIndexScriptGenerator.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, Object> attrRow(String col, String display, String refId,
|
||||||
|
int isKey, int isEntityDisplay) {
|
||||||
|
Map<String, Object> m = new LinkedHashMap<>();
|
||||||
|
m.put("COLUMNNAME", col);
|
||||||
|
m.put("DISPLAYNAME", display);
|
||||||
|
m.put("REFERENCEID", refId);
|
||||||
|
m.put("ISKEY", isKey);
|
||||||
|
m.put("ISENTITYDISPLAY", isEntityDisplay);
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("entities() 返回 ISACTIVE=1 AND ISVIEW=0 的实体目录 {entityId,tablename,name}")
|
@DisplayName("entities() 返回 ISACTIVE=1 AND ISVIEW=0 的实体目录 {entityId,tablename,name}")
|
||||||
void entitiesReturnsCatalog() {
|
void entitiesReturnsCatalog() {
|
||||||
@@ -71,4 +92,37 @@ class MetaCatalogControllerTest {
|
|||||||
assertTrue(captured[0].toUpperCase().contains("ISVIEW=0")
|
assertTrue(captured[0].toUpperCase().contains("ISVIEW=0")
|
||||||
|| captured[0].toUpperCase().contains("ISVIEW = 0"));
|
|| captured[0].toUpperCase().contains("ISVIEW = 0"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("attributes() 映射类型 + 引用列反解 referenceTable + 推断 displayColumn")
|
||||||
|
void attributesMapsTypesAndResolvesReference() {
|
||||||
|
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||||
|
DataDicService dataDic = mock(DataDicService.class);
|
||||||
|
String refUuid = "11111111-1111-1111-1111-111111111111"; // 36 位
|
||||||
|
when(jdbc.query(contains("SYS_ENTITYATTRIBUTE"), (RowMapper) org.mockito.ArgumentMatchers.any(),
|
||||||
|
(Object[]) org.mockito.ArgumentMatchers.any()))
|
||||||
|
.thenReturn(List.of(
|
||||||
|
attrRow("EIP4_ORDER_ID", "主键", "CHAR", 1, 0),
|
||||||
|
attrRow("TITLE", "标题", "NVARCHAR", 0, 0),
|
||||||
|
attrRow("QTY", "数量", "INTEGER", 0, 0),
|
||||||
|
attrRow("CUSTOMERID", "客户", refUuid, 0, 0)));
|
||||||
|
when(dataDic.getReferenceEntityTableName(refUuid)).thenReturn("EIP4_CUSTOMER");
|
||||||
|
|
||||||
|
Map<String, Object> resp = newController(jdbc, dataDic).attributes("eip4_order");
|
||||||
|
|
||||||
|
assertEquals(0, resp.get("status"));
|
||||||
|
Map<String, Object> data = (Map<String, Object>) resp.get("data");
|
||||||
|
assertEquals("EIP4_ORDER", data.get("tablename"));
|
||||||
|
// 无 ISENTITYDISPLAY → 首个非主键非审计的 string 列 = TITLE
|
||||||
|
assertEquals("TITLE", data.get("displayColumn"));
|
||||||
|
List<Map<String, Object>> cols = (List<Map<String, Object>>) data.get("columns");
|
||||||
|
Map<String, Object> qty = cols.stream().filter(c -> "QTY".equals(c.get("column"))).findFirst().get();
|
||||||
|
assertEquals("integer", qty.get("type"));
|
||||||
|
Map<String, Object> cust = cols.stream().filter(c -> "CUSTOMERID".equals(c.get("column"))).findFirst().get();
|
||||||
|
assertEquals("reference", cust.get("type"));
|
||||||
|
assertEquals("EIP4_CUSTOMER", cust.get("referenceTable"));
|
||||||
|
Map<String, Object> title = cols.stream().filter(c -> "TITLE".equals(c.get("column"))).findFirst().get();
|
||||||
|
assertEquals("string", title.get("type"));
|
||||||
|
assertTrue(title.get("referenceTable") == null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user