-- =====================================================
-- 为用户"道玄"（ID: 121）创建缺失的表并添加测试数据
-- =====================================================

-- =====================================================
-- 1. 创建观看历史表 (eb_watch_history)
-- =====================================================
CREATE TABLE IF NOT EXISTS `eb_watch_history` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `uid` int(11) NOT NULL COMMENT '用户ID',
  `target_type` varchar(20) NOT NULL COMMENT '目标类型：room-直播间, work-作品',
  `target_id` varchar(50) NOT NULL COMMENT '目标ID',
  `target_title` varchar(200) DEFAULT NULL COMMENT '目标标题',
  `target_cover` varchar(500) DEFAULT NULL COMMENT '目标封面',
  `duration` int(11) DEFAULT 0 COMMENT '观看时长(秒)',
  `watch_time` datetime DEFAULT NULL COMMENT '观看时间',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  KEY `idx_uid` (`uid`),
  KEY `idx_target` (`target_type`, `target_id`),
  KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='观看历史表';

-- =====================================================
-- 2. 插入观看历史数据
-- =====================================================
INSERT INTO eb_watch_history (uid, target_type, target_id, target_title, target_cover, duration, watch_time, create_time) VALUES
(121, 'room', '1', '欢乐游戏直播', 'https://example.com/cover1.jpg', 1800, '2026-01-03 20:30:00', '2026-01-03 20:30:00'),
(121, 'room', '2', '音乐分享会', 'https://example.com/cover2.jpg', 2400, '2026-01-03 21:00:00', '2026-01-03 21:00:00'),
(121, 'room', '3', '户外探险直播', 'https://example.com/cover3.jpg', 900, '2026-01-02 19:00:00', '2026-01-02 19:00:00'),
(121, 'room', '4', '美食制作教程', 'https://example.com/cover4.jpg', 3600, '2026-01-02 12:00:00', '2026-01-02 12:00:00'),
(121, 'room', '5', '编程技术分享', 'https://example.com/cover5.jpg', 5400, '2026-01-01 14:00:00', '2026-01-01 14:00:00'),
(121, 'work', '1', '搞笑短视频合集', 'https://example.com/work1.jpg', 180, '2026-01-03 22:00:00', '2026-01-03 22:00:00'),
(121, 'work', '2', '旅行Vlog-云南之旅', 'https://example.com/work2.jpg', 600, '2026-01-03 18:00:00', '2026-01-03 18:00:00'),
(121, 'work', '3', '美妆教程分享', 'https://example.com/work3.jpg', 420, '2026-01-02 16:00:00', '2026-01-02 16:00:00');

-- =====================================================
-- 3. 检查并创建直播间点赞表
-- =====================================================
CREATE TABLE IF NOT EXISTS `eb_live_room_like` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `uid` int(11) NOT NULL COMMENT '用户ID',
  `room_id` int(11) NOT NULL COMMENT '直播间ID',
  `like_count` int(11) DEFAULT 1 COMMENT '点赞次数',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_uid_room` (`uid`, `room_id`),
  KEY `idx_room_id` (`room_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='直播间点赞表';

-- 插入直播间点赞数据
INSERT IGNORE INTO eb_live_room_like (uid, room_id, like_count, create_time) VALUES
(121, 1, 15, '2026-01-03 20:35:00'),
(121, 2, 28, '2026-01-03 21:15:00'),
(121, 3, 8, '2026-01-02 19:10:00'),
(121, 4, 42, '2026-01-02 12:30:00'),
(121, 5, 20, '2026-01-01 14:30:00');

-- =====================================================
-- 4. 插入作品关系数据（点赞和收藏）
-- =====================================================
-- 点赞作品 (type=1 或 type='like')
INSERT IGNORE INTO eb_works_relation (uid, works_id, type, create_time) VALUES
(121, 1, 1, '2026-01-03 22:05:00'),
(121, 2, 1, '2026-01-03 18:10:00'),
(121, 3, 1, '2026-01-02 16:15:00'),
(121, 4, 1, '2026-01-01 20:00:00'),
(121, 5, 1, '2026-01-01 15:00:00');

