-- ============================================
-- 粉丝团真实数据设置
-- 确保成员来自真实用户，消息与成员对应
-- ============================================

-- 1. 先查看系统中的真实用户
SELECT '=== 系统真实用户 ===' as info;
SELECT uid, nickname, avatar, phone FROM eb_user WHERE is_del = 0 LIMIT 20;

-- 2. 查看当前粉丝团
SELECT '=== 当前粉丝团 ===' as info;
SELECT id, name, anchor_id, anchor_name, member_count FROM eb_fan_group;

-- 3. 清空测试数据，重新基于真实用户创建
DELETE FROM eb_fan_group_member;
DELETE FROM eb_group_message;

-- 4. 为每个粉丝团添加真实用户作为成员
-- 粉丝团1 (anchor_id=121) 的成员 - 从真实用户中选取（排除主播自己）
INSERT INTO eb_fan_group_member (group_id, uid, nickname, level, intimacy, status, join_time)
SELECT 1, u.uid, u.nickname, 
       FLOOR(1 + RAND() * 5) as level,  -- 随机等级1-5
       FLOOR(100 + RAND() * 5000) as intimacy,  -- 随机亲密度
       1, 
       DATE_SUB(NOW(), INTERVAL FLOOR(RAND() * 30) DAY)
FROM eb_user u 
WHERE u.uid != 121 AND u.is_del = 0 AND u.nickname IS NOT NULL AND u.nickname != ''
ORDER BY RAND()
LIMIT 5;

-- 粉丝团2 (anchor_id=122) 的成员
INSERT INTO eb_fan_group_member (group_id, uid, nickname, level, intimacy, status, join_time)
SELECT 2, u.uid, u.nickname,
       FLOOR(1 + RAND() * 5) as level,
       FLOOR(100 + RAND() * 5000) as intimacy,
       1,
       DATE_SUB(NOW(), INTERVAL FLOOR(RAND() * 30) DAY)
FROM eb_user u 
WHERE u.uid != 122 AND u.is_del = 0 AND u.nickname IS NOT NULL AND u.nickname != ''
  AND u.uid NOT IN (SELECT uid FROM eb_fan_group_member WHERE group_id = 1)
ORDER BY RAND()
LIMIT 5;

-- 粉丝团3 (anchor_id=123) 的成员
INSERT INTO eb_fan_group_member (group_id, uid, nickname, level, intimacy, status, join_time)
SELECT 3, u.uid, u.nickname,
       FLOOR(1 + RAND() * 5) as level,
       FLOOR(100 + RAND() * 5000) as intimacy,
       1,
       DATE_SUB(NOW(), INTERVAL FLOOR(RAND() * 30) DAY)
FROM eb_user u 
WHERE u.uid != 123 AND u.is_del = 0 AND u.nickname IS NOT NULL AND u.nickname != ''
  AND u.uid NOT IN (SELECT uid FROM eb_fan_group_member WHERE group_id IN (1,2))
ORDER BY RAND()
LIMIT 3;

-- 5. 更新粉丝团成员数量
UPDATE eb_fan_group fg SET member_count = (
    SELECT COUNT(*) FROM eb_fan_group_member m WHERE m.group_id = fg.id AND m.status = 1
);

-- 6. 为粉丝团添加真实成员的聊天消息
-- 粉丝团1的消息（由该粉丝团的真实成员发送）
INSERT INTO eb_group_message (group_id, sender_id, content, message_type, create_time)
SELECT 1, m.uid, 
       CASE FLOOR(RAND() * 5)
           WHEN 0 THEN '大家好，很高兴加入粉丝团！'
           WHEN 1 THEN '主播今天直播太精彩了！'
           WHEN 2 THEN '支持主播，冲冲冲！'
           WHEN 3 THEN '期待下次直播~'
           ELSE '粉丝团的氛围真好！'
       END,
       'text',
       DATE_SUB(NOW(), INTERVAL FLOOR(RAND() * 7) DAY)
FROM eb_fan_group_member m
WHERE m.group_id = 1 AND m.status = 1
LIMIT 3;

-- 粉丝团2的消息
INSERT INTO eb_group_message (group_id, sender_id, content, message_type, create_time)
SELECT 2, m.uid,
       CASE FLOOR(RAND() * 5)
           WHEN 0 THEN '新人报到！'
           WHEN 1 THEN '主播唱歌太好听了！'
           WHEN 2 THEN '今天的游戏直播太刺激了'
           WHEN 3 THEN '大家晚上好~'
           ELSE '感谢主播的精彩表演！'
       END,
       'text',
       DATE_SUB(NOW(), INTERVAL FLOOR(RAND() * 7) DAY)
FROM eb_fan_group_member m
WHERE m.group_id = 2 AND m.status = 1
LIMIT 3;

-- 7. 验证数据完整性
SELECT '=== 粉丝团成员（关联真实用户）===' as info;
SELECT m.id, m.group_id, fg.name as fan_group_name, 
       m.uid, m.nickname, u.nickname as real_nickname, u.avatar,
       m.level, m.intimacy, m.join_time
FROM eb_fan_group_member m
LEFT JOIN eb_fan_group fg ON m.group_id = fg.id
LEFT JOIN eb_user u ON m.uid = u.uid
WHERE m.status = 1
ORDER BY m.group_id, m.level DESC;

SELECT '=== 聊天消息（关联真实发送者）===' as info;
SELECT gm.id, gm.group_id, fg.name as fan_group_name,
       gm.sender_id, u.nickname as sender_name, u.avatar as sender_avatar,
       gm.content, gm.create_time
FROM eb_group_message gm
LEFT JOIN eb_fan_group fg ON gm.group_id = fg.id
LEFT JOIN eb_user u ON gm.sender_id = u.uid
WHERE gm.is_deleted = 0
ORDER BY gm.group_id, gm.create_time DESC;

SELECT '=== 粉丝团统计 ===' as info;
SELECT fg.id, fg.name, fg.anchor_name, fg.member_count,
       (SELECT COUNT(*) FROM eb_fan_group_member m WHERE m.group_id = fg.id AND m.status = 1) as actual_members,
       (SELECT COUNT(*) FROM eb_group_message gm WHERE gm.group_id = fg.id AND gm.is_deleted = 0) as message_count
FROM eb_fan_group fg;
