refactor(ai): AI 本地发布裸写下沉到 AiReleaseWriter + 补值断言测试
A5 安全半步:把 AiLocalReleaseExecutor 里所有 sys_serviceflow/sys_menu 裸 JDBC(含 ensureAiMenuParent/tableExists/实体复用查询)收拢到新类 AiReleaseWriter,执行器只剩编排;SQL 逐字保留=零行为变更。 新增 AiReleaseWriterTest 4 值断言(serviceflow 审计/?::xml/bool→int、菜单 INSERT 全列值、UPDATE 全字段、自动建「AI Agent」目录),补上原黄金测试只查表名的宽松缺口。 platform-ai 30/30 绿(黄金测试经搬迁仍绿=行为未变)。与人走五环 MenuRegistrar 的完整统一(两者已分叉)留作专项,见总览 §10 A5。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -8,7 +8,6 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
@@ -18,24 +17,21 @@ import org.springframework.web.server.ResponseStatusException;
|
||||
import com.eip4.platform.ai.model.AiApplyStepResult;
|
||||
import com.eip4.platform.ai.model.AiArtifactType;
|
||||
import com.eip4.platform.ai.model.AiDraft;
|
||||
import com.eip4.platform.metadata.schema.PageSchemaWriter;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@Component
|
||||
public class AiLocalReleaseExecutor {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final AiReleaseWriter writer;
|
||||
private final AiSmokeHttpClient httpClient;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final PageSchemaWriter pageSchemaWriter;
|
||||
|
||||
public AiLocalReleaseExecutor(JdbcTemplate jdbcTemplate, AiSmokeHttpClient httpClient, ObjectMapper objectMapper) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
// A5 收口:所有 sys_serviceflow/sys_menu 裸写下沉到 AiReleaseWriter(页面 schema 仍经其内的 PageSchemaWriter)
|
||||
this.writer = new AiReleaseWriter(jdbcTemplate);
|
||||
this.httpClient = httpClient;
|
||||
this.objectMapper = objectMapper;
|
||||
// M3a 收口:标准层页面 schema 写统一走 PageSchemaWriter(与设计中心新建端点共用单一写真相)
|
||||
this.pageSchemaWriter = new PageSchemaWriter(jdbcTemplate);
|
||||
}
|
||||
|
||||
public List<AiApplyStepResult> apply(String baseUrl, List<AiDraft> drafts) {
|
||||
@@ -93,9 +89,7 @@ public class AiLocalReleaseExecutor {
|
||||
if (root.path("status").asInt(1) != 0) {
|
||||
String message = root.path("msg").asText("unknown");
|
||||
if (message.contains("实体已存在")) {
|
||||
List<String> ids = jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(sys_entity_id) FROM sys_entity WHERE tablename=? AND isactive=1",
|
||||
String.class, tableName);
|
||||
List<String> ids = writer.findActiveEntityIdsByTable(tableName);
|
||||
if (!ids.isEmpty()) {
|
||||
ctx.entityId(ids.get(0));
|
||||
ctx.tableName(tableName);
|
||||
@@ -123,28 +117,13 @@ public class AiLocalReleaseExecutor {
|
||||
for (JsonNode flow : draft.payload().path("flows")) {
|
||||
String key = flow.path("key").asText();
|
||||
String xml = flow.path("contentXml").asText().replace("${entityId}", ctx.entityId());
|
||||
jdbcTemplate.update("""
|
||||
INSERT INTO sys_serviceflow
|
||||
(sys_serviceflow_id, displayname, contentxml, entityid, iscontrol, isupdateable,
|
||||
isactive, createtime, updatetime, createby, updateby, version)
|
||||
VALUES (?, ?, ?::xml, ?, ?, ?, 1, ?, ?, 'AI_AGENT', 'AI_AGENT', 0)
|
||||
ON CONFLICT (sys_serviceflow_id) DO UPDATE SET
|
||||
displayname = EXCLUDED.displayname,
|
||||
contentxml = EXCLUDED.contentxml,
|
||||
entityid = EXCLUDED.entityid,
|
||||
iscontrol = EXCLUDED.iscontrol,
|
||||
isupdateable = EXCLUDED.isupdateable,
|
||||
updatetime = EXCLUDED.updatetime,
|
||||
updateby = EXCLUDED.updateby,
|
||||
version = sys_serviceflow.version + 1
|
||||
""",
|
||||
writer.upsertServiceFlow(
|
||||
key,
|
||||
flow.path("displayName").asText(key),
|
||||
xml,
|
||||
ctx.entityId(),
|
||||
flow.path("control").asBoolean(true) ? 1 : 0,
|
||||
flow.path("updateable").asBoolean(true) ? 1 : 0,
|
||||
now,
|
||||
flow.path("control").asBoolean(true),
|
||||
flow.path("updateable").asBoolean(true),
|
||||
now);
|
||||
count++;
|
||||
}
|
||||
@@ -155,106 +134,23 @@ public class AiLocalReleaseExecutor {
|
||||
// M3a 收口:不再裸 INSERT sys_page_schema;走 PageSchemaWriter 唯一写真相(typed 校验告警 + etag + UPSERT)
|
||||
String schemaJson = objectMapper.writeValueAsString(draft.payload());
|
||||
String name = draft.payload().path("title").asText(draft.targetId());
|
||||
String etag = pageSchemaWriter.upsertStandard(draft.targetId(), name, schemaJson, "AI_AGENT");
|
||||
String etag = writer.upsertPageSchema(draft.targetId(), name, schemaJson);
|
||||
return applied(index, draft, "页面 schema 已发布", Map.of("templateId", draft.targetId(), "etag", etag));
|
||||
}
|
||||
|
||||
private AiApplyStepResult applyMenu(ApplyContext ctx, AiDraft draft, int index) {
|
||||
if (!tableExists("sys_menu")) {
|
||||
if (!writer.tableExists("sys_menu")) {
|
||||
return skipped(index, draft, "本地库缺少 sys_menu,菜单挂载跳过,部署包保留人工 SQL");
|
||||
}
|
||||
String templateId = draft.payload().path("viewTemplateId").asText(draft.targetId());
|
||||
String menuName = draft.payload().path("name").asText(templateId);
|
||||
String parentId = textOrBlank(draft.payload().get("parentId"));
|
||||
String menuRoot = textOrBlank(draft.payload().get("menuroot"));
|
||||
if (parentId.isBlank()) {
|
||||
ParentMenu parent = ensureAiMenuParent(menuRoot);
|
||||
parentId = parent.parentId();
|
||||
menuRoot = parent.menuRoot();
|
||||
}
|
||||
List<String> existing = jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(sys_menu_id) FROM sys_menu WHERE TRIM(view_templateid)=? AND isactive=1",
|
||||
String.class, templateId);
|
||||
String menuId = existing.isEmpty() ? UUID.randomUUID().toString() : existing.get(0);
|
||||
long now = System.currentTimeMillis();
|
||||
if (existing.isEmpty()) {
|
||||
jdbcTemplate.update("""
|
||||
INSERT INTO sys_menu
|
||||
(sys_menu_id, name, parentid, view_templateid, typename, menuroot, seq,
|
||||
isactive, version, createtime, createby, updatetime, updateby)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, 1, 0, ?, 'AI_AGENT', ?, 'AI_AGENT')
|
||||
""",
|
||||
menuId,
|
||||
menuName,
|
||||
parentId,
|
||||
templateId,
|
||||
draft.payload().path("typename").asText("功能菜单"),
|
||||
menuRoot.isBlank() ? null : menuRoot,
|
||||
draft.payload().path("seq").asInt(0),
|
||||
now,
|
||||
now);
|
||||
} else {
|
||||
jdbcTemplate.update("""
|
||||
UPDATE sys_menu
|
||||
SET name=?, parentid=?, view_templateid=?, typename=?, menuroot=?, seq=?,
|
||||
updatetime=?, updateby='AI_AGENT', version=version+1
|
||||
WHERE TRIM(sys_menu_id)=?
|
||||
""",
|
||||
menuName,
|
||||
parentId,
|
||||
templateId,
|
||||
draft.payload().path("typename").asText("功能菜单"),
|
||||
menuRoot.isBlank() ? null : menuRoot,
|
||||
draft.payload().path("seq").asInt(0),
|
||||
now,
|
||||
menuId);
|
||||
}
|
||||
return applied(index, draft, "菜单已挂载", Map.of("menuId", menuId, "parentId", parentId));
|
||||
}
|
||||
|
||||
private ParentMenu ensureAiMenuParent(String preferredRoot) {
|
||||
List<String> existingDir = jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(sys_menu_id) FROM sys_menu WHERE name='AI Agent' AND parentid IS NULL AND isactive=1 ORDER BY seq LIMIT 1",
|
||||
String.class);
|
||||
if (!existingDir.isEmpty()) {
|
||||
String root = preferredRoot;
|
||||
if (root == null || root.isBlank()) {
|
||||
List<String> roots = jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(menuroot) FROM sys_menu WHERE TRIM(sys_menu_id)=?",
|
||||
String.class, existingDir.get(0));
|
||||
root = roots.isEmpty() ? "" : roots.get(0);
|
||||
}
|
||||
return new ParentMenu(existingDir.get(0), root == null ? "" : root);
|
||||
}
|
||||
String root = preferredRoot;
|
||||
if (root == null || root.isBlank()) {
|
||||
List<String> roots = jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(sys_menu_root_id) FROM sys_menu_root WHERE isactive=1 ORDER BY seq LIMIT 1",
|
||||
String.class);
|
||||
if (roots.isEmpty()) {
|
||||
throw new ResponseStatusException(HttpStatus.CONFLICT, "缺少 sys_menu_root,不能自动挂载菜单");
|
||||
}
|
||||
root = roots.get(0);
|
||||
}
|
||||
String dirId = UUID.randomUUID().toString();
|
||||
long now = System.currentTimeMillis();
|
||||
jdbcTemplate.update("""
|
||||
INSERT INTO sys_menu
|
||||
(sys_menu_id, name, parentid, view_templateid, typename, menuroot, seq,
|
||||
isactive, version, createtime, createby, updatetime, updateby)
|
||||
VALUES (?, 'AI Agent', NULL, NULL, '目录', ?, 9999, 1, 0, ?, 'AI_AGENT', ?, 'AI_AGENT')
|
||||
""",
|
||||
dirId, root, now, now);
|
||||
return new ParentMenu(dirId, root);
|
||||
}
|
||||
|
||||
private boolean tableExists(String tableName) {
|
||||
try {
|
||||
Boolean exists = jdbcTemplate.queryForObject("SELECT to_regclass(?) IS NOT NULL", Boolean.class, tableName);
|
||||
return Boolean.TRUE.equals(exists);
|
||||
} catch (RuntimeException ex) {
|
||||
return false;
|
||||
}
|
||||
String typename = draft.payload().path("typename").asText("功能菜单");
|
||||
int seq = draft.payload().path("seq").asInt(0);
|
||||
AiReleaseWriter.MenuWrite mw = writer.upsertMenu(templateId, menuName, parentId, menuRoot, typename, seq,
|
||||
System.currentTimeMillis());
|
||||
return applied(index, draft, "菜单已挂载", Map.of("menuId", mw.menuId(), "parentId", mw.parentId()));
|
||||
}
|
||||
|
||||
private static AiApplyStepResult applied(int index, AiDraft draft, String message, Map<String, Object> details) {
|
||||
@@ -341,6 +237,4 @@ public class AiLocalReleaseExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
private record ParentMenu(String parentId, String menuRoot) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
package com.eip4.platform.ai.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import com.eip4.platform.metadata.schema.PageSchemaWriter;
|
||||
|
||||
/**
|
||||
* AI 本地发布的<strong>受控写真相</strong>(总览 §10 A5)。
|
||||
*
|
||||
* <p>把原先散在 {@link AiLocalReleaseExecutor} 里的裸 {@code JdbcTemplate} 写
|
||||
* (sys_serviceflow / sys_menu)收拢到本类,使执行器只管编排、不直接碰库;
|
||||
* 标准层页面 schema 继续走 {@link PageSchemaWriter}(M3a 单一写真相)。
|
||||
*
|
||||
* <p>SQL 与原执行器逐字一致(纯搬迁、零行为变更)。菜单挂载逻辑刻意<em>未</em>复用
|
||||
* 人走五环的 {@code MenuRegistrar}:两者已分叉(本类 UPDATE 全字段 + operator 字面量
|
||||
* 'AI_AGENT' + 顶级目录「AI Agent」),统一为单一写器属后续专项(见 §10 A5)。
|
||||
*/
|
||||
class AiReleaseWriter {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final PageSchemaWriter pageSchemaWriter;
|
||||
|
||||
AiReleaseWriter(JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.pageSchemaWriter = new PageSchemaWriter(jdbcTemplate);
|
||||
}
|
||||
|
||||
/** 物理表是否存在(本地库可能缺 sys_menu 等)。 */
|
||||
boolean tableExists(String tableName) {
|
||||
try {
|
||||
Boolean exists = jdbcTemplate.queryForObject("SELECT to_regclass(?) IS NOT NULL", Boolean.class, tableName);
|
||||
return Boolean.TRUE.equals(exists);
|
||||
} catch (RuntimeException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** 实体「已存在」时按表名查回既有 sys_entity_id(发布幂等)。 */
|
||||
List<String> findActiveEntityIdsByTable(String tableName) {
|
||||
return jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(sys_entity_id) FROM sys_entity WHERE tablename=? AND isactive=1",
|
||||
String.class, tableName);
|
||||
}
|
||||
|
||||
/** UPSERT 单条服务流(主键冲突则更新 + version+1);createby/updateby 恒 'AI_AGENT'。 */
|
||||
void upsertServiceFlow(String key, String displayName, String contentXml, String entityId,
|
||||
boolean control, boolean updateable, long now) {
|
||||
jdbcTemplate.update("""
|
||||
INSERT INTO sys_serviceflow
|
||||
(sys_serviceflow_id, displayname, contentxml, entityid, iscontrol, isupdateable,
|
||||
isactive, createtime, updatetime, createby, updateby, version)
|
||||
VALUES (?, ?, ?::xml, ?, ?, ?, 1, ?, ?, 'AI_AGENT', 'AI_AGENT', 0)
|
||||
ON CONFLICT (sys_serviceflow_id) DO UPDATE SET
|
||||
displayname = EXCLUDED.displayname,
|
||||
contentxml = EXCLUDED.contentxml,
|
||||
entityid = EXCLUDED.entityid,
|
||||
iscontrol = EXCLUDED.iscontrol,
|
||||
isupdateable = EXCLUDED.isupdateable,
|
||||
updatetime = EXCLUDED.updatetime,
|
||||
updateby = EXCLUDED.updateby,
|
||||
version = sys_serviceflow.version + 1
|
||||
""",
|
||||
key, displayName, contentXml, entityId,
|
||||
control ? 1 : 0, updateable ? 1 : 0, now, now);
|
||||
}
|
||||
|
||||
/** 标准层页面 schema 写,委托 PageSchemaWriter(typed 校验告警 + etag + UPSERT)。返回 etag。 */
|
||||
String upsertPageSchema(String targetId, String name, String schemaJson) {
|
||||
return pageSchemaWriter.upsertStandard(targetId, name, schemaJson, "AI_AGENT");
|
||||
}
|
||||
|
||||
/**
|
||||
* 挂载/更新功能菜单:dedup by view_templateid(命中走 UPDATE 全字段),parentId 为空则
|
||||
* 自动 ensure/复用顶级目录「AI Agent」。返回最终 menuId 与 parentId。
|
||||
*/
|
||||
MenuWrite upsertMenu(String templateId, String menuName, String parentId, String menuRoot,
|
||||
String typename, int seq, long now) {
|
||||
String pid = parentId;
|
||||
String root = menuRoot;
|
||||
if (pid == null || pid.isBlank()) {
|
||||
ParentMenu parent = ensureAiMenuParent(root);
|
||||
pid = parent.parentId();
|
||||
root = parent.menuRoot();
|
||||
}
|
||||
List<String> existing = jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(sys_menu_id) FROM sys_menu WHERE TRIM(view_templateid)=? AND isactive=1",
|
||||
String.class, templateId);
|
||||
String menuId = existing.isEmpty() ? UUID.randomUUID().toString() : existing.get(0);
|
||||
Object rootArg = (root == null || root.isBlank()) ? null : root;
|
||||
if (existing.isEmpty()) {
|
||||
jdbcTemplate.update("""
|
||||
INSERT INTO sys_menu
|
||||
(sys_menu_id, name, parentid, view_templateid, typename, menuroot, seq,
|
||||
isactive, version, createtime, createby, updatetime, updateby)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, 1, 0, ?, 'AI_AGENT', ?, 'AI_AGENT')
|
||||
""",
|
||||
menuId, menuName, pid, templateId, typename, rootArg, seq, now, now);
|
||||
} else {
|
||||
jdbcTemplate.update("""
|
||||
UPDATE sys_menu
|
||||
SET name=?, parentid=?, view_templateid=?, typename=?, menuroot=?, seq=?,
|
||||
updatetime=?, updateby='AI_AGENT', version=version+1
|
||||
WHERE TRIM(sys_menu_id)=?
|
||||
""",
|
||||
menuName, pid, templateId, typename, rootArg, seq, now, menuId);
|
||||
}
|
||||
return new MenuWrite(menuId, pid);
|
||||
}
|
||||
|
||||
private ParentMenu ensureAiMenuParent(String preferredRoot) {
|
||||
List<String> existingDir = jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(sys_menu_id) FROM sys_menu WHERE name='AI Agent' AND parentid IS NULL AND isactive=1 ORDER BY seq LIMIT 1",
|
||||
String.class);
|
||||
if (!existingDir.isEmpty()) {
|
||||
String root = preferredRoot;
|
||||
if (root == null || root.isBlank()) {
|
||||
List<String> roots = jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(menuroot) FROM sys_menu WHERE TRIM(sys_menu_id)=?",
|
||||
String.class, existingDir.get(0));
|
||||
root = roots.isEmpty() ? "" : roots.get(0);
|
||||
}
|
||||
return new ParentMenu(existingDir.get(0), root == null ? "" : root);
|
||||
}
|
||||
String root = preferredRoot;
|
||||
if (root == null || root.isBlank()) {
|
||||
List<String> roots = jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(sys_menu_root_id) FROM sys_menu_root WHERE isactive=1 ORDER BY seq LIMIT 1",
|
||||
String.class);
|
||||
if (roots.isEmpty()) {
|
||||
throw new ResponseStatusException(HttpStatus.CONFLICT, "缺少 sys_menu_root,不能自动挂载菜单");
|
||||
}
|
||||
root = roots.get(0);
|
||||
}
|
||||
String dirId = UUID.randomUUID().toString();
|
||||
long now = System.currentTimeMillis();
|
||||
jdbcTemplate.update("""
|
||||
INSERT INTO sys_menu
|
||||
(sys_menu_id, name, parentid, view_templateid, typename, menuroot, seq,
|
||||
isactive, version, createtime, createby, updatetime, updateby)
|
||||
VALUES (?, 'AI Agent', NULL, NULL, '目录', ?, 9999, 1, 0, ?, 'AI_AGENT', ?, 'AI_AGENT')
|
||||
""",
|
||||
dirId, root, now, now);
|
||||
return new ParentMenu(dirId, root);
|
||||
}
|
||||
|
||||
/** 菜单写结果:最终 menuId 与 parentId。 */
|
||||
record MenuWrite(String menuId, String parentId) {
|
||||
}
|
||||
|
||||
private record ParentMenu(String parentId, String menuRoot) {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user