-- ============================================
-- 许愿树快速建表脚本
-- 执行此脚本创建许愿树所需的所有表
-- ============================================

-- 1. 节日表
CREATE TABLE IF NOT EXISTS `eb_wishtree_festival` (
  `id` int NOT NULL AUTO_INCREMENT COMMENT '节日ID',
  `name` varchar(50) NOT NULL COMMENT '节日名称',
  `icon` varchar(255) DEFAULT '' COMMENT '节日图标',
  `start_date` varchar(20) DEFAULT '' COMMENT '开始日期',
  `end_date` varchar(20) DEFAULT '' COMMENT '结束日期',
  `is_lunar` tinyint DEFAULT 0 COMMENT '是否农历',
  `theme_color` varchar(20) DEFAULT '#FF6B6B' COMMENT '主题色',
  `sort` int DEFAULT 0 COMMENT '排序',
  `status` tinyint DEFAULT 1 COMMENT '状态 0禁用 1启用',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='许愿树节日表';

-- 2. 心愿表
CREATE TABLE IF NOT EXISTS `eb_wishtree_wish` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '心愿ID',
  `uid` int NOT NULL COMMENT '用户ID',
  `festival_id` int DEFAULT 0 COMMENT '节日ID',
  `content` varchar(500) NOT NULL COMMENT '心愿内容',
  `background_id` int DEFAULT 0 COMMENT '背景ID',
  `status` tinyint DEFAULT 1 COMMENT '状态 0待审核 1通过 2拒绝',
  `audit_type` tinyint DEFAULT 0 COMMENT '审核方式 0自动 1人工',
  `audit_remark` varchar(255) DEFAULT '' COMMENT '审核备注',
  `like_count` int DEFAULT 0 COMMENT '点赞数',
  `comment_count` int DEFAULT 0 COMMENT '评论数',
  `is_delete` tinyint DEFAULT 0 COMMENT '是否删除',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_uid` (`uid`),
  KEY `idx_festival` (`festival_id`),
  KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='许愿树心愿表';

-- 3. 点赞表
CREATE TABLE IF NOT EXISTS `eb_wishtree_like` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `uid` int NOT NULL COMMENT '用户ID',
  `wish_id` bigint NOT NULL COMMENT '心愿ID',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_uid_wish` (`uid`, `wish_id`),
  KEY `idx_wish` (`wish_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='许愿树点赞表';

-- 4. 评论表
CREATE TABLE IF NOT EXISTS `eb_wishtree_comment` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `wish_id` bigint NOT NULL COMMENT '心愿ID',
  `uid` int NOT NULL COMMENT '用户ID',
  `content` varchar(255) NOT NULL COMMENT '评论内容',
  `parent_id` bigint DEFAULT 0 COMMENT '父评论ID',
  `status` tinyint DEFAULT 1 COMMENT '状态',
  `is_delete` tinyint DEFAULT 0 COMMENT '是否删除',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_wish` (`wish_id`),
  KEY `idx_uid` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='许愿树评论表';

-- 5. 背景素材表
CREATE TABLE IF NOT EXISTS `eb_wishtree_background` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL COMMENT '背景名称',
  `image` varchar(255) NOT NULL COMMENT '背景图片',
  `festival_id` int DEFAULT 0 COMMENT '关联节日ID',
  `sort` int DEFAULT 0 COMMENT '排序',
  `status` tinyint DEFAULT 1 COMMENT '状态',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='许愿树背景素材表';

-- ============================================
-- 初始化节日数据
-- ============================================
INSERT IGNORE INTO `eb_wishtree_festival` (`id`, `name`, `icon`, `start_date`, `end_date`, `is_lunar`, `theme_color`, `sort`, `status`) VALUES
(1, '元旦', '🎉', '12-31', '01-03', 0, '#FF6B6B', 100, 1),
(2, '春节', '🧧', '除夕', '正月十五', 1, '#E74C3C', 99, 1),
(3, '情人节', '💕', '02-13', '02-15', 0, '#FF69B4', 98, 1),
(4, '生日祝福', '🎂', '全年', '全年', 0, '#FF69B4', 50, 1),
(5, '日常祝福', '✨', '全年', '全年', 0, '#9B59B6', 49, 1);

-- ============================================
-- 验证表是否创建成功
-- ============================================
SELECT 'eb_wishtree_festival' AS table_name, COUNT(*) AS count FROM eb_wishtree_festival;
SELECT 'eb_wishtree_wish' AS table_name, COUNT(*) AS count FROM eb_wishtree_wish;
SELECT 'eb_wishtree_like' AS table_name, COUNT(*) AS count FROM eb_wishtree_like;
SELECT 'eb_wishtree_comment' AS table_name, COUNT(*) AS count FROM eb_wishtree_comment;
SELECT 'eb_wishtree_background' AS table_name, COUNT(*) AS count FROM eb_wishtree_background;
