-- ============================================
-- 用户活动记录测试数据
-- 基于已有用户 (uid: 100-109) 生成
-- ============================================

-- 先查看已有用户
SELECT uid, nickname, phone FROM eb_user WHERE uid BETWEEN 100 AND 120 LIMIT 20;

-- 先查看已有直播间
SELECT id, title, uid, streamer_name FROM eb_live_room LIMIT 10;

-- 先查看已有作品
SELECT id, title, user_id FROM eb_works LIMIT 10;

-- ============================================
-- 1. 观看历史测试数据 (eb_view_history)
-- ============================================
INSERT INTO `eb_view_history` (`user_id`, `target_type`, `target_id`, `target_title`, `view_duration`, `create_time`, `update_time`) VALUES
-- 用户100的观看历史
(100, 'room', '1', '欢乐直播间', 1800, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),
(100, 'room', '2', '音乐之夜', 3600, DATE_SUB(NOW(), INTERVAL 2 DAY), DATE_SUB(NOW(), INTERVAL 2 DAY)),
(100, 'room', '3', '游戏竞技场', 2400, DATE_SUB(NOW(), INTERVAL 3 DAY), DATE_SUB(NOW(), INTERVAL 3 DAY)),
(100, 'work', '1', '我的第一个作品', 120, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),
(100, 'work', '2', '精彩瞬间', 90, DATE_SUB(NOW(), INTERVAL 2 DAY), DATE_SUB(NOW(), INTERVAL 2 DAY)),
(100, 'profile', '101', '林国瑞', 60, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),

-- 用户101的观看历史
(101, 'room', '1', '欢乐直播间', 2700, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),
(101, 'room', '4', '美食探店', 1500, DATE_SUB(NOW(), INTERVAL 2 DAY), DATE_SUB(NOW(), INTERVAL 2 DAY)),
(101, 'work', '3', '旅行日记', 180, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),
(101, 'profile', '100', '张吉惟', 45, DATE_SUB(NOW(), INTERVAL 3 DAY), DATE_SUB(NOW(), INTERVAL 3 DAY)),

-- 用户102的观看历史
(102, 'room', '2', '音乐之夜', 4200, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),
(102, 'room', '5', '户外探险', 1800, DATE_SUB(NOW(), INTERVAL 4 DAY), DATE_SUB(NOW(), INTERVAL 4 DAY)),
(102, 'work', '1', '我的第一个作品', 150, DATE_SUB(NOW(), INTERVAL 2 DAY), DATE_SUB(NOW(), INTERVAL 2 DAY)),

-- 用户103的观看历史
(103, 'room', '1', '欢乐直播间', 900, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),
(103, 'room', '3', '游戏竞技场', 5400, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),
(103, 'work', '2', '精彩瞬间', 200, DATE_SUB(NOW(), INTERVAL 3 DAY), DATE_SUB(NOW(), INTERVAL 3 DAY)),
(103, 'profile', '102', '王小明', 30, DATE_SUB(NOW(), INTERVAL 5 DAY), DATE_SUB(NOW(), INTERVAL 5 DAY)),

-- 用户104的观看历史
(104, 'room', '2', '音乐之夜', 2100, DATE_SUB(NOW(), INTERVAL 2 DAY), DATE_SUB(NOW(), INTERVAL 2 DAY)),
(104, 'work', '4', '日常生活', 80, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),

-- 用户105的观看历史
(105, 'room', '1', '欢乐直播间', 3300, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),
(105, 'room', '4', '美食探店', 2400, DATE_SUB(NOW(), INTERVAL 3 DAY), DATE_SUB(NOW(), INTERVAL 3 DAY)),
(105, 'profile', '103', '李小红', 55, DATE_SUB(NOW(), INTERVAL 2 DAY), DATE_SUB(NOW(), INTERVAL 2 DAY))

ON DUPLICATE KEY UPDATE 
    view_duration = view_duration + VALUES(view_duration),
    update_time = NOW();

