blob: 0ace8eb693aeaf606c30aeb1335a8f87d2197315 [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,
夜雨声烦35c9da92025-05-20 00:12:48 +08007 `passkey` VARCHAR(255) NOT NULL UNIQUE,
8 `user_level` ENUM('lv1', 'lv2', 'lv3', 'vip') DEFAULT 'lv1',
9 `signin_count` INT DEFAULT 0,
10 `last_signin_date` DATE,
夜雨声烦451d71c2025-05-20 00:58:36 +080011 `role` ENUM('USER', 'ADMIN') DEFAULT 'USER' COMMENT '用户角色',
夜雨声烦7affa472025-05-20 19:27:16 +080012 `is_banned` BOOLEAN DEFAULT FALSE COMMENT '是否被封禁',
13 `banned_reason` VARCHAR(255) COMMENT '封禁原因',
14 `banned_at` DATETIME COMMENT '封禁时间',
15 `banned_by` BIGINT COMMENT '操作管理员ID',
夜雨声烦35c9da92025-05-20 00:12:48 +080016 INDEX `idx_user_level` (`user_level`) -- 按等级查询优化
wuchimedes079c1632025-04-02 22:01:20 +080017);
wuchimedesa26ed3f2025-04-25 14:41:09 +080018-- 用户统计表
wuchimedes22ee83c2025-04-25 00:17:47 +080019CREATE TABLE IF NOT EXISTS `user_stats` (
20 user_id INT PRIMARY KEY,
21 passkey VARCHAR(255) NOT NULL UNIQUE,
22 total_upload FLOAT NOT NULL DEFAULT 0,
23 total_download FLOAT NOT NULL DEFAULT 0,
24 last_update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
25 FOREIGN KEY (user_id) REFERENCES users(user_id),
26 FOREIGN KEY (passkey) REFERENCES users(passkey)
27);
wuchimedesa26ed3f2025-04-25 14:41:09 +080028-- 种子表
wuchimedes079c1632025-04-02 22:01:20 +080029CREATE TABLE IF NOT EXISTS `torrents` (
夜雨声烦368e3562025-04-24 01:49:46 +080030 `torrent_id` INT AUTO_INCREMENT PRIMARY KEY,
31 `user_id` INT NOT NULL,
32 `torrent_name` VARCHAR(255) NOT NULL,
wuchimedes8a576e02025-05-13 17:50:46 +080033 `file_path` VARCHAR(255) NOT NULL,
夜雨声烦368e3562025-04-24 01:49:46 +080034 `info_hash` BINARY(20) NOT NULL,
35 `file_size` FLOAT NOT NULL,
223010719e7ca122025-06-03 23:26:44 +080036 `upload_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '种子上传时间(第一次插入时自动填充)',
37 `is_rare` BOOLEAN NOT NULL DEFAULT FALSE COMMENT '是否为冷门种子',
夜雨声烦368e3562025-04-24 01:49:46 +080038 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`)
wuchimedes079c1632025-04-02 22:01:20 +080039);
夜雨声烦368e3562025-04-24 01:49:46 +080040-- Peer表(保持不变)
wuchimedes079c1632025-04-02 22:01:20 +080041CREATE TABLE IF NOT EXISTS `peers` (
夜雨声烦368e3562025-04-24 01:49:46 +080042 `passkey` VARCHAR(255) NOT NULL,
43 `info_hash` BINARY(20) NOT NULL,
44 `peer_id` VARCHAR(20) NOT NULL,
45 `ip_address` VARCHAR(128) NOT NULL,
46 `port` INT NOT NULL,
47 `uploaded` FLOAT NOT NULL,
48 `downloaded` FLOAT NOT NULL,
49 `last_seen` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
50 FOREIGN KEY (`passkey`) REFERENCES `users`(`passkey`),
51 PRIMARY KEY (`passkey`, `info_hash`, `peer_id`)
wuchimedes079c1632025-04-02 22:01:20 +080052);
夜雨声烦368e3562025-04-24 01:49:46 +080053-- 帖子表(新增 hot_score 和 last_calculated 字段)
wuchimedese5722e32025-04-13 17:38:50 +080054CREATE TABLE IF NOT EXISTS `posts` (
夜雨声烦368e3562025-04-24 01:49:46 +080055 `post_id` INT AUTO_INCREMENT PRIMARY KEY,
56 `user_id` INT NOT NULL,
57 `hot_score` DOUBLE DEFAULT 5.0 COMMENT '热度评分',
58 `view_count` INT DEFAULT 0 COMMENT '浏览数',
59 `post_title` VARCHAR(255) NOT NULL,
60 `post_content` TEXT NOT NULL,
wuchimedes8a576e02025-05-13 17:50:46 +080061 `torrent_id` INT DEFAULT NULL,
夜雨声烦368e3562025-04-24 01:49:46 +080062 `post_type` ENUM('resource', 'discussion') NOT NULL,
63 `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
64 `last_calculated` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后热度计算时间',
夜雨声烦070c05a2025-05-13 20:33:50 +080065 `average_rating` DECIMAL(3,2) DEFAULT 0.00 COMMENT '帖子平均评分',
wuchimedes1ab4f5b2025-05-20 16:22:43 +080066 `rating_count` INT DEFAULT 0 COMMENT '总评分人数',
夜雨声烦7affa472025-05-20 19:27:16 +080067 `is_locked` BOOLEAN DEFAULT FALSE COMMENT '是否被锁定',
68 `locked_reason` VARCHAR(255) COMMENT '锁定原因',
69 `locked_at` DATETIME COMMENT '锁定时间',
70 `locked_by` BIGINT COMMENT '操作管理员ID',
夜雨声烦368e3562025-04-24 01:49:46 +080071 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
wuchimedes8a576e02025-05-13 17:50:46 +080072 FOREIGN KEY (`torrent_id`) REFERENCES `torrents`(`torrent_id`),
夜雨声烦368e3562025-04-24 01:49:46 +080073 INDEX `idx_hot_score` (`hot_score`), -- 新增热度索引
74 INDEX `idx_post_type` (`post_type`) -- 新增类型索引
wuchimedese5722e32025-04-13 17:38:50 +080075);
夜雨声烦368e3562025-04-24 01:49:46 +080076-- 标签表(保持不变)
wuchimedes03f93582025-04-21 15:32:59 +080077CREATE TABLE IF NOT EXISTS `tags`(
wuchimedes8a576e02025-05-13 17:50:46 +080078 `tag_id` INT PRIMARY KEY,
79 `tag_name` VARCHAR(255) NOT NULL,
夜雨声烦368e3562025-04-24 01:49:46 +080080 `parent_id` INT DEFAULT NULL,
81 FOREIGN KEY (`parent_id`) REFERENCES `tags`(`tag_id`)
wuchimedes03f93582025-04-21 15:32:59 +080082);
夜雨声烦368e3562025-04-24 01:49:46 +080083-- 帖子标签关联表(保持不变)
wuchimedes03f93582025-04-21 15:32:59 +080084CREATE TABLE IF NOT EXISTS `post_tag` (
夜雨声烦368e3562025-04-24 01:49:46 +080085 `post_id` INT NOT NULL,
86 `tag_id` INT NOT NULL,
87 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
88 FOREIGN KEY (`tag_id`) REFERENCES `tags`(`tag_id`),
89 PRIMARY KEY (`post_id`, `tag_id`)
wuchimedes03f93582025-04-21 15:32:59 +080090);
夜雨声烦368e3562025-04-24 01:49:46 +080091-- 用户关注表(保持不变)
夜雨声烦7e6eb382025-04-22 01:18:00 +080092CREATE TABLE IF NOT EXISTS `user_follows` (
夜雨声烦368e3562025-04-24 01:49:46 +080093 `follower_id` INT NOT NULL,
94 `followed_id` INT NOT NULL,
95 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
96 FOREIGN KEY (`follower_id`) REFERENCES `users`(`user_id`),
97 FOREIGN KEY (`followed_id`) REFERENCES `users`(`user_id`),
98 PRIMARY KEY (`follower_id`, `followed_id`)
夜雨声烦7e6eb382025-04-22 01:18:00 +080099);
夜雨声烦368e3562025-04-24 01:49:46 +0800100-- 私信表(保持不变)
夜雨声烦7e6eb382025-04-22 01:18:00 +0800101CREATE TABLE IF NOT EXISTS `private_messages` (
夜雨声烦368e3562025-04-24 01:49:46 +0800102 `message_id` INT AUTO_INCREMENT PRIMARY KEY,
103 `sender_id` INT NOT NULL,
104 `receiver_id` INT NOT NULL,
105 `content` TEXT NOT NULL,
106 `sent_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
107 `is_read` BOOLEAN DEFAULT false,
108 FOREIGN KEY (`sender_id`) REFERENCES `users`(`user_id`),
109 FOREIGN KEY (`receiver_id`) REFERENCES `users`(`user_id`)
夜雨声烦4527a722025-04-23 17:04:25 +0800110);
夜雨声烦368e3562025-04-24 01:49:46 +0800111-- 评论表(保持不变)
夜雨声烦4527a722025-04-23 17:04:25 +0800112CREATE TABLE IF NOT EXISTS `comments` (
夜雨声烦368e3562025-04-24 01:49:46 +0800113 `comment_id` INT AUTO_INCREMENT PRIMARY KEY,
114 `post_id` INT NOT NULL,
115 `user_id` INT NOT NULL,
116 `parent_comment_id` INT DEFAULT NULL,
117 `content` TEXT NOT NULL,
118 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
119 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
120 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
121 FOREIGN KEY (`parent_comment_id`) REFERENCES `comments`(`comment_id`),
122 INDEX `idx_post_id` (`post_id`) -- 新增评论帖子索引
夜雨声烦4527a722025-04-23 17:04:25 +0800123);
夜雨声烦368e3562025-04-24 01:49:46 +0800124-- 帖子点赞表(保持不变)
125CREATE TABLE IF NOT EXISTS `post_likes` (
126 `user_id` INT NOT NULL,
127 `post_id` INT NOT NULL,
128 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
129 PRIMARY KEY (`user_id`, `post_id`),
130 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
131 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`)
夜雨声烦02063592025-04-23 18:10:00 +0800132);
夜雨声烦368e3562025-04-24 01:49:46 +0800133-- 帖子浏览记录表(新增复合索引)
134CREATE TABLE IF NOT EXISTS `post_views` (
135 `view_id` INT AUTO_INCREMENT PRIMARY KEY,
136 `user_id` INT NOT NULL,
137 `post_id` INT NOT NULL,
138 `view_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
139 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
140 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
141 INDEX `idx_user_view_time` (`user_id`, `view_time` DESC) -- 新增用户浏览时间索引
夜雨声烦f77d8132025-04-24 19:31:18 +0800142);
wuchimedes8a576e02025-05-13 17:50:46 +0800143CREATE TABLE IF NOT EXISTS user_tag_preference (
夜雨声烦f77d8132025-04-24 19:31:18 +0800144 user_id INT NOT NULL COMMENT '用户ID',
145 tag_id INT NOT NULL COMMENT '标签ID',
146 weight DOUBLE DEFAULT 1.0 COMMENT '偏好权重(浏览越多权重越高)',
147 last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新时间',
148 PRIMARY KEY (user_id, tag_id),
149 FOREIGN KEY (user_id) REFERENCES users(user_id),
150 FOREIGN KEY (tag_id) REFERENCES tags(tag_id)
223010718e412ad2025-04-24 22:24:51 +0800151);
夜雨声烦070c05a2025-05-13 20:33:50 +0800152CREATE TABLE IF NOT EXISTS `post_ratings` (
153 `user_id` INT NOT NULL COMMENT '用户ID',
154 `post_id` INT NOT NULL COMMENT '帖子ID',
155 `rating` TINYINT NOT NULL CHECK (`rating` BETWEEN 1 AND 5) COMMENT '评分值(1-5)',
156 `rated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '评分时间',
157 PRIMARY KEY (`user_id`, `post_id`), -- 确保每个用户对同一帖子只能评分一次
158 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
159 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
wuchimedes1ab4f5b2025-05-20 16:22:43 +0800160 INDEX idx_post_ratings_post_id (post_id)
夜雨声烦0a3df4a2025-05-13 21:26:13 +0800161);
162
163CREATE TABLE IF NOT EXISTS `reports` (
164 `report_id` INT AUTO_INCREMENT PRIMARY KEY COMMENT '举报ID',
165 `post_id` INT NOT NULL COMMENT '被举报的帖子ID',
166 `user_id` INT NOT NULL COMMENT '举报人ID',
167 `reason` TEXT NOT NULL COMMENT '举报原因',
223010719e7ca122025-06-03 23:26:44 +0800168 `status` ENUM('pending', 'resolved', 'rejected') DEFAULT 'pending' COMMENT '处理状态(待处理/已解决/已驳回)',
夜雨声烦0a3df4a2025-05-13 21:26:13 +0800169 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '举报时间',
170 `resolved_by` INT DEFAULT NULL COMMENT '处理人ID(管理员)',
171 `resolved_at` TIMESTAMP DEFAULT NULL COMMENT '处理时间',
172 `resolution_notes` TEXT DEFAULT NULL COMMENT '处理备注',
173 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
174 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
175 FOREIGN KEY (`resolved_by`) REFERENCES `users`(`user_id`)
夜雨声烦35c9da92025-05-20 00:12:48 +0800176);
177
178CREATE TABLE IF NOT EXISTS `user_signin` (
179 `signin_id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '签到记录ID',
wuchimedes1ab4f5b2025-05-20 16:22:43 +0800180 `user_id` INT NOT NULL COMMENT '用户ID',
夜雨声烦35c9da92025-05-20 00:12:48 +0800181 `signin_date` DATE NOT NULL COMMENT '签到日期',
182 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`) ON DELETE CASCADE,
183 UNIQUE KEY `unique_user_daily_signin` (`user_id`, `signin_date`) -- 唯一约束:用户每日只能签到一次
184) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;