-- ================================================
-- 微信菜单数据修复脚本
-- ================================================
-- 此脚本用于检查和修复数据库中的无效微信菜单数据

-- ------------------------------------------------
-- 1. 检查所有可能有问题的菜单数据
-- ------------------------------------------------

-- 检查 view 类型但 URL 为空的菜单
SELECT 
    id, 
    menu_name AS '菜单名称', 
    menu_type AS '类型', 
    menu_url AS 'URL', 
    menu_key AS 'Key',
    parent_id AS '父菜单ID',
    '问题：view类型缺少URL' AS '问题描述'
FROM wechat_menu
WHERE menu_type = 'view' 
  AND (menu_url IS NULL OR menu_url = '')
  AND is_enabled = 1;

-- 检查 click 类型但 Key 为空的菜单
SELECT 
    id, 
    menu_name AS '菜单名称', 
    menu_type AS '类型', 
    menu_key AS 'Key',
    parent_id AS '父菜单ID',
    '问题：click类型缺少Key' AS '问题描述'
FROM wechat_menu
WHERE menu_type = 'click' 
  AND (menu_key IS NULL OR menu_key = '')
  AND is_enabled = 1;

-- 检查 miniprogram 类型但缺少必要字段的菜单
SELECT 
    id, 
    menu_name AS '菜单名称', 
    menu_type AS '类型', 
    menu_url AS 'URL',
    appid AS 'AppID',
    pagepath AS 'PagePath',
    parent_id AS '父菜单ID',
    '问题：miniprogram类型缺少必要字段' AS '问题描述'
FROM wechat_menu
WHERE menu_type = 'miniprogram' 
  AND (menu_url IS NULL OR menu_url = '' 
       OR appid IS NULL OR appid = '' 
       OR pagepath IS NULL OR pagepath = '')
  AND is_enabled = 1;

-- ------------------------------------------------
-- 2. 修复方案（请根据实际情况选择执行）
-- ------------------------------------------------

-- 方案A：禁用所有无效的菜单（推荐，不会删除数据）
-- 禁用 view 类型但 URL 为空的菜单
UPDATE wechat_menu 
SET is_enabled = 0,
    description = CONCAT(IFNULL(description, ''), ' [自动禁用：缺少URL]')
WHERE menu_type = 'view' 
  AND (menu_url IS NULL OR menu_url = '')
  AND is_enabled = 1;

-- 禁用 click 类型但 Key 为空的菜单
UPDATE wechat_menu 
SET is_enabled = 0,
    description = CONCAT(IFNULL(description, ''), ' [自动禁用：缺少Key]')
WHERE menu_type = 'click' 
  AND (menu_key IS NULL OR menu_key = '')
  AND is_enabled = 1;

-- 禁用 miniprogram 类型但缺少必要字段的菜单
UPDATE wechat_menu 
SET is_enabled = 0,
    description = CONCAT(IFNULL(description, ''), ' [自动禁用：缺少必要字段]')
WHERE menu_type = 'miniprogram' 
  AND (menu_url IS NULL OR menu_url = '' 
       OR appid IS NULL OR appid = '' 
       OR pagepath IS NULL OR pagepath = '')
  AND is_enabled = 1;

-- ------------------------------------------------
-- 方案B：手动修复特定菜单（示例）
-- ------------------------------------------------

-- 如果您想保留 "首页" 菜单，可以为其添加URL
-- UPDATE wechat_menu 
-- SET menu_url = 'https://your-website.com'
-- WHERE menu_name = '首页' AND menu_type = 'view';

-- 如果您想保留 "帮助中心" 菜单，可以为其添加URL
-- UPDATE wechat_menu 
-- SET menu_url = 'https://your-website.com/help'
-- WHERE menu_name = '帮助中心' AND menu_type = 'view';

-- 或者，如果您想将这些菜单改为 click 类型：
-- UPDATE wechat_menu 
-- SET menu_type = 'click', menu_key = 'MENU_HOME', menu_url = NULL
-- WHERE menu_name = '首页';

-- ------------------------------------------------
-- 方案C：删除无效菜单（慎用！）
-- ------------------------------------------------

-- 如果您确定要删除这些无效菜单，取消下面的注释
-- DELETE FROM wechat_menu 
-- WHERE menu_type = 'view' 
--   AND (menu_url IS NULL OR menu_url = '')
--   AND is_enabled = 1;

-- ------------------------------------------------
-- 3. 修复后验证
-- ------------------------------------------------

-- 查看所有启用的菜单
SELECT 
    id,
    parent_id,
    menu_name,
    menu_type,
    menu_key,
    menu_url,
    is_enabled,
    sort_order
FROM wechat_menu
WHERE is_enabled = 1
ORDER BY parent_id ASC, sort_order ASC;

-- 统计菜单数量
SELECT 
    is_enabled,
    COUNT(*) as count,
    CASE 
        WHEN is_enabled = 1 THEN '启用'
        ELSE '禁用'
    END as status
FROM wechat_menu
GROUP BY is_enabled;

