blob: 051ce5e976f727cae1f63c645fc5c84b27c7ebce [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,
11 INDEX `idx_user_level` (`user_level`) -- 按等级查询优化
wuchimedes079c1632025-04-02 22:01:20 +080012);
wuchimedesa26ed3f2025-04-25 14:41:09 +080013-- 用户统计表
wuchimedes22ee83c2025-04-25 00:17:47 +080014CREATE TABLE IF NOT EXISTS `user_stats` (
15 user_id INT PRIMARY KEY,
16 passkey VARCHAR(255) NOT NULL UNIQUE,
17 total_upload FLOAT NOT NULL DEFAULT 0,
18 total_download FLOAT NOT NULL DEFAULT 0,
19 last_update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
20 FOREIGN KEY (user_id) REFERENCES users(user_id),
21 FOREIGN KEY (passkey) REFERENCES users(passkey)
22);
wuchimedesa26ed3f2025-04-25 14:41:09 +080023-- 种子表
wuchimedes079c1632025-04-02 22:01:20 +080024CREATE TABLE IF NOT EXISTS `torrents` (
夜雨声烦368e3562025-04-24 01:49:46 +080025 `torrent_id` INT AUTO_INCREMENT PRIMARY KEY,
26 `user_id` INT NOT NULL,
27 `torrent_name` VARCHAR(255) NOT NULL,
wuchimedes8a576e02025-05-13 17:50:46 +080028 `file_path` VARCHAR(255) NOT NULL,
夜雨声烦368e3562025-04-24 01:49:46 +080029 `info_hash` BINARY(20) NOT NULL,
30 `file_size` FLOAT NOT NULL,
31 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`)
wuchimedes079c1632025-04-02 22:01:20 +080032);
夜雨声烦368e3562025-04-24 01:49:46 +080033-- Peer表(保持不变)
wuchimedes079c1632025-04-02 22:01:20 +080034CREATE TABLE IF NOT EXISTS `peers` (
夜雨声烦368e3562025-04-24 01:49:46 +080035 `passkey` VARCHAR(255) NOT NULL,
36 `info_hash` BINARY(20) NOT NULL,
37 `peer_id` VARCHAR(20) NOT NULL,
38 `ip_address` VARCHAR(128) NOT NULL,
39 `port` INT NOT NULL,
40 `uploaded` FLOAT NOT NULL,
41 `downloaded` FLOAT NOT NULL,
42 `last_seen` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
43 FOREIGN KEY (`passkey`) REFERENCES `users`(`passkey`),
44 PRIMARY KEY (`passkey`, `info_hash`, `peer_id`)
wuchimedes079c1632025-04-02 22:01:20 +080045);
夜雨声烦368e3562025-04-24 01:49:46 +080046-- 帖子表(新增 hot_score 和 last_calculated 字段)
wuchimedese5722e32025-04-13 17:38:50 +080047CREATE TABLE IF NOT EXISTS `posts` (
夜雨声烦368e3562025-04-24 01:49:46 +080048 `post_id` INT AUTO_INCREMENT PRIMARY KEY,
49 `user_id` INT NOT NULL,
50 `hot_score` DOUBLE DEFAULT 5.0 COMMENT '热度评分',
51 `view_count` INT DEFAULT 0 COMMENT '浏览数',
52 `post_title` VARCHAR(255) NOT NULL,
53 `post_content` TEXT NOT NULL,
wuchimedes8a576e02025-05-13 17:50:46 +080054 `torrent_id` INT DEFAULT NULL,
夜雨声烦368e3562025-04-24 01:49:46 +080055 `post_type` ENUM('resource', 'discussion') NOT NULL,
56 `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
57 `last_calculated` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后热度计算时间',
夜雨声烦070c05a2025-05-13 20:33:50 +080058 `average_rating` DECIMAL(3,2) DEFAULT 0.00 COMMENT '帖子平均评分',
59 `rating_count` INT DEFAULT 0 COMMENT '总评分人数';
夜雨声烦368e3562025-04-24 01:49:46 +080060 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
wuchimedes8a576e02025-05-13 17:50:46 +080061 FOREIGN KEY (`torrent_id`) REFERENCES `torrents`(`torrent_id`),
夜雨声烦368e3562025-04-24 01:49:46 +080062 INDEX `idx_hot_score` (`hot_score`), -- 新增热度索引
63 INDEX `idx_post_type` (`post_type`) -- 新增类型索引
wuchimedese5722e32025-04-13 17:38:50 +080064);
夜雨声烦368e3562025-04-24 01:49:46 +080065-- 标签表(保持不变)
wuchimedes03f93582025-04-21 15:32:59 +080066CREATE TABLE IF NOT EXISTS `tags`(
wuchimedes8a576e02025-05-13 17:50:46 +080067 `tag_id` INT PRIMARY KEY,
68 `tag_name` VARCHAR(255) NOT NULL,
夜雨声烦368e3562025-04-24 01:49:46 +080069 `parent_id` INT DEFAULT NULL,
70 FOREIGN KEY (`parent_id`) REFERENCES `tags`(`tag_id`)
wuchimedes03f93582025-04-21 15:32:59 +080071);
夜雨声烦368e3562025-04-24 01:49:46 +080072-- 帖子标签关联表(保持不变)
wuchimedes03f93582025-04-21 15:32:59 +080073CREATE TABLE IF NOT EXISTS `post_tag` (
夜雨声烦368e3562025-04-24 01:49:46 +080074 `post_id` INT NOT NULL,
75 `tag_id` INT NOT NULL,
76 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
77 FOREIGN KEY (`tag_id`) REFERENCES `tags`(`tag_id`),
78 PRIMARY KEY (`post_id`, `tag_id`)
wuchimedes03f93582025-04-21 15:32:59 +080079);
夜雨声烦368e3562025-04-24 01:49:46 +080080-- 用户关注表(保持不变)
夜雨声烦7e6eb382025-04-22 01:18:00 +080081CREATE TABLE IF NOT EXISTS `user_follows` (
夜雨声烦368e3562025-04-24 01:49:46 +080082 `follower_id` INT NOT NULL,
83 `followed_id` INT NOT NULL,
84 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
85 FOREIGN KEY (`follower_id`) REFERENCES `users`(`user_id`),
86 FOREIGN KEY (`followed_id`) REFERENCES `users`(`user_id`),
87 PRIMARY KEY (`follower_id`, `followed_id`)
夜雨声烦7e6eb382025-04-22 01:18:00 +080088);
夜雨声烦368e3562025-04-24 01:49:46 +080089-- 私信表(保持不变)
夜雨声烦7e6eb382025-04-22 01:18:00 +080090CREATE TABLE IF NOT EXISTS `private_messages` (
夜雨声烦368e3562025-04-24 01:49:46 +080091 `message_id` INT AUTO_INCREMENT PRIMARY KEY,
92 `sender_id` INT NOT NULL,
93 `receiver_id` INT NOT NULL,
94 `content` TEXT NOT NULL,
95 `sent_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
96 `is_read` BOOLEAN DEFAULT false,
97 FOREIGN KEY (`sender_id`) REFERENCES `users`(`user_id`),
98 FOREIGN KEY (`receiver_id`) REFERENCES `users`(`user_id`)
夜雨声烦4527a722025-04-23 17:04:25 +080099);
夜雨声烦368e3562025-04-24 01:49:46 +0800100-- 评论表(保持不变)
夜雨声烦4527a722025-04-23 17:04:25 +0800101CREATE TABLE IF NOT EXISTS `comments` (
夜雨声烦368e3562025-04-24 01:49:46 +0800102 `comment_id` INT AUTO_INCREMENT PRIMARY KEY,
103 `post_id` INT NOT NULL,
104 `user_id` INT NOT NULL,
105 `parent_comment_id` INT DEFAULT NULL,
106 `content` TEXT NOT NULL,
107 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
108 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
109 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
110 FOREIGN KEY (`parent_comment_id`) REFERENCES `comments`(`comment_id`),
111 INDEX `idx_post_id` (`post_id`) -- 新增评论帖子索引
夜雨声烦4527a722025-04-23 17:04:25 +0800112);
夜雨声烦368e3562025-04-24 01:49:46 +0800113-- 帖子点赞表(保持不变)
114CREATE TABLE IF NOT EXISTS `post_likes` (
115 `user_id` INT NOT NULL,
116 `post_id` INT NOT NULL,
117 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
118 PRIMARY KEY (`user_id`, `post_id`),
119 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
120 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`)
夜雨声烦02063592025-04-23 18:10:00 +0800121);
夜雨声烦368e3562025-04-24 01:49:46 +0800122-- 帖子浏览记录表(新增复合索引)
123CREATE TABLE IF NOT EXISTS `post_views` (
124 `view_id` INT AUTO_INCREMENT PRIMARY KEY,
125 `user_id` INT NOT NULL,
126 `post_id` INT NOT NULL,
127 `view_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
128 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
129 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
130 INDEX `idx_user_view_time` (`user_id`, `view_time` DESC) -- 新增用户浏览时间索引
夜雨声烦f77d8132025-04-24 19:31:18 +0800131);
wuchimedes8a576e02025-05-13 17:50:46 +0800132CREATE TABLE IF NOT EXISTS user_tag_preference (
夜雨声烦f77d8132025-04-24 19:31:18 +0800133 user_id INT NOT NULL COMMENT '用户ID',
134 tag_id INT NOT NULL COMMENT '标签ID',
135 weight DOUBLE DEFAULT 1.0 COMMENT '偏好权重(浏览越多权重越高)',
136 last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新时间',
137 PRIMARY KEY (user_id, tag_id),
138 FOREIGN KEY (user_id) REFERENCES users(user_id),
139 FOREIGN KEY (tag_id) REFERENCES tags(tag_id)
223010718e412ad2025-04-24 22:24:51 +0800140);
夜雨声烦070c05a2025-05-13 20:33:50 +0800141CREATE TABLE IF NOT EXISTS `post_ratings` (
142 `user_id` INT NOT NULL COMMENT '用户ID',
143 `post_id` INT NOT NULL COMMENT '帖子ID',
144 `rating` TINYINT NOT NULL CHECK (`rating` BETWEEN 1 AND 5) COMMENT '评分值(1-5)',
145 `rated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '评分时间',
146 PRIMARY KEY (`user_id`, `post_id`), -- 确保每个用户对同一帖子只能评分一次
147 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
148 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
149 INDEX idx_post_ratings_post_id ON post_ratings (post_id)
夜雨声烦0a3df4a2025-05-13 21:26:13 +0800150);
151
152CREATE TABLE IF NOT EXISTS `reports` (
153 `report_id` INT AUTO_INCREMENT PRIMARY KEY COMMENT '举报ID',
154 `post_id` INT NOT NULL COMMENT '被举报的帖子ID',
155 `user_id` INT NOT NULL COMMENT '举报人ID',
156 `reason` TEXT NOT NULL COMMENT '举报原因',
157 `status` ENUM('pending', 'resolved', 'rejected') DEFAULT 'pending' COMMENT '处理状态(待处理/已解决/已驳回)',
158 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '举报时间',
159 `resolved_by` INT DEFAULT NULL COMMENT '处理人ID(管理员)',
160 `resolved_at` TIMESTAMP DEFAULT NULL COMMENT '处理时间',
161 `resolution_notes` TEXT DEFAULT NULL COMMENT '处理备注',
162 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
163 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
164 FOREIGN KEY (`resolved_by`) REFERENCES `users`(`user_id`)
夜雨声烦35c9da92025-05-20 00:12:48 +0800165);
166
167CREATE TABLE IF NOT EXISTS `user_signin` (
168 `signin_id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '签到记录ID',
169 `user_id` BIGINT NOT NULL COMMENT '用户ID',
170 `signin_date` DATE NOT NULL COMMENT '签到日期',
171 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`) ON DELETE CASCADE,
172 UNIQUE KEY `unique_user_daily_signin` (`user_id`, `signin_date`) -- 唯一约束:用户每日只能签到一次
173) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;