-- 快速验证粉丝数统计
-- 用于确认关注数据和统计逻辑是否正确

-- 1. 查看所有主播
SELECT 
    uid,
    nickname,
    phone,
    is_streamer,
    streamer_level
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;

-- 3. 统计每个主播的粉丝数（正确的统计方式）
SELECT 
    u.uid as '主播ID',
    u.nickname as '主播昵称',
    COUNT(fr.id) as '粉丝数'
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
ORDER BY COUNT(fr.id) DESC;

-- 4. 查看具体某个主播的粉丝列表（替换 1 为实际的主播ID）
SELECT 
    fr.follower_id as '粉丝ID',
    u.nickname as '粉丝昵称',
    u.phone as '粉丝手机号',
    fr.follow_status as '关注状态',
    fr.create_time as '关注时间'
FROM eb_follow_record fr
LEFT JOIN eb_user u ON fr.follower_id = u.uid
WHERE fr.followed_id = 1  -- 替换为实际的主播ID
    AND fr.follow_status IN ('1', '关注') 
    AND fr.is_deleted = 0
ORDER BY fr.create_time DESC;

-- 5. 检查关注状态字段的值分布
SELECT 
    follow_status as '状态值',
    COUNT(*) as '记录数',
    GROUP_CONCAT(DISTINCT id ORDER BY id LIMIT 5) as '示例ID'
FROM eb_follow_record
GROUP BY follow_status;

-- 6. 检查是否有被逻辑删除的记录
SELECT 
    is_deleted as '删除标记',
    COUNT(*) as '记录数'
FROM eb_follow_record
GROUP BY is_deleted;

-- 7. 如果发现状态值不一致，可以执行以下修复（谨慎执行）
-- 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';

-- 8. 测试插入一条关注记录（用于测试）
-- 注意：需要替换为实际存在的用户ID
-- INSERT INTO eb_follow_record (follower_id, follower_nickname, followed_id, followed_nickname, follow_status, is_deleted, create_time)
-- VALUES (2, '测试用户', 1, '测试主播', '1', 0, NOW());