-- ============================================
-- 2. 直播间点赞记录测试数据 (eb_live_room_like)
-- ============================================
INSERT INTO `eb_live_room_like` (`user_id`, `room_id`, `like_count`, `create_time`, `update_time`) VALUES
-- 用户100点赞的直播间
(100, 1, 15, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),
(100, 2, 8, DATE_SUB(NOW(), INTERVAL 2 DAY), DATE_SUB(NOW(), INTERVAL 2 DAY)),
(100, 3, 22, DATE_SUB(NOW(), INTERVAL 3 DAY), DATE_SUB(NOW(), INTERVAL 3 DAY)),

-- 用户101点赞的直播间
(101, 1, 30, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),
(101, 4, 12, DATE_SUB(NOW(), INTERVAL 2 DAY), DATE_SUB(NOW(), INTERVAL 2 DAY)),

-- 用户102点赞的直播间
(102, 2, 45, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),
(102, 5, 18, DATE_SUB(NOW(), INTERVAL 4 DAY), DATE_SUB(NOW(), INTERVAL 4 DAY)),

-- 用户103点赞的直播间
(103, 1, 5, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),
(103, 3, 88, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),

-- 用户104点赞的直播间
(104, 2, 25, DATE_SUB(NOW(), INTERVAL 2 DAY), DATE_SUB(NOW(), INTERVAL 2 DAY)),
(104, 1, 10, DATE_SUB(NOW(), INTERVAL 3 DAY), DATE_SUB(NOW(), INTERVAL 3 DAY)),

-- 用户105点赞的直播间
(105, 1, 50, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),
(105, 4, 33, DATE_SUB(NOW(), INTERVAL 3 DAY), DATE_SUB(NOW(), INTERVAL 3 DAY))

ON DUPLICATE KEY UPDATE 
    like_count = like_count + VALUES(like_count),
    update_time = NOW();

-- ============================================
-- 3. 作品关系测试数据 (eb_works_relation) - 点赞和收藏
-- ============================================
INSERT INTO `eb_works_relation` (`uid`, `works_id`, `type`, `create_time`) VALUES
-- 用户100的点赞和收藏
(100, 1, 'like', DATE_SUB(NOW(), INTERVAL 1 DAY)),
(100, 2, 'like', DATE_SUB(NOW(), INTERVAL 2 DAY)),
(100, 3, 'like', DATE_SUB(NOW(), INTERVAL 3 DAY)),
(100, 1, 'collect', DATE_SUB(NOW(), INTERVAL 1 DAY)),
(100, 2, 'collect', DATE_SUB(NOW(), INTERVAL 2 DAY)),

-- 用户101的点赞和收藏
(101, 1, 'like', DATE_SUB(NOW(), INTERVAL 1 DAY)),
(101, 3, 'like', DATE_SUB(NOW(), INTERVAL 2 DAY)),
(101, 4, 'like', DATE_SUB(NOW(), INTERVAL 3 DAY)),
(101, 3, 'collect', DATE_SUB(NOW(), INTERVAL 1 DAY)),

-- 用户102的点赞和收藏
(102, 1, 'like', DATE_SUB(NOW(), INTERVAL 2 DAY)),
(102, 2, 'like', DATE_SUB(NOW(), INTERVAL 3 DAY)),
(102, 1, 'collect', DATE_SUB(NOW(), INTERVAL 2 DAY)),
(102, 4, 'collect', DATE_SUB(NOW(), INTERVAL 4 DAY)),

-- 用户103的点赞和收藏
(103, 2, 'like', DATE_SUB(NOW(), INTERVAL 1 DAY)),
(103, 5, 'like', DATE_SUB(NOW(), INTERVAL 2 DAY)),
(103, 2, 'collect', DATE_SUB(NOW(), INTERVAL 1 DAY)),

-- 用户104的点赞和收藏
(104, 1, 'like', DATE_SUB(NOW(), INTERVAL 1 DAY)),
(104, 4, 'like', DATE_SUB(NOW(), INTERVAL 2 DAY)),
(104, 5, 'like', DATE_SUB(NOW(), INTERVAL 3 DAY)),
(104, 4, 'collect', DATE_SUB(NOW(), INTERVAL 2 DAY)),

-- 用户105的点赞和收藏
(105, 2, 'like', DATE_SUB(NOW(), INTERVAL 1 DAY)),
(105, 3, 'like', DATE_SUB(NOW(), INTERVAL 2 DAY)),
(105, 3, 'collect', DATE_SUB(NOW(), INTERVAL 2 DAY)),
(105, 5, 'collect', DATE_SUB(NOW(), INTERVAL 3 DAY))

