-- 文章系统数据库迁移脚本
-- 创建时间: 2024-01-01
-- 描述: 创建文章相关的数据库表结构

-- 文章分类表
CREATE TABLE IF NOT EXISTS article_categories (
    id VARCHAR(36) PRIMARY KEY COMMENT '分类唯一标识',
    name VARCHAR(100) NOT NULL COMMENT '分类名称',
    description TEXT COMMENT '分类描述',
    sort_order INT DEFAULT 0 COMMENT '排序',
    active BOOLEAN DEFAULT TRUE COMMENT '是否启用',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    deleted_at TIMESTAMP NULL COMMENT '软删除时间',
    INDEX idx_article_categories_active (active),
    INDEX idx_article_categories_sort_order (sort_order),
    INDEX idx_article_categories_deleted_at (deleted_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='文章分类表';

-- 文章标签表
CREATE TABLE IF NOT EXISTS article_tags (
    id VARCHAR(36) PRIMARY KEY COMMENT '标签唯一标识',
    name VARCHAR(50) NOT NULL COMMENT '标签名称',
    color VARCHAR(20) DEFAULT '#1890ff' COMMENT '标签颜色',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    deleted_at TIMESTAMP NULL COMMENT '软删除时间',
    UNIQUE KEY uk_article_tags_name (name),
    INDEX idx_article_tags_deleted_at (deleted_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='文章标签表';

-- 文章表
CREATE TABLE IF NOT EXISTS articles (
    id VARCHAR(36) PRIMARY KEY COMMENT '文章唯一标识',
    title VARCHAR(200) NOT NULL COMMENT '文章标题',
    content TEXT NOT NULL COMMENT '文章内容',
    summary VARCHAR(500) COMMENT '文章摘要',
    cover_image VARCHAR(500) COMMENT '封面图片',
    category_id VARCHAR(36) COMMENT '分类ID',
    status ENUM('draft', 'published', 'archived') DEFAULT 'draft' COMMENT '文章状态',
    is_featured BOOLEAN DEFAULT FALSE COMMENT '是否推荐',
    published_at TIMESTAMP NULL COMMENT '发布时间',
    view_count INT DEFAULT 0 COMMENT '阅读量',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    deleted_at TIMESTAMP NULL COMMENT '软删除时间',
    INDEX idx_articles_category_id (category_id),
    INDEX idx_articles_status (status),
    INDEX idx_articles_is_featured (is_featured),
    INDEX idx_articles_published_at (published_at),
    INDEX idx_articles_created_at (created_at),
    INDEX idx_articles_deleted_at (deleted_at),
    FOREIGN KEY (category_id) REFERENCES article_categories(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='文章表';

-- 文章标签关联表
CREATE TABLE IF NOT EXISTS article_tag_relations (
    id VARCHAR(36) PRIMARY KEY COMMENT '关联记录唯一标识',
    article_id VARCHAR(36) NOT NULL COMMENT '文章ID',
    tag_id VARCHAR(36) NOT NULL COMMENT '标签ID',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    UNIQUE KEY uk_article_tag_relation (article_id, tag_id),
    INDEX idx_article_tag_relations_article_id (article_id),
    INDEX idx_article_tag_relations_tag_id (tag_id),
    FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE,
    FOREIGN KEY (tag_id) REFERENCES article_tags(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='文章标签关联表';



-- 插入默认分类数据
INSERT INTO article_categories (id, name, description, sort_order, active) VALUES
('cat-001', '技术分享', '技术相关的文章分享', 1, true),
('cat-002', '产品介绍', '产品功能介绍和使用指南', 2, true),
('cat-003', '公司动态', '公司最新动态和新闻', 3, true),
('cat-004', '行业资讯', '行业相关的最新资讯', 4, true),
('cat-005', '用户指南', '用户使用指南和帮助文档', 5, true)
ON DUPLICATE KEY UPDATE
name = VALUES(name),
description = VALUES(description),
sort_order = VALUES(sort_order),
active = VALUES(active);

-- 插入默认标签数据
INSERT INTO article_tags (id, name, color) VALUES
('tag-001', '技术', '#1890ff'),
('tag-002', '产品', '#52c41a'),
('tag-003', '新闻', '#faad14'),
('tag-004', '教程', '#722ed1'),
('tag-005', '更新', '#13c2c2'),
('tag-006', 'API', '#eb2f96'),
('tag-007', '文档', '#fa8c16'),
('tag-008', '公告', '#f5222d')
ON DUPLICATE KEY UPDATE
name = VALUES(name),
color = VALUES(color);
