-- 用户表
CREATE TABLE IF NOT EXISTS users (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    open_id VARCHAR(64) NOT NULL UNIQUE COMMENT '微信OpenID',
    union_id VARCHAR(64) DEFAULT '' COMMENT '微信UnionID',
    nickname VARCHAR(64) DEFAULT '' COMMENT '昵称',
    avatar VARCHAR(255) DEFAULT '' COMMENT '头像',
    gender TINYINT DEFAULT 0 COMMENT '性别 0:未知 1:男 2:女',
    phone VARCHAR(20) DEFAULT '' COMMENT '手机号',
    email VARCHAR(100) DEFAULT '' COMMENT '邮箱',
    status TINYINT DEFAULT 1 COMMENT '状态 1:正常 0:禁用',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_open_id (open_id),
    INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';

-- 用户资料表
CREATE TABLE IF NOT EXISTS user_profiles (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    user_id BIGINT UNSIGNED NOT NULL COMMENT '用户ID',
    real_name VARCHAR(32) DEFAULT '' COMMENT '真实姓名',
    birthday VARCHAR(10) DEFAULT '' COMMENT '生日 YYYY-MM-DD',
    birth_time VARCHAR(8) DEFAULT '' COMMENT '出生时间 HH:mm:ss',
    gender TINYINT DEFAULT 0 COMMENT '性别',
    zodiac VARCHAR(10) DEFAULT '' COMMENT '生肖',
    constellation VARCHAR(20) DEFAULT '' COMMENT '星座',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_user_id (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户资料表';

-- 订单表
CREATE TABLE IF NOT EXISTS orders (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    order_no VARCHAR(32) NOT NULL UNIQUE COMMENT '订单号',
    user_id BIGINT UNSIGNED NOT NULL COMMENT '用户ID',
    type VARCHAR(20) NOT NULL COMMENT '订单类型 wish:许愿 vip:会员',
    product_id BIGINT UNSIGNED DEFAULT 0 COMMENT '商品ID',
    product_name VARCHAR(100) DEFAULT '' COMMENT '商品名称',
    amount INT NOT NULL DEFAULT 0 COMMENT '金额（分）',
    status VARCHAR(20) DEFAULT 'pending' COMMENT '状态 pending:待支付 paid:已支付 cancelled:已取消 refunded:已退款',
    pay_time TIMESTAMP NULL COMMENT '支付时间',
    expire_time TIMESTAMP NULL COMMENT '过期时间',
    remark VARCHAR(255) DEFAULT '' COMMENT '备注',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_order_no (order_no),
    INDEX idx_user_id (user_id),
    INDEX idx_status (status),
    INDEX idx_type (type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单表';

-- 许愿树表
CREATE TABLE IF NOT EXISTS wish_trees (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL COMMENT '名称',
    description VARCHAR(255) DEFAULT '' COMMENT '描述',
    type VARCHAR(20) DEFAULT 'pine' COMMENT '树类型 pine:松树 sakura:樱花 bamboo:竹子',
    max_wishes INT DEFAULT 100 COMMENT '最大许愿条数',
    sort INT DEFAULT 0 COMMENT '排序',
    status TINYINT DEFAULT 1 COMMENT '状态',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='许愿树表';

-- 许愿表
CREATE TABLE IF NOT EXISTS wishes (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    user_id BIGINT UNSIGNED NOT NULL COMMENT '用户ID',
    tree_id BIGINT UNSIGNED NOT NULL COMMENT '许愿树ID',
    content VARCHAR(500) NOT NULL COMMENT '许愿内容',
    type VARCHAR(20) DEFAULT 'free' COMMENT '类型 free:免费 paid:付费',
    position INT DEFAULT 0 COMMENT '位置',
    status TINYINT DEFAULT 1 COMMENT '状态 1:正常 0:隐藏/删除',
    is_robot TINYINT DEFAULT 0 COMMENT '是否机器人发布',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_user_id (user_id),
    INDEX idx_tree_id (tree_id),
    INDEX idx_status (status),
    INDEX idx_type (type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='许愿表';

-- 许愿商品表
CREATE TABLE IF NOT EXISTS wish_products (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL COMMENT '商品名称',
    description VARCHAR(255) DEFAULT '' COMMENT '描述',
    price INT NOT NULL DEFAULT 0 COMMENT '价格（分）',
    duration INT DEFAULT 7 COMMENT '展示时长（天）',
    position INT DEFAULT 0 COMMENT '优先位置',
    status TINYINT DEFAULT 1 COMMENT '状态',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='许愿商品表';

-- 插入默认许愿树
INSERT IGNORE INTO wish_trees (id, name, description, type, max_wishes, sort) VALUES
(1, '祈福树', '许下美好愿望，祈福平安顺遂', 'pine', 100, 0),
(2, '姻缘树', '祈求姻缘美满，爱情甜蜜', 'sakura', 50, 1),
(3, '事业树', '祈愿事业顺利，步步高升', 'bamboo', 80, 2);

-- 插入默认许愿商品
INSERT IGNORE INTO wish_products (id, name, description, price, duration, position) VALUES
(1, '普通许愿条', '基础许愿条，展示7天', 100, 7, 0),
(2, '精品许愿条', '精品许愿条，展示30天，优先位置', 500, 30, 10),
(3, '至尊许愿条', '至尊许愿条，展示90天，置顶显示', 2000, 90, 100);
