CREATE TABLE IF NOT EXISTS `resident_register_reward_config` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `sys_origin` varchar(32) NOT NULL COMMENT '来源系统',
  `enabled` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否启用',
  `gold_amount` bigint NOT NULL DEFAULT '0' COMMENT '金币数量',
  `reward_group_id` bigint DEFAULT NULL COMMENT '奖励组ID',
  `daily_limit` bigint NOT NULL DEFAULT '0' COMMENT '每日发奖份数限制，0表示不限',
  `create_time` datetime(3) DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime(3) DEFAULT NULL COMMENT '更新时间',
  `create_user` bigint DEFAULT NULL COMMENT '创建用户',
  `update_user` bigint DEFAULT NULL COMMENT '更新用户',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_resident_register_reward_sys_origin` (`sys_origin`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='常驻注册奖励配置';

CREATE TABLE IF NOT EXISTS `resident_register_reward_log` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `event_id` varchar(128) NOT NULL COMMENT '注册事件ID',
  `stream_message_id` varchar(64) NOT NULL DEFAULT '' COMMENT 'Redis Stream消息ID',
  `sys_origin` varchar(32) NOT NULL COMMENT '来源系统',
  `user_id` bigint NOT NULL COMMENT '用户ID',
  `claim_date` varchar(10) NOT NULL DEFAULT '' COMMENT '领取日期，格式 YYYY-MM-DD',
  `gold_amount` bigint NOT NULL DEFAULT '0' COMMENT '金币数量',
  `reward_group_id` bigint DEFAULT NULL COMMENT '奖励组ID',
  `grant_mode` varchar(32) NOT NULL DEFAULT '' COMMENT '发奖决策模式：ISSUE_REWARD/QUOTA_EXHAUSTED',
  `status` varchar(32) NOT NULL COMMENT '处理状态',
  `error_message` varchar(255) NOT NULL DEFAULT '' COMMENT '错误信息',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_register_reward_event` (`event_id`),
  KEY `idx_register_reward_sys_user` (`sys_origin`, `user_id`),
  KEY `idx_register_reward_claim` (`sys_origin`, `claim_date`, `grant_mode`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='常驻注册奖励处理日志';

SET @current_schema := DATABASE();

SET @add_daily_limit_sql := IF(
  NOT EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_schema = @current_schema
      AND table_name = 'resident_register_reward_config'
      AND column_name = 'daily_limit'
  ),
  'ALTER TABLE `resident_register_reward_config` ADD COLUMN `daily_limit` bigint NOT NULL DEFAULT 0 COMMENT ''每日发奖份数限制，0表示不限'' AFTER `reward_group_id`',
  'SELECT 1'
);

PREPARE add_daily_limit_stmt FROM @add_daily_limit_sql;
EXECUTE add_daily_limit_stmt;
DEALLOCATE PREPARE add_daily_limit_stmt;

SET @add_claim_date_sql := IF(
  NOT EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_schema = @current_schema
      AND table_name = 'resident_register_reward_log'
      AND column_name = 'claim_date'
  ),
  'ALTER TABLE `resident_register_reward_log` ADD COLUMN `claim_date` varchar(10) NOT NULL DEFAULT '''' COMMENT ''领取日期，格式 YYYY-MM-DD'' AFTER `user_id`',
  'SELECT 1'
);

PREPARE add_claim_date_stmt FROM @add_claim_date_sql;
EXECUTE add_claim_date_stmt;
DEALLOCATE PREPARE add_claim_date_stmt;

SET @add_grant_mode_sql := IF(
  NOT EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_schema = @current_schema
      AND table_name = 'resident_register_reward_log'
      AND column_name = 'grant_mode'
  ),
  'ALTER TABLE `resident_register_reward_log` ADD COLUMN `grant_mode` varchar(32) NOT NULL DEFAULT '''' COMMENT ''发奖决策模式：ISSUE_REWARD/QUOTA_EXHAUSTED'' AFTER `reward_group_id`',
  'SELECT 1'
);

PREPARE add_grant_mode_stmt FROM @add_grant_mode_sql;
EXECUTE add_grant_mode_stmt;
DEALLOCATE PREPARE add_grant_mode_stmt;

SET @add_claim_index_sql := IF(
  NOT EXISTS (
    SELECT 1
    FROM information_schema.statistics
    WHERE table_schema = @current_schema
      AND table_name = 'resident_register_reward_log'
      AND index_name = 'idx_register_reward_claim'
  ),
  'ALTER TABLE `resident_register_reward_log` ADD KEY `idx_register_reward_claim` (`sys_origin`, `claim_date`, `grant_mode`)',
  'SELECT 1'
);

PREPARE add_claim_index_stmt FROM @add_claim_index_sql;
EXECUTE add_claim_index_stmt;
DEALLOCATE PREPARE add_claim_index_stmt;
