-- 为指定用户添加测试数据
-- 使用方法: 将 @USER_ID 替换为实际的用户ID

-- 设置用户ID变量 (修改这里的值为移动端登录的用户ID)
SET @USER_ID = 121;

-- 查看当前用户信息
SELECT uid, nickname, phone FROM eb_user WHERE uid = @USER_ID;

-- ========================================
-- 0. 检查必要的表是否存在
-- ========================================
-- 如果 eb_view_history 表不存在，先创建它
CREATE TABLE IF NOT EXISTS eb_view_history (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL COMMENT '用户ID',
    target_type VARCHAR(20) NOT NULL COMMENT '目标类型: room-直播间, work-作品, profile-用户主页',
    target_id VARCHAR(50) NOT NULL COMMENT '目标ID',
    target_title VARCHAR(200) DEFAULT NULL COMMENT '目标标题',
    view_duration INT DEFAULT 0 COMMENT '观看时长(秒)',
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    INDEX idx_user_id (user_id),
    INDEX idx_target (target_type, target_id),
    UNIQUE KEY uk_user_target (user_id, target_type, target_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户观看历史';

-- ========================================
-- 1. 添加观看历史
-- ========================================
-- 先检查是否有直播间数据
SELECT COUNT(*) as room_count FROM eb_live_room;

-- 添加直播间观看历史
INSERT IGNORE INTO eb_view_history (user_id, target_type, target_id, target_title, view_duration, create_time, update_time)
SELECT @USER_ID, 'room', id, title, FLOOR(RAND() * 3600), NOW() - INTERVAL FLOOR(RAND() * 7) DAY, NOW()
FROM eb_live_room LIMIT 5;

-- 检查是否有作品数据
SELECT COUNT(*) as works_count FROM eb_works;

-- 添加作品观看历史
INSERT IGNORE INTO eb_view_history (user_id, target_type, target_id, target_title, view_duration, create_time, update_time)
SELECT @USER_ID, 'work', id, title, FLOOR(RAND() * 600), NOW() - INTERVAL FLOOR(RAND() * 7) DAY, NOW()
FROM eb_works LIMIT 5;

-- ========================================
-- 2. 添加直播间点赞
-- ========================================
-- 检查 eb_live_room_like 表是否存在
CREATE TABLE IF NOT EXISTS eb_live_room_like (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL COMMENT '用户ID',
    room_id INT NOT NULL COMMENT '直播间ID',
    like_count INT DEFAULT 1 COMMENT '点赞次数',
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    INDEX idx_user_id (user_id),
    INDEX idx_room_id (room_id),
    UNIQUE KEY uk_user_room (user_id, room_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='直播间点赞记录';

INSERT IGNORE INTO eb_live_room_like (user_id, room_id, like_count, create_time)
SELECT @USER_ID, id, FLOOR(RAND() * 10) + 1, NOW() - INTERVAL FLOOR(RAND() * 7) DAY
FROM eb_live_room LIMIT 5;

-- ========================================
-- 3. 添加作品点赞 (type=1)
-- ========================================
INSERT IGNORE INTO eb_works_relation (uid, works_id, type, create_time)
SELECT @USER_ID, id, 1, NOW() - INTERVAL FLOOR(RAND() * 7) DAY
FROM eb_works LIMIT 5;

-- ========================================
-- 4. 添加作品收藏 (type=2)
-- ========================================
INSERT IGNORE INTO eb_works_relation (uid, works_id, type, create_time)
SELECT @USER_ID, id, 2, NOW() - INTERVAL FLOOR(RAND() * 7) DAY
FROM eb_works WHERE id NOT IN (SELECT works_id FROM eb_works_relation WHERE uid = @USER_ID AND type = 2) LIMIT 4;

-- ========================================
-- 5. 添加关注记录
-- ========================================
-- 检查 eb_follow_record 表是否存在
CREATE TABLE IF NOT EXISTS eb_follow_record (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    follower_id INT NOT NULL COMMENT '关注者ID',
    follower_nickname VARCHAR(100) DEFAULT NULL COMMENT '关注者昵称',
    followed_id INT NOT NULL COMMENT '被关注者ID',
    followed_nickname VARCHAR(100) DEFAULT NULL COMMENT '被关注者昵称',
    follow_status VARCHAR(20) DEFAULT '1' COMMENT '关注状态: 1或关注=已关注',
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    INDEX idx_follower (follower_id),
    INDEX idx_followed (followed_id),
    UNIQUE KEY uk_follow (follower_id, followed_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='关注记录';

INSERT IGNORE INTO eb_follow_record (follower_id, follower_nickname, followed_id, followed_nickname, follow_status, create_time)
SELECT @USER_ID, 
       (SELECT nickname FROM eb_user WHERE uid = @USER_ID),
       uid, 
       nickname, 
       1, 
       NOW() - INTERVAL FLOOR(RAND() * 30) DAY
FROM eb_user 
WHERE uid != @USER_ID 
AND uid NOT IN (SELECT followed_id FROM eb_follow_record WHERE follower_id = @USER_ID)
LIMIT 6;

-- ========================================
-- 验证数据
-- ========================================
SELECT '观看历史' as type, COUNT(*) as count FROM eb_view_history WHERE user_id = @USER_ID
UNION ALL
SELECT '直播间点赞', COUNT(*) FROM eb_live_room_like WHERE user_id = @USER_ID
UNION ALL
SELECT '作品点赞', COUNT(*) FROM eb_works_relation WHERE uid = @USER_ID AND type = 1
UNION ALL
SELECT '作品收藏', COUNT(*) FROM eb_works_relation WHERE uid = @USER_ID AND type = 2
UNION ALL
SELECT '关注记录', COUNT(*) FROM eb_follow_record WHERE follower_id = @USER_ID AND (follow_status = 1 OR follow_status = '关注');

-- ========================================
-- 查看所有用户列表（用于确定移动端登录的用户ID）
-- ========================================
SELECT uid, nickname, phone, create_time FROM eb_user ORDER BY uid DESC LIMIT 20;
