CREATE TABLE IF NOT EXISTS `first_recharge_reward_config` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `sys_origin` varchar(32) NOT NULL COMMENT '来源系统',
  `enabled` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否启用',
  `min_app_version` varchar(32) NOT NULL DEFAULT '1.5.0' COMMENT '最低生效App版本',
  `create_time` datetime(3) DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime(3) DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_first_recharge_reward_sys_origin` (`sys_origin`),
  KEY `idx_first_recharge_reward_enabled` (`enabled`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='首冲奖励配置';

CREATE TABLE IF NOT EXISTS `first_recharge_reward_level` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `config_id` bigint NOT NULL COMMENT '首冲奖励主配置ID',
  `level` int NOT NULL COMMENT '档位',
  `recharge_amount_cents` bigint NOT NULL DEFAULT '0' COMMENT '充值金额门槛，单位为分',
  `google_product_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Google商品ID',
  `reward_group_id` bigint NOT NULL DEFAULT '0' COMMENT '奖励资源组ID',
  `reward_group_name` varchar(255) NOT NULL DEFAULT '' COMMENT '奖励资源组名称快照',
  `enabled` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否启用',
  `create_time` datetime(3) DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime(3) DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_first_recharge_reward_level` (`config_id`, `level`),
  UNIQUE KEY `uk_first_recharge_reward_amount` (`config_id`, `recharge_amount_cents`),
  KEY `idx_first_recharge_reward_level_google_product` (`google_product_id`),
  KEY `idx_first_recharge_reward_level_config` (`config_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='首冲奖励充值档位';

CREATE TABLE IF NOT EXISTS `first_recharge_reward_grant_record` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `event_id` varchar(128) 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 '来源系统',
  `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 '本次充值金额，单位为分',
  `recharge_threshold_cents` bigint NOT NULL DEFAULT '0' COMMENT '命中档位门槛，单位为分',
  `level` int NOT NULL COMMENT '命中档位',
  `pay_platform` varchar(64) NOT NULL DEFAULT '' COMMENT '支付平台：GOOGLE/MIFA_PAY/PAYER_MAX等',
  `payment_method` varchar(32) NOT NULL DEFAULT '' COMMENT '充值方式：GOOGLE/THIRD_PARTY',
  `google_product_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Google商品ID',
  `source_order_id` varchar(128) NOT NULL DEFAULT '' COMMENT '来源订单ID',
  `reward_group_id` bigint NOT NULL DEFAULT '0' COMMENT '奖励资源组ID',
  `reward_group_name` varchar(255) NOT NULL DEFAULT '' COMMENT '奖励资源组名称快照',
  `reward_group_track_id` bigint NOT NULL DEFAULT '0' COMMENT '奖励资源组发放幂等追踪ID',
  `status` varchar(32) NOT NULL DEFAULT 'PENDING' COMMENT '整体发放状态：PENDING/SUCCESS/FAILED',
  `reward_group_status` varchar(32) NOT NULL DEFAULT 'PENDING' COMMENT '奖励组发放状态：PENDING/SUCCESS/FAILED',
  `retry_count` int NOT NULL DEFAULT '0' COMMENT '重试次数',
  `last_error` varchar(1024) NOT NULL DEFAULT '' COMMENT '最近一次失败原因',
  `event_time` datetime(3) DEFAULT NULL 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_first_recharge_reward_event` (`event_id`),
  UNIQUE KEY `uk_first_recharge_reward_user` (`sys_origin`, `user_id`),
  KEY `idx_first_recharge_reward_status` (`sys_origin`, `user_id`, `status`),
  KEY `idx_first_recharge_reward_grant_google_product` (`google_product_id`),
  KEY `idx_first_recharge_reward_order` (`source_order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='首冲奖励发放记录';
