From 0ef280ca1db4c8f280be5030ccfea35441ca1e51 Mon Sep 17 00:00:00 2001
From: 彭雪彬 <1724387007@qq.com>
Date: Wed, 16 Jul 2025 11:24:29 +0800
Subject: [PATCH] 骑手同步数接口修改

---
 oying-common/src/main/java/com/oying/utils/RedisUtils.java |   82 ++++++++++++++++++++++++++++++++++++++---
 1 files changed, 76 insertions(+), 6 deletions(-)

diff --git a/oying-common/src/main/java/com/oying/utils/RedisUtils.java b/oying-common/src/main/java/com/oying/utils/RedisUtils.java
index ccd9126..361c573 100644
--- a/oying-common/src/main/java/com/oying/utils/RedisUtils.java
+++ b/oying-common/src/main/java/com/oying/utils/RedisUtils.java
@@ -5,11 +5,14 @@
 import com.google.common.collect.Sets;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.redis.connection.RedisConnection;
 import org.springframework.data.redis.connection.RedisConnectionFactory;
 import org.springframework.data.redis.core.*;
 import org.springframework.data.redis.serializer.StringRedisSerializer;
 import org.springframework.stereotype.Component;
+
+import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
@@ -18,16 +21,59 @@
  * @author Z
  */
 @Component
-@SuppressWarnings({"unchecked","all"})
+@SuppressWarnings({"unchecked", "all"})
 public class RedisUtils {
     private static final Logger log = LoggerFactory.getLogger(RedisUtils.class);
-
+    @Value("${jwt.generate-order-sn}")
+    private String generateOrderSn;
+    @Value("${wx.enabled}")
+    private Boolean wxEnabled;
+    private static final String T = "T-";
+    private static final String OY = "OY-";
     private RedisTemplate<Object, Object> redisTemplate;
 
     public RedisUtils(RedisTemplate<Object, Object> redisTemplate) {
         this.redisTemplate = redisTemplate;
         this.redisTemplate.setKeySerializer(new StringRedisSerializer());
         this.redisTemplate.setHashKeySerializer(new StringRedisSerializer());
+    }
+
+    /**
+     * 生成18位订单编号:8位日期+4位秒+7位以上自增id
+     *
+     * @param i
+     * @return
+     */
+    public String generateOrderSn(Integer i) {
+        StringBuilder sb = new StringBuilder();
+        String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
+        String key = generateOrderSn + i + date;
+        Long increment = increment(key);
+        sb.append(date);
+        sb.append(String.format("%04d", i));
+        String incrementStr = increment.toString();
+        if (incrementStr.length() <= 7) {
+            sb.append(String.format("%07d", increment));
+        } else {
+            sb.append(incrementStr);
+        }
+        if (wxEnabled) {
+            // 生产环境
+            return OY + sb.toString();
+        } else {
+            // 测试环境
+            return T + sb.toString();
+        }
+    }
+
+    /**
+     * 判断key是否过期
+     *
+     * @param key
+     * @return
+     */
+    public boolean isExpire(Object key) {
+        return getExpire(key) > 1 ? false : true;
     }
 
     /**
@@ -182,9 +228,10 @@
 
     /**
      * 批量模糊删除key
+     *
      * @param pattern
      */
-    public void scanDel(String pattern){
+    public void scanDel(String pattern) {
         ScanOptions options = ScanOptions.scanOptions().match(pattern).build();
         try (Cursor<byte[]> cursor = redisTemplate.executeWithStickyConnection(
                 (RedisCallback<Cursor<byte[]>>) connection -> (Cursor<byte[]>) new ConvertingCursor<>(
@@ -228,7 +275,7 @@
     /**
      * 普通缓存获取
      *
-     * @param key 键
+     * @param key   键
      * @param clazz 列表中元素的类型
      * @return 值
      */
@@ -255,7 +302,7 @@
      * @return 值
      */
     public String getStr(String key) {
-        if(StrUtil.isBlank(key)){
+        if (StrUtil.isBlank(key)) {
             return null;
         }
         Object value = redisTemplate.opsForValue().get(key);
@@ -275,7 +322,7 @@
     public List<Object> multiGet(List<String> keys) {
         List list = redisTemplate.opsForValue().multiGet(Sets.newHashSet(keys));
         List resultList = Lists.newArrayList();
-        Optional.ofNullable(list).ifPresent(e-> list.forEach(ele-> Optional.ofNullable(ele).ifPresent(resultList::add)));
+        Optional.ofNullable(list).ifPresent(e -> list.forEach(ele -> Optional.ofNullable(ele).ifPresent(resultList::add)));
         return resultList;
     }
 
@@ -345,6 +392,27 @@
         }
     }
 
+    /**
+     * 分布式ID生成器
+     *
+     * @param key   键
+     * @param value 值
+     * @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期,注意:这里将会替换原有的时间
+     * @return true成功 false 失败
+     */
+    public boolean setIfAbsent(String key, Object value, long time) {
+        try {
+            if (time > 0) {
+                Boolean result = redisTemplate.opsForValue().setIfAbsent(key, value, time, TimeUnit.SECONDS);
+                return result;
+            } else {
+                return false;
+            }
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+            return false;
+        }
+    }
     // ================================Map=================================
 
     /**
@@ -771,6 +839,7 @@
 
     /**
      * 递增
+     *
      * @param key
      * @return
      */
@@ -780,6 +849,7 @@
 
     /**
      * 递减
+     *
      * @param key
      * @return
      */

--
Gitblit v1.9.3