USE luojia_channel; -- 指定目标数据库

## 用户表
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
                        `id` BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID',
                        `username` VARCHAR(50) NOT NULL COMMENT '用户名',
                        `real_name` VARCHAR(50) COMMENT '实名',
                        `password` VARCHAR(100) NOT NULL COMMENT '密码',
                        `phone` VARCHAR(20) UNIQUE COMMENT '注册手机号',
                        `email` VARCHAR(100) UNIQUE COMMENT '邮箱',
                        `student_id` VARCHAR(20) UNIQUE COMMENT '学号',
                        `avatar` VARCHAR(512) COMMENT '头像URL',
                        `gender` TINYINT DEFAULT 0 COMMENT '性别（0未知，1男，2女）',
                        `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                        `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                        `status` INT DEFAULT 1 COMMENT '状态（1正常 2冻结）',
                        `role` INT DEFAULT 1 COMMENT '身份（1普通用户 2管理员 3超级管理员）',
                        `integral` INT DEFAULT 0 COMMENT '用户积分',
                        `college` VARCHAR(100) COMMENT '所属学院',
                        INDEX `idx_username` (`username`),
                        UNIQUE INDEX `uk_phone` (`phone`),
                        UNIQUE INDEX `uk_email` (`email`),
                        UNIQUE INDEX `uk_student_id` (`student_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';

## 分类表
DROP TABLE IF EXISTS `category`;
CREATE TABLE `category` (
                            `id` BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID',
                            `name` VARCHAR(50) NOT NULL UNIQUE COMMENT '分类名称',
                            `description` VARCHAR(255) COMMENT '分类描述',
                            `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                            `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='分类表';

## 文件表
DROP TABLE IF EXISTS `lj_file`;
CREATE TABLE `lj_file` (
                           `id` BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID',
                           `file_name` VARCHAR(255) NOT NULL COMMENT '文件名',
                           `file_url` VARCHAR(512) NOT NULL COMMENT 'MinIO存储路径',
                           `file_size` BIGINT NOT NULL COMMENT '文件大小（字节）',
                           `file_md5` VARCHAR(32) NOT NULL COMMENT '文件MD5值',
                           `file_type` VARCHAR(50) NOT NULL COMMENT '文件类型（image/video）',
                           `file_status` INT NOT NULL DEFAULT 0 COMMENT '文件状态（0:正在上传, 1:上传成功, 2:失败或删除, 3:审核中）',
                           `user_id` BIGINT NOT NULL COMMENT '上传用户ID',
                           `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                           `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                           INDEX idx_user_id (user_id),
                           INDEX idx_file_status (file_status),
                           INDEX idx_file_md5 (file_md5)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='文件存储表';

## 图文帖子表
DROP TABLE IF EXISTS `post`;
CREATE TABLE `post` (
                        `id` BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID',
                        `title` VARCHAR(255) NOT NULL COMMENT '标题',
                        `image` VARCHAR(512) NOT NULL COMMENT '图片url',
                        `content` TEXT NOT NULL COMMENT '文字内容',
                        `status` TINYINT NOT NULL DEFAULT 0 COMMENT '是否匿名，0否1是',
                        `like_count` INT DEFAULT 0 COMMENT '点赞数',
                        `comment_count` INT DEFAULT 0 COMMENT '评论数',
                        `favorite_count` INT DEFAULT 0 COMMENT '收藏数',
                        `view_count` INT DEFAULT 0 COMMENT '浏览数',
                        `user_id` BIGINT NOT NULL COMMENT '发布用户ID',
                        `category_id` BIGINT NOT NULL COMMENT '分类ID',
                        `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                        `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                        INDEX idx_user_id (user_id),
                        INDEX idx_category_id (category_id),
                        INDEX idx_create_time (create_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='图文帖子表';

## 视频表
DROP TABLE IF EXISTS `video`;
CREATE TABLE `video` (
                         `id` BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID',
                         `title` VARCHAR(255) NOT NULL COMMENT '标题',
                         `cover` VARCHAR(512) NOT NULL COMMENT '封面图片url',
                         `video_file` VARCHAR(512) NOT NULL COMMENT '视频文件url',
                         `play_count` BIGINT DEFAULT 0 COMMENT '播放次数',
                         `like_count` INT DEFAULT 0 COMMENT '点赞数',
                         `comment_count` INT DEFAULT 0 COMMENT '评论数',
                         `favorite_count` INT DEFAULT 0 COMMENT '收藏数',
                         `view_count` INT DEFAULT 0 COMMENT '浏览数',
                         `user_id` BIGINT NOT NULL COMMENT '发布用户ID',
                         `duration` INT NOT NULL COMMENT '视频时长（秒）',
                         `category_id` BIGINT NOT NULL COMMENT '分类ID',
                         `tags` VARCHAR(255) COMMENT '标签（逗号分隔）',
                         `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                         `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                         INDEX idx_user_id (user_id),
                         INDEX idx_category_id (category_id),
                         INDEX idx_duration (duration),
                         INDEX idx_create_time (create_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='视频表';

## 评论表
DROP TABLE IF EXISTS `comment`;
CREATE TABLE `comment` (
                           `id` BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID',
                           `content` TEXT NOT NULL COMMENT '评论内容',
                           `like_count` INT DEFAULT 0 COMMENT '点赞数',
                           `reply_count` INT DEFAULT 0 COMMENT '回复数',
                           `user_id` BIGINT NOT NULL COMMENT '评论用户ID',
                           `post_id` BIGINT NOT NULL COMMENT '关联的帖子ID',
                           `parent_comment_id` BIGINT DEFAULT 0 COMMENT '父评论ID',
                           `reply_user_id` BIGINT DEFAULT 0 COMMENT '回复用户ID',
                           `top_id` BIGINT COMMENT '顶层评论ID',
                           `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                           `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                           INDEX idx_post_id (post_id),
                           INDEX idx_user_id (user_id),
                           INDEX idx_create_time (create_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='评论表';

## 点赞记录表
DROP TABLE IF EXISTS `like_record`;
CREATE TABLE `like_record` (
                               `id` BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID',
                               `user_id` BIGINT NOT NULL COMMENT '用户ID',
                               `post_type` VARCHAR(20) NOT NULL COMMENT '帖子类型（post/video）',
                               `post_id` BIGINT NOT NULL COMMENT '关联的帖子ID',
                               `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                               INDEX idx_user_id (user_id),
                               INDEX idx_post_type (post_type),
                               INDEX idx_post_id (post_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='点赞记录表';

## 收藏记录表
DROP TABLE IF EXISTS `favorite_record`;
CREATE TABLE `favorite_record` (
                                   `id` BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID',
                                   `user_id` BIGINT NOT NULL COMMENT '用户ID',
                                   `post_type` VARCHAR(20) NOT NULL COMMENT '帖子类型（post/video）',
                                   `post_id` BIGINT NOT NULL COMMENT '关联的帖子ID',
                                   `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                   INDEX idx_user_id (user_id),
                                   INDEX idx_post_type (post_type),
                                   INDEX idx_post_id (post_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='收藏记录表';

## 浏览记录表
DROP TABLE IF EXISTS `view_record`;
CREATE TABLE `view_record` (
                               `id` BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID',
                               `user_id` BIGINT NOT NULL COMMENT '用户ID',
                               `post_type` VARCHAR(20) NOT NULL COMMENT '帖子类型（post/video）',
                               `post_id` BIGINT NOT NULL COMMENT '关联的帖子ID',
                               `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                               INDEX idx_user_id (user_id),
                               INDEX idx_post_type (post_type),
                               INDEX idx_post_id (post_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='浏览记录表';

## 私信消息表
DROP TABLE IF EXISTS `message`;
CREATE TABLE message (
                         id BIGINT AUTO_INCREMENT PRIMARY KEY,
                         message_type TINYINT NOT NULL COMMENT '0系统消息，1私信消息，2评论通知',
                         content TEXT NOT NULL,
                         sender_id BIGINT NOT NULL,
                         receiver_id BIGINT NOT NULL,
                         create_time DATETIME NOT NULL,
                         INDEX idx_sender_id (sender_id),
                         INDEX idx_receiver_id (receiver_id),
                         INDEX idx_create_time (create_time)
);

## 关注表
DROP TABLE IF EXISTS `follow`;
CREATE TABLE `follow`  (
                              `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
                              `user_id` bigint(20) UNSIGNED NOT NULL COMMENT '用户id',
                              `follow_user_id` bigint(20) UNSIGNED NOT NULL COMMENT '关联的用户id',
                              `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                              PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='关注表';

## 反馈工单表
DROP TABLE IF EXISTS `feedback_ticket`;
CREATE TABLE `feedback_ticket` (
                                   `id` BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID',
                                   `user_id` BIGINT NOT NULL COMMENT '用户ID',
                                   `content` TEXT NOT NULL COMMENT '反馈内容',
                                   `feedback_type` VARCHAR(50) NOT NULL COMMENT '反馈类型，例如：功能建议、问题反馈等',
                                   `status` INT NOT NULL COMMENT '工单状态，1:待处理，2:处理中，3:已处理，4超时',
                                   `contact_info` VARCHAR(100) COMMENT '联系方式，如手机号、邮箱等',
                                   `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                   `handle_time` DATETIME COMMENT '处理时间',
                                   INDEX idx_user_id (`user_id`),
                                   INDEX idx_feedback_type (`feedback_type`),
                                   INDEX idx_status (`status`),
                                   INDEX idx_create_time (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='反馈工单表';
