#!/bin/bash
# ============================================================
# 脚本名称：import_menu.sh
# 功能说明：导入完整系统菜单（RuoYi系统菜单 + 盘古业务菜单）
# 作 者：pangu
# 创建时间：2026-01-31
# 使用方法：./import_menu.sh
# ============================================================

set -e

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

# SQL文件路径
SQL_FILE="$(cd "$(dirname "$0")" && pwd)/sys_menu_complete.sql"

echo "============================================================"
echo "导入系统菜单数据"
echo "============================================================"
echo "数据库: $DB_NAME"
echo "SQL文件: $SQL_FILE"
echo ""

# 检查SQL文件是否存在
if [ ! -f "$SQL_FILE" ]; then
    echo "❌ 错误: SQL文件不存在: $SQL_FILE"
    exit 1
fi

# 确认导入
read -p "此操作将清空并重新导入菜单数据，是否继续？(y/n): " confirm
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
    echo "❌ 操作已取消"
    exit 0
fi

# 执行导入
echo ""
echo "正在导入菜单数据..."
mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" --default-character-set=utf8mb4 "$DB_NAME" < "$SQL_FILE"

if [ $? -eq 0 ]; then
    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 menu_id < 2000 THEN 'RuoYi系统菜单'
                ELSE '盘古业务菜单'
            END AS '菜单类型',
            COUNT(*) AS '数量'
        FROM sys_menu
        GROUP BY CASE WHEN menu_id < 2000 THEN 'RuoYi系统菜单' ELSE '盘古业务菜单' END
        WITH ROLLUP;"
    echo ""
    echo "顶级菜单："
    mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" --default-character-set=utf8mb4 "$DB_NAME" -e "
        SELECT menu_id, menu_name, order_num, visible, status 
        FROM sys_menu 
        WHERE parent_id = 0 
        ORDER BY order_num;"
else
    echo ""
    echo "❌ 菜单数据导入失败！"
    exit 1
fi

echo ""
echo "============================================================"
echo "导入完成！请刷新前端页面查看菜单。"
echo "============================================================"
