From 5a6d1dfb6422e13b8d4aa724a2b900ad9b07c1a9 Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Fri, 5 Dec 2025 19:56:03 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E8=BF=87=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../adapter/app/FreightRestController.java | 19 ++++++++++++++++--- .../rds/entity/freight/FreightBalance.java | 6 ++++++ 2 files changed, 22 insertions(+), 3 deletions(-) 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; + }