-- ============================================
-- 支付宝多服务商架构 — DDL + 初始数据
-- 执行前请确认所在数据库为 payment_platform_prod
-- ============================================

-- 1. 创建服务商表
CREATE TABLE IF NOT EXISTS pay_service_provider (
    id                BIGSERIAL PRIMARY KEY,
    name              VARCHAR(64)  NOT NULL,
    scope_label       VARCHAR(64)  DEFAULT 'DOMESTIC',
    app_id            VARCHAR(64)  NOT NULL,
    app_private_key   TEXT         NOT NULL,
    alipay_public_key TEXT         NOT NULL,
    server_url        VARCHAR(256) DEFAULT 'https://openapi.alipay.com/gateway.do',
    sign_type         VARCHAR(16)  DEFAULT 'RSA2',
    format            VARCHAR(16)  DEFAULT 'JSON',
    charset           VARCHAR(16)  DEFAULT 'UTF-8',
    provider_status   VARCHAR(32)  DEFAULT 'ACTIVE',
    description       VARCHAR(512),
    status            VARCHAR(1)   DEFAULT '0',    -- MyBatis-Plus @TableLogic 软删除
    uuid              VARCHAR(64),
    created_time      TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_time      TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 2. 插入当前服务商（密钥来自旧 application.yml）
INSERT INTO pay_service_provider (name, scope_label, app_id, app_private_key, alipay_public_key, server_url, sign_type, format, charset, provider_status, status)
VALUES (
    '国内服务商',
    'DOMESTIC',
    '2021005122654283',
    'MIIEogIBAAKCAQEAnhU7Ix3+klh7k+5+Vx10tmqS2MkWD3MIkB+GpwZm94SccEISoLjCatW56U1QJo7D9SP1rSwyJ3dtvbiEpje8f0D+iaHilX9tkdWZMnsI9XbQqmqLkjDPFdmII3sa4p9Mx1UUOtIGmPhKYmnR8jM85SEmf9ivuFOKZISny5LxDYam2o3czGKjRiLgb7oNs4LKf9isc1k4r/XeCEAhLPejFgilSiKWC8DQbiSG15DvOKrr0BxM1gb0YUngQr11diUSYx5LRXWjhUE7WkPaSGkLDVuZ+ZAHddH9DddPrfjoQpiq7ROJNTkxVuNo5Bqont5fvLnzIN9/9pR8bJVLNBDFlQIDAQABAoIBADb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ShZhHTeSBN32ZGUFYsOjLzM8g0Xui2w2NAQBrmeHCGNXrV+vFRX6+s5cccUtohtpjYK07mz4RbeScWL/oebNubDR1XsdiUKe+JQsHT5MMksKk/kmBbIfIJ3wDdeJLlk=',
    'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqUX9WqZPwh57HqR2RiAEYe8GWrXl8Zz9YC7dz7dnfGY1k/ma8/w18TC7txausbCCHCEWl52836+gdD1uQumCxPYPtkoWcZy8984kCE3whaV9O2PoaCd6Q3/Ww0WpRAvJDGDpa032t3vuTPSUbbU0N8iYfPua9a8z1JjbD4hDiIVMN797PllUDQEuIfNV+C06usGZxL01e/zBdMiIun1HhrVqHQ+p+GjHQXZ58kqP1EIGOg4/1HPib5i4umXULnVsCYZ0dvvdyrSiJxCnqMZZmtVSVm9rA+TYaiEoVf1RlchZPOKBFhF1gMGRsYh3LPL9cU4lbqt6DpZiLggsudD4NQIDAQAB',
    'https://openapi.alipay.com/gateway.do',
    'RSA2', 'JSON', 'UTF-8',
    'ACTIVE', '0'
);

-- 3. pay_enterprise 加字段
ALTER TABLE pay_enterprise ADD COLUMN IF NOT EXISTS service_provider_id BIGINT;
ALTER TABLE pay_enterprise ADD COLUMN IF NOT EXISTS scope_label VARCHAR(64);

-- 4. 旧企业全部绑定到第一个服务商
UPDATE pay_enterprise SET service_provider_id = 1, scope_label = 'DOMESTIC' WHERE service_provider_id IS NULL;
