-- 微信支付公钥配置SQL脚本
-- 适用于租户ID: 10547

-- 1. 查看当前支付配置
SELECT 
    id,
    tenant_id,
    mch_id,
    app_id,
    merchant_serial_number,
    pub_key,
    pub_key_id,
    api_key,
    apiclient_key,
    apiclient_cert
FROM sys_payment 
WHERE tenant_id = 10547 AND type = 0;

-- 2. 更新公钥配置（请根据实际情况修改公钥ID）
UPDATE sys_payment SET 
    pub_key = 'wechatpay_public_key.pem',
    pub_key_id = 'PUB_KEY_ID_0112422897022025011300326200001208'  -- 请替换为实际的公钥ID
WHERE tenant_id = 10547 AND type = 0;

-- 3. 验证更新结果
SELECT 
    id,
    tenant_id,
    mch_id,
    app_id,
    merchant_serial_number,
    pub_key,
    pub_key_id,
    CASE 
        WHEN pub_key IS NOT NULL AND pub_key != '' AND pub_key_id IS NOT NULL AND pub_key_id != '' 
        THEN '✅ 公钥配置完整'
        ELSE '❌ 公钥配置不完整'
    END AS config_status
FROM sys_payment 
WHERE tenant_id = 10547 AND type = 0;

-- 4. 如果需要清除公钥配置（回退到自动证书模式）
-- UPDATE sys_payment SET 
--     pub_key = NULL,
--     pub_key_id = NULL
-- WHERE tenant_id = 10547 AND type = 0;

-- 5. 检查所有租户的公钥配置状态
SELECT 
    tenant_id,
    mch_id,
    CASE 
        WHEN pub_key IS NOT NULL AND pub_key != '' AND pub_key_id IS NOT NULL AND pub_key_id != '' 
        THEN '公钥模式'
        WHEN merchant_serial_number IS NOT NULL AND merchant_serial_number != ''
        THEN '自动证书模式'
        ELSE '配置不完整'
    END AS payment_mode,
    pub_key,
    pub_key_id
FROM sys_payment 
WHERE type = 0
ORDER BY tenant_id;
