SET @current_schema := DATABASE();

SET @add_newbie_pool_enabled_sql := IF(
  NOT EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_schema = @current_schema
      AND table_name = 'smash_egg_config'
      AND column_name = 'newbie_pool_enabled'
  ),
  'ALTER TABLE `smash_egg_config` ADD COLUMN `newbie_pool_enabled` tinyint(1) NOT NULL DEFAULT 0 COMMENT ''是否启用新人奖池'' AFTER `rtp_basis_points`',
  'SELECT 1'
);
PREPARE add_newbie_pool_enabled_stmt FROM @add_newbie_pool_enabled_sql;
EXECUTE add_newbie_pool_enabled_stmt;
DEALLOCATE PREPARE add_newbie_pool_enabled_stmt;

SET @add_newbie_window_days_sql := IF(
  NOT EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_schema = @current_schema
      AND table_name = 'smash_egg_config'
      AND column_name = 'newbie_window_days'
  ),
  'ALTER TABLE `smash_egg_config` ADD COLUMN `newbie_window_days` int NOT NULL DEFAULT 7 COMMENT ''新人资格窗口天数'' AFTER `newbie_pool_enabled`',
  'SELECT 1'
);
PREPARE add_newbie_window_days_stmt FROM @add_newbie_window_days_sql;
EXECUTE add_newbie_window_days_stmt;
DEALLOCATE PREPARE add_newbie_window_days_stmt;

SET @add_newbie_max_draw_count_sql := IF(
  NOT EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_schema = @current_schema
      AND table_name = 'smash_egg_config'
      AND column_name = 'newbie_max_draw_count'
  ),
  'ALTER TABLE `smash_egg_config` ADD COLUMN `newbie_max_draw_count` int NOT NULL DEFAULT 3 COMMENT ''每个用户最多使用新人奖池的抽奖请求次数'' AFTER `newbie_window_days`',
  'SELECT 1'
);
PREPARE add_newbie_max_draw_count_stmt FROM @add_newbie_max_draw_count_sql;
EXECUTE add_newbie_max_draw_count_stmt;
DEALLOCATE PREPARE add_newbie_max_draw_count_stmt;

SET @add_newbie_min_recharge_amount_sql := IF(
  NOT EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_schema = @current_schema
      AND table_name = 'smash_egg_config'
      AND column_name = 'newbie_min_recharge_amount'
  ),
  'ALTER TABLE `smash_egg_config` ADD COLUMN `newbie_min_recharge_amount` bigint NOT NULL DEFAULT 0 COMMENT ''新人奖池最低累计充值金额，0表示不限制'' AFTER `newbie_max_draw_count`',
  'SELECT 1'
);
PREPARE add_newbie_min_recharge_amount_stmt FROM @add_newbie_min_recharge_amount_sql;
EXECUTE add_newbie_min_recharge_amount_stmt;
DEALLOCATE PREPARE add_newbie_min_recharge_amount_stmt;

SET @add_reward_pool_type_sql := IF(
  NOT EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_schema = @current_schema
      AND table_name = 'smash_egg_reward_config'
      AND column_name = 'pool_type'
  ),
  'ALTER TABLE `smash_egg_reward_config` ADD COLUMN `pool_type` varchar(32) NOT NULL DEFAULT ''GENERAL'' COMMENT ''奖池类型 GENERAL/NEWBIE'' AFTER `sys_origin`',
  'SELECT 1'
);
PREPARE add_reward_pool_type_stmt FROM @add_reward_pool_type_sql;
EXECUTE add_reward_pool_type_stmt;
DEALLOCATE PREPARE add_reward_pool_type_stmt;

SET @add_draw_record_pool_type_sql := IF(
  NOT EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_schema = @current_schema
      AND table_name = 'smash_egg_draw_record'
      AND column_name = 'pool_type'
  ),
  'ALTER TABLE `smash_egg_draw_record` ADD COLUMN `pool_type` varchar(32) NOT NULL DEFAULT ''GENERAL'' COMMENT ''奖池类型 GENERAL/NEWBIE'' AFTER `user_id`',
  'SELECT 1'
);
PREPARE add_draw_record_pool_type_stmt FROM @add_draw_record_pool_type_sql;
EXECUTE add_draw_record_pool_type_stmt;
DEALLOCATE PREPARE add_draw_record_pool_type_stmt;

SET @add_reward_pool_index_sql := IF(
  NOT EXISTS (
    SELECT 1
    FROM information_schema.statistics
    WHERE table_schema = @current_schema
      AND table_name = 'smash_egg_reward_config'
      AND index_name = 'idx_smash_egg_reward_pool'
  ),
  'ALTER TABLE `smash_egg_reward_config` ADD KEY `idx_smash_egg_reward_pool` (`config_id`, `pool_type`, `enabled`, `sort`)',
  'SELECT 1'
);
PREPARE add_reward_pool_index_stmt FROM @add_reward_pool_index_sql;
EXECUTE add_reward_pool_index_stmt;
DEALLOCATE PREPARE add_reward_pool_index_stmt;

SET @add_draw_user_pool_index_sql := IF(
  NOT EXISTS (
    SELECT 1
    FROM information_schema.statistics
    WHERE table_schema = @current_schema
      AND table_name = 'smash_egg_draw_record'
      AND index_name = 'idx_smash_egg_draw_record_user_pool'
  ),
  'ALTER TABLE `smash_egg_draw_record` ADD KEY `idx_smash_egg_draw_record_user_pool` (`sys_origin`, `user_id`, `pool_type`, `status`, `create_time`)',
  'SELECT 1'
);
PREPARE add_draw_user_pool_index_stmt FROM @add_draw_user_pool_index_sql;
EXECUTE add_draw_user_pool_index_stmt;
DEALLOCATE PREPARE add_draw_user_pool_index_stmt;