ON DUPLICATE KEY UPDATE create_time = VALUES(create_time);

-- ============================================
-- 4. 搜索历史测试数据 (eb_search_history)
-- ============================================
INSERT INTO `eb_search_history` (`user_id`, `keyword`, `search_type`, `search_count`, `is_deleted`, `create_time`, `update_time`) VALUES
-- 用户100的搜索历史
(100, '游戏直播', 2, 5, 0, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),
(100, '音乐', 0, 3, 0, DATE_SUB(NOW(), INTERVAL 2 DAY), DATE_SUB(NOW(), INTERVAL 2 DAY)),
(100, '美食', 3, 2, 0, DATE_SUB(NOW(), INTERVAL 3 DAY), DATE_SUB(NOW(), INTERVAL 3 DAY)),
(100, '林国瑞', 1, 1, 0, DATE_SUB(NOW(), INTERVAL 4 DAY), DATE_SUB(NOW(), INTERVAL 4 DAY)),

-- 用户101的搜索历史
(101, '户外', 2, 4, 0, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),
(101, '旅行', 3, 6, 0, DATE_SUB(NOW(), INTERVAL 2 DAY), DATE_SUB(NOW(), INTERVAL 2 DAY)),
(101, '张吉惟', 1, 2, 0, DATE_SUB(NOW(), INTERVAL 3 DAY), DATE_SUB(NOW(), INTERVAL 3 DAY)),

-- 用户102的搜索历史
(102, '音乐直播', 2, 8, 0, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),
(102, '唱歌', 0, 3, 0, DATE_SUB(NOW(), INTERVAL 2 DAY), DATE_SUB(NOW(), INTERVAL 2 DAY)),

-- 用户103的搜索历史
(103, '电竞', 2, 10, 0, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),
(103, 'LOL', 0, 5, 0, DATE_SUB(NOW(), INTERVAL 2 DAY), DATE_SUB(NOW(), INTERVAL 2 DAY)),
(103, '王者荣耀', 0, 7, 0, DATE_SUB(NOW(), INTERVAL 3 DAY), DATE_SUB(NOW(), INTERVAL 3 DAY)),

-- 用户104的搜索历史
(104, '美食探店', 2, 4, 0, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),
(104, '火锅', 3, 2, 0, DATE_SUB(NOW(), INTERVAL 2 DAY), DATE_SUB(NOW(), INTERVAL 2 DAY)),

-- 用户105的搜索历史
(105, '舞蹈', 3, 6, 0, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 1 DAY)),
(105, '健身', 0, 3, 0, DATE_SUB(NOW(), INTERVAL 2 DAY), DATE_SUB(NOW(), INTERVAL 2 DAY))

ON DUPLICATE KEY UPDATE 
    search_count = search_count + 1,
    update_time = NOW();

-- ============================================
-- 5. 热门搜索测试数据 (eb_hot_search)
-- ============================================
INSERT INTO `eb_hot_search` (`keyword`, `search_type`, `hot_score`, `search_count`, `sort_order`, `status`, `create_time`, `update_time`) VALUES
('游戏直播', 2, 1000, 500, 1, 1, NOW(), NOW()),
('音乐', 0, 850, 420, 2, 1, NOW(), NOW()),
('美食探店', 2, 780, 380, 3, 1, NOW(), NOW()),
('户外探险', 2, 650, 320, 4, 1, NOW(), NOW()),
('电竞比赛', 2, 600, 300, 5, 1, NOW(), NOW()),
('舞蹈', 3, 550, 280, 6, 1, NOW(), NOW()),
('旅行日记', 3, 500, 250, 7, 1, NOW(), NOW()),
('健身教程', 3, 450, 220, 8, 1, NOW(), NOW()),
('唱歌', 0, 400, 200, 9, 1, NOW(), NOW()),
('搞笑视频', 3, 380, 190, 10, 1, NOW(), NOW())

ON DUPLICATE KEY UPDATE 
    hot_score = VALUES(hot_score),
    search_count = VALUES(search_count),
    update_time = NOW();

