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