blob: 8a2387c57d572f081d4289111f9984666f006316 [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,
36 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`)
wuchimedes079c1632025-04-02 22:01:20 +080037);
夜雨声烦368e3562025-04-24 01:49:46 +080038-- Peer表(保持不变)
wuchimedes079c1632025-04-02 22:01:20 +080039CREATE TABLE IF NOT EXISTS `peers` (
夜雨声烦368e3562025-04-24 01:49:46 +080040 `passkey` VARCHAR(255) NOT NULL,
41 `info_hash` BINARY(20) NOT NULL,
42 `peer_id` VARCHAR(20) NOT NULL,
43 `ip_address` VARCHAR(128) NOT NULL,
44 `port` INT NOT NULL,
45 `uploaded` FLOAT NOT NULL,
46 `downloaded` FLOAT NOT NULL,
47 `last_seen` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
48 FOREIGN KEY (`passkey`) REFERENCES `users`(`passkey`),
49 PRIMARY KEY (`passkey`, `info_hash`, `peer_id`)
wuchimedes079c1632025-04-02 22:01:20 +080050);
夜雨声烦368e3562025-04-24 01:49:46 +080051-- 帖子表(新增 hot_score 和 last_calculated 字段)
wuchimedese5722e32025-04-13 17:38:50 +080052CREATE TABLE IF NOT EXISTS `posts` (
夜雨声烦368e3562025-04-24 01:49:46 +080053 `post_id` INT AUTO_INCREMENT PRIMARY KEY,
54 `user_id` INT NOT NULL,
55 `hot_score` DOUBLE DEFAULT 5.0 COMMENT '热度评分',
56 `view_count` INT DEFAULT 0 COMMENT '浏览数',
57 `post_title` VARCHAR(255) NOT NULL,
58 `post_content` TEXT NOT NULL,
wuchimedes8a576e02025-05-13 17:50:46 +080059 `torrent_id` INT DEFAULT NULL,
夜雨声烦368e3562025-04-24 01:49:46 +080060 `post_type` ENUM('resource', 'discussion') NOT NULL,
61 `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
62 `last_calculated` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后热度计算时间',
夜雨声烦070c05a2025-05-13 20:33:50 +080063 `average_rating` DECIMAL(3,2) DEFAULT 0.00 COMMENT '帖子平均评分',
wuchimedes1ab4f5b2025-05-20 16:22:43 +080064 `rating_count` INT DEFAULT 0 COMMENT '总评分人数',
夜雨声烦7affa472025-05-20 19:27:16 +080065 `is_locked` BOOLEAN DEFAULT FALSE COMMENT '是否被锁定',
66 `locked_reason` VARCHAR(255) COMMENT '锁定原因',
67 `locked_at` DATETIME COMMENT '锁定时间',
68 `locked_by` BIGINT COMMENT '操作管理员ID',
夜雨声烦368e3562025-04-24 01:49:46 +080069 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
wuchimedes8a576e02025-05-13 17:50:46 +080070 FOREIGN KEY (`torrent_id`) REFERENCES `torrents`(`torrent_id`),
夜雨声烦368e3562025-04-24 01:49:46 +080071 INDEX `idx_hot_score` (`hot_score`), -- 新增热度索引
72 INDEX `idx_post_type` (`post_type`) -- 新增类型索引
wuchimedese5722e32025-04-13 17:38:50 +080073);
夜雨声烦368e3562025-04-24 01:49:46 +080074-- 标签表(保持不变)
wuchimedes03f93582025-04-21 15:32:59 +080075CREATE TABLE IF NOT EXISTS `tags`(
wuchimedes8a576e02025-05-13 17:50:46 +080076 `tag_id` INT PRIMARY KEY,
77 `tag_name` VARCHAR(255) NOT NULL,
夜雨声烦368e3562025-04-24 01:49:46 +080078 `parent_id` INT DEFAULT NULL,
79 FOREIGN KEY (`parent_id`) REFERENCES `tags`(`tag_id`)
wuchimedes03f93582025-04-21 15:32:59 +080080);
夜雨声烦368e3562025-04-24 01:49:46 +080081-- 帖子标签关联表(保持不变)
wuchimedes03f93582025-04-21 15:32:59 +080082CREATE TABLE IF NOT EXISTS `post_tag` (
夜雨声烦368e3562025-04-24 01:49:46 +080083 `post_id` INT NOT NULL,
84 `tag_id` INT NOT NULL,
85 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
86 FOREIGN KEY (`tag_id`) REFERENCES `tags`(`tag_id`),
87 PRIMARY KEY (`post_id`, `tag_id`)
wuchimedes03f93582025-04-21 15:32:59 +080088);
夜雨声烦368e3562025-04-24 01:49:46 +080089-- 用户关注表(保持不变)
夜雨声烦7e6eb382025-04-22 01:18:00 +080090CREATE TABLE IF NOT EXISTS `user_follows` (
夜雨声烦368e3562025-04-24 01:49:46 +080091 `follower_id` INT NOT NULL,
92 `followed_id` INT NOT NULL,
93 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
94 FOREIGN KEY (`follower_id`) REFERENCES `users`(`user_id`),
95 FOREIGN KEY (`followed_id`) REFERENCES `users`(`user_id`),
96 PRIMARY KEY (`follower_id`, `followed_id`)
夜雨声烦7e6eb382025-04-22 01:18:00 +080097);
夜雨声烦368e3562025-04-24 01:49:46 +080098-- 私信表(保持不变)
夜雨声烦7e6eb382025-04-22 01:18:00 +080099CREATE TABLE IF NOT EXISTS `private_messages` (
夜雨声烦368e3562025-04-24 01:49:46 +0800100 `message_id` INT AUTO_INCREMENT PRIMARY KEY,
101 `sender_id` INT NOT NULL,
102 `receiver_id` INT NOT NULL,
103 `content` TEXT NOT NULL,
104 `sent_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
105 `is_read` BOOLEAN DEFAULT false,
106 FOREIGN KEY (`sender_id`) REFERENCES `users`(`user_id`),
107 FOREIGN KEY (`receiver_id`) REFERENCES `users`(`user_id`)
夜雨声烦4527a722025-04-23 17:04:25 +0800108);
夜雨声烦368e3562025-04-24 01:49:46 +0800109-- 评论表(保持不变)
夜雨声烦4527a722025-04-23 17:04:25 +0800110CREATE TABLE IF NOT EXISTS `comments` (
夜雨声烦368e3562025-04-24 01:49:46 +0800111 `comment_id` INT AUTO_INCREMENT PRIMARY KEY,
112 `post_id` INT NOT NULL,
113 `user_id` INT NOT NULL,
114 `parent_comment_id` INT DEFAULT NULL,
115 `content` TEXT NOT NULL,
116 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
117 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
118 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
119 FOREIGN KEY (`parent_comment_id`) REFERENCES `comments`(`comment_id`),
120 INDEX `idx_post_id` (`post_id`) -- 新增评论帖子索引
夜雨声烦4527a722025-04-23 17:04:25 +0800121);
夜雨声烦368e3562025-04-24 01:49:46 +0800122-- 帖子点赞表(保持不变)
123CREATE TABLE IF NOT EXISTS `post_likes` (
124 `user_id` INT NOT NULL,
125 `post_id` INT NOT NULL,
126 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
127 PRIMARY KEY (`user_id`, `post_id`),
128 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
129 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`)
夜雨声烦02063592025-04-23 18:10:00 +0800130);
夜雨声烦368e3562025-04-24 01:49:46 +0800131-- 帖子浏览记录表(新增复合索引)
132CREATE TABLE IF NOT EXISTS `post_views` (
133 `view_id` INT AUTO_INCREMENT PRIMARY KEY,
134 `user_id` INT NOT NULL,
135 `post_id` INT NOT NULL,
136 `view_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
137 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
138 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
139 INDEX `idx_user_view_time` (`user_id`, `view_time` DESC) -- 新增用户浏览时间索引
夜雨声烦f77d8132025-04-24 19:31:18 +0800140);
wuchimedes8a576e02025-05-13 17:50:46 +0800141CREATE TABLE IF NOT EXISTS user_tag_preference (
夜雨声烦f77d8132025-04-24 19:31:18 +0800142 user_id INT NOT NULL COMMENT '用户ID',
143 tag_id INT NOT NULL COMMENT '标签ID',
144 weight DOUBLE DEFAULT 1.0 COMMENT '偏好权重(浏览越多权重越高)',
145 last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新时间',
146 PRIMARY KEY (user_id, tag_id),
147 FOREIGN KEY (user_id) REFERENCES users(user_id),
148 FOREIGN KEY (tag_id) REFERENCES tags(tag_id)
223010718e412ad2025-04-24 22:24:51 +0800149);
夜雨声烦070c05a2025-05-13 20:33:50 +0800150CREATE TABLE IF NOT EXISTS `post_ratings` (
151 `user_id` INT NOT NULL COMMENT '用户ID',
152 `post_id` INT NOT NULL COMMENT '帖子ID',
153 `rating` TINYINT NOT NULL CHECK (`rating` BETWEEN 1 AND 5) COMMENT '评分值(1-5)',
154 `rated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '评分时间',
155 PRIMARY KEY (`user_id`, `post_id`), -- 确保每个用户对同一帖子只能评分一次
156 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
157 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
wuchimedes1ab4f5b2025-05-20 16:22:43 +0800158 INDEX idx_post_ratings_post_id (post_id)
夜雨声烦0a3df4a2025-05-13 21:26:13 +0800159);
160
161CREATE TABLE IF NOT EXISTS `reports` (
162 `report_id` INT AUTO_INCREMENT PRIMARY KEY COMMENT '举报ID',
163 `post_id` INT NOT NULL COMMENT '被举报的帖子ID',
164 `user_id` INT NOT NULL COMMENT '举报人ID',
165 `reason` TEXT NOT NULL COMMENT '举报原因',
166 `status` ENUM('pending', 'resolved', 'rejected') DEFAULT 'pending' COMMENT '处理状态(待处理/已解决/已驳回)',
167 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '举报时间',
168 `resolved_by` INT DEFAULT NULL COMMENT '处理人ID(管理员)',
169 `resolved_at` TIMESTAMP DEFAULT NULL COMMENT '处理时间',
170 `resolution_notes` TEXT DEFAULT NULL COMMENT '处理备注',
171 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
172 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
173 FOREIGN KEY (`resolved_by`) REFERENCES `users`(`user_id`)
夜雨声烦35c9da92025-05-20 00:12:48 +0800174);
175
176CREATE TABLE IF NOT EXISTS `user_signin` (
177 `signin_id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '签到记录ID',
wuchimedes1ab4f5b2025-05-20 16:22:43 +0800178 `user_id` INT NOT NULL COMMENT '用户ID',
夜雨声烦35c9da92025-05-20 00:12:48 +0800179 `signin_date` DATE NOT NULL COMMENT '签到日期',
180 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`) ON DELETE CASCADE,
181 UNIQUE KEY `unique_user_daily_signin` (`user_id`, `signin_date`) -- 唯一约束:用户每日只能签到一次
182) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;