feat(design): publish 菜单注册 MenuRegistrar(dedup+新功能目录)(建物3-Task12)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,14 +1,104 @@
|
||||
package com.eip4.platform.metadata.publish;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
/** 平台内菜单注册唯一入口(Task 12 实现)。 */
|
||||
/**
|
||||
* 平台内菜单注册唯一入口(建物 3 · 发布编排)。
|
||||
*
|
||||
* <p>镜像 {@code AiLocalReleaseExecutor.applyMenu / ensureAiMenuParent}:
|
||||
* dedup by view_templateid(同一页面只挂一条菜单,重复发布走 UPDATE 而非新建),
|
||||
* parentMenuId 为空时自动 ensure/复用顶级目录「新功能」(人走五环语义;AI 链用「AI Agent」)。
|
||||
*/
|
||||
@Service
|
||||
public class MenuRegistrar {
|
||||
|
||||
private static final String NEW_FEATURE_DIR = "新功能";
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
public MenuRegistrar(JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册/更新功能菜单。
|
||||
*
|
||||
* @param templateId 页面标准层 templateId(写入 sys_menu.view_templateid)
|
||||
* @param menuName 菜单显示名
|
||||
* @param parentMenuId 上级菜单 id;为空则自动挂到「新功能」顶级目录
|
||||
* @param operator 操作人(写入 createby/updateby)
|
||||
* @return sys_menu_id(新建则为新 UUID,命中则为既有 id)—— 幂等保证
|
||||
*/
|
||||
public String register(String templateId, String menuName, String parentMenuId, String operator) {
|
||||
List<String> existing = jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(sys_menu_id) FROM sys_menu WHERE TRIM(view_templateid)=? AND isactive=1",
|
||||
String.class, templateId);
|
||||
long now = System.currentTimeMillis();
|
||||
if (!existing.isEmpty()) {
|
||||
String menuId = existing.get(0);
|
||||
jdbcTemplate.update(
|
||||
"UPDATE sys_menu SET name=?, view_templateid=?, updatetime=?, updateby=?, version=version+1"
|
||||
+ " WHERE TRIM(sys_menu_id)=?",
|
||||
menuName, templateId, now, operator, menuId);
|
||||
return menuId;
|
||||
}
|
||||
|
||||
String parentId = parentMenuId;
|
||||
String menuRoot;
|
||||
if (parentId == null || parentId.isBlank()) {
|
||||
ParentMenu parent = ensureNewFeatureParent();
|
||||
parentId = parent.parentId();
|
||||
menuRoot = parent.menuRoot();
|
||||
} else {
|
||||
List<String> roots = jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(menuroot) FROM sys_menu WHERE TRIM(sys_menu_id)=?", String.class, parentId);
|
||||
menuRoot = roots.isEmpty() ? null : roots.get(0);
|
||||
}
|
||||
|
||||
String menuId = UUID.randomUUID().toString();
|
||||
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,?,?,?,?)",
|
||||
menuId, menuName, parentId, templateId, "功能菜单",
|
||||
(menuRoot == null || menuRoot.isBlank()) ? null : menuRoot, 0,
|
||||
now, operator, now, operator);
|
||||
return menuId;
|
||||
}
|
||||
|
||||
private ParentMenu ensureNewFeatureParent() {
|
||||
List<String> existingDir = jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(sys_menu_id) FROM sys_menu WHERE name='" + NEW_FEATURE_DIR
|
||||
+ "' AND parentid IS NULL AND isactive=1 ORDER BY seq LIMIT 1",
|
||||
String.class);
|
||||
if (!existingDir.isEmpty()) {
|
||||
List<String> roots = jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(menuroot) FROM sys_menu WHERE TRIM(sys_menu_id)=?",
|
||||
String.class, existingDir.get(0));
|
||||
return new ParentMenu(existingDir.get(0), roots.isEmpty() ? "" : roots.get(0));
|
||||
}
|
||||
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,不能自动挂载菜单");
|
||||
}
|
||||
String 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(?,?,NULL,NULL,?,?,?,1,0,?,?,?,?)",
|
||||
dirId, NEW_FEATURE_DIR, "目录", root, 9999, now, "DESIGN_PUBLISH", now, "DESIGN_PUBLISH");
|
||||
return new ParentMenu(dirId, root);
|
||||
}
|
||||
|
||||
private record ParentMenu(String parentId, String menuRoot) {}
|
||||
}
|
||||
|
||||
@@ -42,6 +42,11 @@ public class PublishOrchestrator {
|
||||
return flowService.addDefaultFlowService(entityId, ctx);
|
||||
}
|
||||
|
||||
/** 注册/更新功能菜单,返回 sys_menu_id(幂等)。 */
|
||||
public String registerMenu(String templateId, String menuName, String parentMenuId, String operator) {
|
||||
return menuRegistrar.register(templateId, menuName, parentMenuId, operator);
|
||||
}
|
||||
|
||||
/** 返回实体当前持久化的流名列表(发布后读 SYS_SERVICEFLOW,经 Task 10 预览服务)。 */
|
||||
public List<String> flowNames(String entityId) {
|
||||
List<String> out = new ArrayList<>();
|
||||
|
||||
@@ -214,7 +214,11 @@ public class DesignFeatureController {
|
||||
data.put("templateId", templateId);
|
||||
data.put("flowsCreated", flowsCreated);
|
||||
data.put("flows", publishOrchestrator.flowNames(entityId));
|
||||
// menuId / deploymentPackage 由 Task 12 / 13 补齐
|
||||
|
||||
String parentMenuId = body.get("parentMenuId") == null ? null : body.get("parentMenuId").toString().trim();
|
||||
String menuId = publishOrchestrator.registerMenu(templateId, menuName, parentMenuId, operator);
|
||||
data.put("menuId", menuId);
|
||||
// deploymentPackage 由 Task 13 补齐
|
||||
Map<String, Object> resp = new LinkedHashMap<>();
|
||||
resp.put("status", 0);
|
||||
resp.put("data", data);
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.eip4.platform.metadata.publish;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
class MenuRegistrarTest {
|
||||
|
||||
@Test
|
||||
void firstRegisterInsertsOneRowUnderNewFeatureParent() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
// dedup 查:无现有菜单
|
||||
when(jdbc.queryForList(
|
||||
eq("SELECT TRIM(sys_menu_id) FROM sys_menu WHERE TRIM(view_templateid)=? AND isactive=1"),
|
||||
eq(String.class), eq("TPL-1"))).thenReturn(List.of());
|
||||
// ensure '新功能' 目录:已存在,返回其 id
|
||||
when(jdbc.queryForList(
|
||||
eq("SELECT TRIM(sys_menu_id) FROM sys_menu WHERE name='新功能' AND parentid IS NULL AND isactive=1 ORDER BY seq LIMIT 1"),
|
||||
eq(String.class))).thenReturn(List.of("DIR-NEW"));
|
||||
when(jdbc.queryForList(
|
||||
eq("SELECT TRIM(menuroot) FROM sys_menu WHERE TRIM(sys_menu_id)=?"),
|
||||
eq(String.class), eq("DIR-NEW"))).thenReturn(List.of("ROOT-1"));
|
||||
|
||||
MenuRegistrar reg = new MenuRegistrar(jdbc);
|
||||
String menuId = reg.register("TPL-1", "演示功能", null, "admin");
|
||||
|
||||
assertThat(menuId).isNotBlank();
|
||||
// 校验 INSERT 被调用一次(11 个绑定参数,含 templateId / parentId / menuRoot)。
|
||||
verify(jdbc, times(1)).update(
|
||||
anyString(),
|
||||
eq(menuId), eq("演示功能"), eq("DIR-NEW"), eq("TPL-1"),
|
||||
eq("功能菜单"), eq("ROOT-1"), eq(0),
|
||||
any(), eq("admin"), any(), eq("admin"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void republishDedupsByTemplateIdAndUpdatesInsteadOfInsert() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
when(jdbc.queryForList(
|
||||
eq("SELECT TRIM(sys_menu_id) FROM sys_menu WHERE TRIM(view_templateid)=? AND isactive=1"),
|
||||
eq(String.class), eq("TPL-1"))).thenReturn(List.of("MENU-EXIST"));
|
||||
|
||||
MenuRegistrar reg = new MenuRegistrar(jdbc);
|
||||
String menuId = reg.register("TPL-1", "演示功能改名", null, "admin");
|
||||
|
||||
assertThat(menuId).isEqualTo("MENU-EXIST");
|
||||
// 命中 → 不查 '新功能' 目录、不 INSERT,只 UPDATE(5 个绑定参数)。
|
||||
verify(jdbc, times(1)).update(
|
||||
org.mockito.ArgumentMatchers.contains("UPDATE sys_menu"),
|
||||
eq("演示功能改名"), eq("TPL-1"), any(), eq("admin"), eq("MENU-EXIST"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user