CREATE TABLE IF NOT EXISTS `recharge_reward_grant_record` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `config_id` bigint NOT NULL COMMENT '充值奖励主配置ID',
  `level_id` bigint NOT NULL COMMENT '充值奖励档位ID',
  `sys_origin` varchar(32) NOT NULL COMMENT '来源系统',
  `cycle_key` varchar(32) NOT NULL COMMENT '充值周期标识，周期开始日yyyyMMdd',
  `recharge_date` int NOT NULL COMMENT '充值年月yyyyMM，用于后台筛选',
  `period_start_at` datetime(3) NOT NULL COMMENT '周期开始时间，活动时区墙上时间',
  `period_end_at` datetime(3) NOT NULL COMMENT '周期结束时间，活动时区墙上时间',
  `user_id` bigint NOT NULL COMMENT '用户ID',
  `account` varchar(64) NOT NULL DEFAULT '' COMMENT '用户账号快照',
  `user_avatar` varchar(1024) NOT NULL DEFAULT '' COMMENT '用户头像快照',
  `user_nickname` varchar(255) NOT NULL DEFAULT '' COMMENT '用户昵称快照',
  `country_code` varchar(32) NOT NULL DEFAULT '' COMMENT '国家编码快照',
  `country_name` varchar(128) NOT NULL DEFAULT '' COMMENT '国家名称快照',
  `recharge_amount_cents` bigint NOT NULL DEFAULT '0' COMMENT '发放时周期累计充值金额，单位为分',
  `level` int NOT NULL COMMENT '奖励档位',
  `recharge_threshold_cents` bigint NOT NULL DEFAULT '0' COMMENT '充值金额门槛，单位为分',
  `reward_gold` bigint NOT NULL DEFAULT '0' COMMENT '金币奖励数量',
  `reward_group_id` bigint DEFAULT NULL COMMENT '奖励组ID',
  `reward_group_name` varchar(255) NOT NULL DEFAULT '' COMMENT '奖励组名称快照',
  `reward_group_track_id` bigint NOT NULL DEFAULT '0' COMMENT '奖励组发放幂等追踪ID',
  `gold_event_id` varchar(128) NOT NULL DEFAULT '' COMMENT '金币发放钱包事件ID',
  `status` varchar(32) NOT NULL DEFAULT 'PENDING' COMMENT '整体发放状态：PENDING/SUCCESS/FAILED',
  `reward_group_status` varchar(32) NOT NULL DEFAULT 'SKIPPED' COMMENT '奖励组发放状态：PENDING/SUCCESS/FAILED/SKIPPED',
  `reward_gold_status` varchar(32) NOT NULL DEFAULT 'SKIPPED' COMMENT '金币发放状态：PENDING/SUCCESS/FAILED/SKIPPED',
  `retry_count` int NOT NULL DEFAULT '0' COMMENT '重试次数',
  `last_error` varchar(1024) NOT NULL DEFAULT '' COMMENT '最近一次失败原因',
  `sent_at` datetime(3) DEFAULT NULL COMMENT '整体发放成功时间',
  `create_time` datetime(3) DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime(3) DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_recharge_reward_grant_level` (`sys_origin`, `cycle_key`, `user_id`, `level`),
  KEY `idx_recharge_reward_grant_config_cycle` (`config_id`, `cycle_key`, `status`),
  KEY `idx_recharge_reward_grant_user` (`user_id`, `recharge_date`),
  KEY `idx_recharge_reward_grant_date` (`sys_origin`, `recharge_date`, `status`),
  KEY `idx_recharge_reward_gold_event` (`gold_event_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='充值奖励周期发放记录';
