-- ============================================
-- 粉丝团系统完整设置脚本
-- 执行此脚本以完善粉丝团功能
-- ============================================

-- 1. 查看当前粉丝团数据
SELECT '=== 当前粉丝团 ===' as info;
SELECT id, name, anchor_id, anchor_name, member_count, badge, status FROM eb_fan_group;

-- 2. 查看粉丝团成员表结构
SELECT '=== 成员表结构 ===' as info;
DESC eb_fan_group_member;

-- 3. 插入粉丝团成员测试数据（如果不存在）
-- 粉丝团1的成员
INSERT IGNORE INTO eb_fan_group_member (group_id, uid, nickname, level, intimacy, status, join_time)
VALUES 
(1, 122, '用户小红', 3, 2500, 1, DATE_SUB(NOW(), INTERVAL 10 DAY)),
(1, 123, '用户小华', 2, 1200, 1, DATE_SUB(NOW(), INTERVAL 7 DAY)),
(1, 124, '用户阿杰', 1, 500, 1, DATE_SUB(NOW(), INTERVAL 3 DAY));

-- 粉丝团2的成员
INSERT IGNORE INTO eb_fan_group_member (group_id, uid, nickname, level, intimacy, status, join_time)
VALUES 
(2, 121, '主播小明', 5, 8000, 1, DATE_SUB(NOW(), INTERVAL 20 DAY)),
(2, 125, '用户小美', 4, 5500, 1, DATE_SUB(NOW(), INTERVAL 15 DAY)),
(2, 126, '用户大壮', 2, 1800, 1, DATE_SUB(NOW(), INTERVAL 5 DAY));

-- 粉丝团3的成员
INSERT IGNORE INTO eb_fan_group_member (group_id, uid, nickname, level, intimacy, status, join_time)
VALUES 
(3, 127, '快乐粉丝A', 3, 3200, 1, DATE_SUB(NOW(), INTERVAL 12 DAY)),
(3, 128, '快乐粉丝B', 2, 1500, 1, DATE_SUB(NOW(), INTERVAL 8 DAY));

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

-- 5. 创建群组消息表（如果不存在）
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='群组消息表';

-- 6. 插入一些测试聊天消息
INSERT IGNORE INTO eb_group_message (id, group_id, sender_id, content, message_type, create_time)
VALUES 
(1, 1, 122, '大家好，我是新来的粉丝！', 'text', DATE_SUB(NOW(), INTERVAL 2 DAY)),
(2, 1, 123, '欢迎欢迎！', 'text', DATE_SUB(NOW(), INTERVAL 2 DAY)),
(3, 1, 121, '感谢大家的支持！', 'text', DATE_SUB(NOW(), INTERVAL 1 DAY)),
(4, 2, 125, '今晚直播太精彩了！', 'text', DATE_SUB(NOW(), INTERVAL 1 DAY)),
(5, 2, 126, '同意！期待下次直播', 'text', DATE_SUB(NOW(), INTERVAL 1 DAY));

-- 7. 验证最终数据
SELECT '=== 粉丝团列表 ===' as info;
SELECT id, name, anchor_name, member_count, badge, badge_color, status, create_time FROM eb_fan_group;

SELECT '=== 粉丝团成员 ===' as info;
SELECT m.id, m.group_id, fg.name as group_name, m.uid, m.nickname, m.level, m.intimacy, m.status, m.join_time
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;

SELECT '=== 聊天消息 ===' as info;
SELECT gm.id, gm.group_id, fg.name as group_name, gm.sender_id, gm.content, gm.create_time
FROM eb_group_message gm
LEFT JOIN eb_fan_group fg ON gm.group_id = fg.id
WHERE gm.is_deleted = 0
ORDER BY gm.create_time DESC;

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