-- 创建知识库分类表
CREATE TABLE `yz_knowledge_category` (
  `category_id` INT NOT NULL AUTO_INCREMENT COMMENT '分类ID',
  `category_name` VARCHAR(100) NOT NULL COMMENT '分类名称',
  `category_desc` VARCHAR(500) DEFAULT NULL COMMENT '分类描述',
  `parent_id` INT DEFAULT 0 COMMENT '父分类ID，0表示顶级分类',
  `sort_order` INT DEFAULT 0 COMMENT '排序序号',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`category_id`),
  KEY `idx_parent_id` (`parent_id`),
  KEY `idx_sort_order` (`sort_order`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='知识库分类表';

-- 创建知识库标签表
CREATE TABLE `yz_knowledge_tags` (
  `tag_id` INT NOT NULL AUTO_INCREMENT COMMENT '标签ID',
  `tag_name` VARCHAR(50) NOT NULL COMMENT '标签名称',
  `tag_color` VARCHAR(20) DEFAULT NULL COMMENT '标签颜色',
  `usage_count` INT DEFAULT 0 COMMENT '使用次数',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`tag_id`),
  UNIQUE KEY `uk_tag_name` (`tag_name`),
  KEY `idx_usage_count` (`usage_count`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='知识库标签表';

-- 创建知识库内容表
CREATE TABLE `yz_knowledge` (
  `knowledge_id` INT NOT NULL AUTO_INCREMENT COMMENT '知识ID',
  `title` VARCHAR(200) NOT NULL COMMENT '标题',
  `category_id` INT DEFAULT NULL COMMENT '分类ID',
  `tags` TEXT COMMENT '标签JSON数组，存储标签名称',
  `author` VARCHAR(50) NOT NULL COMMENT '作者',
  `content` LONGTEXT COMMENT '正文内容（富文本）',
  `summary` VARCHAR(500) DEFAULT NULL COMMENT '摘要',
  `cover_url` VARCHAR(500) DEFAULT NULL COMMENT '封面图片URL',
  `status` TINYINT DEFAULT 0 COMMENT '状态：0-草稿，1-已发布，2-已归档',
  `view_count` INT DEFAULT 0 COMMENT '查看次数',
  `like_count` INT DEFAULT 0 COMMENT '点赞数',
  `is_recommend` TINYINT DEFAULT 0 COMMENT '是否推荐：0-否，1-是',
  `is_top` TINYINT DEFAULT 0 COMMENT '是否置顶：0-否，1-是',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `create_by` VARCHAR(50) DEFAULT NULL COMMENT '创建人',
  `update_by` VARCHAR(50) DEFAULT NULL COMMENT '更新人',
  PRIMARY KEY (`knowledge_id`),
  KEY `idx_category_id` (`category_id`),
  KEY `idx_author` (`author`),
  KEY `idx_status` (`status`),
  KEY `idx_create_time` (`create_time`),
  KEY `idx_view_count` (`view_count`),
  KEY `idx_is_recommend` (`is_recommend`),
  KEY `idx_is_top` (`is_top`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='知识库内容表';

-- 创建知识库收藏表
CREATE TABLE `yz_knowledge_favorites` (
  `favorite_id` INT NOT NULL AUTO_INCREMENT COMMENT '收藏ID',
  `knowledge_id` INT NOT NULL COMMENT '知识ID',
  `user_id` INT NOT NULL COMMENT '用户ID',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`favorite_id`),
  UNIQUE KEY `uk_knowledge_user` (`knowledge_id`, `user_id`),
  KEY `idx_user_id` (`user_id`),
  CONSTRAINT `yz_fk_fav_knowledge` FOREIGN KEY (`knowledge_id`) REFERENCES `yz_knowledge` (`knowledge_id`) ON DELETE CASCADE,
  CONSTRAINT `yz_fk_fav_user` FOREIGN KEY (`user_id`) REFERENCES `yz_users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='知识库收藏表';

-- 插入默认分类
INSERT INTO `yz_knowledge_category` (`category_name`, `category_desc`, `parent_id`, `sort_order`) VALUES
('技术文档', '技术相关的文档资料', 0, 1),
('产品手册', '产品使用手册和说明', 0, 2),
('用户指南', '用户操作指南和教程', 0, 3),
('常见问题', '常见问题解答', 0, 4),
('API文档', 'API接口文档', 0, 5),
('Vue', 'Vue框架相关', 1, 101),
('React', 'React框架相关', 1, 102),
('Go', 'Go语言相关', 1, 103);

-- 插入默认标签
INSERT INTO `yz_knowledge_tags` (`tag_name`, `tag_color`, `usage_count`) VALUES
('Vue', '#4CAF50', 0),
('React', '#42A5F5', 0),
('TypeScript', '#3178C6', 0),
('Element Plus', '#409EFF', 0),
('Vue Router', '#4FC08D', 0),
('Pinia', '#FFD54F', 0),
('Go', '#00ADD8', 0),
('Python', '#3776AB', 0);

--插入默认知识
INSERT INTO `yz_knowledge` (`title`, `category_id`, `tags`, `author`, `content`, `summary`, `cover_url`, `status`, `view_count`, `like_count`, `is_recommend`, `is_top`, `create_time`, `update_time`, `create_by`, `update_by`) VALUES
('Vue', 1, 'Vue', 'admin', 'Vue is a progressive framework for building user interfaces.', 'Vue is a progressive framework for building user interfaces.', 'https://vuejs.org/images/logo.png', 1, 0, 0, 0, 0, '2021-01-01 00:00:00', '2021-01-01 00:00:00', 'admin', 'admin');