From ffa5f49a2bcb6311486d00777b3629538eb3e6f0 Mon Sep 17 00:00:00 2001 From: kongdeqiang <123456> Date: 星期一, 18 三月 2024 17:28:19 +0800 Subject: [PATCH] 新增心跳缓存 --- src/main/java/com/boying/service/impl/BarrierServiceImpl.java | 28 +++++- src/main/java/com/boying/service/impl/OutParkServiceImpl.java | 38 ++++++-- src/main/java/com/boying/controller/car/PlateServlet3.java | 62 +++++++++------ src/main/java/com/boying/service/impl/ParkServiceImpl.java | 23 +++++ src/main/java/com/boying/util/RedisJsonUtil.java | 37 +++++++++ src/main/java/com/boying/service/ParkService.java | 2 src/main/java/com/boying/controller/BarrierController.java | 30 +++++++ src/main/java/com/boying/controller/ParkController.java | 9 ++ src/main/java/com/boying/controller/phone/YCPayController.java | 11 ++ 9 files changed, 198 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/boying/controller/BarrierController.java b/src/main/java/com/boying/controller/BarrierController.java index 3858b25..478572e 100644 --- a/src/main/java/com/boying/controller/BarrierController.java +++ b/src/main/java/com/boying/controller/BarrierController.java @@ -8,11 +8,13 @@ import com.boying.service.BarrierService; import com.boying.service.OutParkService; import com.boying.service.ParkService; +import com.boying.util.RedisJsonUtil; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.io.IOException; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.util.List; @@ -25,6 +27,7 @@ private final BarrierService barrierService; private final OutParkService outParkService; private final ParkService parkService; + private final RedisJsonUtil redisJsonUtil; @PostMapping("findPage") public Object findPage(Page page, String parkId) { @@ -48,12 +51,19 @@ public Object save(Barrier barrier) { barrier.setUpdateTime(LocalDateTime.now()); barrierService.saveOrUpdate(barrier); + try { + redisJsonUtil.set("barrier-"+barrier.getCode(),barrier); + } catch (IOException e) { + e.printStackTrace(); + } return R.ok("淇濆瓨鎴愬姛"); } @PostMapping("delete") public Object delete(Integer id) { + Barrier byId = barrierService.getById(id); barrierService.removeById(id); + redisJsonUtil.del("barrier-"+byId.getCode()); return R.ok("鍒犻櫎鎴愬姛"); } @@ -77,6 +87,11 @@ b.setType2(1); b.setUpdateTime(LocalDateTime.now()); barrierService.saveOrUpdate(b); + try { + redisJsonUtil.set("barrier-"+b.getCode(),b); + } catch (IOException e) { + e.printStackTrace(); + } return R.ok("璇锋眰鎴愬姛"); } @@ -104,6 +119,11 @@ b.setType2(1); b.setUpdateTime(LocalDateTime.now()); barrierService.saveOrUpdate(b); + try { + redisJsonUtil.set("barrier-"+b.getCode(),b); + } catch (IOException e) { + e.printStackTrace(); + } return R.ok("璇锋眰鎴愬姛"); }else { System.out.println("鎵嬪姩寮�鍑哄彛闂�"); @@ -119,12 +139,22 @@ b.setType2(1); b.setUpdateTime(LocalDateTime.now()); barrierService.saveOrUpdate(b); + try { + redisJsonUtil.set("barrier-"+b.getCode(),b); + } catch (IOException e) { + e.printStackTrace(); + } return R.ok("璇锋眰鎴愬姛"); }else { //璁¤垂 b.setType2(1); b.setUpdateTime(LocalDateTime.now()); barrierService.saveOrUpdate(b); + try { + redisJsonUtil.set("barrier-"+b.getCode(),b); + } catch (IOException e) { + e.printStackTrace(); + } return R.ok("璇锋眰鎴愬姛"); } } diff --git a/src/main/java/com/boying/controller/ParkController.java b/src/main/java/com/boying/controller/ParkController.java index dc87fe5..b709e68 100644 --- a/src/main/java/com/boying/controller/ParkController.java +++ b/src/main/java/com/boying/controller/ParkController.java @@ -9,6 +9,7 @@ import com.boying.entity.User; import com.boying.service.ParkService; import com.boying.service.UserService; +import com.boying.util.RedisJsonUtil; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; @@ -17,6 +18,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -32,6 +34,7 @@ private StringRedisTemplate redisTemplate; private final ParkService parkService; private final UserService userService; + private final RedisJsonUtil redisJsonUtil; @PostMapping("/findPage") public Object findPage(Page page) { @@ -58,12 +61,18 @@ redisTemplate.opsForValue().set("car_park_" + park.getId(),"0",30, TimeUnit.DAYS); redisTemplate.opsForValue().set("park_up_" + park.getId(),"true",30, TimeUnit.DAYS); } + try { + redisJsonUtil.set("park-"+park.getId(), park); + } catch (IOException e) { + e.printStackTrace(); + } return R.ok("淇濆瓨鎴愬姛"); } @PostMapping("/delete") public Object delete(Long id) { parkService.removeById(id); + redisJsonUtil.del("park-"+id); return R.ok("鍒犻櫎鎴愬姛"); } diff --git a/src/main/java/com/boying/controller/car/PlateServlet3.java b/src/main/java/com/boying/controller/car/PlateServlet3.java index fc8ce0c..c9ec120 100644 --- a/src/main/java/com/boying/controller/car/PlateServlet3.java +++ b/src/main/java/com/boying/controller/car/PlateServlet3.java @@ -12,6 +12,7 @@ import com.boying.service.*; import com.boying.util.DateUtilOther; import com.boying.util.FileUtil; +import com.boying.util.RedisJsonUtil; import com.boying.util.StringUtil; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -61,6 +62,7 @@ private final FileInfoService fileInfoService; private final TicketBlackService ticketBlackService; private final OutParkLogService outParkLogService; + private final RedisJsonUtil redisJsonUtil; private final String LEDURL = "http://192.168.31.212:9988/LedShow/IGetResultDataAllLine"; /** @@ -140,7 +142,7 @@ @PostMapping("/status") public void heart(String serialno,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { Barrier barrier = barrierService.findByCode(serialno); - Park park = parkService.getById(barrier.getParkId()); + Park park = parkService.getOneById(barrier.getParkId()); String redis = redisTemplate.opsForValue().get("park_up_" + park.getId()); String redisnum = redisTemplate.opsForValue().get("car_park_" + park.getId()); if(barrier!=null){ @@ -171,37 +173,49 @@ barrier.setStatus(0); barrier.setUpdateTime(LocalDateTime.now()); barrierService.saveOrUpdate(barrier); + try { + redisJsonUtil.set("barrier-"+barrier.getCode(),barrier); + } catch (IOException e) { + e.printStackTrace(); + } easyOpen(request,response); }else { //涓嶉渶寮�闂� barrier.setStatus(0); barrier.setUpdateTime(LocalDateTime.now()); barrierService.saveOrUpdate(barrier); - OutPark outPark = outParkService.count4(barrier.getId()); - if(outPark!=null){ - if(barrier.getType() == 0){ - int i = Integer.parseInt(redisnum); - i--; - if(i<0){ - String s= "2."+outPark.getCarNo()+"缂磋垂瀹屾垚锛屽満鍐呭仠杞︽暟涓�"+i+"\n"; - writeTxt2(s); - redisTemplate.opsForValue().set("car_park_" + park.getId(),"0",30, TimeUnit.DAYS); - redisTemplate.opsForValue().set("park_up_" + park.getId(),"true",30, TimeUnit.DAYS); - }else { - String s= "2."+outPark.getCarNo()+"缂磋垂瀹屾垚锛屽満鍐呭仠杞︽暟涓�"+i+"\n"; - writeTxt2(s); - redisTemplate.opsForValue().set("car_park_" + park.getId(),Integer.toString(i),30, TimeUnit.DAYS); - redisTemplate.opsForValue().set("park_up_" + park.getId(),"true",30, TimeUnit.DAYS); + try { + redisJsonUtil.set("barrier-"+barrier.getCode(),barrier); + } catch (IOException e) { + e.printStackTrace(); + } + if(barrier.getType() == 0){ + OutPark outPark = outParkService.count4(barrier.getId()); + if(outPark!=null){ + if(barrier.getType() == 0){ + int i = Integer.parseInt(redisnum); + i--; + if(i<0){ + String s= "2."+outPark.getCarNo()+"缂磋垂瀹屾垚锛屽満鍐呭仠杞︽暟涓�"+i+"\n"; + writeTxt2(s); + redisTemplate.opsForValue().set("car_park_" + park.getId(),"0",30, TimeUnit.DAYS); + redisTemplate.opsForValue().set("park_up_" + park.getId(),"true",30, TimeUnit.DAYS); + }else { + String s= "2."+outPark.getCarNo()+"缂磋垂瀹屾垚锛屽満鍐呭仠杞︽暟涓�"+i+"\n"; + writeTxt2(s); + redisTemplate.opsForValue().set("car_park_" + park.getId(),Integer.toString(i),30, TimeUnit.DAYS); + redisTemplate.opsForValue().set("park_up_" + park.getId(),"true",30, TimeUnit.DAYS); + } } + outPark.setStatus2(1); + outParkService.saveOrUpdate(outPark); + easyOpen(request,response); + OutParkLog byOutPark = outParkLogService.getByOutPark(outPark.getCarNo(), outPark.getId()); + byOutPark.setOpenTime(LocalDateTime.now()); + outParkLogService.saveOrUpdate(byOutPark); + enterParkService.deleteByCarNo(outPark.getCarNo(), barrier.getParkId()); + redisJsonUtil.del("outPark-"+outPark.getBarrierId()); } - outPark.setStatus2(1); - outParkService.saveOrUpdate(outPark); - easyOpen(request,response); - easyOpen(request,response); - OutParkLog byOutPark = outParkLogService.getByOutPark(outPark.getCarNo(), outPark.getId()); - byOutPark.setOpenTime(LocalDateTime.now()); - outParkLogService.saveOrUpdate(byOutPark); - enterParkService.deleteByCarNo(outPark.getCarNo(), barrier.getParkId()); } if(barrier.getType() == 1){ boolean in = barrierService.getDateDifIn(barrier.getId()); diff --git a/src/main/java/com/boying/controller/phone/YCPayController.java b/src/main/java/com/boying/controller/phone/YCPayController.java index 368f811..f5ec8c0 100644 --- a/src/main/java/com/boying/controller/phone/YCPayController.java +++ b/src/main/java/com/boying/controller/phone/YCPayController.java @@ -14,9 +14,11 @@ import com.boying.service.*; import com.boying.util.DateUtilOther; import com.boying.util.HTTPEntityUtil; +import com.boying.util.RedisJsonUtil; import com.google.gson.Gson; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -36,6 +38,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; /** * @author kdq @@ -60,6 +63,8 @@ private final EnterParkService enterParkService; private final WXService wxService; private final OutParkLogService outParkLogService; + private final RedisJsonUtil redisJsonUtil; + /** * 鏌ヨ璁㈠崟鎺ュ彛 @@ -680,6 +685,12 @@ if(status.equals("03")){ outPark.setStatus(1); outParkService.saveOrUpdate(outPark); + //缂撳瓨鍦╮edis閲� + try { + redisJsonUtil.set("outPark-"+outPark.getBarrierId(), outPark); + } catch (IOException e) { + e.printStackTrace(); + } } } } diff --git a/src/main/java/com/boying/service/ParkService.java b/src/main/java/com/boying/service/ParkService.java index edad453..86d4b18 100644 --- a/src/main/java/com/boying/service/ParkService.java +++ b/src/main/java/com/boying/service/ParkService.java @@ -12,4 +12,6 @@ */ public interface ParkService extends IService<Park> { int count1(); + + Park getOneById(Integer parkId); } diff --git a/src/main/java/com/boying/service/impl/BarrierServiceImpl.java b/src/main/java/com/boying/service/impl/BarrierServiceImpl.java index 1d83789..6303eac 100644 --- a/src/main/java/com/boying/service/impl/BarrierServiceImpl.java +++ b/src/main/java/com/boying/service/impl/BarrierServiceImpl.java @@ -14,10 +14,12 @@ import com.boying.mapper.ParkMapper; import com.boying.service.BarrierService; import com.boying.service.EnterParkService; +import com.boying.util.RedisJsonUtil; import lombok.AllArgsConstructor; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; +import java.io.IOException; import java.time.ZoneId; import java.util.Date; import java.util.List; @@ -37,17 +39,29 @@ private OutParkMapper outParkMapper; private StringRedisTemplate redisTemplate; private ParkMapper parkMapper; + private RedisJsonUtil redisJsonUtil; @Override public Barrier findByCode(String code) { - 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 { + barrier = redisJsonUtil.get("barrier-"+code,Barrier.class); + if(barrier != null){ + return barrier; + }else { + QueryWrapper<Barrier> wrapper = new QueryWrapper<>(); + wrapper.lambda() + .eq(Barrier::getCode,code); + List<Barrier> list = list(wrapper); + if(list.size()>0){ + redisJsonUtil.set("barrier-"+code,list.get(0)); + return list.get(0); + } + } + } catch (IOException e) { + e.printStackTrace(); return null; } + return null; } @Override diff --git a/src/main/java/com/boying/service/impl/OutParkServiceImpl.java b/src/main/java/com/boying/service/impl/OutParkServiceImpl.java index fdfe4f7..a18bfbc 100644 --- a/src/main/java/com/boying/service/impl/OutParkServiceImpl.java +++ b/src/main/java/com/boying/service/impl/OutParkServiceImpl.java @@ -4,14 +4,17 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.boying.entity.OutPark; +import com.boying.entity.Park; import com.boying.entity.Statistic; import com.boying.mapper.OutParkMapper; import com.boying.mapper.StatisticMapper; import com.boying.service.OutParkService; import com.boying.service.StatisticService; +import com.boying.util.RedisJsonUtil; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; +import java.io.IOException; import java.util.Date; import java.util.List; import java.util.Map; @@ -27,6 +30,7 @@ @AllArgsConstructor public class OutParkServiceImpl extends ServiceImpl<OutParkMapper, OutPark> implements OutParkService { private final OutParkMapper outParkMapper; + private final RedisJsonUtil redisJsonUtil; @Override public int count1() { @@ -109,18 +113,30 @@ @Override public OutPark count4(Integer bId) { - 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 { + outPark = redisJsonUtil.get("outPark-"+bId, OutPark.class); + if(outPark != null){ + 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){ + redisJsonUtil.set("outPark-"+bId, outParks.get(0)); + return outParks.get(0); + }else{ + return null; + } + } + } catch (IOException e) { + e.printStackTrace(); } + return null; } @Override diff --git a/src/main/java/com/boying/service/impl/ParkServiceImpl.java b/src/main/java/com/boying/service/impl/ParkServiceImpl.java index 4e0d8be..320a6a9 100644 --- a/src/main/java/com/boying/service/impl/ParkServiceImpl.java +++ b/src/main/java/com/boying/service/impl/ParkServiceImpl.java @@ -2,15 +2,19 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.boying.entity.Barrier; import com.boying.entity.Park; import com.boying.entity.Ticket; import com.boying.mapper.ParkMapper; import com.boying.mapper.TicketMapper; import com.boying.service.ParkService; import com.boying.service.TicketService; +import com.boying.util.RedisJsonUtil; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; +import java.io.IOException; +import java.util.List; import java.util.Map; /** @@ -24,6 +28,7 @@ @AllArgsConstructor public class ParkServiceImpl extends ServiceImpl<ParkMapper, Park> implements ParkService { private final ParkMapper parkMapper; + private final RedisJsonUtil redisJsonUtil; @Override public int count1() { QueryWrapper<Park> wrapper = new QueryWrapper<>(); @@ -36,4 +41,22 @@ return sum.intValue(); } } + + @Override + public Park getOneById(Integer parkId) { + Park park = null; + try { + park = redisJsonUtil.get("park-"+parkId, Park.class); + if(park != null){ + return park; + }else { + park = getById(parkId); + redisJsonUtil.set("park-"+parkId, park); + return park; + } + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } } diff --git a/src/main/java/com/boying/util/RedisJsonUtil.java b/src/main/java/com/boying/util/RedisJsonUtil.java new file mode 100644 index 0000000..9307bf4 --- /dev/null +++ b/src/main/java/com/boying/util/RedisJsonUtil.java @@ -0,0 +1,37 @@ +package com.boying.util; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.ibatis.jdbc.Null; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.io.IOException; + +/** + * @author kdq + * @version 1.0.0 + * @ClassName RedisJsonUtil.java + * @Description TODO + * @createTime 2024骞�03鏈�18鏃� 15:20:00 + */ +@Component +public class RedisJsonUtil { + @Resource + private StringRedisTemplate stringRedisTemplate; + + private final ObjectMapper objectMapper = new ObjectMapper(); + + public <T> void set(String key, T value) throws IOException { + String jsonValue = objectMapper.writeValueAsString(value); + stringRedisTemplate.opsForValue().set(key, jsonValue); + } + + public <T> T get(String key, Class<T> type) throws IOException { + String jsonValue = stringRedisTemplate.opsForValue().get(key); + return jsonValue == null ? null : objectMapper.readValue(jsonValue, type); + } + public void del(String key) { + stringRedisTemplate.delete(key); + } +} -- Gitblit v1.9.1