huminmin
8 天以前 b48ed5159987e8cee81bc765010cfd9393ad7003
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package cn.iocoder.yudao.module.mes.dal.redis.dv.metering;
 
import cn.iocoder.yudao.module.mes.dal.redis.RedisKeyConstants;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
 
import java.time.Duration;
import java.time.LocalDate;
 
/**
 * MES 计量器具到期提醒 Redis DAO
 *
 * @author 超级管理员
 */
@Repository
public class MesDvMeteringRemindRedisDAO {
 
    /**
     * 提醒去重 KEY 的过期时间(60 天),同一器具同一到期日 60 天内只提醒一次
     */
    private static final Duration REMIND_TIMEOUT = Duration.ofDays(60);
    /**
     * 扫描门闩的过期时间(30 分钟),避免多实例/多次并发重复扫描
     */
    private static final Duration SCAN_LOCK_TIMEOUT = Duration.ofMinutes(30);
 
    @Resource
    private StringRedisTemplate stringRedisTemplate;
 
    /**
     * 尝试标记已提醒(同一器具同一到期日 60 天内只提醒一次)
     *
     * @param meteringId 计量器具编号
     * @param nextCheckDate 到期日
     * @return true 表示首次标记成功,需要发送提醒
     */
    public boolean tryMarkReminded(Long meteringId, LocalDate nextCheckDate) {
        String key = RedisKeyConstants.METERING_EXPIRE_REMIND + meteringId + ":" + nextCheckDate;
        return Boolean.TRUE.equals(stringRedisTemplate.opsForValue().setIfAbsent(key, "1", REMIND_TIMEOUT));
    }
 
    /**
     * 尝试获取当天的扫描门闩,避免多实例并发重复扫描
     *
     * @param today 当天日期
     * @return true 表示获取成功(可以执行扫描)
     */
    public boolean tryAcquireScanLock(LocalDate today) {
        String key = RedisKeyConstants.METERING_EXPIRE_REMIND_SCAN + today;
        return Boolean.TRUE.equals(stringRedisTemplate.opsForValue().setIfAbsent(key, "1", SCAN_LOCK_TIMEOUT));
    }
 
}