blob: 1ee0c8794a858d7d797cb1f2937e5bcc6cfde719 [file] [log] [blame]
夜雨声烦368e3562025-04-24 01:49:46 +08001-- 用户表(保持不变)
wuchimedes079c1632025-04-02 22:01:20 +08002CREATE TABLE IF NOT EXISTS `users` (
夜雨声烦368e3562025-04-24 01:49:46 +08003 `user_id` INT AUTO_INCREMENT PRIMARY KEY,
4 `user_name` VARCHAR(255) NOT NULL,
5 `password` VARCHAR(255) NOT NULL,
6 `email` VARCHAR(255) NOT NULL UNIQUE,
7 `passkey` VARCHAR(255) NOT NULL UNIQUE
wuchimedes079c1632025-04-02 22:01:20 +08008);
wuchimedesa26ed3f2025-04-25 14:41:09 +08009-- 用户统计表
wuchimedes22ee83c2025-04-25 00:17:47 +080010CREATE TABLE IF NOT EXISTS `user_stats` (
11 user_id INT PRIMARY KEY,
12 passkey VARCHAR(255) NOT NULL UNIQUE,
13 total_upload FLOAT NOT NULL DEFAULT 0,
14 total_download FLOAT NOT NULL DEFAULT 0,
15 last_update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
16 FOREIGN KEY (user_id) REFERENCES users(user_id),
17 FOREIGN KEY (passkey) REFERENCES users(passkey)
18);
wuchimedesa26ed3f2025-04-25 14:41:09 +080019-- 种子表
wuchimedes079c1632025-04-02 22:01:20 +080020CREATE TABLE IF NOT EXISTS `torrents` (
夜雨声烦368e3562025-04-24 01:49:46 +080021 `torrent_id` INT AUTO_INCREMENT PRIMARY KEY,
22 `user_id` INT NOT NULL,
23 `torrent_name` VARCHAR(255) NOT NULL,
wuchimedes8a576e02025-05-13 17:50:46 +080024 `file_path` VARCHAR(255) NOT NULL,
夜雨声烦368e3562025-04-24 01:49:46 +080025 `info_hash` BINARY(20) NOT NULL,
26 `file_size` FLOAT NOT NULL,
27 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`)
wuchimedes079c1632025-04-02 22:01:20 +080028);
夜雨声烦368e3562025-04-24 01:49:46 +080029-- Peer表(保持不变)
wuchimedes079c1632025-04-02 22:01:20 +080030CREATE TABLE IF NOT EXISTS `peers` (
夜雨声烦368e3562025-04-24 01:49:46 +080031 `passkey` VARCHAR(255) NOT NULL,
32 `info_hash` BINARY(20) NOT NULL,
33 `peer_id` VARCHAR(20) NOT NULL,
34 `ip_address` VARCHAR(128) NOT NULL,
35 `port` INT NOT NULL,
36 `uploaded` FLOAT NOT NULL,
37 `downloaded` FLOAT NOT NULL,
38 `last_seen` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
39 FOREIGN KEY (`passkey`) REFERENCES `users`(`passkey`),
40 PRIMARY KEY (`passkey`, `info_hash`, `peer_id`)
wuchimedes079c1632025-04-02 22:01:20 +080041);
夜雨声烦368e3562025-04-24 01:49:46 +080042-- 帖子表(新增 hot_score 和 last_calculated 字段)
wuchimedese5722e32025-04-13 17:38:50 +080043CREATE TABLE IF NOT EXISTS `posts` (
夜雨声烦368e3562025-04-24 01:49:46 +080044 `post_id` INT AUTO_INCREMENT PRIMARY KEY,
45 `user_id` INT NOT NULL,
46 `hot_score` DOUBLE DEFAULT 5.0 COMMENT '热度评分',
47 `view_count` INT DEFAULT 0 COMMENT '浏览数',
48 `post_title` VARCHAR(255) NOT NULL,
49 `post_content` TEXT NOT NULL,
wuchimedes8a576e02025-05-13 17:50:46 +080050 `torrent_id` INT DEFAULT NULL,
夜雨声烦368e3562025-04-24 01:49:46 +080051 `post_type` ENUM('resource', 'discussion') NOT NULL,
52 `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
53 `last_calculated` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后热度计算时间',
夜雨声烦070c05a2025-05-13 20:33:50 +080054 `average_rating` DECIMAL(3,2) DEFAULT 0.00 COMMENT '帖子平均评分',
55 `rating_count` INT DEFAULT 0 COMMENT '总评分人数';
夜雨声烦368e3562025-04-24 01:49:46 +080056 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
wuchimedes8a576e02025-05-13 17:50:46 +080057 FOREIGN KEY (`torrent_id`) REFERENCES `torrents`(`torrent_id`),
夜雨声烦368e3562025-04-24 01:49:46 +080058 INDEX `idx_hot_score` (`hot_score`), -- 新增热度索引
59 INDEX `idx_post_type` (`post_type`) -- 新增类型索引
wuchimedese5722e32025-04-13 17:38:50 +080060);
夜雨声烦368e3562025-04-24 01:49:46 +080061-- 标签表(保持不变)
wuchimedes03f93582025-04-21 15:32:59 +080062CREATE TABLE IF NOT EXISTS `tags`(
wuchimedes8a576e02025-05-13 17:50:46 +080063 `tag_id` INT PRIMARY KEY,
64 `tag_name` VARCHAR(255) NOT NULL,
夜雨声烦368e3562025-04-24 01:49:46 +080065 `parent_id` INT DEFAULT NULL,
66 FOREIGN KEY (`parent_id`) REFERENCES `tags`(`tag_id`)
wuchimedes03f93582025-04-21 15:32:59 +080067);
夜雨声烦368e3562025-04-24 01:49:46 +080068-- 帖子标签关联表(保持不变)
wuchimedes03f93582025-04-21 15:32:59 +080069CREATE TABLE IF NOT EXISTS `post_tag` (
夜雨声烦368e3562025-04-24 01:49:46 +080070 `post_id` INT NOT NULL,
71 `tag_id` INT NOT NULL,
72 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
73 FOREIGN KEY (`tag_id`) REFERENCES `tags`(`tag_id`),
74 PRIMARY KEY (`post_id`, `tag_id`)
wuchimedes03f93582025-04-21 15:32:59 +080075);
夜雨声烦368e3562025-04-24 01:49:46 +080076-- 用户关注表(保持不变)
夜雨声烦7e6eb382025-04-22 01:18:00 +080077CREATE TABLE IF NOT EXISTS `user_follows` (
夜雨声烦368e3562025-04-24 01:49:46 +080078 `follower_id` INT NOT NULL,
79 `followed_id` INT NOT NULL,
80 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
81 FOREIGN KEY (`follower_id`) REFERENCES `users`(`user_id`),
82 FOREIGN KEY (`followed_id`) REFERENCES `users`(`user_id`),
83 PRIMARY KEY (`follower_id`, `followed_id`)
夜雨声烦7e6eb382025-04-22 01:18:00 +080084);
夜雨声烦368e3562025-04-24 01:49:46 +080085-- 私信表(保持不变)
夜雨声烦7e6eb382025-04-22 01:18:00 +080086CREATE TABLE IF NOT EXISTS `private_messages` (
夜雨声烦368e3562025-04-24 01:49:46 +080087 `message_id` INT AUTO_INCREMENT PRIMARY KEY,
88 `sender_id` INT NOT NULL,
89 `receiver_id` INT NOT NULL,
90 `content` TEXT NOT NULL,
91 `sent_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
92 `is_read` BOOLEAN DEFAULT false,
93 FOREIGN KEY (`sender_id`) REFERENCES `users`(`user_id`),
94 FOREIGN KEY (`receiver_id`) REFERENCES `users`(`user_id`)
夜雨声烦4527a722025-04-23 17:04:25 +080095);
夜雨声烦368e3562025-04-24 01:49:46 +080096-- 评论表(保持不变)
夜雨声烦4527a722025-04-23 17:04:25 +080097CREATE TABLE IF NOT EXISTS `comments` (
夜雨声烦368e3562025-04-24 01:49:46 +080098 `comment_id` INT AUTO_INCREMENT PRIMARY KEY,
99 `post_id` INT NOT NULL,
100 `user_id` INT NOT NULL,
101 `parent_comment_id` INT DEFAULT NULL,
102 `content` TEXT NOT NULL,
103 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
104 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
105 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
106 FOREIGN KEY (`parent_comment_id`) REFERENCES `comments`(`comment_id`),
107 INDEX `idx_post_id` (`post_id`) -- 新增评论帖子索引
夜雨声烦4527a722025-04-23 17:04:25 +0800108);
夜雨声烦368e3562025-04-24 01:49:46 +0800109-- 帖子点赞表(保持不变)
110CREATE TABLE IF NOT EXISTS `post_likes` (
111 `user_id` INT NOT NULL,
112 `post_id` INT NOT NULL,
113 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
114 PRIMARY KEY (`user_id`, `post_id`),
115 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
116 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`)
夜雨声烦02063592025-04-23 18:10:00 +0800117);
夜雨声烦368e3562025-04-24 01:49:46 +0800118-- 帖子浏览记录表(新增复合索引)
119CREATE TABLE IF NOT EXISTS `post_views` (
120 `view_id` INT AUTO_INCREMENT PRIMARY KEY,
121 `user_id` INT NOT NULL,
122 `post_id` INT NOT NULL,
123 `view_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
124 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
125 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
126 INDEX `idx_user_view_time` (`user_id`, `view_time` DESC) -- 新增用户浏览时间索引
夜雨声烦f77d8132025-04-24 19:31:18 +0800127);
wuchimedes8a576e02025-05-13 17:50:46 +0800128CREATE TABLE IF NOT EXISTS user_tag_preference (
夜雨声烦f77d8132025-04-24 19:31:18 +0800129 user_id INT NOT NULL COMMENT '用户ID',
130 tag_id INT NOT NULL COMMENT '标签ID',
131 weight DOUBLE DEFAULT 1.0 COMMENT '偏好权重(浏览越多权重越高)',
132 last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新时间',
133 PRIMARY KEY (user_id, tag_id),
134 FOREIGN KEY (user_id) REFERENCES users(user_id),
135 FOREIGN KEY (tag_id) REFERENCES tags(tag_id)
223010718e412ad2025-04-24 22:24:51 +0800136);
夜雨声烦070c05a2025-05-13 20:33:50 +0800137CREATE TABLE IF NOT EXISTS `post_ratings` (
138 `user_id` INT NOT NULL COMMENT '用户ID',
139 `post_id` INT NOT NULL COMMENT '帖子ID',
140 `rating` TINYINT NOT NULL CHECK (`rating` BETWEEN 1 AND 5) COMMENT '评分值(1-5)',
141 `rated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '评分时间',
142 PRIMARY KEY (`user_id`, `post_id`), -- 确保每个用户对同一帖子只能评分一次
143 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
144 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
145 INDEX idx_post_ratings_post_id ON post_ratings (post_id)
夜雨声烦0a3df4a2025-05-13 21:26:13 +0800146);
147
148CREATE TABLE IF NOT EXISTS `reports` (
149 `report_id` INT AUTO_INCREMENT PRIMARY KEY COMMENT '举报ID',
150 `post_id` INT NOT NULL COMMENT '被举报的帖子ID',
151 `user_id` INT NOT NULL COMMENT '举报人ID',
152 `reason` TEXT NOT NULL COMMENT '举报原因',
153 `status` ENUM('pending', 'resolved', 'rejected') DEFAULT 'pending' COMMENT '处理状态(待处理/已解决/已驳回)',
154 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '举报时间',
155 `resolved_by` INT DEFAULT NULL COMMENT '处理人ID(管理员)',
156 `resolved_at` TIMESTAMP DEFAULT NULL COMMENT '处理时间',
157 `resolution_notes` TEXT DEFAULT NULL COMMENT '处理备注',
158 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
159 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
160 FOREIGN KEY (`resolved_by`) REFERENCES `users`(`user_id`)
夜雨声烦368e3562025-04-24 01:49:46 +0800161);