SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

USE hyapp_lucky_gift;

-- 幸运礼物配置收敛为“阶段 + 倍率 + 自动概率 + 高水位标记”。
-- 旧预算来源、展示广播、风控上限和预算拆分统计不再进入发布契约或新事实表。
SET @drop_col := (
  SELECT IF(COUNT(*) > 0, 'ALTER TABLE lucky_gift_stage_tiers DROP COLUMN reward_source', 'SELECT 1')
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_gift_stage_tiers' AND COLUMN_NAME = 'reward_source'
);
PREPARE stmt FROM @drop_col;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_col := (
  SELECT IF(COUNT(*) > 0, 'ALTER TABLE lucky_gift_stage_tiers DROP COLUMN broadcast_level', 'SELECT 1')
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_gift_stage_tiers' AND COLUMN_NAME = 'broadcast_level'
);
PREPARE stmt FROM @drop_col;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_col := (
  SELECT IF(COUNT(*) > 0, 'ALTER TABLE lucky_gift_rule_versions DROP COLUMN max_single_payout', 'SELECT 1')
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_gift_rule_versions' AND COLUMN_NAME = 'max_single_payout'
);
PREPARE stmt FROM @drop_col;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_col := (
  SELECT IF(COUNT(*) > 0, 'ALTER TABLE lucky_gift_rule_versions DROP COLUMN user_hourly_payout_cap', 'SELECT 1')
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_gift_rule_versions' AND COLUMN_NAME = 'user_hourly_payout_cap'
);
PREPARE stmt FROM @drop_col;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_col := (
  SELECT IF(COUNT(*) > 0, 'ALTER TABLE lucky_gift_rule_versions DROP COLUMN user_daily_payout_cap', 'SELECT 1')
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_gift_rule_versions' AND COLUMN_NAME = 'user_daily_payout_cap'
);
PREPARE stmt FROM @drop_col;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_col := (
  SELECT IF(COUNT(*) > 0, 'ALTER TABLE lucky_gift_rule_versions DROP COLUMN device_daily_payout_cap', 'SELECT 1')
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_gift_rule_versions' AND COLUMN_NAME = 'device_daily_payout_cap'
);
PREPARE stmt FROM @drop_col;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_col := (
  SELECT IF(COUNT(*) > 0, 'ALTER TABLE lucky_gift_rule_versions DROP COLUMN room_hourly_payout_cap', 'SELECT 1')
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_gift_rule_versions' AND COLUMN_NAME = 'room_hourly_payout_cap'
);
PREPARE stmt FROM @drop_col;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_col := (
  SELECT IF(COUNT(*) > 0, 'ALTER TABLE lucky_gift_rule_versions DROP COLUMN anchor_daily_payout_cap', 'SELECT 1')
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_gift_rule_versions' AND COLUMN_NAME = 'anchor_daily_payout_cap'
);
PREPARE stmt FROM @drop_col;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_col := (
  SELECT IF(COUNT(*) > 0, 'ALTER TABLE lucky_draw_records DROP COLUMN budget_sources_json', 'SELECT 1')
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_draw_records' AND COLUMN_NAME = 'budget_sources_json'
);
PREPARE stmt FROM @drop_col;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_col := (
  SELECT IF(COUNT(*) > 0, 'ALTER TABLE lucky_draw_records DROP COLUMN room_atmosphere_reward_coins', 'SELECT 1')
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_draw_records' AND COLUMN_NAME = 'room_atmosphere_reward_coins'
);
PREPARE stmt FROM @drop_col;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_col := (
  SELECT IF(COUNT(*) > 0, 'ALTER TABLE lucky_draw_records DROP COLUMN activity_subsidy_coins', 'SELECT 1')
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_draw_records' AND COLUMN_NAME = 'activity_subsidy_coins'
);
PREPARE stmt FROM @drop_col;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_col := (
  SELECT IF(COUNT(*) > 0, 'ALTER TABLE lucky_draw_pool_stats DROP COLUMN room_atmosphere_reward_coins', 'SELECT 1')
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_draw_pool_stats' AND COLUMN_NAME = 'room_atmosphere_reward_coins'
);
PREPARE stmt FROM @drop_col;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_col := (
  SELECT IF(COUNT(*) > 0, 'ALTER TABLE lucky_draw_pool_stats DROP COLUMN activity_subsidy_coins', 'SELECT 1')
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_draw_pool_stats' AND COLUMN_NAME = 'activity_subsidy_coins'
);
PREPARE stmt FROM @drop_col;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_col := (
  SELECT IF(COUNT(*) > 0, 'ALTER TABLE lucky_draw_pool_day_stats DROP COLUMN room_atmosphere_reward_coin', 'SELECT 1')
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_draw_pool_day_stats' AND COLUMN_NAME = 'room_atmosphere_reward_coin'
);
PREPARE stmt FROM @drop_col;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_col := (
  SELECT IF(COUNT(*) > 0, 'ALTER TABLE lucky_draw_pool_day_stats DROP COLUMN activity_subsidy_coin', 'SELECT 1')
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_draw_pool_day_stats' AND COLUMN_NAME = 'activity_subsidy_coin'
);
PREPARE stmt FROM @drop_col;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

DROP TABLE IF EXISTS lucky_risk_counters;
DROP TABLE IF EXISTS lucky_activity_budgets;
DROP TABLE IF EXISTS lucky_room_atmosphere_pools;
