-- ============================================
-- 粉丝团测试数据设置脚本
-- 基于真实用户创建粉丝团测试数据
-- ============================================

-- ==================== 第一部分：创建表（如果不存在）====================

-- 1. 创建粉丝团表
CREATE TABLE IF NOT EXISTS eb_fan_group (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '粉丝团ID',
    anchor_id INT UNSIGNED NOT NULL COMMENT '主播ID',
    anchor_name VARCHAR(100) DEFAULT NULL COMMENT '主播名称',
    group_id BIGINT UNSIGNED DEFAULT NULL COMMENT '关联群组ID',
    name VARCHAR(100) NOT NULL COMMENT '粉丝团名称',
    badge VARCHAR(50) DEFAULT '粉丝' COMMENT '粉丝徽章',
    badge_color VARCHAR(20) DEFAULT '#FF6B9D' COMMENT '徽章颜色',
    member_count INT DEFAULT 0 COMMENT '成员数量',
    status TINYINT DEFAULT 1 COMMENT '状态 1-正常 0-禁用',
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    PRIMARY KEY (id),
    KEY idx_anchor_id (anchor_id),
    KEY idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='粉丝团表';

-- 2. 创建粉丝团成员表
CREATE TABLE IF NOT EXISTS eb_fan_group_member (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '成员ID',
    group_id INT UNSIGNED NOT NULL COMMENT '粉丝团ID',
    uid INT UNSIGNED NOT NULL COMMENT '用户ID',
    nickname VARCHAR(100) DEFAULT NULL COMMENT '用户昵称',
    level INT DEFAULT 1 COMMENT '粉丝等级 1-10',
    intimacy INT DEFAULT 0 COMMENT '亲密度',
    status TINYINT DEFAULT 1 COMMENT '状态 1-正常 0-退出',
    join_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '加入时间',
    PRIMARY KEY (id),
    UNIQUE KEY uk_group_uid (group_id, uid),
    KEY idx_uid (uid),
    KEY idx_level (level)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='粉丝团成员表';

-- 3. 创建群组消息表
CREATE TABLE IF NOT EXISTS eb_group_message (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '消息ID',
    group_id INT UNSIGNED NOT NULL COMMENT '粉丝团ID',
    sender_id INT UNSIGNED NOT NULL COMMENT '发送者ID',
    content TEXT COMMENT '消息内容',
    message_type VARCHAR(20) DEFAULT 'text' COMMENT '消息类型 text/image/gift',
    is_deleted TINYINT DEFAULT 0 COMMENT '是否删除',
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    PRIMARY KEY (id),
    KEY idx_group_id (group_id),
    KEY idx_sender_id (sender_id),
    KEY idx_create_time (create_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='群组消息表';

-- ==================== 第二部分：查看可用用户 ====================

SELECT '=== 可用用户列表（前20个）===' as info;
SELECT uid, nickname, avatar, phone FROM eb_user WHERE status = 1 LIMIT 20;

-- ==================== 第三部分：创建粉丝团（如果不存在）====================

-- 为用户100创建粉丝团（如果不存在）
INSERT INTO eb_fan_group (anchor_id, anchor_name, name, badge, badge_color, member_count, status, create_time)
SELECT 100, u.nickname, CONCAT(u.nickname, '的粉丝团'), '星光', '#FF6B9D', 0, 1, NOW()
FROM eb_user u
WHERE u.uid = 100 AND u.status = 1
AND NOT EXISTS (SELECT 1 FROM eb_fan_group WHERE anchor_id = 100);

-- 为用户101创建粉丝团（如果不存在）
INSERT INTO eb_fan_group (anchor_id, anchor_name, name, badge, badge_color, member_count, status, create_time)
SELECT 101, u.nickname, CONCAT(u.nickname, '的粉丝团'), '梦想', '#6B9DFF', 0, 1, NOW()
FROM eb_user u
WHERE u.uid = 101 AND u.status = 1
AND NOT EXISTS (SELECT 1 FROM eb_fan_group WHERE anchor_id = 101);

-- 为用户102创建粉丝团（如果不存在）
INSERT INTO eb_fan_group (anchor_id, anchor_name, name, badge, badge_color, member_count, status, create_time)
SELECT 102, u.nickname, CONCAT(u.nickname, '的粉丝团'), '快乐', '#9DFF6B', 0, 1, NOW()
FROM eb_user u
WHERE u.uid = 102 AND u.status = 1
AND NOT EXISTS (SELECT 1 FROM eb_fan_group WHERE anchor_id = 102);

-- ==================== 第四部分：添加粉丝团成员 ====================

-- 获取粉丝团ID
SET @fg1 = (SELECT id FROM eb_fan_group WHERE anchor_id = 100 LIMIT 1);
SET @fg2 = (SELECT id FROM eb_fan_group WHERE anchor_id = 101 LIMIT 1);
SET @fg3 = (SELECT id FROM eb_fan_group WHERE anchor_id = 102 LIMIT 1);

-- 为粉丝团1添加成员（从真实用户中选取）
INSERT IGNORE INTO eb_fan_group_member (group_id, uid, nickname, level, intimacy, status, join_time)
SELECT @fg1, 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 NOT IN (100, 101, 102) 
  AND u.status = 1 
  AND u.nickname IS NOT NULL 
  AND u.nickname != ''
  AND @fg1 IS NOT NULL
ORDER BY RAND()
LIMIT 5;

-- 为粉丝团2添加成员
INSERT IGNORE INTO eb_fan_group_member (group_id, uid, nickname, level, intimacy, status, join_time)
SELECT @fg2, 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 NOT IN (100, 101, 102) 
  AND u.status = 1 
  AND u.nickname IS NOT NULL 
  AND u.nickname != ''
  AND u.uid NOT IN (SELECT uid FROM eb_fan_group_member WHERE group_id = @fg1)
  AND @fg2 IS NOT NULL
ORDER BY RAND()
LIMIT 4;

-- 为粉丝团3添加成员
INSERT IGNORE INTO eb_fan_group_member (group_id, uid, nickname, level, intimacy, status, join_time)
SELECT @fg3, 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 NOT IN (100, 101, 102) 
  AND u.status = 1 
  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 (@fg1, @fg2))
  AND @fg3 IS NOT NULL
ORDER BY RAND()
LIMIT 3;

-- ==================== 第五部分：添加聊天消息 ====================

-- 为粉丝团1添加消息
INSERT INTO eb_group_message (group_id, sender_id, content, message_type, create_time)
SELECT @fg1, 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 = @fg1 AND m.status = 1 AND @fg1 IS NOT NULL
LIMIT 5;

-- 为粉丝团2添加消息
INSERT INTO eb_group_message (group_id, sender_id, content, message_type, create_time)
SELECT @fg2, 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 = @fg2 AND m.status = 1 AND @fg2 IS NOT NULL
LIMIT 4;

-- ==================== 第六部分：更新成员数量 ====================

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
);

-- ==================== 第七部分：验证数据 ====================

SELECT '=== 粉丝团列表 ===' as info;
SELECT id, name, anchor_id, anchor_name, badge, badge_color, member_count, status, create_time 
FROM eb_fan_group ORDER BY id;

SELECT '=== 粉丝团成员 ===' as info;
SELECT m.id, m.group_id, fg.name as fan_group_name, m.uid, m.nickname, 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
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, 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
LIMIT 20;

SELECT '=== 数据设置完成 ===' as info;
