-- =============================================
-- 知识库模块数据库表结构
-- 数据库: MySQL 8.0+
-- 字符集: utf8mb4
-- 创建时间: 2026-01-28
-- =============================================

-- 1. 评论表
CREATE TABLE IF NOT EXISTS `comment` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '评论ID',
    `knowledge_id` BIGINT NOT NULL COMMENT '知识ID',
    `user_id` BIGINT NOT NULL COMMENT '用户ID',
    `parent_id` BIGINT DEFAULT 0 COMMENT '父评论ID',
    `content` TEXT NOT NULL COMMENT '评论内容',
    `like_count` INT DEFAULT 0 COMMENT '点赞数',
    `status` TINYINT DEFAULT 1 COMMENT '状态: 0-删除, 1-正常',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    PRIMARY KEY (`id`),
    KEY `idx_knowledge_id` (`knowledge_id`),
    KEY `idx_user_id` (`user_id`),
    KEY `idx_parent_id` (`parent_id`),
    KEY `idx_knowledge_status` (`knowledge_id`, `status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='评论表';

-- 2. 知识文档表
CREATE TABLE IF NOT EXISTS `knowledge` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '知识ID',
    `title` VARCHAR(255) NOT NULL COMMENT '标题',
    `subtitle` VARCHAR(500) COMMENT '副标题',
    `content` LONGTEXT NOT NULL COMMENT '内容(HTML格式)',
    `plain_content` LONGTEXT COMMENT '纯文本内容(用于搜索)',
    `summary` TEXT COMMENT '摘要',
    `category_id` BIGINT NOT NULL COMMENT '分类ID',
    `author_id` BIGINT NOT NULL COMMENT '作者ID',
    `cover_image` VARCHAR(255) COMMENT '封面图',
    `view_count` INT DEFAULT 0 COMMENT '浏览量',
    `like_count` INT DEFAULT 0 COMMENT '点赞数',
    `collect_count` INT DEFAULT 0 COMMENT '收藏数',
    `version` INT DEFAULT 1 COMMENT '版本号',
    `status` ENUM('DRAFT', 'PUBLISHED', 'REVIEWING', 'ARCHIVED') DEFAULT 'DRAFT' COMMENT '状态',
    `publish_time` DATETIME COMMENT '发布时间',
    `expire_time` DATETIME COMMENT '过期时间',
    `is_top` TINYINT DEFAULT 0 COMMENT '是否置顶',
    `tags` VARCHAR(500) COMMENT '标签(逗号分隔)',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    PRIMARY KEY (`id`),
    KEY `idx_category_id` (`category_id`),
    KEY `idx_author_id` (`author_id`),
    KEY `idx_status` (`status`),
    KEY `idx_publish_time` (`publish_time`),
    KEY `idx_title` (`title`(191)),
    KEY `idx_status_publish_time` (`status`, `publish_time`),
    KEY `idx_category_status` (`category_id`, `status`),
    FULLTEXT KEY `ft_content` (`plain_content`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='知识文档表';

-- 3. 用户点赞记录表（可选，用于防止重复点赞）
CREATE TABLE IF NOT EXISTS `knowledge_like_record` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '记录ID',
    `user_id` BIGINT NOT NULL COMMENT '用户ID',
    `knowledge_id` BIGINT NOT NULL COMMENT '知识ID',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '点赞时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_user_knowledge` (`user_id`, `knowledge_id`),
    KEY `idx_knowledge_id` (`knowledge_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='知识点赞记录表';

-- 4. 评论点赞记录表（可选，用于防止重复点赞）
CREATE TABLE IF NOT EXISTS `comment_like_record` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '记录ID',
    `user_id` BIGINT NOT NULL COMMENT '用户ID',
    `comment_id` BIGINT NOT NULL COMMENT '评论ID',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '点赞时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_user_comment` (`user_id`, `comment_id`),
    KEY `idx_comment_id` (`comment_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='评论点赞记录表';

-- 5. 知识分类表（可选）
CREATE TABLE IF NOT EXISTS `knowledge_category` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '分类ID',
    `parent_id` BIGINT DEFAULT 0 COMMENT '父分类ID',
    `name` VARCHAR(100) NOT NULL COMMENT '分类名称',
    `description` VARCHAR(500) COMMENT '分类描述',
    `sort` INT DEFAULT 0 COMMENT '排序',
    `status` TINYINT DEFAULT 1 COMMENT '状态: 0-禁用, 1-启用',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    PRIMARY KEY (`id`),
    KEY `idx_parent_id` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='知识分类表';

-- 插入测试数据（可选）
-- 知识分类测试数据
INSERT INTO `knowledge_category` (`id`, `parent_id`, `name`, `description`, `sort`, `status`) VALUES
(1, 0, '技术文档', '技术相关文档', 1, 1),
(2, 0, '产品手册', '产品使用手册', 2, 1),
(3, 0, '常见问题', '常见问题解答', 3, 1),
(4, 1, 'Java开发', 'Java开发相关', 1, 1),
(5, 1, '前端开发', '前端开发相关', 2, 1);

-- 知识文档测试数据（需要先有用户数据）
-- INSERT INTO `knowledge` (`title`, `subtitle`, `content`, `plain_content`, `summary`, `category_id`, `author_id`, `status`) VALUES
-- ('Spring Boot入门教程', '快速上手Spring Boot', '<h1>Spring Boot入门</h1><p>这是一篇Spring Boot入门教程...</p>', 'Spring Boot入门 这是一篇Spring Boot入门教程...', '本文介绍Spring Boot的基本使用', 4, 1, 'PUBLISHED');

-- 查询语句示例
-- 查询已发布的知识文档
-- SELECT * FROM knowledge WHERE status = 'PUBLISHED' ORDER BY publish_time DESC LIMIT 10;

-- 查询某个知识的所有评论
-- SELECT c.*, u.username FROM comment c LEFT JOIN sys_user u ON c.user_id = u.user_id WHERE c.knowledge_id = 1 AND c.status = 1 ORDER BY c.create_time DESC;

-- 查询热门知识（按点赞数排序）
-- SELECT * FROM knowledge WHERE status = 'PUBLISHED' ORDER BY like_count DESC LIMIT 10;

-- 查询最新知识
-- SELECT * FROM knowledge WHERE status = 'PUBLISHED' ORDER BY publish_time DESC LIMIT 10;
