-- ============================================
-- 完善粉丝团系统数据库
-- ============================================

-- 1. 查看粉丝团表结构
DESC eb_fan_group;

-- 2. 查看粉丝团成员表结构
DESC eb_fan_group_member;

-- 3. 查看当前粉丝团数据
SELECT * FROM eb_fan_group;

-- 4. 插入粉丝团成员测试数据
-- 为粉丝团1（星光粉丝团，主播121）添加成员
INSERT INTO eb_fan_group_member (group_id, uid, nickname, level, intimacy, status, join_time)
SELECT 1, 122, '用户小红', 3, 2500, 1, DATE_SUB(NOW(), INTERVAL 10 DAY)
WHERE NOT EXISTS (SELECT 1 FROM eb_fan_group_member WHERE group_id = 1 AND uid = 122);

INSERT INTO eb_fan_group_member (group_id, uid, nickname, level, intimacy, status, join_time)
SELECT 1, 123, '用户小华', 2, 1200, 1, DATE_SUB(NOW(), INTERVAL 7 DAY)
WHERE NOT EXISTS (SELECT 1 FROM eb_fan_group_member WHERE group_id = 1 AND uid = 123);

INSERT INTO eb_fan_group_member (group_id, uid, nickname, level, intimacy, status, join_time)
SELECT 1, 124, '用户阿杰', 1, 500, 1, DATE_SUB(NOW(), INTERVAL 3 DAY)
WHERE NOT EXISTS (SELECT 1 FROM eb_fan_group_member WHERE group_id = 1 AND uid = 124);

-- 为粉丝团2（梦想家族，主播122）添加成员
INSERT INTO eb_fan_group_member (group_id, uid, nickname, level, intimacy, status, join_time)
SELECT 2, 121, '主播小明', 5, 8000, 1, DATE_SUB(NOW(), INTERVAL 20 DAY)
WHERE NOT EXISTS (SELECT 1 FROM eb_fan_group_member WHERE group_id = 2 AND uid = 121);

INSERT INTO eb_fan_group_member (group_id, uid, nickname, level, intimacy, status, join_time)
SELECT 2, 125, '用户小美', 4, 5500, 1, DATE_SUB(NOW(), INTERVAL 15 DAY)
WHERE NOT EXISTS (SELECT 1 FROM eb_fan_group_member WHERE group_id = 2 AND uid = 125);

INSERT INTO eb_fan_group_member (group_id, uid, nickname, level, intimacy, status, join_time)
SELECT 2, 126, '用户大壮', 2, 1800, 1, DATE_SUB(NOW(), INTERVAL 5 DAY)
WHERE NOT EXISTS (SELECT 1 FROM eb_fan_group_member WHERE group_id = 2 AND uid = 126);

-- 为粉丝团3（快乐大本营，主播123）添加成员
INSERT INTO eb_fan_group_member (group_id, uid, nickname, level, intimacy, status, join_time)
SELECT 3, 127, '快乐粉丝A', 3, 3200, 1, DATE_SUB(NOW(), INTERVAL 12 DAY)
WHERE NOT EXISTS (SELECT 1 FROM eb_fan_group_member WHERE group_id = 3 AND uid = 127);

INSERT INTO eb_fan_group_member (group_id, uid, nickname, level, intimacy, status, join_time)
SELECT 3, 128, '快乐粉丝B', 2, 1500, 1, DATE_SUB(NOW(), INTERVAL 8 DAY)
WHERE NOT EXISTS (SELECT 1 FROM eb_fan_group_member WHERE group_id = 3 AND uid = 128);

-- 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. 验证数据
SELECT '粉丝团列表' as info;
SELECT id, name, anchor_name, member_count, badge, status FROM eb_fan_group;

SELECT '粉丝团成员' as info;
SELECT m.*, fg.name as group_name 
FROM eb_fan_group_member m 
LEFT JOIN eb_fan_group fg ON m.group_id = fg.id
ORDER BY m.group_id, m.level DESC;

-- 7. 创建群组消息表（如果不存在）
CREATE TABLE IF NOT EXISTS eb_group_message (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    group_id BIGINT UNSIGNED NOT NULL COMMENT '群组ID',
    sender_id INT UNSIGNED NOT NULL COMMENT '发送者ID',
    content TEXT COMMENT '消息内容',
    message_type VARCHAR(20) DEFAULT 'text' COMMENT '消息类型',
    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)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='群组消息表';
