From bd9b6a58beb6b478ad27d645aa1cc0da99c25d7d Mon Sep 17 00:00:00 2001 From: 青城 <1662047068@qq.com> Date: 星期三, 09 七月 2025 18:02:37 +0800 Subject: [PATCH] 协调审批 --- src/main/java/com/ruoyi/approve/utils/DailyRedisCounter.java | 72 ++++++++++++++++++++++++++++++++++++ 1 files changed, 72 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/ruoyi/approve/utils/DailyRedisCounter.java b/src/main/java/com/ruoyi/approve/utils/DailyRedisCounter.java new file mode 100644 index 0000000..be00664 --- /dev/null +++ b/src/main/java/com/ruoyi/approve/utils/DailyRedisCounter.java @@ -0,0 +1,72 @@ +package com.ruoyi.approve.utils; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.concurrent.TimeUnit; + + +//鍩轰簬redis鐨勪竴涓瘡鏃ヨ鏁板櫒 +@Component +public class DailyRedisCounter { + private static final String KEY_PREFIX = "daily_counter:"; + private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyyMMdd"); + private final StringRedisTemplate redisTemplate; + + public DailyRedisCounter(StringRedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } + + /** + * 鑾峰彇鎸囧畾璁℃暟鍣ㄥ湪浠婃棩鐨勬暟鍊硷紝骞惰嚜澧�1 + * @param counterName 璁℃暟鍣ㄥ悕绉帮紙渚嬪锛歭ogin_count銆乷rder_count锛� + * @return 浠婃棩鑷鍚庣殑璁℃暟鍊� + */ + public long incrementAndGet(String counterName) { + String key = getKey(counterName); + long count = redisTemplate.opsForValue().increment(key, 1); + + // 浠呭湪绗竴娆¤缃椂璁剧疆杩囨湡鏃堕棿锛堥伩鍏嶉噸澶嶈缃級 +// if (count == 0) { +// long secondsUntilMidnight = calculateSecondsUntilMidnight(); +// redisTemplate.expire(key, secondsUntilMidnight, TimeUnit.SECONDS); +// } + + return count; + } + + /** + * 鑾峰彇鎸囧畾璁℃暟鍣ㄥ湪浠婃棩鐨勫綋鍓嶆暟鍊� + * @param counterName 璁℃暟鍣ㄥ悕绉� + * @return 浠婃棩褰撳墠璁℃暟鍊硷紝鑻ヤ笉瀛樺湪鍒欒繑鍥�0 + */ + public long getCurrentCount(String counterName) { + String key = getKey(counterName); + String value = redisTemplate.opsForValue().get(key); + return value != null ? Long.parseLong(value) : 0; + } + + /** + * 璁$畻璺濈娆℃棩鍑屾櫒鐨勭鏁� + */ + private long calculateSecondsUntilMidnight() { + LocalDate tomorrow = LocalDate.now().plusDays(1); + LocalDate midnight = tomorrow.atStartOfDay().toLocalDate(); + return java.time.Duration.between( + LocalDate.now().atTime(23, 59, 59), + midnight.atTime(0, 0, 0) + ).getSeconds() + 1; + } + + /** + * 鐢熸垚Redis閿� + */ + private String getKey(String counterName) { + String today = LocalDate.now().format(DATE_FORMAT); + return KEY_PREFIX + counterName + ":" + today; + } +} -- Gitblit v1.9.3