-- 测试主播统计数据
-- 用于验证粉丝数、关注数等统计是否正确

-- 1. 查看所有主播用户
SELECT uid, nickname, is_streamer, streamer_level, phone
FROM eb_user 
WHERE is_streamer = 1
ORDER BY uid;

-- 2. 查看关注记录表的数据
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
LIMIT 20;

-- 3. 统计每个用户的粉丝数（有多少人关注了我）
SELECT 
    u.uid,
    u.nickname,
    u.is_streamer,
    COUNT(fr.id) as fans_count
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;

-- 4. 统计每个用户的关注数（我关注了多少人）
SELECT 
    u.uid,
    u.nickname,
    u.is_streamer,
    COUNT(fr.id) as following_count
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
WHERE u.is_streamer = 1
GROUP BY u.uid, u.nickname, u.is_streamer
ORDER BY following_count DESC;

-- 5. 查看某个特定主播的详细统计（替换 USER_ID 为实际的用户ID）
SET @USER_ID = 1;

SELECT 
    '粉丝数' as stat_type,
    COUNT(*) as count
FROM eb_follow_record 
WHERE followed_id = @USER_ID 
    AND follow_status IN ('1', '关注') 
    AND is_deleted = 0

UNION ALL

SELECT 
    '关注数' as stat_type,
    COUNT(*) as count
FROM eb_follow_record 
WHERE follower_id = @USER_ID 
    AND follow_status IN ('1', '关注') 
    AND is_deleted = 0

UNION ALL

SELECT 
    '直播间数' as stat_type,
    COUNT(*) as count
FROM eb_live_room 
WHERE uid = @USER_ID

UNION ALL

SELECT 
    '正在直播' as stat_type,
    COUNT(*) as count
FROM eb_live_room 
WHERE uid = @USER_ID 
    AND is_live = 1;

-- 6. 查看谁关注了某个主播
SELECT 
    fr.follower_id,
    u.nickname as follower_nickname,
    fr.follow_status,
    fr.create_time
FROM eb_follow_record fr
LEFT JOIN eb_user u ON fr.follower_id = u.uid
WHERE fr.followed_id = @USER_ID 
    AND fr.follow_status IN ('1', '关注') 
    AND fr.is_deleted = 0
ORDER BY fr.create_time DESC;

-- 7. 查看某个主播关注了谁
SELECT 
    fr.followed_id,
    u.nickname as followed_nickname,
    fr.follow_status,
    fr.create_time
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;

-- 8. 检查关注状态的数据类型和值
SELECT 
    follow_status,
    COUNT(*) as count,
    GROUP_CONCAT(DISTINCT CONCAT('ID:', id) SEPARATOR ', ') as sample_ids
FROM eb_follow_record
GROUP BY follow_status;

-- 9. 修复可能的数据问题：将字符串状态统一为数字
-- UPDATE eb_follow_record SET follow_status = '1' WHERE follow_status = '关注';
-- UPDATE eb_follow_record SET follow_status = '0' WHERE follow_status = '取消关注';
