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 77ea0c4a..b683ddb3 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 @@ -18,7 +18,9 @@ import com.red.circle.wallet.app.service.UserFreightService; import java.math.BigDecimal; import java.time.Duration; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.time.temporal.TemporalAdjusters; import java.util.List; import com.red.circle.wallet.infra.database.rds.entity.freight.FreightBalance; @@ -136,13 +138,19 @@ public class FreightRestController extends BaseController { throw new RuntimeException("This operation is only allowed on the 1st of each month"); } - String monthKey = "freight:threshold:" + today.format(DateTimeFormatter.ofPattern("yyyy-MM")); + String monthKey = "freight:threshold:" + cmd.getReqUserId() + ":" + today.format(DateTimeFormatter.ofPattern("yyyy-MM")); + LocalDate lastDay = today.with(TemporalAdjusters.lastDayOfMonth()); + LocalDateTime endOfMonth = lastDay.atTime(23, 59, 59); + long ttl = Duration.between(LocalDateTime.now(), endOfMonth).getSeconds(); + if (ttl < 1) { + ttl = 1; + } // 使用 Redis SETNX 实现“当月仅执行一次” Boolean firstDo = redisTemplate.opsForValue().setIfAbsent( monthKey, "done", - Duration.ofDays(today.lengthOfMonth()) + Duration.ofSeconds(ttl) ); if (Boolean.FALSE.equals(firstDo)) { @@ -162,7 +170,12 @@ public class FreightRestController extends BaseController { */ @GetMapping("/threshold") public FreightBalance getFreightThreshold(AppAccountCmd cmd) { - return freightBalanceService.getFreightBalance(cmd.getReqUserId()); + FreightBalance freightBalance = freightBalanceService.getFreightBalance(cmd.getReqUserId()); + if (freightBalance != null) { + String monthKey = "freight:threshold:" + cmd.getReqUserId() + ":" + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM")); + freightBalance.setSettinged(redisTemplate.opsForValue().get(monthKey) != null); + } + return freightBalance; } /** diff --git a/rc-service/rc-service-wallet/wallet-infrastructure/src/main/java/com/red/circle/wallet/infra/database/rds/entity/freight/FreightBalance.java b/rc-service/rc-service-wallet/wallet-infrastructure/src/main/java/com/red/circle/wallet/infra/database/rds/entity/freight/FreightBalance.java index bb6e9160..42aeb0b1 100644 --- a/rc-service/rc-service-wallet/wallet-infrastructure/src/main/java/com/red/circle/wallet/infra/database/rds/entity/freight/FreightBalance.java +++ b/rc-service/rc-service-wallet/wallet-infrastructure/src/main/java/com/red/circle/wallet/infra/database/rds/entity/freight/FreightBalance.java @@ -112,4 +112,10 @@ public class FreightBalance extends TimestampBaseEntity { @TableField("delivery_threshold") private Integer deliveryThreshold; + /** + * 是否设置过 + */ + @TableField(exist = false) + private Boolean settinged; + }