-- ============================================
-- 粉丝团系统完整性检查脚本
-- ============================================

-- 1. 检查表结构
SELECT '=== 1. 检查表结构 ===' as info;

-- 检查eb_fan_group表
SELECT 'eb_fan_group表字段:' as info;
SHOW COLUMNS FROM eb_fan_group;

-- 检查eb_fan_group_member表
SELECT 'eb_fan_group_member表字段:' as info;
SHOW COLUMNS FROM eb_fan_group_member;

-- 检查eb_group_message表
SELECT 'eb_group_message表字段:' as info;
SHOW COLUMNS FROM eb_group_message;

-- 2. 检查数据完整性
SELECT '=== 2. 数据统计 ===' as info;
SELECT 
    (SELECT COUNT(*) FROM eb_fan_group) as total_fan_groups,
    (SELECT COUNT(*) FROM eb_fan_group WHERE status = 1) as active_fan_groups,
    (SELECT COUNT(*) FROM eb_fan_group_member WHERE status = 1) as total_members,
    (SELECT COUNT(*) FROM eb_group_message WHERE is_deleted = 0) as total_messages;

-- 3. 检查粉丝团列表
SELECT '=== 3. 粉丝团列表 ===' as info;
SELECT 
    fg.id,
    fg.name,
    fg.anchor_id,
    fg.anchor_name,
    fg.badge,
    fg.member_count,
    (SELECT COUNT(*) FROM eb_fan_group_member m WHERE m.group_id = fg.id AND m.status = 1) as actual_members,
    (SELECT COUNT(*) FROM eb_group_message gm WHERE gm.group_id = fg.id AND gm.is_deleted = 0) as message_count,
    fg.status,
    fg.create_time
FROM eb_fan_group fg
ORDER BY fg.id;

-- 4. 修复成员数量不一致
SELECT '=== 4. 修复成员数量 ===' as info;
UPDATE eb_fan_group fg SET member_count = (
    SELECT COUNT(*) FROM eb_fan_group_member m WHERE m.group_id = fg.id AND m.status = 1
);

-- 5. 检查管理端菜单
SELECT '=== 5. 检查管理端菜单 ===' as info;
SELECT id, pid, name, path, component, is_show 
FROM eb_system_menu 
WHERE name LIKE '%粉丝团%' OR path LIKE '%fanGroup%' OR path LIKE '%fan%group%';

-- 6. 如果菜单不存在，查找合适的父菜单
SELECT '=== 6. 可用的父菜单 ===' as info;
SELECT id, name, path FROM eb_system_menu WHERE pid = 0 AND is_show = 1 ORDER BY sort;

SELECT '=== 检查完成 ===' as info;
