-- =============================================================
-- 风控平台 数据库增量脚本  风险警示-附件字段
-- 适用版本: 风控平台 2026-07-07 之前的部署
--
-- 需求:
--   风险警示发布时支持上传附件(基于 MinIO 对象存储),
--   详情页支持查看与下载附件。
--
-- 本脚本特征:
--   - 全部 SQL 幂等,可重复执行(基于 INFORMATION_SCHEMA 判断)
--   - 新环境用 risk_control_platform.sql 部署;已有环境用本脚本增量升级
--   - 不修改业务表已有数据,仅追加字段
-- =============================================================

SET NAMES utf8mb4;

-- =============================================================
-- 1. 风险警示表 risk_warning 加附件字段
-- =============================================================

-- 1.1 attachment_url 附件访问 URL(MinIO public 桶路径)
SET @sql = (SELECT IF(
  (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = DATABASE()
       AND TABLE_NAME   = 'risk_warning'
       AND COLUMN_NAME  = 'attachment_url') = 0,
  'ALTER TABLE `risk_warning` ADD COLUMN `attachment_url` VARCHAR(500) DEFAULT NULL COMMENT ''附件访问URL(MinIO对象存储路径)'' AFTER `remark`',
  'SELECT 1'));
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 1.2 attachment_name 附件原始文件名(下载时显示用)
SET @sql = (SELECT IF(
  (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = DATABASE()
       AND TABLE_NAME   = 'risk_warning'
       AND COLUMN_NAME  = 'attachment_name') = 0,
  'ALTER TABLE `risk_warning` ADD COLUMN `attachment_name` VARCHAR(200) DEFAULT NULL COMMENT ''附件原始文件名'' AFTER `attachment_url`',
  'SELECT 1'));
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;


-- =============================================================
-- 2. 验证清单(可手动 SELECT 确认)
-- =============================================================
-- SELECT COLUMN_NAME, COLUMN_TYPE, COLUMN_COMMENT
--   FROM INFORMATION_SCHEMA.COLUMNS
--  WHERE TABLE_SCHEMA = DATABASE()
--    AND TABLE_NAME = 'risk_warning'
--    AND COLUMN_NAME IN ('attachment_url', 'attachment_name');