-- ============================================
-- 6. 关注记录补充数据 (eb_follow_record)
-- 确保有足够的关注数据用于测试
-- ============================================
INSERT IGNORE INTO `eb_follow_record` (`follower_id`, `follower_nickname`, `followed_id`, `followed_nickname`, `follow_status`, `is_deleted`, `create_time`) VALUES
-- 用户100关注的人
(100, '吉惟同学', 101, '林国瑞', 1, 0, DATE_SUB(NOW(), INTERVAL 10 DAY)),
(100, '吉惟同学', 102, '王小明', 1, 0, DATE_SUB(NOW(), INTERVAL 8 DAY)),
(100, '吉惟同学', 103, '李小红', 1, 0, DATE_SUB(NOW(), INTERVAL 5 DAY)),

-- 用户101关注的人
(101, '林国瑞', 100, '吉惟同学', 1, 0, DATE_SUB(NOW(), INTERVAL 9 DAY)),
(101, '林国瑞', 104, '赵小刚', 1, 0, DATE_SUB(NOW(), INTERVAL 7 DAY)),

-- 用户102关注的人
(102, '王小明', 100, '吉惟同学', 1, 0, DATE_SUB(NOW(), INTERVAL 6 DAY)),
(102, '王小明', 101, '林国瑞', 1, 0, DATE_SUB(NOW(), INTERVAL 4 DAY)),
(102, '王小明', 105, '孙小美', 1, 0, DATE_SUB(NOW(), INTERVAL 3 DAY)),

-- 用户103关注的人
(103, '李小红', 100, '吉惟同学', 1, 0, DATE_SUB(NOW(), INTERVAL 5 DAY)),
(103, '李小红', 102, '王小明', 1, 0, DATE_SUB(NOW(), INTERVAL 2 DAY)),

-- 用户104关注的人
(104, '赵小刚', 101, '林国瑞', 1, 0, DATE_SUB(NOW(), INTERVAL 4 DAY)),
(104, '赵小刚', 103, '李小红', 1, 0, DATE_SUB(NOW(), INTERVAL 3 DAY)),

-- 用户105关注的人
(105, '孙小美', 100, '吉惟同学', 1, 0, DATE_SUB(NOW(), INTERVAL 3 DAY)),
(105, '孙小美', 101, '林国瑞', 1, 0, DATE_SUB(NOW(), INTERVAL 2 DAY)),
(105, '孙小美', 104, '赵小刚', 1, 0, DATE_SUB(NOW(), INTERVAL 1 DAY));

-- ============================================
-- 7. 验证数据插入结果
-- ============================================
SELECT '观看历史' as table_name, COUNT(*) as count FROM eb_view_history
UNION ALL
SELECT '直播间点赞' as table_name, COUNT(*) as count FROM eb_live_room_like
UNION ALL
SELECT '作品关系(点赞/收藏)' as table_name, COUNT(*) as count FROM eb_works_relation
UNION ALL
SELECT '搜索历史' as table_name, COUNT(*) as count FROM eb_search_history
UNION ALL
SELECT '热门搜索' as table_name, COUNT(*) as count FROM eb_hot_search
UNION ALL
SELECT '关注记录' as table_name, COUNT(*) as count FROM eb_follow_record;

-- ============================================
-- 8. 查看用户100的活动统计
-- ============================================
SELECT 
    '用户100活动统计' as title,
    (SELECT COUNT(*) FROM eb_view_history WHERE user_id = 100) as view_count,
    (SELECT COUNT(*) FROM eb_live_room_like WHERE user_id = 100) as liked_room_count,
    (SELECT COUNT(*) FROM eb_works_relation WHERE uid = 100 AND type = 'like') as liked_work_count,
    (SELECT COUNT(*) FROM eb_works_relation WHERE uid = 100 AND type = 'collect') as collected_work_count,
    (SELECT COUNT(*) FROM eb_follow_record WHERE follower_id = 100 AND follow_status = 1) as following_count,
    (SELECT COUNT(*) FROM eb_follow_record WHERE followed_id = 100 AND follow_status = 1) as follower_count,
    (SELECT COUNT(*) FROM eb_search_history WHERE user_id = 100 AND is_deleted = 0) as search_history_count;

SELECT '测试数据插入完成！' as message;
