CREATE TABLE workflow_reservation_invoice_generation (
    id BIGINT NOT NULL COMMENT 'Invoice 生成记录 ID',
    hotel_id VARCHAR(64) NOT NULL COMMENT '酒店 ID',
    source_type VARCHAR(32) NOT NULL COMMENT '生成来源类型：MANUAL、TASK、ORDER',
    order_id BIGINT NULL COMMENT '可选关联订单 ID',
    task_id BIGINT NULL COMMENT '可选关联任务 ID',
    source_message_id BIGINT NULL COMMENT '可选来源消息 ID',
    template_code VARCHAR(64) NOT NULL COMMENT '模板编码',
    template_version VARCHAR(32) NOT NULL COMMENT '模板版本',
    invoice_payload_json LONGTEXT NOT NULL COMMENT '归一化后的 Invoice 业务字段 JSON',
    calculated_totals_json LONGTEXT NULL COMMENT '后端计算金额摘要 JSON',
    generated_excel_object_key VARCHAR(512) NULL COMMENT '生成 Excel OSS 对象 Key',
    pdf_object_key VARCHAR(512) NULL COMMENT '生成 PDF OSS 对象 Key',
    pdf_url VARCHAR(1024) NULL COMMENT '生成 PDF 访问 URL',
    generation_status VARCHAR(32) NOT NULL COMMENT '生成状态',
    safe_error_code VARCHAR(128) NULL COMMENT '安全错误码',
    safe_error_summary VARCHAR(1024) NULL COMMENT '安全错误摘要',
    created_by VARCHAR(128) NOT NULL COMMENT '创建人用户标识',
    created_at DATETIME(6) NOT NULL COMMENT '记录创建 UTC 时间',
    updated_at DATETIME(6) NOT NULL COMMENT '记录更新 UTC 时间',
    PRIMARY KEY (id),
    KEY idx_reservation_invoice_hotel_created (hotel_id, created_at),
    KEY idx_reservation_invoice_hotel_source_created (hotel_id, source_type, created_at),
    KEY idx_reservation_invoice_hotel_order_created (hotel_id, order_id, created_at),
    KEY idx_reservation_invoice_hotel_task_created (hotel_id, task_id, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='Reservation Proforma Invoice 生成记录表';
