CREATE TABLE IF NOT EXISTS blind_box_config (
    id varchar(36) NOT NULL COMMENT '盲盒ID',
    tenant_id varchar(36) NOT NULL COMMENT '租户ID',
    name varchar(64) NOT NULL COMMENT '盲盒名称',
    cover_url varchar(255) DEFAULT NULL COMMENT '封面图',
    description text DEFAULT NULL COMMENT '盲盒描述',
    price decimal(10,2) NOT NULL COMMENT '盲盒售价',
    status tinyint NOT NULL DEFAULT 1 COMMENT '状态：1-上架；0-下架',
    created_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    created_by varchar(32) DEFAULT NULL COMMENT '创建人',
    updated_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    updated_by varchar(32) DEFAULT NULL COMMENT '更新人',
    deleted tinyint(1) NOT NULL DEFAULT 0 COMMENT '逻辑删除 1已删除 0未删除',
    version int NOT NULL DEFAULT 1 COMMENT '数据版本',
    PRIMARY KEY (id),
    KEY idx_box_tenant_status (tenant_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='盲盒配置';

CREATE TABLE IF NOT EXISTS blind_box_pool (
    id BIGINT NOT NULL AUTO_INCREMENT,
    tenant_id varchar(36) NOT NULL COMMENT '租户ID',
    blind_box_id varchar(36) NOT NULL COMMENT '盲盒ID',
    reward_gift_id varchar(36) NOT NULL COMMENT '中奖礼物ID',
    reward_price decimal(10,2) NOT NULL COMMENT '盲盒赠送价快照',
    weight int NOT NULL COMMENT '抽奖权重',
    remaining_stock int DEFAULT NULL COMMENT '剩余库存（NULL 表示不限量）',
    valid_from datetime DEFAULT NULL COMMENT '生效时间',
    valid_to datetime DEFAULT NULL COMMENT '失效时间',
    status tinyint NOT NULL DEFAULT 1 COMMENT '状态：1-启用；0-停用',
    created_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    created_by varchar(32) DEFAULT NULL COMMENT '创建人',
    updated_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    updated_by varchar(32) DEFAULT NULL COMMENT '更新人',
    deleted tinyint(1) NOT NULL DEFAULT 0 COMMENT '逻辑删除 1已删除 0未删除',
    version int NOT NULL DEFAULT 1 COMMENT '数据版本',
    PRIMARY KEY (id),
    KEY idx_pool_tenant_box (tenant_id, blind_box_id, status),
    KEY idx_pool_reward (tenant_id, reward_gift_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='盲盒奖池配置';

CREATE TABLE IF NOT EXISTS blind_box_reward (
    id varchar(36) NOT NULL COMMENT '奖励ID',
    tenant_id varchar(36) NOT NULL COMMENT '租户ID',
    customer_id varchar(36) NOT NULL COMMENT '顾客ID',
    blind_box_id varchar(36) NOT NULL COMMENT '盲盒ID',
    reward_gift_id varchar(36) NOT NULL COMMENT '中奖礼物ID',
    reward_price decimal(10,2) NOT NULL COMMENT '中奖礼物价格',
    box_price decimal(10,2) NOT NULL COMMENT '盲盒价格',
    subsidy_amount decimal(10,2) NOT NULL DEFAULT 0 COMMENT '补贴金额',
    reward_stock_snapshot int DEFAULT NULL COMMENT '抽奖时库存快照',
    seed varchar(128) NOT NULL COMMENT '随机种子',
    status varchar(16) NOT NULL DEFAULT 'UNUSED' COMMENT 'UNUSED / USED / REFUNDED',
    created_by_order varchar(36) NOT NULL COMMENT '来源订单ID',
    expires_at datetime NOT NULL COMMENT '到期时间',
    used_order_id varchar(36) DEFAULT NULL COMMENT '兑现订单ID',
    used_clerk_id varchar(36) DEFAULT NULL COMMENT '兑现店员ID',
    used_time datetime DEFAULT NULL COMMENT '兑现时间',
    created_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    created_by varchar(32) DEFAULT NULL COMMENT '创建人',
    updated_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    updated_by varchar(32) DEFAULT NULL COMMENT '更新人',
    deleted tinyint(1) NOT NULL DEFAULT 0 COMMENT '逻辑删除 1已删除 0未删除',
    version int NOT NULL DEFAULT 1 COMMENT '数据版本',
    PRIMARY KEY (id),
    KEY idx_reward_customer_status (customer_id, status),
    KEY idx_reward_tenant_blind (tenant_id, blind_box_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='盲盒奖励明细';

ALTER TABLE play_order_info
ADD COLUMN payment_source varchar(32) NOT NULL DEFAULT 'BALANCE' COMMENT '支付来源（余额、第三方、盲盒奖励等）' AFTER pay_method,
ADD COLUMN source_reward_id varchar(36) DEFAULT NULL COMMENT '盲盒奖励引用ID' AFTER payment_source;

UPDATE play_order_info SET payment_source = 'BALANCE' WHERE payment_source IS NULL;
