-- 微信小程序配置检查和修复SQL脚本
-- 用于解决"租户 10550 的小程序未配置"问题

-- 1. 检查当前cms_website_field表中租户10550的配置
SELECT
    id,
    name,
    value,
    tenant_id,
    deleted,
    comments
FROM cms_website_field
WHERE tenant_id = 10550
  AND deleted = 0
ORDER BY name;

-- 2. 检查是否已有AppID和AppSecret配置
SELECT
    id,
    name,
    value,
    tenant_id,
    deleted,
    comments
FROM cms_website_field
WHERE tenant_id = 10550
  AND name IN ('AppID', 'AppSecret')
  AND deleted = 0;

-- 3. 如果没有AppID配置，创建一个（请替换为实际的AppID）
INSERT INTO cms_website_field (
    type,
    name,
    value,
    tenant_id,
    comments,
    deleted,
    create_time
)
SELECT 0, 'AppID', 'wx1234567890abcdef', 10550, '微信小程序AppID', 0, NOW()
WHERE NOT EXISTS (
    SELECT 1 FROM cms_website_field
    WHERE name = 'AppID' AND tenant_id = 10550 AND deleted = 0
);

-- 4. 如果没有AppSecret配置，创建一个（请替换为实际的AppSecret）
INSERT INTO cms_website_field (
    type,
    name,
    value,
    tenant_id,
    comments,
    deleted,
    create_time
)
SELECT 0, 'AppSecret', 'abcdef1234567890abcdef1234567890', 10550, '微信小程序AppSecret', 0, NOW()
WHERE NOT EXISTS (
    SELECT 1 FROM cms_website_field
    WHERE name = 'AppSecret' AND tenant_id = 10550 AND deleted = 0
);

-- 5. 验证配置是否创建成功
SELECT
    id,
    name,
    value,
    tenant_id,
    deleted,
    comments,
    create_time
FROM cms_website_field
WHERE tenant_id = 10550
  AND name IN ('AppID', 'AppSecret')
  AND deleted = 0;

-- 6. 检查sys_setting表中是否有mp-weixin配置（用于对比）
SELECT
    setting_id,
    setting_key,
    content,
    tenant_id,
    deleted,
    comments
FROM gxwebsoft_core.sys_setting
WHERE setting_key = 'mp-weixin'
  AND tenant_id = 10550
  AND deleted = 0;

-- 7. 查看所有租户的mp-weixin配置情况
SELECT
    setting_id,
    setting_key,
    content,
    tenant_id,
    deleted
FROM gxwebsoft_core.sys_setting
WHERE setting_key = 'mp-weixin'
  AND deleted = 0
ORDER BY tenant_id;

-- 8. 如果你有实际的微信小程序配置，请更新这些值
-- 更新AppID（请替换为实际值）
-- UPDATE cms_website_field
-- SET value = '你的实际AppID'
-- WHERE name = 'AppID' AND tenant_id = 10550 AND deleted = 0;

-- 更新AppSecret（请替换为实际值）
-- UPDATE cms_website_field
-- SET value = '你的实际AppSecret'
-- WHERE name = 'AppSecret' AND tenant_id = 10550 AND deleted = 0;
