-- 详细诊断主播统计数据
-- 这个脚本会显示每个主播的详细统计信息

-- 1. 查看所有主播的基本信息
SELECT '=== 主播基本信息 ===' as info;
SELECT uid, nickname, phone, is_streamer, streamer_level, streamer_certified_time
FROM eb_user 
WHERE is_streamer = 1
ORDER BY uid;

-- 2. 查看每个主播的粉丝数详情
SELECT '=== 粉丝数统计 ===' as info;
SELECT 
    u.uid,
    u.nickname,
    COUNT(f.id) as fansCount,
    GROUP_CONCAT(CONCAT('粉丝ID:', f.follower_id) SEPARATOR ', ') as fansList
FROM eb_user u
LEFT JOIN eb_follow_record f ON f.followed_id = u.uid 
    AND f.follow_status IN ('1', '关注') 
    AND f.is_deleted = 0
WHERE u.is_streamer = 1
GROUP BY u.uid, u.nickname
ORDER BY u.uid;

-- 3. 查看每个主播的直播间数量
SELECT '=== 直播间统计 ===' as info;
SELECT 
    u.uid,
    u.nickname,
    COUNT(r.id) as roomCount,
    GROUP_CONCAT(CONCAT('房间ID:', r.id, '(', r.title, ')') SEPARATOR ', ') as roomList
FROM eb_user u
LEFT JOIN eb_live_room r ON r.uid = u.uid
WHERE u.is_streamer = 1
GROUP BY u.uid, u.nickname
ORDER BY u.uid;

-- 4. 查看每个主播的总点赞数
SELECT '=== 点赞数统计 ===' as info;
SELECT 
    u.uid,
    u.nickname,
    COALESCE(SUM(r.like_count), 0) as totalLikeCount,
    GROUP_CONCAT(CONCAT('房间ID:', r.id, '(点赞:', r.like_count, ')') SEPARATOR ', ') as likeDetails
FROM eb_user u
LEFT JOIN eb_live_room r ON r.uid = u.uid
WHERE u.is_streamer = 1
GROUP BY u.uid, u.nickname
ORDER BY u.uid;

-- 5. 完整的统计汇总（与后台API返回的数据一致）
SELECT '=== 完整统计汇总 ===' as info;
SELECT 
    u.uid as userId,
    u.nickname,
    u.phone,
    u.streamer_level as streamerLevel,
    u.streamer_certified_time as certifiedTime,
    -- 粉丝数
    (SELECT COUNT(*) 
     FROM eb_follow_record f 
     WHERE f.followed_id = u.uid 
     AND f.follow_status IN ('1', '关注') 
     AND f.is_deleted = 0) as fansCount,
    -- 直播间数
    (SELECT COUNT(*) 
     FROM eb_live_room r 
     WHERE r.uid = u.uid) as roomCount,
    -- 总点赞数
    (SELECT COALESCE(SUM(r.like_count), 0) 
     FROM eb_live_room r 
     WHERE r.uid = u.uid) as totalLikeCount,
    -- 本月直播次数
    (SELECT COUNT(*) 
     FROM eb_live_room r 
     WHERE r.uid = u.uid 
     AND DATE_FORMAT(r.create_time, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%m')) as monthRooms,
    -- 是否被封禁
    EXISTS(SELECT 1 
           FROM eb_streamer_ban b 
           WHERE b.user_id = u.uid 
           AND b.is_active = 1 
           AND (b.ban_end_time IS NULL OR b.ban_end_time > NOW())) as isBanned
FROM eb_user u 
WHERE u.is_streamer = 1
ORDER BY u.uid;

-- 6. 检查eb_follow_record表的数据
SELECT '=== 关注记录表检查 ===' as info;
SELECT 
    id,
    follower_id,
    followed_id,
    follow_status,
    is_deleted,
    create_time
FROM eb_follow_record
WHERE followed_id IN (SELECT uid FROM eb_user WHERE is_streamer = 1)
ORDER BY followed_id, create_time DESC
LIMIT 20;

-- 7. 检查eb_live_room表的like_count字段
SELECT '=== 直播间点赞数检查 ===' as info;
SELECT 
    id,
    uid,
    title,
    like_count,
    create_time
FROM eb_live_room
WHERE uid IN (SELECT uid FROM eb_user WHERE is_streamer = 1)
ORDER BY uid, create_time DESC
LIMIT 20;
