-- 调试关注页面显示问题的 SQL 脚本
-- 用于排查为什么关注的主播直播间不显示

-- ============================================
-- 第一部分：查看基础数据
-- ============================================

-- 1. 查看所有用户（包括主播状态）
SELECT 
    uid,
    nickname,
    phone,
    is_streamer,
    streamer_level,
    create_time
FROM eb_user
ORDER BY uid;

-- 2. 查看所有直播间
SELECT 
    id,
    title,
    uid as streamer_uid,
    is_live,
    view_count,
    like_count,
    online_count,
    create_time,
    started_at
FROM eb_live_room
ORDER BY create_time DESC;

-- 3. 查看所有关注记录
SELECT 
    id,
    follower_id,
    follower_nickname,
    followed_id,
    followed_nickname,
    follow_status,
    is_deleted,
    create_time
FROM eb_follow_record
ORDER BY create_time DESC;

-- ============================================
-- 第二部分：关联查询 - 查看关注关系和直播间
-- ============================================

-- 4. 查看每个用户关注了哪些主播
SELECT 
    u.uid as user_id,
    u.nickname as user_name,
    COUNT(DISTINCT fr.followed_id) as following_count,
    GROUP_CONCAT(DISTINCT fr.followed_nickname SEPARATOR ', ') as following_list
FROM eb_user u
LEFT JOIN eb_follow_record fr ON u.uid = fr.follower_id 
    AND fr.follow_status IN ('1', '关注') 
    AND fr.is_deleted = 0
GROUP BY u.uid, u.nickname
ORDER BY following_count DESC;

-- 5. 查看每个主播有多少粉丝
SELECT 
    u.uid as streamer_id,
    u.nickname as streamer_name,
    u.is_streamer,
    COUNT(DISTINCT fr.follower_id) as fans_count,
    GROUP_CONCAT(DISTINCT fr.follower_nickname SEPARATOR ', ') as fans_list
FROM eb_user u
LEFT JOIN eb_follow_record fr ON u.uid = fr.followed_id 
    AND fr.follow_status IN ('1', '关注') 
    AND fr.is_deleted = 0
WHERE u.is_streamer = 1
GROUP BY u.uid, u.nickname, u.is_streamer
ORDER BY fans_count DESC;

-- 6. 查看每个主播的直播间状态
SELECT 
    u.uid as streamer_id,
    u.nickname as streamer_name,
    u.is_streamer,
    COUNT(DISTINCT lr.id) as room_count,
    SUM(CASE WHEN lr.is_live = 1 THEN 1 ELSE 0 END) as live_room_count,
    GROUP_CONCAT(
        DISTINCT CONCAT(lr.id, ':', lr.title, '(', IF(lr.is_live=1, '直播中', '未开播'), ')') 
        SEPARATOR ' | '
    ) as rooms_info
FROM eb_user u
LEFT JOIN eb_live_room lr ON u.uid = lr.uid
WHERE u.is_streamer = 1
GROUP BY u.uid, u.nickname, u.is_streamer
ORDER BY live_room_count DESC, room_count DESC;

-- ============================================
-- 第三部分：模拟关注页面的查询逻辑
-- ============================================

-- 7. 模拟某个用户的关注页面应该显示的内容
-- 替换 @USER_ID 为实际的用户ID
SET @USER_ID = 1;

-- 7.1 查看该用户关注了哪些人
SELECT 
    fr.followed_id as followed_user_id,
    fr.followed_nickname,
    u.is_streamer,
    fr.follow_status,
    fr.create_time as followed_at
FROM eb_follow_record fr
LEFT JOIN eb_user u ON fr.followed_id = u.uid
WHERE fr.follower_id = @USER_ID 
    AND fr.follow_status IN ('1', '关注') 
    AND fr.is_deleted = 0
ORDER BY fr.create_time DESC;

-- 7.2 查看该用户关注的主播中，哪些有直播间
SELECT 
    u.uid as streamer_id,
    u.nickname as streamer_name,
    lr.id as room_id,
    lr.title as room_title,
    lr.is_live,
    lr.view_count,
    lr.online_count,
    lr.create_time as room_created_at,
    lr.started_at as live_started_at
FROM eb_follow_record fr
INNER JOIN eb_user u ON fr.followed_id = u.uid AND u.is_streamer = 1
INNER JOIN eb_live_room lr ON u.uid = lr.uid
WHERE fr.follower_id = @USER_ID 
    AND fr.follow_status IN ('1', '关注') 
    AND fr.is_deleted = 0
ORDER BY lr.is_live DESC, lr.started_at DESC;

-- 7.3 查看该用户关注的主播中，哪些正在直播
SELECT 
    u.uid as streamer_id,
    u.nickname as streamer_name,
    u.avatar as streamer_avatar,
    lr.id as room_id,
    lr.title as room_title,
    lr.cover_url,
    lr.view_count,
    lr.online_count,
    lr.like_count,
    lr.started_at as live_started_at,
    TIMESTAMPDIFF(MINUTE, lr.started_at, NOW()) as live_duration_minutes
FROM eb_follow_record fr
INNER JOIN eb_user u ON fr.followed_id = u.uid AND u.is_streamer = 1
INNER JOIN eb_live_room lr ON u.uid = lr.uid AND lr.is_live = 1
WHERE fr.follower_id = @USER_ID 
    AND fr.follow_status IN ('1', '关注') 
    AND fr.is_deleted = 0
