-- ============================================================
-- 05: 品牌字段映射增加启用/禁用开关
-- 执行目标库：cnc_business
-- 幂等：是（通过 IF NOT EXISTS 检查列是否已存在）
-- ============================================================

-- 1. 增加 is_enabled 列（默认启用）
SET @dbname = 'cnc_business';
SET @tablename = 'cnc_brand_field_mapping';
SET @columnname = 'is_enabled';
SET @preparedStatement = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = @dbname
      AND TABLE_NAME = @tablename
      AND COLUMN_NAME = @columnname
  ) > 0,
  'SELECT 1',
  'ALTER TABLE cnc_brand_field_mapping ADD COLUMN is_enabled tinyint(1) NOT NULL DEFAULT 1 COMMENT ''是否启用：1=启用 0=禁用'' AFTER is_required'
));
PREPARE alterIfNotExists FROM @preparedStatement;
EXECUTE alterIfNotExists;
DEALLOCATE PREPARE alterIfNotExists;

-- 2. 根据采集示例数据，禁用未出现的字段映射
--    采集示例中只出现了：_io_status, Tag5, Tag8, Tag9, Tag11, Tag22, Tag23
--    未出现的先设为禁用，后续可通过后台开关启用
UPDATE cnc_brand_field_mapping SET is_enabled = 0
WHERE field_name IN ('Tag14','Tag17','Tag18','Tag19','Tag20','Tag21','Tag24','Tag25','Tag26')
  AND is_enabled = 1;

-- 3. 验证结果
SELECT id, standard_field, field_name, is_required, is_enabled
FROM cnc_brand_field_mapping
ORDER BY id;
