From bccd25039a08f8833b72ff906d156da63018db98 Mon Sep 17 00:00:00 2001
From: kongdeqiang <123456>
Date: 星期二, 19 三月 2024 15:10:13 +0800
Subject: [PATCH] 新增心跳缓存

---
 src/main/java/com/boying/service/impl/BarrierServiceImpl.java  |  104 +++++++++++++++----------
 src/main/java/com/boying/service/impl/OutParkServiceImpl.java  |   63 ++++++---------
 src/main/java/com/boying/controller/BarrierController.java     |   34 +++++---
 src/main/java/com/boying/controller/phone/YCPayController.java |    4 
 4 files changed, 109 insertions(+), 96 deletions(-)

diff --git a/src/main/java/com/boying/controller/BarrierController.java b/src/main/java/com/boying/controller/BarrierController.java
index 478572e..7089007 100644
--- a/src/main/java/com/boying/controller/BarrierController.java
+++ b/src/main/java/com/boying/controller/BarrierController.java
@@ -1,5 +1,6 @@
 package com.boying.controller;
 
+import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.boying.common.R;
@@ -10,6 +11,7 @@
 import com.boying.service.ParkService;
 import com.boying.util.RedisJsonUtil;
 import lombok.RequiredArgsConstructor;
+import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
@@ -27,7 +29,8 @@
     private final BarrierService barrierService;
     private final OutParkService outParkService;
     private final ParkService parkService;
-    private final RedisJsonUtil redisJsonUtil;
+    private StringRedisTemplate redisTemplate;
+
 
     @PostMapping("findPage")
     public Object findPage(Page page, String parkId) {
@@ -39,7 +42,7 @@
         for(Barrier barrier:records){
             long l = System.currentTimeMillis() - barrier.getUpdateTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
             System.out.println(l);
-            if(l>10000){
+            if(l>20000){
                 barrier.setStatus(1);
             }
         }
@@ -52,8 +55,9 @@
         barrier.setUpdateTime(LocalDateTime.now());
         barrierService.saveOrUpdate(barrier);
         try {
-            redisJsonUtil.set("barrier-"+barrier.getCode(),barrier);
-        } catch (IOException e) {
+            String jsonValue = JSON.toJSONString(barrier);
+            redisTemplate.opsForValue().set("barrier-"+barrier.getCode(), jsonValue);
+        } catch (Exception e) {
             e.printStackTrace();
         }
         return R.ok("淇濆瓨鎴愬姛");
@@ -63,7 +67,7 @@
     public Object delete(Integer id) {
         Barrier byId = barrierService.getById(id);
         barrierService.removeById(id);
-        redisJsonUtil.del("barrier-"+byId.getCode());
+        redisTemplate.delete("barrier-"+byId.getCode());
         return R.ok("鍒犻櫎鎴愬姛");
     }
 
@@ -88,8 +92,9 @@
         b.setUpdateTime(LocalDateTime.now());
         barrierService.saveOrUpdate(b);
         try {
-            redisJsonUtil.set("barrier-"+b.getCode(),b);
-        } catch (IOException e) {
+            String jsonValue = JSON.toJSONString(b);
+            redisTemplate.opsForValue().set("barrier-"+b.getCode(), jsonValue);
+        } catch (Exception e) {
             e.printStackTrace();
         }
         return R.ok("璇锋眰鎴愬姛");
@@ -120,8 +125,9 @@
                b.setUpdateTime(LocalDateTime.now());
                barrierService.saveOrUpdate(b);
                try {
-                   redisJsonUtil.set("barrier-"+b.getCode(),b);
-               } catch (IOException e) {
+                   String jsonValue = JSON.toJSONString(b);
+                   redisTemplate.opsForValue().set("barrier-"+b.getCode(), jsonValue);
+               } catch (Exception e) {
                    e.printStackTrace();
                }
                return R.ok("璇锋眰鎴愬姛");
@@ -140,8 +146,9 @@
                    b.setUpdateTime(LocalDateTime.now());
                    barrierService.saveOrUpdate(b);
                    try {
-                       redisJsonUtil.set("barrier-"+b.getCode(),b);
-                   } catch (IOException e) {
+                       String jsonValue = JSON.toJSONString(b);
+                       redisTemplate.opsForValue().set("barrier-"+b.getCode(), jsonValue);
+                   } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return R.ok("璇锋眰鎴愬姛");
@@ -151,8 +158,9 @@
                    b.setUpdateTime(LocalDateTime.now());
                    barrierService.saveOrUpdate(b);
                    try {
-                       redisJsonUtil.set("barrier-"+b.getCode(),b);
-                   } catch (IOException e) {
+                       String jsonValue = JSON.toJSONString(b);
+                       redisTemplate.opsForValue().set("barrier-"+b.getCode(), jsonValue);
+                   } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return R.ok("璇锋眰鎴愬姛");
diff --git a/src/main/java/com/boying/controller/phone/YCPayController.java b/src/main/java/com/boying/controller/phone/YCPayController.java
index a517dc6..f2872ab 100644
--- a/src/main/java/com/boying/controller/phone/YCPayController.java
+++ b/src/main/java/com/boying/controller/phone/YCPayController.java
@@ -682,8 +682,8 @@
                 outPark.setStatus(1);
                 outParkService.saveOrUpdate(outPark);
                 //缂撳瓨鍦╮edis閲�
-//                String jsonValue = JSON.toJSONString(outPark);
-//                redisTemplate.opsForValue().set("outPark-"+outPark.getBarrierId(), jsonValue);
+                String jsonValue = JSON.toJSONString(outPark);
+                redisTemplate.opsForValue().set("outPark-"+outPark.getBarrierId(), jsonValue);
             }
         }
     }
