面试功能开发
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
package com.ruoyi.cms.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
|
||||
/**
|
||||
* 面试菜单权限自动迁移
|
||||
* 应用启动后自动检查并添加面试列表菜单及按钮权限到 sys_menu 和 sys_role_menu 表
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class InterviewMenuMigration {
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void migrate() {
|
||||
log.info("开始检查面试菜单权限...");
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
// Step 1: 查找岗位管理目录的 menu_id
|
||||
Long parentId = null;
|
||||
try (Statement stmt = conn.createStatement();
|
||||
ResultSet rs = stmt.executeQuery(
|
||||
"SELECT menu_id FROM sys_menu WHERE path = 'management' AND menu_type = 'M' LIMIT 1")) {
|
||||
if (rs.next()) {
|
||||
parentId = rs.getLong("menu_id");
|
||||
}
|
||||
}
|
||||
|
||||
if (parentId == null) {
|
||||
log.warn("未找到 management 目录菜单,跳过面试菜单迁移");
|
||||
return;
|
||||
}
|
||||
|
||||
log.info("找到 management 目录菜单,parent_id={}", parentId);
|
||||
|
||||
// Step 2: 插入面试列表菜单(幂等)
|
||||
Long menuId = insertMenuIfNotExists(conn, 2100L, "面试列表", parentId, 10,
|
||||
"interview-list", "Management/InterviewList", "InterviewList",
|
||||
"C", "cms:interview:list", "message");
|
||||
|
||||
if (menuId == null) {
|
||||
log.warn("面试列表菜单插入失败(可能已存在但 parent_id 不同)");
|
||||
return;
|
||||
}
|
||||
|
||||
// Step 3: 插入按钮权限
|
||||
insertMenuIfNotExists(conn, 2101L, "面试查询", menuId, 1,
|
||||
"", "", "", "F", "cms:interview:query", "#");
|
||||
insertMenuIfNotExists(conn, 2102L, "面试新增", menuId, 2,
|
||||
"", "", "", "F", "cms:interview:add", "#");
|
||||
insertMenuIfNotExists(conn, 2103L, "面试修改", menuId, 3,
|
||||
"", "", "", "F", "cms:interview:edit", "#");
|
||||
insertMenuIfNotExists(conn, 2104L, "面试删除", menuId, 4,
|
||||
"", "", "", "F", "cms:interview:remove", "#");
|
||||
|
||||
// Step 4: 将新菜单赋给所有非管理员角色
|
||||
try (Statement stmt = conn.createStatement()) {
|
||||
stmt.execute(
|
||||
"INSERT INTO sys_role_menu (role_id, menu_id) " +
|
||||
"SELECT r.role_id, " + menuId + " FROM sys_role r " +
|
||||
"WHERE r.role_key != 'admin' " +
|
||||
"AND NOT EXISTS (SELECT 1 FROM sys_role_menu rm WHERE rm.role_id = r.role_id AND rm.menu_id = " + menuId + ")");
|
||||
} catch (Exception e) {
|
||||
log.debug("角色菜单关联已存在或插入失败: {}", e.getMessage());
|
||||
}
|
||||
|
||||
// 按钮也赋给角色
|
||||
for (long btnId : new long[]{2101L, 2102L, 2103L, 2104L}) {
|
||||
try (Statement stmt = conn.createStatement()) {
|
||||
stmt.execute(
|
||||
"INSERT INTO sys_role_menu (role_id, menu_id) " +
|
||||
"SELECT r.role_id, " + btnId + " FROM sys_role r " +
|
||||
"WHERE r.role_key != 'admin' " +
|
||||
"AND NOT EXISTS (SELECT 1 FROM sys_role_menu rm WHERE rm.role_id = r.role_id AND rm.menu_id = " + btnId + ")");
|
||||
} catch (Exception e) {
|
||||
log.debug("按钮角色关联已存在: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
log.info("面试菜单权限迁移完成");
|
||||
} catch (Exception e) {
|
||||
log.error("面试菜单迁移失败: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 幂等插入菜单记录
|
||||
*/
|
||||
private Long insertMenuIfNotExists(Connection conn, Long menuId, String menuName,
|
||||
Long parentId, int orderNum, String path,
|
||||
String component, String routeName,
|
||||
String menuType, String perms, String icon) {
|
||||
try {
|
||||
// 检查是否已存在
|
||||
try (PreparedStatement ps = conn.prepareStatement(
|
||||
"SELECT 1 FROM sys_menu WHERE menu_id = ?")) {
|
||||
ps.setLong(1, menuId);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
log.debug("菜单已存在: {} (id={})", menuName, menuId);
|
||||
return menuId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 插入新菜单
|
||||
try (PreparedStatement ps = conn.prepareStatement(
|
||||
"INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, query, route_name, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) " +
|
||||
"VALUES (?, ?, ?, ?, ?, ?, '', ?, 1, 0, ?, '0', '0', ?, ?, 'admin', CURRENT_TIMESTAMP)")) {
|
||||
ps.setLong(1, menuId);
|
||||
ps.setString(2, menuName);
|
||||
ps.setLong(3, parentId);
|
||||
ps.setInt(4, orderNum);
|
||||
ps.setString(5, path);
|
||||
ps.setString(6, component);
|
||||
ps.setString(7, routeName);
|
||||
ps.setString(8, menuType);
|
||||
ps.setString(9, perms);
|
||||
ps.setString(10, icon);
|
||||
ps.executeUpdate();
|
||||
log.info("菜单创建成功: {} (id={}, perms={})", menuName, menuId, perms);
|
||||
return menuId;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("菜单创建失败: {} (id={}), error={}", menuName, menuId, e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user