-- ============================================
-- 粉丝团系统完整检查与完善脚本
-- 执行此脚本以检查和完善粉丝团功能
-- ============================================

-- ==================== 第一部分：检查表结构 ====================

-- 1. 检查粉丝团表是否存在及结构
SELECT '=== 1. 粉丝团表结构 ===' as info;
DESC eb_fan_group;

-- 2. 检查粉丝团成员表结构
SELECT '=== 2. 粉丝团成员表结构 ===' as info;
DESC eb_fan_group_member;

-- 3. 检查群组消息表结构
SELECT '=== 3. 群组消息表结构 ===' as info;
DESC eb_group_message;

-- ==================== 第二部分：检查现有数据 ====================

-- 4. 查看当前粉丝团数据
SELECT '=== 4. 当前粉丝团列表 ===' as info;
SELECT 
    fg.id,
    fg.name,
    fg.anchor_id,
    fg.anchor_name,
    u.nickname as anchor_real_name,
    u.avatar as anchor_avatar,
    fg.badge,
    fg.badge_color,
    fg.member_count,
    fg.status,
    fg.create_time
FROM eb_fan_group fg
LEFT JOIN eb_user u ON fg.anchor_id = u.uid
ORDER BY fg.id;

-- 5. 查看粉丝团成员数据
SELECT '=== 5. 粉丝团成员列表 ===' as info;
SELECT 
    m.id,
    m.group_id as fan_group_id,
    fg.name as fan_group_name,
    m.uid,
    m.nickname as member_nickname,
    u.nickname as real_nickname,
    u.avatar,
    m.level,
    m.intimacy,
    m.status,
    m.join_time
FROM eb_fan_group_member m
LEFT JOIN eb_fan_group fg ON m.group_id = fg.id
LEFT JOIN eb_user u ON m.uid = u.uid
WHERE m.status = 1
ORDER BY m.group_id, m.level DESC, m.intimacy DESC;

-- 6. 查看群组消息
SELECT '=== 6. 粉丝团聊天消息 ===' as info;
SELECT 
    gm.id,
    gm.group_id,
    fg.name as fan_group_name,
    gm.sender_id,
    u.nickname as sender_name,
    gm.content,
    gm.message_type,
    gm.is_deleted,
    gm.create_time
FROM eb_group_message gm
LEFT JOIN eb_fan_group fg ON gm.group_id = fg.id
LEFT JOIN eb_user u ON gm.sender_id = u.uid
WHERE gm.is_deleted = 0
ORDER BY gm.group_id, gm.create_time DESC
LIMIT 50;

-- ==================== 第三部分：数据统计 ====================

-- 7. 粉丝团统计
SELECT '=== 7. 粉丝团统计 ===' as info;
SELECT 
    COUNT(*) as total_fan_groups,
    SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as active_fan_groups,
    SUM(member_count) as total_members
FROM eb_fan_group;

-- 8. 各粉丝团成员数量对比（实际 vs 记录）
SELECT '=== 8. 成员数量校验 ===' as info;
SELECT 
    fg.id,
    fg.name,
    fg.member_count as recorded_count,
    COUNT(m.id) as actual_count,
    CASE 
        WHEN fg.member_count = COUNT(m.id) THEN '✓ 一致'
        ELSE '✗ 不一致'
    END as status
FROM eb_fan_group fg
LEFT JOIN eb_fan_group_member m ON fg.id = m.group_id AND m.status = 1
GROUP BY fg.id, fg.name, fg.member_count;

-- ==================== 第四部分：数据修复 ====================

-- 9. 修复粉丝团成员数量（如果不一致）
SELECT '=== 9. 修复成员数量 ===' 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
);

-- 10. 更新粉丝团主播名称（从用户表同步）
SELECT '=== 10. 同步主播名称 ===' as info;
UPDATE eb_fan_group fg
JOIN eb_user u ON fg.anchor_id = u.uid
SET fg.anchor_name = u.nickname
WHERE fg.anchor_name IS NULL OR fg.anchor_name = '' OR fg.anchor_name != u.nickname;

-- ==================== 第五部分：验证修复结果 ====================

-- 11. 再次检查成员数量
SELECT '=== 11. 修复后成员数量校验 ===' as info;
SELECT 
    fg.id,
    fg.name,
    fg.anchor_name,
    fg.member_count as recorded_count,
    COUNT(m.id) as actual_count
FROM eb_fan_group fg
LEFT JOIN eb_fan_group_member m ON fg.id = m.group_id AND m.status = 1
GROUP BY fg.id, fg.name, fg.anchor_name, fg.member_count;

SELECT '=== 粉丝团检查完成 ===' as info;
