diff --git a/rc-service/rc-service-wallet/wallet-adapter/src/main/java/com/red/circle/wallet/adapter/app/FreightRestController.java b/rc-service/rc-service-wallet/wallet-adapter/src/main/java/com/red/circle/wallet/adapter/app/FreightRestController.java index d48aadbf..77ea0c4a 100644 --- a/rc-service/rc-service-wallet/wallet-adapter/src/main/java/com/red/circle/wallet/adapter/app/FreightRestController.java +++ b/rc-service/rc-service-wallet/wallet-adapter/src/main/java/com/red/circle/wallet/adapter/app/FreightRestController.java @@ -4,8 +4,10 @@ import com.red.circle.common.business.dto.cmd.AppExtCommand; import com.red.circle.common.business.dto.cmd.app.AppAccountCmd; import com.red.circle.common.business.dto.cmd.app.AppFlowCmd; import com.red.circle.common.business.dto.cmd.app.AppUserIdCmd; +import com.red.circle.component.redis.service.RedisService; import com.red.circle.framework.core.asserts.ResponseAssert; import com.red.circle.framework.web.controller.BaseController; +import com.red.circle.tool.core.date.ZonedDateTimeAsiaRiyadhUtils; import com.red.circle.wallet.app.dto.clientobject.FreightAccountCO; import com.red.circle.wallet.app.dto.clientobject.FreightDealerUserSearchResultCO; import com.red.circle.wallet.app.dto.clientobject.FreightRunningWaterCO; @@ -14,6 +16,9 @@ import com.red.circle.wallet.app.dto.clientobject.FreightSellerCO; import com.red.circle.wallet.app.dto.cmd.*; import com.red.circle.wallet.app.service.UserFreightService; import java.math.BigDecimal; +import java.time.Duration; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import java.util.List; import com.red.circle.wallet.infra.database.rds.entity.freight.FreightBalance; @@ -21,6 +26,7 @@ import com.red.circle.wallet.infra.database.rds.service.freight.FreightBalanceSe import com.red.circle.wallet.inner.error.FreightErrorCode; import com.red.circle.wallet.inner.error.WalletErrorCode; import lombok.RequiredArgsConstructor; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -41,6 +47,7 @@ public class FreightRestController extends BaseController { private final UserFreightService userFreightService; private final FreightBalanceService freightBalanceService; + private final RedisTemplate redisTemplate; /** * 货运账户余额. @@ -123,7 +130,26 @@ public class FreightRestController extends BaseController { @PutMapping("/threshold") public Boolean updateFreightBalance(@RequestBody @Validated FreightBalanceUpdateCmd cmd) { - FreightBalance freightBalance = freightBalanceService.getById(cmd.getReqUserId()); + // 校验日期是否为每月一号 + LocalDate today = LocalDate.now(); + if (today.getDayOfMonth() != 1 && today.getMonthValue() != 12) { + throw new RuntimeException("This operation is only allowed on the 1st of each month"); + } + + String monthKey = "freight:threshold:" + today.format(DateTimeFormatter.ofPattern("yyyy-MM")); + + // 使用 Redis SETNX 实现“当月仅执行一次” + Boolean firstDo = redisTemplate.opsForValue().setIfAbsent( + monthKey, + "done", + Duration.ofDays(today.lengthOfMonth()) + ); + + if (Boolean.FALSE.equals(firstDo)) { + throw new RuntimeException("This operation has been performed this month and cannot be repeated"); + } + + FreightBalance freightBalance = freightBalanceService.getFreightBalance(cmd.getReqUserId()); ResponseAssert.notNull(FreightErrorCode.NOT_FREIGHT, freightBalance); freightBalance.setDeliveryThreshold(cmd.getDeliveryThreshold()); return freightBalanceService.updateSelectiveById(freightBalance); @@ -136,7 +162,7 @@ public class FreightRestController extends BaseController { */ @GetMapping("/threshold") public FreightBalance getFreightThreshold(AppAccountCmd cmd) { - return freightBalanceService.getById(cmd.getReqUserId()); + return freightBalanceService.getFreightBalance(cmd.getReqUserId()); } /** diff --git a/rc-service/rc-service-wallet/wallet-application/src/main/java/com/red/circle/wallet/app/command/freight/SendFreightShipCmdExe.java b/rc-service/rc-service-wallet/wallet-application/src/main/java/com/red/circle/wallet/app/command/freight/SendFreightShipCmdExe.java index e5362cea..f8693e08 100644 --- a/rc-service/rc-service-wallet/wallet-application/src/main/java/com/red/circle/wallet/app/command/freight/SendFreightShipCmdExe.java +++ b/rc-service/rc-service-wallet/wallet-application/src/main/java/com/red/circle/wallet/app/command/freight/SendFreightShipCmdExe.java @@ -102,7 +102,7 @@ public class SendFreightShipCmdExe { ResponseAssert.isTrue(WalletErrorCode.INSUFFICIENT_BALANCE, ArithmeticUtils.gte(cmd.getQuantity(), BigDecimal.ZERO)); - FreightBalance freightBalance = freightBalanceService.getById(cmd.getReqUserId()); + FreightBalance freightBalance = freightBalanceService.getFreightBalance(cmd.getReqUserId()); ResponseAssert.notNull(FreightErrorCode.NOT_FREIGHT, freightBalance); long result = cmd.getQuantity().divide(new BigDecimal(10000), RoundingMode.DOWN).setScale(0, RoundingMode.DOWN).longValue();