设置过状态处理

This commit is contained in:
tianfeng 2025-12-05 19:56:03 +08:00
parent 70742b3362
commit 5a6d1dfb64
2 changed files with 22 additions and 3 deletions

View File

@ -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;
}
/**

View File

@ -112,4 +112,10 @@ public class FreightBalance extends TimestampBaseEntity {
@TableField("delivery_threshold")
private Integer deliveryThreshold;
/**
* 是否设置过
*/
@TableField(exist = false)
private Boolean settinged;
}