-- 测试用户 43 (xiaofeng) 的关注页面
-- 该用户关注了用户 41 (夏至已至)，而用户 41 有一个正在直播的房间

SET @USER_ID = 43;

-- 1. 查看用户 43 关注了哪些人
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;

-- 2. 查看用户 43 关注的主播中，哪些有直播间
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;

-- 3. 查看用户 43 关注的主播中，哪些正在直播
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.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;

-- 4. 诊断：为什么可能看不到
-- 检查关注状态的值
SELECT 
    '关注状态检查' as check_type,
    follow_status,
    COUNT(*) as count
FROM eb_follow_record
WHERE follower_id = @USER_ID
GROUP BY follow_status;

-- 5. 检查被关注用户的主播状态
SELECT 
    '被关注用户检查' as check_type,
    u.uid,
    u.nickname,
    u.is_streamer,
    fr.follow_status
FROM eb_follow_record fr
LEFT JOIN eb_user u ON fr.followed_id = u.uid
WHERE fr.follower_id = @USER_ID;

-- 6. 检查直播间的 uid 关联
SELECT 
    '直播间关联检查' as check_type,
    lr.id as room_id,
    lr.title,
    lr.uid as room_uid,
    lr.is_live,
    u.uid as user_uid,
    u.nickname,
    u.is_streamer
FROM eb_live_room lr
LEFT JOIN eb_user u ON lr.uid = u.uid
WHERE lr.uid IN (
    SELECT followed_id 
    FROM eb_follow_record 
    WHERE follower_id = @USER_ID 
        AND follow_status IN ('1', '关注') 
        AND is_deleted = 0
);
