SET @current_schema := DATABASE();

SET @add_wheel_reward_total_limit_sql := IF(
  NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = @current_schema
      AND table_name = 'wheel_reward_config'
      AND column_name = 'total_limit'
  ),
  'ALTER TABLE `wheel_reward_config` ADD COLUMN `total_limit` bigint NOT NULL DEFAULT 0 COMMENT ''奖品总发放上限，0表示不限'' AFTER `probability`',
  'SELECT 1'
);
PREPARE add_wheel_reward_total_limit_stmt FROM @add_wheel_reward_total_limit_sql;
EXECUTE add_wheel_reward_total_limit_stmt;
DEALLOCATE PREPARE add_wheel_reward_total_limit_stmt;

SET @add_wheel_reward_user_limit_sql := IF(
  NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = @current_schema
      AND table_name = 'wheel_reward_config'
      AND column_name = 'user_limit'
  ),
  'ALTER TABLE `wheel_reward_config` ADD COLUMN `user_limit` bigint NOT NULL DEFAULT 0 COMMENT ''单用户发放上限，0表示不限'' AFTER `total_limit`',
  'SELECT 1'
);
PREPARE add_wheel_reward_user_limit_stmt FROM @add_wheel_reward_user_limit_sql;
EXECUTE add_wheel_reward_user_limit_stmt;
DEALLOCATE PREPARE add_wheel_reward_user_limit_stmt;

SET @add_wheel_reward_issued_count_sql := IF(
  NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = @current_schema
      AND table_name = 'wheel_reward_config'
      AND column_name = 'issued_count'
  ),
  'ALTER TABLE `wheel_reward_config` ADD COLUMN `issued_count` bigint NOT NULL DEFAULT 0 COMMENT ''已占用发放数量，PENDING/SUCCESS/FAILED发奖占用'' AFTER `user_limit`',
  'SELECT 1'
);
PREPARE add_wheel_reward_issued_count_stmt FROM @add_wheel_reward_issued_count_sql;
EXECUTE add_wheel_reward_issued_count_stmt;
DEALLOCATE PREPARE add_wheel_reward_issued_count_stmt;

SET @add_wheel_draw_reward_config_id_sql := IF(
  NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = @current_schema
      AND table_name = 'wheel_draw_record'
      AND column_name = 'reward_config_id'
  ),
  'ALTER TABLE `wheel_draw_record` ADD COLUMN `reward_config_id` bigint NOT NULL DEFAULT 0 COMMENT ''命中奖项配置ID'' AFTER `paid_gold`',
  'SELECT 1'
);
PREPARE add_wheel_draw_reward_config_id_stmt FROM @add_wheel_draw_reward_config_id_sql;
EXECUTE add_wheel_draw_reward_config_id_stmt;
DEALLOCATE PREPARE add_wheel_draw_reward_config_id_stmt;

SET @add_wheel_draw_retry_count_sql := IF(
  NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = @current_schema
      AND table_name = 'wheel_draw_record'
      AND column_name = 'retry_count'
  ),
  'ALTER TABLE `wheel_draw_record` ADD COLUMN `retry_count` int NOT NULL DEFAULT 0 COMMENT ''发奖尝试次数'' AFTER `status`',
  'SELECT 1'
);
PREPARE add_wheel_draw_retry_count_stmt FROM @add_wheel_draw_retry_count_sql;
EXECUTE add_wheel_draw_retry_count_stmt;
DEALLOCATE PREPARE add_wheel_draw_retry_count_stmt;

SET @add_wheel_draw_grant_time_sql := IF(
  NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = @current_schema
      AND table_name = 'wheel_draw_record'
      AND column_name = 'grant_time'
  ),
  'ALTER TABLE `wheel_draw_record` ADD COLUMN `grant_time` datetime(3) DEFAULT NULL COMMENT ''发奖成功时间'' AFTER `error_message`',
  'SELECT 1'
);
PREPARE add_wheel_draw_grant_time_stmt FROM @add_wheel_draw_grant_time_sql;
EXECUTE add_wheel_draw_grant_time_stmt;
DEALLOCATE PREPARE add_wheel_draw_grant_time_stmt;

SET @add_wheel_draw_reward_user_idx_sql := IF(
  NOT EXISTS (
    SELECT 1 FROM information_schema.statistics
    WHERE table_schema = @current_schema
      AND table_name = 'wheel_draw_record'
      AND index_name = 'idx_wheel_draw_reward_user'
  ),
  'ALTER TABLE `wheel_draw_record` ADD INDEX `idx_wheel_draw_reward_user` (`reward_config_id`, `user_id`, `status`)',
  'SELECT 1'
);
PREPARE add_wheel_draw_reward_user_idx_stmt FROM @add_wheel_draw_reward_user_idx_sql;
EXECUTE add_wheel_draw_reward_user_idx_stmt;
DEALLOCATE PREPARE add_wheel_draw_reward_user_idx_stmt;

SET @wheel_reward_probability_max := (
  SELECT COALESCE(MAX(`probability`), 0) FROM `wheel_reward_config`
);
SET @wheel_record_probability_max := (
  SELECT COALESCE(MAX(`probability`), 0) FROM `wheel_draw_record`
);

UPDATE `wheel_reward_config`
SET `probability` = `probability` * 100
WHERE @wheel_reward_probability_max > 0
  AND @wheel_reward_probability_max <= 10000;

UPDATE `wheel_draw_record`
SET `probability` = `probability` * 100
WHERE @wheel_record_probability_max > 0
  AND @wheel_record_probability_max <= 10000;
