-- 直播间点赞功能数据库表
-- 执行命令: mysql -u root -p zhibo < live_room_like_tables.sql

USE zhibo;

-- 1. 创建直播间点赞记录表
CREATE TABLE IF NOT EXISTS eb_live_room_like (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID',
    room_id INT NOT NULL COMMENT '直播间ID',
    user_id INT NOT NULL COMMENT '用户ID',
    user_nickname VARCHAR(50) COMMENT '用户昵称',
    like_count INT DEFAULT 1 COMMENT '点赞次数（该用户对该直播间的累计点赞）',
    last_like_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后点赞时间',
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    INDEX idx_room_id (room_id),
    INDEX idx_user_id (user_id),
    UNIQUE INDEX idx_user_room (user_id, room_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='直播间点赞记录表';

-- 2. 为直播间表添加点赞数字段
ALTER TABLE eb_live_room 
ADD COLUMN IF NOT EXISTS like_count INT DEFAULT 0 COMMENT '总点赞数' AFTER view_count;

-- 3. 创建索引以提升查询性能
ALTER TABLE eb_live_room_like 
ADD INDEX IF NOT EXISTS idx_last_like_time (last_like_time);

-- 4. 初始化现有直播间的点赞数为0
UPDATE eb_live_room SET like_count = 0 WHERE like_count IS NULL;

-- 5. 查看表结构
DESC eb_live_room_like;
DESC eb_live_room;

-- 6. 插入测试数据（可选）
-- INSERT INTO eb_live_room_like (room_id, user_id, user_nickname, like_count) 
-- VALUES (8, 43, 'xiaofeng', 10);

-- 7. 验证数据
SELECT 
    '直播间点赞记录表' as table_name,
    COUNT(*) as record_count
FROM eb_live_room_like
UNION ALL
SELECT 
    '直播间表（有点赞数）' as table_name,
    COUNT(*) as record_count
FROM eb_live_room
WHERE like_count IS NOT NULL;

SELECT '数据库表创建完成！' as status;