ORDER BY lr.started_at DESC;

-- ============================================
-- 第四部分：诊断问题
-- ============================================

-- 8. 检查是否有用户关注了主播但看不到直播间的情况
SELECT 
    '问题诊断' as diagnosis_type,
    fr.follower_id,
    u1.nickname as follower_name,
    fr.followed_id,
    u2.nickname as followed_name,
    u2.is_streamer,
    COUNT(lr.id) as room_count,
    SUM(CASE WHEN lr.is_live = 1 THEN 1 ELSE 0 END) as live_room_count,
    CASE 
        WHEN u2.is_streamer = 0 THEN '被关注者不是主播'
        WHEN COUNT(lr.id) = 0 THEN '主播没有创建直播间'
        WHEN SUM(CASE WHEN lr.is_live = 1 THEN 1 ELSE 0 END) = 0 THEN '主播有直播间但未开播'
        ELSE '正常：主播正在直播'
    END as status_description
FROM eb_follow_record fr
LEFT JOIN eb_user u1 ON fr.follower_id = u1.uid
LEFT JOIN eb_user u2 ON fr.followed_id = u2.uid
LEFT JOIN eb_live_room lr ON u2.uid = lr.uid
WHERE fr.follow_status IN ('1', '关注') 
    AND fr.is_deleted = 0
GROUP BY fr.follower_id, u1.nickname, fr.followed_id, u2.nickname, u2.is_streamer
ORDER BY fr.follower_id, fr.followed_id;

-- 9. 检查直播间的 uid 字段是否正确关联到主播
SELECT 
    lr.id as room_id,
    lr.title as room_title,
    lr.uid as room_uid,
    u.uid as user_uid,
    u.nickname as user_nickname,
    u.is_streamer,
    CASE 
        WHEN lr.uid IS NULL THEN '直播间没有关联用户ID'
        WHEN u.uid IS NULL THEN '直播间关联的用户不存在'
        WHEN u.is_streamer = 0 THEN '直播间关联的用户不是主播'
        ELSE '正常'
    END as status
FROM eb_live_room lr
LEFT JOIN eb_user u ON lr.uid = u.uid
ORDER BY lr.create_time DESC;

-- 10. 检查关注状态字段的值分布
SELECT 
    follow_status,
    COUNT(*) as count,
    GROUP_CONCAT(DISTINCT CONCAT(follower_nickname, '->', followed_nickname) SEPARATOR '; ') as examples
FROM eb_follow_record
GROUP BY follow_status;

-- ============================================
-- 第五部分：修复建议
-- ============================================

-- 11. 如果发现关注状态值不一致，可以执行以下修复（谨慎执行）
-- 将字符串状态统一为数字
-- UPDATE eb_follow_record SET follow_status = '1' WHERE follow_status = '关注';
-- UPDATE eb_follow_record SET follow_status = '0' WHERE follow_status = '取消关注' OR follow_status = '0';

-- 12. 如果发现直播间的 uid 字段为空，需要修复
-- UPDATE eb_live_room lr
-- INNER JOIN eb_user u ON lr.streamer_name = u.nickname AND u.is_streamer = 1
-- SET lr.uid = u.uid
-- WHERE lr.uid IS NULL;

-- ============================================
-- 第六部分：测试数据插入（用于测试）
-- ============================================

-- 13. 创建测试用户和关注关系（如果需要）
-- 注意：执行前请确认用户ID不冲突

-- 插入测试主播
-- INSERT INTO eb_user (uid, nickname, phone, is_streamer, streamer_level, create_time)
-- VALUES (100, '测试主播1', '13800000001', 1, 1, NOW());

-- 插入测试普通用户
-- INSERT INTO eb_user (uid, nickname, phone, is_streamer, create_time)
-- VALUES (101, '测试用户1', '13800000002', 0, NOW());

-- 插入关注记录
-- INSERT INTO eb_follow_record (follower_id, follower_nickname, followed_id, followed_nickname, follow_status, is_deleted, create_time)
-- VALUES (101, '测试用户1', 100, '测试主播1', '1', 0, NOW());

-- 插入测试直播间
-- INSERT INTO eb_live_room (title, uid, stream_key, is_live, view_count, online_count, create_time, started_at)
-- VALUES ('测试直播间', 100, 'test_stream_key_001', 1, 0, 0, NOW(), NOW());

-- ============================================
-- 第七部分：清理测试数据
-- ============================================

-- 14. 清理测试数据（如果需要）
-- DELETE FROM eb_live_room WHERE uid = 100;
-- DELETE FROM eb_follow_record WHERE follower_id = 101 OR followed_id = 100;
-- DELETE FROM eb_user WHERE uid IN (100, 101);

-- ============================================
-- 使用说明
-- ============================================
-- 1. 先执行第一部分和第二部分，查看基础数据
-- 2. 在第三部分中，将 @USER_ID 替换为实际的用户ID，查看该用户应该看到的内容
-- 3. 执行第四部分，诊断可能的问题
-- 4. 根据诊断结果，参考第五部分的修复建议
-- 5. 如果需要测试，可以使用第六部分创建测试数据
-- 6. 测试完成后，使用第七部分清理测试数据
