管理员每天最多两个小时任务

This commit is contained in:
tianfeng 2025-11-28 14:58:02 +08:00
parent fd006517c8
commit 7a700909a7

View File

@ -381,8 +381,8 @@ public class TaskListener implements MessageListener {
String redisKey = "admin:heartbeat:" + userId;
String incValue = redisService.getString(redisKey);
// 计算当前半月周期的过期时间
long expireSeconds = calculateHalfMonthExpireSeconds();
// 计算到第二天0点的过期时间
long expireSeconds = calculateDailyExpireSeconds();
// 心跳计数每次心跳+1
if (StringUtils.isEmpty(incValue)) {
@ -392,6 +392,13 @@ public class TaskListener implements MessageListener {
} else {
// 递增心跳计数
long currentCount = redisService.increment(redisKey, 1, expireSeconds, TimeUnit.SECONDS);
// 每天最多2小时 = 120次心跳
if (currentCount > 120) {
log.info("管理员麦克风使用时长已达今日上限(2小时),跳过记录, userId={}, currentCount={}", userId, currentCount);
return;
}
if (currentCount % 60 != 0) {
return;
}
@ -489,46 +496,23 @@ public class TaskListener implements MessageListener {
}
}
/**
* 计算当前半月周期的过期时间
* <p>
* 规则
* - 上半月(1-15日)过期时间到16号0点0分0秒
* - 下半月(16日-月末)过期时间到下月1号0点0分0秒
* </p>
*
* @return 距离当前周期结束的秒数
*/
private long calculateHalfMonthExpireSeconds() {
private long calculateDailyExpireSeconds() {
// 使用沙特时区
ZoneId riyadhZone = ZoneId.of("Asia/Riyadh");
ZonedDateTime now = ZonedDateTime.now(riyadhZone);
int currentDay = now.getDayOfMonth();
ZonedDateTime expireTime;
if (currentDay <= 15) {
// 上半月过期到当月16号0点
expireTime = now.withDayOfMonth(16)
.withHour(0)
.withMinute(0)
.withSecond(0)
.withNano(0);
} else {
// 下半月过期到下月1号0点
expireTime = now.plusMonths(1)
.withDayOfMonth(1)
.withHour(0)
.withMinute(0)
.withSecond(0)
.withNano(0);
}
// 过期到第二天0点
ZonedDateTime expireTime = now.plusDays(1)
.withHour(0)
.withMinute(0)
.withSecond(0)
.withNano(0);
// 计算秒数差
long expireSeconds = Duration.between(now, expireTime).getSeconds();
log.debug("计算半月周期过期时间, currentDay={}, expireTime={}, expireSeconds={}",
currentDay, expireTime, expireSeconds);
log.debug("计算每日过期时间, currentTime={}, expireTime={}, expireSeconds={}",
now, expireTime, expireSeconds);
return expireSeconds;
}