充值送券处理

This commit is contained in:
tianfeng 2025-12-30 15:44:37 +08:00
parent 37df973c54
commit 4b3f02ecfb

View File

@ -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.app.util.DateTimeAsiaRiyadhUtils;
import com.red.circle.other.infra.database.rds.service.activity.UserActivityRechargeService; 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.other.inner.endpoint.activity.api.UserActivityRechargeClientApi;
import com.red.circle.tool.core.text.StringUtils;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
@ -46,41 +46,35 @@ public class UserActivityRechargeClientController implements UserActivityRecharg
log.info("Spins充值任务, userId={}, amount={}, totalAmount={}", userId, amount, totalAmount); log.info("Spins充值任务, userId={}, amount={}, totalAmount={}", userId, amount, totalAmount);
if (totalAmount.compareTo(BigDecimal.ONE) >= 0) { // 转换为整数金额美元
spinsUserTaskProgressService.updateTaskProgress( int progressValue = totalAmount.intValue();
new SpinsTaskProgressUpdateCmd() spinsUserTaskProgressService.updateTaskProgress(
.setUserId(userId) new SpinsTaskProgressUpdateCmd()
.setTaskCode("SPINS_RECHARGE_1") .setUserId(userId)
.setProgressValue(1) .setTaskCode("SPINS_RECHARGE_1")
); .setProgressValue(progressValue)
} );
if (totalAmount.compareTo(BigDecimal.TEN) >= 0) { spinsUserTaskProgressService.updateTaskProgress(
spinsUserTaskProgressService.updateTaskProgress( new SpinsTaskProgressUpdateCmd()
new SpinsTaskProgressUpdateCmd() .setUserId(userId)
.setUserId(userId) .setTaskCode("SPINS_RECHARGE_10")
.setTaskCode("SPINS_RECHARGE_10") .setProgressValue(progressValue)
.setProgressValue(1) );
);
}
if (totalAmount.compareTo(new BigDecimal("50")) >= 0) { spinsUserTaskProgressService.updateTaskProgress(
spinsUserTaskProgressService.updateTaskProgress( new SpinsTaskProgressUpdateCmd()
new SpinsTaskProgressUpdateCmd() .setUserId(userId)
.setUserId(userId) .setTaskCode("SPINS_RECHARGE_50")
.setTaskCode("SPINS_RECHARGE_50") .setProgressValue(progressValue)
.setProgressValue(1) );
);
}
if (totalAmount.compareTo(new BigDecimal("100")) >= 0) { spinsUserTaskProgressService.updateTaskProgress(
spinsUserTaskProgressService.updateTaskProgress( new SpinsTaskProgressUpdateCmd()
new SpinsTaskProgressUpdateCmd() .setUserId(userId)
.setUserId(userId) .setTaskCode("SPINS_RECHARGE_100")
.setTaskCode("SPINS_RECHARGE_100") .setProgressValue(progressValue)
.setProgressValue(1) );
);
}
log.info("Spins充值任务处理成功, userId={}, totalAmount={}", userId, totalAmount); log.info("Spins充值任务处理成功, userId={}, totalAmount={}", userId, totalAmount);
} catch (Exception e) { } catch (Exception e) {
@ -96,18 +90,20 @@ public class UserActivityRechargeClientController implements UserActivityRecharg
*/ */
private BigDecimal incrementDailyRechargeAmount(Long userId, BigDecimal amount) { private BigDecimal incrementDailyRechargeAmount(Long userId, BigDecimal amount) {
String redisKey = "spins:daily:recharge:amount:" + userId; 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(); long expireSeconds = DateTimeAsiaRiyadhUtils.getSecondsUntilMidnight();
redisService.setString(redisKey, amount.toString(), expireSeconds, TimeUnit.SECONDS); redisService.expire(redisKey, 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;
} }
// 转换回美元
return new BigDecimal(totalCents).divide(new BigDecimal("100"), 2, RoundingMode.DOWN);
} }
} }