diff --git a/src/main/java/com/boying/service/impl/BarrierServiceImpl.java b/src/main/java/com/boying/service/impl/BarrierServiceImpl.java
index 831e7eb..bb86265 100644
--- a/src/main/java/com/boying/service/impl/BarrierServiceImpl.java
+++ b/src/main/java/com/boying/service/impl/BarrierServiceImpl.java
@@ -4,6 +4,7 @@
 import cn.hutool.core.date.DateUtil;
 import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.boying.entity.Barrier;
 import com.boying.entity.EnterPark;
@@ -45,38 +46,38 @@
 
     @Override
     public Barrier findByCode(String code) {
-//        Barrier barrier = null;
-//        try {
-//           String s  =  redisTemplate.opsForValue().get("barrier-"+code);
-//           if(!StringUtil.isNullOrEmpty(s)){
-//              barrier =  JSON.parseObject(s, Barrier.class);
-//               return barrier;
-//           }else {
-//               QueryWrapper<Barrier> wrapper = new QueryWrapper<>();
-//               wrapper.lambda()
-//                       .eq(Barrier::getCode,code);
-//               List<Barrier> list = list(wrapper);
-//               if(list.size()>0){
-//                   String jsonValue = JSON.toJSONString(list.get(0));
-//                   redisTemplate.opsForValue().set("barrier-"+code, jsonValue);
-//                   return list.get(0);
-//               }
-//           }
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//            return null;
-//        }
-//      return null;
-
-        QueryWrapper<Barrier> wrapper = new QueryWrapper<>();
-        wrapper.lambda()
-                .eq(Barrier::getCode,code);
-        List<Barrier> list = list(wrapper);
-        if(list.size()>0){
-            return list.get(0);
-        }else {
+        Barrier barrier = null;
+        try {
+           String s  =  redisTemplate.opsForValue().get("barrier-"+code);
+           if(!StringUtil.isNullOrEmpty(s)){
+              barrier =  JSON.parseObject(s, Barrier.class);
+               return barrier;
+           }else {
+               QueryWrapper<Barrier> wrapper = new QueryWrapper<>();
+               wrapper.lambda()
+                       .eq(Barrier::getCode,code);
+               List<Barrier> list = list(wrapper);
+               if(list.size()>0){
+                   String jsonValue = JSON.toJSONString(list.get(0));
+                   redisTemplate.opsForValue().set("barrier-"+code, jsonValue);
+                   return list.get(0);
+               }
+           }
+        } catch (Exception e) {
+            e.printStackTrace();
             return null;
         }
+      return null;
+
+//        QueryWrapper<Barrier> wrapper = new QueryWrapper<>();
+//        wrapper.lambda()
+//                .eq(Barrier::getCode,code);
+//        List<Barrier> list = list(wrapper);
+//        if(list.size()>0){
+//            return list.get(0);
+//        }else {
+//            return null;
+//        }
     }
 
     @Override
@@ -93,13 +94,22 @@
     }
 
     @Override
