-- =============================================
-- 新闻文本分类系统数据库设计
-- Database: news_classifier
-- =============================================

CREATE DATABASE IF NOT EXISTS `news_classifier`
DEFAULT CHARACTER SET utf8mb4
DEFAULT COLLATE utf8mb4_unicode_ci;

USE `news_classifier`;

-- =============================================
-- 用户表
-- =============================================
DROP TABLE IF EXISTS `sys_user`;
CREATE TABLE `sys_user`
(
    `id`              BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY COMMENT '用户ID',
    `username`        VARCHAR(50) NOT NULL COMMENT '用户名',
    `password`        VARCHAR(255) NOT NULL COMMENT '密码(BCrypt加密)',
    `email`           VARCHAR(100) DEFAULT NULL COMMENT '邮箱',
    `phone`           VARCHAR(20)  DEFAULT NULL COMMENT '手机号',
    `avatar`          VARCHAR(255) DEFAULT NULL COMMENT '头像URL',
    `role`            VARCHAR(20) NOT NULL DEFAULT 'USER' COMMENT '角色: ADMIN, USER',
    `status`          TINYINT NOT NULL DEFAULT 1 COMMENT '状态: 0-禁用, 1-启用',
    `last_login_time` DATETIME    DEFAULT NULL COMMENT '最后登录时间',
    `last_login_ip`   VARCHAR(50)  DEFAULT NULL COMMENT '最后登录IP',
    `deleted`         TINYINT NOT NULL DEFAULT 0 COMMENT '逻辑删除: 0-未删除, 1-已删除',
    `create_time`     DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_time`     DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    UNIQUE KEY `uk_username` (`username`),
    UNIQUE KEY `uk_email` (`email`),
    KEY `idx_status` (`status`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_unicode_ci COMMENT ='用户表';

-- =============================================
-- 新闻分类表
-- =============================================
DROP TABLE IF EXISTS `news_category`;
CREATE TABLE `news_category`
(
    `id`          BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY COMMENT '分类ID',
    `name`        VARCHAR(50) NOT NULL COMMENT '分类名称',
    `code`        VARCHAR(20) NOT NULL COMMENT '分类代码',
    `description` VARCHAR(200) DEFAULT NULL COMMENT '分类描述',
    `icon`        VARCHAR(100) DEFAULT NULL COMMENT '图标',
    `sort_order`  INT NOT NULL DEFAULT 0 COMMENT '排序',
    `status`      TINYINT NOT NULL DEFAULT 1 COMMENT '状态: 0-禁用, 1-启用',
    `deleted`     TINYINT NOT NULL DEFAULT 0 COMMENT '逻辑删除',
    `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    UNIQUE KEY `uk_code` (`code`),
    KEY `idx_status` (`status`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_unicode_ci COMMENT ='新闻分类表';

-- 初始化分类数据
INSERT INTO `news_category` (`name`, `code`, `description`, `sort_order`)
VALUES ('时政', 'POLITICS', '政治时政类新闻', 1),
       ('财经', 'FINANCE', '财经金融类新闻', 2),
       ('科技', 'TECHNOLOGY', '科学技术类新闻', 3),
       ('体育', 'SPORTS', '体育竞技类新闻', 4),
       ('娱乐', 'ENTERTAINMENT', '娱乐休闲类新闻', 5),
       ('健康', 'HEALTH', '健康养生类新闻', 6),
       ('教育', 'EDUCATION', '教育类新闻', 7),
       ('生活', 'LIFE', '生活类新闻', 8),
       ('国际', 'INTERNATIONAL', '国际新闻', 9),
       ('军事', 'MILITARY', '军事类新闻', 10);

-- =============================================
-- 新闻表
-- =============================================
DROP TABLE IF EXISTS `news`;
CREATE TABLE `news`
(
    `id`              BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY COMMENT '新闻ID',
    `title`           VARCHAR(200) NOT NULL COMMENT '新闻标题',
    `content`         MEDIUMTEXT NOT NULL COMMENT '新闻内容',
    `summary`         TEXT DEFAULT NULL COMMENT '新闻摘要',
    `source`          VARCHAR(100) DEFAULT NULL COMMENT '新闻来源',
    `source_url`      VARCHAR(500) DEFAULT NULL COMMENT '原文链接',
    `author`          VARCHAR(50)  DEFAULT NULL COMMENT '作者',
    `category_id`     BIGINT UNSIGNED NOT NULL COMMENT '分类ID',
    `category_code`   VARCHAR(20) NOT NULL COMMENT '分类代码',
    `cover_image`     VARCHAR(500) DEFAULT NULL COMMENT '封面图片',
    `publish_time`    DATETIME    DEFAULT NULL COMMENT '发布时间',
    `view_count`      BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '浏览次数',
    `like_count`      INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '点赞数',
    `comment_count`   INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '评论数',
    `collect_count`   INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '收藏数',
    `is_hot`          TINYINT NOT NULL DEFAULT 0 COMMENT '是否热门: 0-否, 1-是',
    `is_top`          TINYINT NOT NULL DEFAULT 0 COMMENT '是否置顶: 0-否, 1-是',
    `status`          TINYINT NOT NULL DEFAULT 1 COMMENT '状态: 0-草稿, 1-已发布, 2-已下架',
    `classifier_type` VARCHAR(20) DEFAULT NULL COMMENT '分类算法: ML, DL, HYBRID, MANUAL',
    `confidence`      DECIMAL(5, 4) DEFAULT NULL COMMENT '分类置信度',
    `deleted`         TINYINT NOT NULL DEFAULT 0 COMMENT '逻辑删除',
    `create_time`     DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_time`     DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    KEY `idx_category` (`category_id`, `category_code`),
    KEY `idx_publish_time` (`publish_time`),
    KEY `idx_status` (`status`),
    KEY `idx_is_hot` (`is_hot`),
    KEY `idx_is_top` (`is_top`),
    FULLTEXT KEY `ft_title_content` (`title`, `content`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_unicode_ci COMMENT ='新闻表';

-- =============================================
-- 标签表
-- =============================================
DROP TABLE IF EXISTS `news_tag`;
CREATE TABLE `news_tag`
(
    `id`          BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY COMMENT '标签ID',
    `name`        VARCHAR(50) NOT NULL COMMENT '标签名称',
    `color`       VARCHAR(20) DEFAULT NULL COMMENT '标签颜色',
    `use_count`   INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '使用次数',
    `deleted`     TINYINT NOT NULL DEFAULT 0 COMMENT '逻辑删除',
    `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    UNIQUE KEY `uk_name` (`name`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_unicode_ci COMMENT ='标签表';

-- =============================================
-- 新闻标签关联表
-- =============================================
DROP TABLE IF EXISTS `news_tag_relation`;
CREATE TABLE `news_tag_relation`
(
    `id`         BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY COMMENT '关联ID',
    `news_id`    BIGINT UNSIGNED NOT NULL COMMENT '新闻ID',
    `tag_id`     BIGINT UNSIGNED NOT NULL COMMENT '标签ID',
    `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    UNIQUE KEY `uk_news_tag` (`news_id`, `tag_id`),
    KEY `idx_news_id` (`news_id`),
    KEY `idx_tag_id` (`tag_id`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_unicode_ci COMMENT ='新闻标签关联表';

-- =============================================
-- 用户收藏表
-- =============================================
DROP TABLE IF EXISTS `user_collect`;
CREATE TABLE `user_collect`
(
    `id`         BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY COMMENT '收藏ID',
    `user_id`    BIGINT UNSIGNED NOT NULL COMMENT '用户ID',
    `news_id`    BIGINT UNSIGNED NOT NULL COMMENT '新闻ID',
    `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '收藏时间',
    UNIQUE KEY `uk_user_news` (`user_id`, `news_id`),
    KEY `idx_user_id` (`user_id`),
    KEY `idx_news_id` (`news_id`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_unicode_ci COMMENT ='用户收藏表';

-- =============================================
-- 用户浏览历史表
-- =============================================
DROP TABLE IF EXISTS `user_history`;
CREATE TABLE `user_history`
(
    `id`         BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY COMMENT '历史ID',
    `user_id`    BIGINT UNSIGNED NOT NULL COMMENT '用户ID',
    `news_id`    BIGINT UNSIGNED NOT NULL COMMENT '新闻ID',
    `view_time`  DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '浏览时间',
    `duration`   INT UNSIGNED DEFAULT NULL COMMENT '停留时长(秒)',
    KEY `idx_user_id` (`user_id`),
    KEY `idx_view_time` (`view_time`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_unicode_ci COMMENT ='用户浏览历史表';

-- =============================================
-- 操作日志表
-- =============================================
DROP TABLE IF EXISTS `sys_log`;
CREATE TABLE `sys_log`
(
    `id`          BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY COMMENT '日志ID',
    `user_id`     BIGINT UNSIGNED DEFAULT NULL COMMENT '用户ID',
    `username`    VARCHAR(50) DEFAULT NULL COMMENT '用户名',
    `module`      VARCHAR(50) DEFAULT NULL COMMENT '模块',
    `operation`   VARCHAR(100) NOT NULL COMMENT '操作',
    `method`      VARCHAR(200) DEFAULT NULL COMMENT '方法',
    `params`      TEXT DEFAULT NULL COMMENT '参数',
    `ip`          VARCHAR(50) DEFAULT NULL COMMENT 'IP地址',
    `status`      TINYINT NOT NULL DEFAULT 1 COMMENT '状态: 0-失败, 1-成功',
    `error_msg`   VARCHAR(500) DEFAULT NULL COMMENT '错误信息',
    `execute_time` INT UNSIGNED DEFAULT NULL COMMENT '执行时长(ms)',
    `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    KEY `idx_user_id` (`user_id`),
    KEY `idx_create_time` (`create_time`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_unicode_ci COMMENT ='操作日志表';

-- =============================================
-- 系统配置表
-- =============================================
DROP TABLE IF EXISTS `sys_config`;
CREATE TABLE `sys_config`
(
    `id`          BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY COMMENT '配置ID',
    `config_key`  VARCHAR(100) NOT NULL COMMENT '配置键',
    `config_value` TEXT DEFAULT NULL COMMENT '配置值',
    `description` VARCHAR(200) DEFAULT NULL COMMENT '描述',
    `group`       VARCHAR(50) DEFAULT NULL COMMENT '分组',
    `deleted`     TINYINT NOT NULL DEFAULT 0 COMMENT '逻辑删除',
    `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    UNIQUE KEY `uk_config_key` (`config_key`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_unicode_ci COMMENT ='系统配置表';

-- =============================================
-- 初始化管理员用户 (密码: admin123)
-- =============================================
INSERT INTO `sys_user` (`username`, `password`, `email`, `role`)
VALUES ('admin', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iKTVKIUi', 'admin@newsclassifier.com', 'ADMIN');

-- =============================================
-- 初始化系统配置
-- =============================================
INSERT INTO `sys_config` (`config_key`, `config_value`, `description`, `group`)
VALUES ('crawler.enabled', 'true', '是否启用新闻爬取', 'crawler'),
       ('crawler.interval', '30', '爬取间隔(分钟)', 'crawler'),
       ('classifier.mode', 'hybrid', '分类模式: traditional, deep_learning, hybrid', 'classifier'),
       ('classifier.confidence', '0.75', '置信度阈值', 'classifier'),
       ('system.title', '新闻分类系统', '系统标题', 'system');
