package com.ruoyi.web.controller.tool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.domain.AjaxResult;

@RestController
@RequestMapping("/tool/sql")
public class SqlExecuteController
{
    @PostMapping("/execute")
    public AjaxResult executeSql()
    {
        StringBuilder result = new StringBuilder();
        Connection conn = null;
        Statement stmt = null;
        
        try
        {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://172.28.135.68:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8", "root", "123456");
            stmt = conn.createStatement();
            
            String[] sqls = {
                "CREATE TABLE IF NOT EXISTS `sys_realtime_news` (" +
                "  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID'," +
                "  `title` varchar(200) NOT NULL COMMENT '动态标题'," +
                "  `summary` varchar(500) DEFAULT NULL COMMENT '简介'," +
                "  `cover_image` varchar(500) DEFAULT NULL COMMENT '封面图片'," +
                "  `publish_time` datetime NOT NULL COMMENT '发布时间'," +
                "  `content_cn` longtext COMMENT '中文内容'," +
                "  `content_en` longtext COMMENT '英文内容'," +
                "  `is_top` char(1) DEFAULT '0' COMMENT '是否置顶（0否 1是）'," +
                "  `status` char(1) DEFAULT '0' COMMENT '状态（0正常 1停用）'," +
                "  `create_by` varchar(64) DEFAULT '' COMMENT '创建者'," +
                "  `create_time` datetime DEFAULT NULL COMMENT '创建时间'," +
                "  `update_by` varchar(64) DEFAULT '' COMMENT '更新者'," +
                "  `update_time` datetime DEFAULT NULL COMMENT '更新时间'," +
                "  `remark` varchar(500) DEFAULT NULL COMMENT '备注'," +
                "  PRIMARY KEY (`id`)" +
                ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='实时动态表'",
                
                "CREATE TABLE IF NOT EXISTS `sys_milestone` (" +
                "  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID'," +
                "  `event_name` varchar(200) NOT NULL COMMENT '事件名称'," +
                "  `event_time` datetime NOT NULL COMMENT '事件时间'," +
                "  `event_description` longtext COMMENT '事件描述'," +
                "  `status` char(1) DEFAULT '0' COMMENT '状态（0正常 1停用）'," +
                "  `create_by` varchar(64) DEFAULT '' COMMENT '创建者'," +
                "  `create_time` datetime DEFAULT NULL COMMENT '创建时间'," +
                "  `update_by` varchar(64) DEFAULT '' COMMENT '更新者'," +
                "  `update_time` datetime DEFAULT NULL COMMENT '更新时间'," +
                "  `remark` varchar(500) DEFAULT NULL COMMENT '备注'," +
                "  PRIMARY KEY (`id`)" +
                ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='里程碑事件表'",
                
                "CREATE TABLE IF NOT EXISTS `sys_project` (" +
                "  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID'," +
                "  `project_name` varchar(200) NOT NULL COMMENT '项目名称'," +
                "  `project_status` char(1) DEFAULT '0' COMMENT '项目状态（0筹备中 1进行中 2已完成 3暂停）'," +
                "  `lead_unit` varchar(200) DEFAULT NULL COMMENT '牵头单位'," +
                "  `core_units` varchar(500) DEFAULT NULL COMMENT '核心参与单位'," +
                "  `project_leader` varchar(100) DEFAULT NULL COMMENT '负责人'," +
                "  `contact_phone` varchar(20) DEFAULT NULL COMMENT '联系电话'," +
                "  `project_background` longtext COMMENT '项目背景'," +
                "  `overall_goal` longtext COMMENT '总体目标'," +
                "  `implementation_period` varchar(200) DEFAULT NULL COMMENT '实施周期'," +
                "  `project_detail` longtext COMMENT '项目详情'," +
                "  `create_time` datetime DEFAULT NULL COMMENT '创建时间'," +
                "  `update_time` datetime DEFAULT NULL COMMENT '更新时间'," +
                "  `create_by` varchar(64) DEFAULT '' COMMENT '创建者'," +
                "  `update_by` varchar(64) DEFAULT '' COMMENT '更新者'," +
                "  `remark` varchar(500) DEFAULT NULL COMMENT '备注'," +
                "  PRIMARY KEY (`id`)" +
                ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='项目表'",
                
                "CREATE TABLE IF NOT EXISTS `sys_project_progress` (" +
                "  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID'," +
                "  `project_id` bigint(20) NOT NULL COMMENT '项目ID'," +
                "  `project_name` varchar(200) NOT NULL COMMENT '项目名称'," +
                "  `current_status` char(1) DEFAULT '0' COMMENT '当前状态（0正常 1延迟 2风险）'," +
                "  `current_achievement` varchar(500) DEFAULT NULL COMMENT '目前成果'," +
                "  `progress_detail` longtext COMMENT '进展详情'," +
                "  `update_time` datetime DEFAULT NULL COMMENT '更新时间'," +
                "  `update_by` varchar(64) DEFAULT '' COMMENT '更新人'," +
                "  `create_time` datetime DEFAULT NULL COMMENT '创建时间'," +
                "  `create_by` varchar(64) DEFAULT '' COMMENT '创建者'," +
                "  `remark` varchar(500) DEFAULT NULL COMMENT '备注'," +
                "  PRIMARY KEY (`id`)," +
                "  KEY `idx_project_id` (`project_id`)" +
                ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='项目进展表'",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) VALUES ('进展情况管理', 0, 5, 'progress', NULL, 1, 0, 'M', '0', '0', NULL, 'guide', 'admin', now(), '进展情况管理目录')",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '实时动态管理', menu_id, 1, 'realtimeNews', 'progress/realtimeNews/index', 1, 0, 'C', '0', '0', 'progress:realtimeNews:list', 'list', 'admin', now(), '实时动态管理菜单' FROM sys_menu WHERE menu_name = '进展情况管理' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '实时动态查询', menu_id, 1, '#', '', 1, 0, 'F', '0', '0', 'progress:realtimeNews:query', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '实时动态管理' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '实时动态新增', menu_id, 2, '#', '', 1, 0, 'F', '0', '0', 'progress:realtimeNews:add', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '实时动态管理' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '实时动态修改', menu_id, 3, '#', '', 1, 0, 'F', '0', '0', 'progress:realtimeNews:edit', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '实时动态管理' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '实时动态删除', menu_id, 4, '#', '', 1, 0, 'F', '0', '0', 'progress:realtimeNews:remove', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '实时动态管理' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '实时动态导出', menu_id, 5, '#', '', 1, 0, 'F', '0', '0', 'progress:realtimeNews:export', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '实时动态管理' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '里程碑事件管理', menu_id, 2, 'milestone', 'progress/milestone/index', 1, 0, 'C', '0', '0', 'progress:milestone:list', 'time', 'admin', now(), '里程碑事件管理菜单' FROM sys_menu WHERE menu_name = '进展情况管理' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '里程碑事件查询', menu_id, 1, '#', '', 1, 0, 'F', '0', '0', 'progress:milestone:query', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '里程碑事件管理' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '里程碑事件新增', menu_id, 2, '#', '', 1, 0, 'F', '0', '0', 'progress:milestone:add', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '里程碑事件管理' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '里程碑事件修改', menu_id, 3, '#', '', 1, 0, 'F', '0', '0', 'progress:milestone:edit', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '里程碑事件管理' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '里程碑事件删除', menu_id, 4, '#', '', 1, 0, 'F', '0', '0', 'progress:milestone:remove', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '里程碑事件管理' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '里程碑事件导出', menu_id, 5, '#', '', 1, 0, 'F', '0', '0', 'progress:milestone:export', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '里程碑事件管理' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) VALUES ('项目管理', 0, 6, 'project', NULL, 1, 0, 'M', '0', '0', NULL, 'example', 'admin', now(), '项目管理目录')",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '项目简介', menu_id, 1, 'intro', 'project/intro/index', 1, 0, 'C', '0', '0', 'project:intro:list', 'documentation', 'admin', now(), '项目简介菜单' FROM sys_menu WHERE menu_name = '项目管理' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '项目简介编辑', menu_id, 1, '#', '', 1, 0, 'F', '0', '0', 'project:intro:edit', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '项目简介' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '项目列表', menu_id, 2, 'list', 'project/list/index', 1, 0, 'C', '0', '0', 'project:info:list', 'list', 'admin', now(), '项目列表菜单' FROM sys_menu WHERE menu_name = '项目管理' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '项目查询', menu_id, 1, '#', '', 1, 0, 'F', '0', '0', 'project:info:query', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '项目列表' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '项目新增', menu_id, 2, '#', '', 1, 0, 'F', '0', '0', 'project:info:add', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '项目列表' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '项目修改', menu_id, 3, '#', '', 1, 0, 'F', '0', '0', 'project:info:edit', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '项目列表' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '项目删除', menu_id, 4, '#', '', 1, 0, 'F', '0', '0', 'project:info:remove', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '项目列表' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '项目导出', menu_id, 5, '#', '', 1, 0, 'F', '0', '0', 'project:info:export', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '项目列表' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '项目进展更新', menu_id, 3, 'progress', 'project/progress/index', 1, 0, 'C', '0', '0', 'project:progress:list', 'time', 'admin', now(), '项目进展更新菜单' FROM sys_menu WHERE menu_name = '项目管理' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '进展查询', menu_id, 1, '#', '', 1, 0, 'F', '0', '0', 'project:progress:query', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '项目进展更新' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '进展新增', menu_id, 2, '#', '', 1, 0, 'F', '0', '0', 'project:progress:add', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '项目进展更新' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '进展修改', menu_id, 3, '#', '', 1, 0, 'F', '0', '0', 'project:progress:edit', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '项目进展更新' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '进展删除', menu_id, 4, '#', '', 1, 0, 'F', '0', '0', 'project:progress:remove', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '项目进展更新' ORDER BY menu_id DESC LIMIT 1",
                
                "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) SELECT '进展导出', menu_id, 5, '#', '', 1, 0, 'F', '0', '0', 'project:progress:export', '#', 'admin', now(), '' FROM sys_menu WHERE menu_name = '项目进展更新' ORDER BY menu_id DESC LIMIT 1"
            };
            
            for (String sql : sqls)
            {
                stmt.execute(sql);
                result.append("执行成功: ").append(sql.substring(0, Math.min(50, sql.length()))).append("...\n");
            }
            
            return AjaxResult.success("SQL执行成功\n" + result.toString());
        }
        catch (Exception e)
        {
            return AjaxResult.error("SQL执行失败: " + e.getMessage());
        }
        finally
        {
            try
            {
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    @PostMapping("/cleanDuplicateMenus")
    public AjaxResult cleanDuplicateMenus()
    {
        StringBuilder result = new StringBuilder();
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        
        try
        {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://172.28.135.68:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8", "root", "123456");
            stmt = conn.createStatement();
            
            rs = stmt.executeQuery("SELECT menu_id FROM sys_menu WHERE menu_name = '进展情况管理' ORDER BY menu_id");
            
            java.util.List<Long> menuIds = new java.util.ArrayList<>();
            while (rs.next())
            {
                menuIds.add(rs.getLong("menu_id"));
            }
            rs.close();
            
            if (menuIds.size() > 1)
            {
                for (int i = 1; i < menuIds.size(); i++)
                {
                    Long menuId = menuIds.get(i);
                    stmt.execute("DELETE FROM sys_menu WHERE menu_id = " + menuId);
                    result.append("删除重复菜单ID: ").append(menuId).append("\n");
                }
                result.append("成功删除 ").append(menuIds.size() - 1).append(" 个重复的'进展情况管理'菜单");
            }
            else
            {
                result.append("没有发现重复的'进展情况管理'菜单");
            }
            
            return AjaxResult.success("清理完成\n" + result.toString());
        }
        catch (Exception e)
        {
            return AjaxResult.error("清理失败: " + e.getMessage());
        }
        finally
        {
            try
            {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    @PostMapping("/deleteMenu")
    public AjaxResult deleteMenu(String menuName)
    {
        StringBuilder result = new StringBuilder();
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        
        try
        {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://172.28.135.68:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8", "root", "123456");
            stmt = conn.createStatement();
            
            rs = stmt.executeQuery("SELECT menu_id FROM sys_menu WHERE menu_name = '" + menuName + "'");
            
            if (rs.next())
            {
                Long menuId = rs.getLong("menu_id");
                rs.close();
                
                stmt.execute("DELETE FROM sys_menu WHERE menu_id = " + menuId);
                result.append("成功删除菜单: ").append(menuName).append(" (ID: ").append(menuId).append(")");
            }
            else
            {
                result.append("未找到菜单: ").append(menuName);
            }
            
            return AjaxResult.success("删除完成\n" + result.toString());
        }
        catch (Exception e)
        {
            return AjaxResult.error("删除失败: " + e.getMessage());
        }
        finally
        {
            try
            {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    @PostMapping("/deleteMenuById")
    public AjaxResult deleteMenuById(Long menuId)
    {
        StringBuilder result = new StringBuilder();
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        
        try
        {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://172.28.135.68:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8", "root", "123456");
            stmt = conn.createStatement();
            
            rs = stmt.executeQuery("SELECT menu_name FROM sys_menu WHERE menu_id = " + menuId);
            
            if (rs.next())
            {
                String menuName = rs.getString("menu_name");
                rs.close();
                
                stmt.execute("DELETE FROM sys_menu WHERE menu_id = " + menuId);
                result.append("成功删除菜单: ").append(menuName).append(" (ID: ").append(menuId).append(")");
            }
            else
            {
                result.append("未找到菜单ID: ").append(menuId);
            }
            
            return AjaxResult.success("删除完成\n" + result.toString());
        }
        catch (Exception e)
        {
            return AjaxResult.error("删除失败: " + e.getMessage());
        }
        finally
        {
            try
            {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    @PostMapping("/listMenus")
    public AjaxResult listMenus()
    {
        StringBuilder result = new StringBuilder();
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        
        try
        {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://172.28.135.68:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8", "root", "123456");
            stmt = conn.createStatement();
            
            rs = stmt.executeQuery("SELECT menu_id, menu_name, parent_id FROM sys_menu WHERE parent_id = 0 ORDER BY order_num");
            
            result.append("顶级菜单列表:\n");
            while (rs.next())
            {
                result.append("ID: ").append(rs.getLong("menu_id"))
                      .append(", 名称: ").append(rs.getString("menu_name"))
                      .append("\n");
            }
            
            return AjaxResult.success(result.toString());
        }
        catch (Exception e)
        {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
        finally
        {
            try
            {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}
