diff --git a/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/activity/UserActivityRechargeClientController.java b/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/activity/UserActivityRechargeClientController.java index bc971ab9..3a619b3e 100644 --- a/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/activity/UserActivityRechargeClientController.java +++ b/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/activity/UserActivityRechargeClientController.java @@ -8,13 +8,13 @@ import com.red.circle.other.app.service.SpinsUserTaskProgressService; import com.red.circle.other.app.util.DateTimeAsiaRiyadhUtils; import com.red.circle.other.infra.database.rds.service.activity.UserActivityRechargeService; import com.red.circle.other.inner.endpoint.activity.api.UserActivityRechargeClientApi; -import com.red.circle.tool.core.text.StringUtils; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.concurrent.TimeUnit; /** @@ -46,41 +46,35 @@ public class UserActivityRechargeClientController implements UserActivityRecharg log.info("Spins充值任务, userId={}, amount={}, totalAmount={}", userId, amount, totalAmount); - if (totalAmount.compareTo(BigDecimal.ONE) >= 0) { - spinsUserTaskProgressService.updateTaskProgress( - new SpinsTaskProgressUpdateCmd() - .setUserId(userId) - .setTaskCode("SPINS_RECHARGE_1") - .setProgressValue(1) - ); - } + // 转换为整数金额(美元) + int progressValue = totalAmount.intValue(); + spinsUserTaskProgressService.updateTaskProgress( + new SpinsTaskProgressUpdateCmd() + .setUserId(userId) + .setTaskCode("SPINS_RECHARGE_1") + .setProgressValue(progressValue) + ); - if (totalAmount.compareTo(BigDecimal.TEN) >= 0) { - spinsUserTaskProgressService.updateTaskProgress( - new SpinsTaskProgressUpdateCmd() - .setUserId(userId) - .setTaskCode("SPINS_RECHARGE_10") - .setProgressValue(1) - ); - } + spinsUserTaskProgressService.updateTaskProgress( + new SpinsTaskProgressUpdateCmd() + .setUserId(userId) + .setTaskCode("SPINS_RECHARGE_10") + .setProgressValue(progressValue) + ); - if (totalAmount.compareTo(new BigDecimal("50")) >= 0) { - spinsUserTaskProgressService.updateTaskProgress( - new SpinsTaskProgressUpdateCmd() - .setUserId(userId) - .setTaskCode("SPINS_RECHARGE_50") - .setProgressValue(1) - ); - } + spinsUserTaskProgressService.updateTaskProgress( + new SpinsTaskProgressUpdateCmd() + .setUserId(userId) + .setTaskCode("SPINS_RECHARGE_50") + .setProgressValue(progressValue) + ); - if (totalAmount.compareTo(new BigDecimal("100")) >= 0) { - spinsUserTaskProgressService.updateTaskProgress( - new SpinsTaskProgressUpdateCmd() - .setUserId(userId) - .setTaskCode("SPINS_RECHARGE_100") - .setProgressValue(1) - ); - } + spinsUserTaskProgressService.updateTaskProgress( + new SpinsTaskProgressUpdateCmd() + .setUserId(userId) + .setTaskCode("SPINS_RECHARGE_100") + .setProgressValue(progressValue) + ); log.info("Spins充值任务处理成功, userId={}, totalAmount={}", userId, totalAmount); } catch (Exception e) { @@ -96,18 +90,20 @@ public class UserActivityRechargeClientController implements UserActivityRecharg */ private BigDecimal incrementDailyRechargeAmount(Long userId, BigDecimal amount) { String redisKey = "spins:daily:recharge:amount:" + userId; - String amountStr = redisService.getString(redisKey); - if (StringUtils.isBlank(amountStr)) { + // 转换为分(避免浮点数精度问题) + long amountInCents = amount.multiply(new BigDecimal("100")).longValue(); + + // 使用 Redis increment 原子操作 + Long totalCents = redisService.increment(redisKey, amountInCents); + + // 首次创建时设置过期时间 + if (totalCents.equals(amountInCents)) { long expireSeconds = DateTimeAsiaRiyadhUtils.getSecondsUntilMidnight(); - redisService.setString(redisKey, amount.toString(), expireSeconds, TimeUnit.SECONDS); - return amount; - } else { - BigDecimal currentAmount = new BigDecimal(amountStr); - BigDecimal newAmount = currentAmount.add(amount); - long expireSeconds = DateTimeAsiaRiyadhUtils.getSecondsUntilMidnight(); - redisService.setString(redisKey, newAmount.toString(), expireSeconds, TimeUnit.SECONDS); - return newAmount; + redisService.expire(redisKey, expireSeconds, TimeUnit.SECONDS); } + + // 转换回美元 + return new BigDecimal(totalCents).divide(new BigDecimal("100"), 2, RoundingMode.DOWN); } }