-- 收藏作品 (type=2 或 type='collect')
INSERT IGNORE INTO eb_works_relation (uid, works_id, type, create_time) VALUES
(121, 1, 2, '2026-01-03 22:06:00'),
(121, 2, 2, '2026-01-03 18:12:00'),
(121, 6, 2, '2026-01-02 10:00:00'),
(121, 7, 2, '2026-01-01 11:00:00');

-- =====================================================
-- 5. 插入关注记录
-- =====================================================
INSERT IGNORE INTO eb_follow_record (uid, follow_uid, create_time) VALUES
(121, 100, '2026-01-03 20:00:00'),
(121, 101, '2026-01-03 15:00:00'),
(121, 102, '2026-01-02 18:00:00'),
(121, 103, '2026-01-02 12:00:00'),
(121, 104, '2026-01-01 20:00:00'),
(121, 105, '2026-01-01 10:00:00');

-- 被关注记录
INSERT IGNORE INTO eb_follow_record (uid, follow_uid, create_time) VALUES
(100, 121, '2026-01-03 21:00:00'),
(101, 121, '2026-01-03 16:00:00'),
(106, 121, '2026-01-02 20:00:00'),
(107, 121, '2026-01-02 14:00:00');

-- =====================================================
-- 6. 插入搜索历史
-- =====================================================
INSERT IGNORE INTO eb_search_history (uid, keyword, search_type, search_count, create_time) VALUES
(121, '游戏直播', 1, 3, '2026-01-03 20:00:00'),
(121, '音乐', 1, 2, '2026-01-03 19:00:00'),
(121, '美食教程', 2, 1, '2026-01-02 11:00:00'),
(121, '旅行Vlog', 2, 2, '2026-01-02 10:00:00'),
(121, '编程学习', 1, 4, '2026-01-01 13:00:00'),
(121, '户外运动', 1, 1, '2026-01-01 09:00:00');

-- =====================================================
-- 7. 创建并插入好友关系
-- =====================================================
CREATE TABLE IF NOT EXISTS `eb_friend` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `uid` int(11) NOT NULL COMMENT '用户ID',
  `friend_uid` int(11) NOT NULL COMMENT '好友用户ID',
  `remark` varchar(50) DEFAULT NULL COMMENT '备注名',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_uid_friend` (`uid`, `friend_uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='好友关系表';

INSERT IGNORE INTO eb_friend (uid, friend_uid, remark, create_time) VALUES
(121, 100, '游戏主播', '2026-01-03 21:35:00'),
(121, 101, '音乐达人', '2026-01-03 16:10:00'),
(100, 121, '道玄', '2026-01-03 21:35:00'),
(101, 121, '道玄', '2026-01-03 16:10:00');

-- =====================================================
-- 8. 更新用户余额和积分
-- =====================================================
UPDATE eb_user SET 
    now_money = 500.00,
    integral = 1200,
    experience = 350
WHERE uid = 121;

-- =====================================================
-- 9. 验证数据
-- =====================================================
SELECT '=== 用户道玄(ID:121)数据统计 ===' as 信息;

SELECT '观看历史' as 类型, COUNT(*) as 数量 FROM eb_watch_history WHERE uid = 121
UNION ALL
SELECT '直播间点赞', COUNT(*) FROM eb_live_room_like WHERE uid = 121
UNION ALL
SELECT '作品点赞', COUNT(*) FROM eb_works_relation WHERE uid = 121 AND type = 1
UNION ALL
SELECT '作品收藏', COUNT(*) FROM eb_works_relation WHERE uid = 121 AND type = 2
UNION ALL
SELECT '关注数', COUNT(*) FROM eb_follow_record WHERE uid = 121
UNION ALL
SELECT '粉丝数', COUNT(*) FROM eb_follow_record WHERE follow_uid = 121
UNION ALL
SELECT '搜索历史', COUNT(*) FROM eb_search_history WHERE uid = 121
UNION ALL
SELECT '好友数', COUNT(*) FROM eb_friend WHERE uid = 121;

-- 检查用户余额
SELECT uid, nickname, now_money as 余额, integral as 积分, experience as 经验 
FROM eb_user WHERE uid = 121;

SELECT '数据插入完成！' as 结果;
