SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 旧 finance-application:audit 曾兼容用户提现审核；先迁移到专属权限，避免删除申请模块时意外收回仍在使用的提现审核能力。
INSERT IGNORE INTO admin_role_permissions (role_id, permission_id)
SELECT existing.role_id, withdrawal_permission.id
FROM admin_role_permissions existing
JOIN admin_permissions legacy_permission
  ON legacy_permission.id = existing.permission_id
JOIN admin_permissions withdrawal_permission
  ON withdrawal_permission.code = 'finance-withdrawal:audit'
WHERE legacy_permission.code = 'finance-application:audit';

-- 权限 code 有唯一索引；这里只匹配 8 个废弃 code。角色权限表规模受角色数约束，一次性清理不会扫描业务流水或申请历史表。
DELETE role_permission
FROM admin_role_permissions role_permission
JOIN admin_permissions permission
  ON permission.id = role_permission.permission_id
WHERE permission.code IN (
  'finance-application:create',
  'finance-application:audit',
  'finance-operation:user-coin-credit',
  'finance-operation:user-coin-debit',
  'finance-operation:user-wallet-credit',
  'finance-operation:user-wallet-debit',
  'finance-operation:coin-seller-coin-credit',
  'finance-operation:coin-seller-coin-debit'
);

DELETE FROM admin_permissions
WHERE code IN (
  'finance-application:create',
  'finance-application:audit',
  'finance-operation:user-coin-credit',
  'finance-operation:user-coin-debit',
  'finance-operation:user-wallet-credit',
  'finance-operation:user-wallet-debit',
  'finance-operation:coin-seller-coin-credit',
  'finance-operation:coin-seller-coin-debit'
);
