#!/bin/bash
# ============================================================
# 脚本名称：fix_admin_permissions.sh
# 功能说明：修复管理员角色菜单权限（添加所有菜单权限）
# 作 者：pangu
# 创建时间：2026-01-31
# 使用方法：./fix_admin_permissions.sh
# ============================================================

set -e

# 数据库配置
DB_HOST="8.148.25.55"
DB_PORT="3306"
DB_USER="root"
DB_PASS="aly2024A"
DB_NAME="pguser-db"

echo "============================================================"
echo "管理员角色菜单权限修复工具"
echo "============================================================"
echo ""

# 1. 检查当前管理员角色权限
echo "1. 检查当前管理员角色权限..."
mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" --default-character-set=utf8mb4 "$DB_NAME" -e "
SELECT 
    CASE 
        WHEN m.menu_id < 2000 THEN 'RuoYi系统菜单'
        ELSE '盘古业务菜单'
    END AS menu_type,
    COUNT(*) as count
FROM sys_role_menu rm
INNER JOIN sys_menu m ON rm.menu_id = m.menu_id
WHERE rm.role_id = 1
GROUP BY CASE WHEN m.menu_id < 2000 THEN 'RuoYi系统菜单' ELSE '盘古业务菜单' END
WITH ROLLUP;
" 2>&1 | grep -v "Warning"

echo ""
echo "2. 菜单总数统计..."
mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" --default-character-set=utf8mb4 "$DB_NAME" -e "
SELECT 
    '系统菜单总数' AS item,
    COUNT(*) as count
FROM sys_menu
UNION ALL
SELECT 
    '管理员已分配' AS item,
    COUNT(*) as count
FROM sys_role_menu
WHERE role_id = 1;
" 2>&1 | grep -v "Warning"

echo ""
read -p "是否为管理员角色添加所有菜单权限？(y/n): " confirm

if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
    echo ""
    echo "正在修复管理员角色权限..."
    
    # 备份当前权限
    echo "  - 创建权限备份..."
    mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" --default-character-set=utf8mb4 "$DB_NAME" -e "
    CREATE TABLE IF NOT EXISTS sys_role_menu_backup_$(date +%Y%m%d) AS 
    SELECT * FROM sys_role_menu WHERE role_id = 1;
    " 2>&1 | grep -v "Warning"
    
    # 删除现有权限
    echo "  - 清空现有权限..."
    mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" --default-character-set=utf8mb4 "$DB_NAME" -e "
    DELETE FROM sys_role_menu WHERE role_id = 1;
    " 2>&1 | grep -v "Warning"
    
    # 添加所有菜单权限
    echo "  - 添加所有菜单权限..."
    mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" --default-character-set=utf8mb4 "$DB_NAME" -e "
    INSERT INTO sys_role_menu (role_id, menu_id)
    SELECT 1, menu_id FROM sys_menu;
    " 2>&1 | grep -v "Warning"
    
    echo ""
    echo "✅ 权限修复完成！"
    echo ""
    echo "修复后的权限统计："
    mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" --default-character-set=utf8mb4 "$DB_NAME" -e "
    SELECT 
        CASE 
            WHEN m.menu_id < 2000 THEN 'RuoYi系统菜单'
            ELSE '盘古业务菜单'
        END AS menu_type,
        COUNT(*) as count
    FROM sys_role_menu rm
    INNER JOIN sys_menu m ON rm.menu_id = m.menu_id
    WHERE rm.role_id = 1
    GROUP BY CASE WHEN m.menu_id < 2000 THEN 'RuoYi系统菜单' ELSE '盘古业务菜单' END
    WITH ROLLUP;
    " 2>&1 | grep -v "Warning"
    
    echo ""
    echo "顶级菜单权限："
    mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" --default-character-set=utf8mb4 "$DB_NAME" -e "
    SELECT m.menu_id, m.menu_name, m.order_num
    FROM sys_menu m
    INNER JOIN sys_role_menu rm ON m.menu_id = rm.menu_id
    WHERE rm.role_id = 1 AND m.parent_id = 0
    ORDER BY m.order_num;
    " 2>&1 | grep -v "Warning"
    
    echo ""
    echo "⚠️  请退出登录并重新登录，菜单权限才会生效！"
else
    echo ""
    echo "❌ 操作已取消"
fi

echo ""
echo "============================================================"
echo "修复完成！"
echo "============================================================"
