SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

USE hyapp_user;

-- 只新增无索引的展示列，并强制 INSTANT 算法；不支持元数据即时变更时直接失败，避免意外触发全表重建。
SET @apps_logo_column_exists = (
  SELECT COUNT(*)
  FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'apps'
    AND COLUMN_NAME = 'logo_url'
);
SET @apps_logo_ddl = IF(
  @apps_logo_column_exists = 0,
  'ALTER TABLE apps ADD COLUMN logo_url VARCHAR(1024) NOT NULL DEFAULT '''' COMMENT ''应用 Logo 的公网访问地址'', ALGORITHM=INSTANT',
  'SELECT 1'
);
PREPARE apps_logo_stmt FROM @apps_logo_ddl;
EXECUTE apps_logo_stmt;
DEALLOCATE PREPARE apps_logo_stmt;

SET @now_ms = CAST(UNIX_TIMESTAMP(UTC_TIMESTAMP(3)) * 1000 AS UNSIGNED);

-- app_code 有唯一索引，五个常量键只会做定点查找；不在 apps 表的 legacy App 由 admin-server 财务源配置提供 Logo。
UPDATE apps
SET logo_url = CASE app_code
    WHEN 'lalu' THEN 'https://media.haiyihy.com/admin/apps/logos/lalu.png'
    WHEN 'huwaa' THEN 'https://media.haiyihy.com/admin/apps/logos/huwaa.png'
    WHEN 'fami' THEN 'https://media.haiyihy.com/admin/apps/logos/fami.png'
    WHEN 'aslan' THEN 'https://media.haiyihy.com/admin/apps/logos/aslan.png'
    WHEN 'yumi' THEN 'https://media.haiyihy.com/admin/apps/logos/yumi.png'
    ELSE logo_url
  END,
  updated_at_ms = @now_ms
WHERE app_code IN ('lalu', 'huwaa', 'fami', 'aslan', 'yumi')
  AND logo_url <> CASE app_code
    WHEN 'lalu' THEN 'https://media.haiyihy.com/admin/apps/logos/lalu.png'
    WHEN 'huwaa' THEN 'https://media.haiyihy.com/admin/apps/logos/huwaa.png'
    WHEN 'fami' THEN 'https://media.haiyihy.com/admin/apps/logos/fami.png'
    WHEN 'aslan' THEN 'https://media.haiyihy.com/admin/apps/logos/aslan.png'
    WHEN 'yumi' THEN 'https://media.haiyihy.com/admin/apps/logos/yumi.png'
    ELSE logo_url
  END;