-    public boolean getDateDifIn(Integer barrierId) {
-        QueryWrapper<EnterPark> wrapper = new QueryWrapper<>();
-        wrapper.lambda()
-                .eq(EnterPark::getBarrierId,barrierId)
-                .orderByDesc(EnterPark::getId)
-                .last(" limit 1");
-        EnterPark enterPark = enterParkMapper.selectOne(wrapper);
+    public boolean getDateDifIn(Integer parkId) {
+        EnterPark enterPark=null;
+        String s1 = redisTemplate.opsForValue().get("enterPark_dif_" + parkId);
+        if(StringUtils.isBlank(s1)){
+            QueryWrapper<EnterPark> wrapper = new QueryWrapper<>();
+            wrapper.lambda()
+                    .eq(EnterPark::getParkId,parkId)
+                    .orderByDesc(EnterPark::getId)
+                    .last(" limit 1");
+            enterPark = enterParkMapper.selectOne(wrapper);
+            String jsonValue = JSON.toJSONString(enterPark);
+            redisTemplate.opsForValue().set("enterPark_dif_"+ parkId, jsonValue);
+        }else {
+            enterPark = JSON.parseObject(s1, EnterPark.class);
+        }
+
         if(enterPark != null){
             long dif = DateUtil.between(Date.from( enterPark.getCreateTime().atZone( ZoneId.systemDefault()).toInstant()), new Date(), DateUnit.SECOND, false);
             if(dif >= 5 && dif <= 7){
@@ -123,12 +133,20 @@
 
     @Override
     public boolean getDateDifOut(Integer barrierId) {
-        QueryWrapper<OutPark> wrapper = new QueryWrapper<>();
-        wrapper.lambda()
-                .eq(OutPark::getBarrierId,barrierId)
-                .orderByDesc(OutPark::getId)
-                .last(" limit 1");
-        OutPark outPark = outParkMapper.selectOne(wrapper);
+        OutPark outPark=null;
+        String s1 = redisTemplate.opsForValue().get("outPark_dif_" + barrierId);
+        if(StringUtils.isBlank(s1)){
+            QueryWrapper<OutPark> wrapper = new QueryWrapper<>();
+            wrapper.lambda()
+                    .eq(OutPark::getBarrierId,barrierId)
+                    .orderByDesc(OutPark::getId)
+                    .last(" limit 1");
+            outPark = outParkMapper.selectOne(wrapper);
+            String jsonValue = JSON.toJSONString(outPark);
+            redisTemplate.opsForValue().set("outPark_dif_"+ barrierId, jsonValue);
+        }else {
+            outPark = JSON.parseObject(s1, OutPark.class);
+        }
         if(outPark != null){
             long dif = DateUtil.between(Date.from( outPark.getCreateTime().atZone( ZoneId.systemDefault()).toInstant()), new Date(), DateUnit.SECOND, false);
             if(dif >= 20 && dif <= 23){
diff --git a/src/main/java/com/boying/service/impl/OutParkServiceImpl.java b/src/main/java/com/boying/service/impl/OutParkServiceImpl.java
index affe3db..bd3bbbc 100644
--- a/src/main/java/com/boying/service/impl/OutParkServiceImpl.java
+++ b/src/main/java/com/boying/service/impl/OutParkServiceImpl.java
@@ -118,45 +118,32 @@
 
     @Override
     public OutPark count4(Integer bId) {
-//        OutPark outPark = null;
-//        try {
-//            String s  =  redisTemplate.opsForValue().get("outPark-"+bId);
-//            if(!StringUtil.isNullOrEmpty(s)){
-//                outPark =  JSON.parseObject(s, OutPark.class);
-//                return outPark;
-//            }else {
-//                QueryWrapper<OutPark> wrapper = new QueryWrapper<>();
-//                wrapper.lambda()
-//                        .eq(OutPark::getBarrierId,bId)
-//                        .eq(OutPark::getStatus,1)
-//                        .eq(OutPark::getStatus2,0)
-//                        .orderByDesc(OutPark::getCreateTime);
-//                List<OutPark> outParks = outParkMapper.selectList(wrapper);
-//                if(outParks.size()>0){
-//                    String jsonValue = JSON.toJSONString(outParks.get(0));
-//                    redisTemplate.opsForValue().set("outPark-"+bId, jsonValue);
-//                    return outParks.get(0);
-//                }else{
-//                    return null;
-//                }
-//            }
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//        }
-//        return null;
-
-        QueryWrapper<OutPark> wrapper = new QueryWrapper<>();
-        wrapper.lambda()
-                .eq(OutPark::getBarrierId,bId)
-                .eq(OutPark::getStatus,1)
-                .eq(OutPark::getStatus2,0)
-                .orderByDesc(OutPark::getCreateTime);
-        List<OutPark> outParks = outParkMapper.selectList(wrapper);
-        if(outParks.size()>0){
-            return outParks.get(0);
-        }else{
-            return null;
+        OutPark outPark = null;
+        try {
+            String s  =  redisTemplate.opsForValue().get("outPark-"+bId);
+            if(!StringUtil.isNullOrEmpty(s)){
+                outPark =  JSON.parseObject(s, OutPark.class);
+                return outPark;
+            }else {
+                return null;
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
         }
+        return null;
+//
+//        QueryWrapper<OutPark> wrapper = new QueryWrapper<>();
+//        wrapper.lambda()
+//                .eq(OutPark::getBarrierId,bId)
+//                .eq(OutPark::getStatus,1)
+//                .eq(OutPark::getStatus2,0)
+//                .orderByDesc(OutPark::getCreateTime);
+//        List<OutPark> outParks = outParkMapper.selectList(wrapper);
+//        if(outParks.size()>0){
+//            return outParks.get(0);
+//        }else{
+//            return null;
+//        }
     }
 
     @Override

--
Gitblit v1.9.1