From 0cfceeb72a3ebc59da573d0fa73d5c831662f344 Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Tue, 6 May 2025 21:12:50 +0800 Subject: [PATCH] =?UTF-8?q?refactor(smartCam):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E8=BD=A6=E7=89=8C=E5=8F=B7=E6=9B=B4=E6=96=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 SmartCamCarDataService 中添加车牌号更新逻辑 - 在 SmartCamCarLicenseSnapshotDataService 中实现批量修改功能- 优化代码格式和可读性 --- .../service/SmartCamCarDataService.java | 7 + ...SmartCamCarLicenseSnapshotDataService.java | 215 +++++++++++------- 2 files changed, 144 insertions(+), 78 deletions(-) diff --git a/src/main/java/cn/lihongjie/coal/smartCamCarData/service/SmartCamCarDataService.java b/src/main/java/cn/lihongjie/coal/smartCamCarData/service/SmartCamCarDataService.java index 21f07ec7..96e98ea4 100644 --- a/src/main/java/cn/lihongjie/coal/smartCamCarData/service/SmartCamCarDataService.java +++ b/src/main/java/cn/lihongjie/coal/smartCamCarData/service/SmartCamCarDataService.java @@ -363,6 +363,7 @@ public class SmartCamCarDataService SmartCamCarDataEntity entity = this.repository.get(request.getId()); + String number = entity.getNumber(); this.repository.save(entity); if (entity.getEntry() != null) { @@ -377,6 +378,12 @@ public class SmartCamCarDataService request.setId(entity.getExit().getId()); smartCamCarLicenseSnapshotDataService.humanModify(request); } + + smartCamCarLicenseSnapshotDataService.batchModify( + request.getNumber(), + number, + entity.getEntry() != null ? entity.getEntry().getInfoTimeObj() : null, + entity.getExit() != null ? entity.getExit().getInfoTimeObj() : null, entity.getOrganizationId()); } public void merge(MergeRequest request) { diff --git a/src/main/java/cn/lihongjie/coal/smartCamCarLicenseSnapshotData/service/SmartCamCarLicenseSnapshotDataService.java b/src/main/java/cn/lihongjie/coal/smartCamCarLicenseSnapshotData/service/SmartCamCarLicenseSnapshotDataService.java index ea783b02..47cd1ebb 100644 --- a/src/main/java/cn/lihongjie/coal/smartCamCarLicenseSnapshotData/service/SmartCamCarLicenseSnapshotDataService.java +++ b/src/main/java/cn/lihongjie/coal/smartCamCarLicenseSnapshotData/service/SmartCamCarLicenseSnapshotDataService.java @@ -323,7 +323,9 @@ public class SmartCamCarLicenseSnapshotDataService return; } - if (!Pattern.matches("^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$", entity.getNumber())){ + if (!Pattern.matches( + "^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$", + entity.getNumber())) { log.info("车牌号格式不正确 {}", entity.getNumber()); @@ -331,14 +333,24 @@ public class SmartCamCarLicenseSnapshotDataService } boolean entry = StringUtils.equals(entity.getSmartCam().getDirection(), "0"); - RBucket bucket = redissonClient.getBucket(entity.getOrganizationId() + ":" + entity.getNumber() + ":" + entity.getSmartCam().getDirection()); + RBucket bucket = + redissonClient.getBucket( + entity.getOrganizationId() + + ":" + + entity.getNumber() + + ":" + + entity.getSmartCam().getDirection()); - if (bucket.isExists()){ + if (bucket.isExists()) { - log.info("车牌号已存在 {} {} {}", entity.getOrganizationId(), entity.getNumber() , bucket.remainTimeToLive()); + log.info( + "车牌号已存在 {} {} {}", + entity.getOrganizationId(), + entity.getNumber(), + bucket.remainTimeToLive()); return; - }else { + } else { bucket.set(entity.getNumber(), Duration.ofMinutes(10)); } @@ -347,39 +359,36 @@ public class SmartCamCarLicenseSnapshotDataService // 把上次的未结束的数据置为结束 - List notFinished = smartCamCarDataRepository.findAll( - new Specification() { - @Override - public Predicate toPredicate( - Root root, - CriteriaQuery query, - CriteriaBuilder criteriaBuilder) { - - return criteriaBuilder.and( - criteriaBuilder.equal( - root.get("organizationId"), - entity.getOrganizationId()), - criteriaBuilder.equal( - root.get("number"), entity.getNumber()), - criteriaBuilder.equal(root.get("finished"), false)); - } - }); - - - - + List notFinished = + smartCamCarDataRepository.findAll( + new Specification() { + @Override + public Predicate toPredicate( + Root root, + CriteriaQuery query, + CriteriaBuilder criteriaBuilder) { + return criteriaBuilder.and( + criteriaBuilder.equal( + root.get("organizationId"), + entity.getOrganizationId()), + criteriaBuilder.equal( + root.get("number"), entity.getNumber()), + criteriaBuilder.equal(root.get("finished"), false)); + } + }); for (SmartCamCarDataEntity smartCamCarDataEntity : notFinished) { - smartCamCarDataEntity.setFinished(true); - log.info("智能摄像头车辆数据关闭上一次进行中的数据 {} {}", smartCamCarDataEntity.getId(), smartCamCarDataEntity.getNumber()); + log.info( + "智能摄像头车辆数据关闭上一次进行中的数据 {} {}", + smartCamCarDataEntity.getId(), + smartCamCarDataEntity.getNumber()); smartCamCarDataRepository.save(smartCamCarDataEntity); } - SmartCamCarDataEntity smartCamCarDataEntity = new SmartCamCarDataEntity(); smartCamCarDataEntity.setNumber(entity.getNumber()); @@ -391,57 +400,45 @@ public class SmartCamCarLicenseSnapshotDataService smartCamCarDataEntity.setFinished(false); smartCamCarDataRepository.save(smartCamCarDataEntity); + } else { + List notFinished = + smartCamCarDataRepository.findAll( + new Specification() { + @Override + public Predicate toPredicate( + Root root, + CriteriaQuery query, + CriteriaBuilder criteriaBuilder) { + return criteriaBuilder.and( + criteriaBuilder.equal( + root.get("organizationId"), + entity.getOrganizationId()), + criteriaBuilder.equal( + root.get("number"), entity.getNumber()), + criteriaBuilder.equal(root.get("finished"), false), + criteriaBuilder.isNull(root.get("exitTime")), + criteriaBuilder.isNotNull(root.get("entryTime"))); + } + }); + List list = + notFinished.stream() + .sorted(Comparator.comparing(SmartCamCarDataEntity::getCreateTime)) + .toList(); - - - - }else { - - List notFinished = smartCamCarDataRepository.findAll( - new Specification() { - @Override - public Predicate toPredicate( - Root root, - CriteriaQuery query, - CriteriaBuilder criteriaBuilder) { - - return criteriaBuilder.and( - criteriaBuilder.equal( - root.get("organizationId"), - entity.getOrganizationId()), - criteriaBuilder.equal( - root.get("number"), entity.getNumber()), - criteriaBuilder.equal(root.get("finished"), false), - criteriaBuilder.isNull(root.get("exitTime")), - criteriaBuilder.isNotNull(root.get("entryTime")) - - - ); - } - - }); - - - List list = notFinished.stream().sorted(Comparator.comparing(SmartCamCarDataEntity::getCreateTime)).toList(); - - - if (CollectionUtils.isNotEmpty(list)){ + if (CollectionUtils.isNotEmpty(list)) { for (int i = 0; i < list.size() - 1; i++) { - // 对于之前未完成的数据,置为结束 list.get(i).setFinished(true); smartCamCarDataRepository.save(list.get(i)); - } - SmartCamCarDataEntity last = list.get(list.size() - 1); last.setExit(entity); @@ -451,10 +448,7 @@ public class SmartCamCarLicenseSnapshotDataService smartCamCarDataRepository.save(last); - - - - }else { + } else { // 没有找到进入的记录,创建一个 @@ -468,10 +462,7 @@ public class SmartCamCarLicenseSnapshotDataService smartCamCarDataEntity.setFinished(true); smartCamCarDataRepository.save(smartCamCarDataEntity); - } - - } } catch (Exception e) { @@ -668,18 +659,22 @@ public class SmartCamCarLicenseSnapshotDataService } } - public Page findSimilar(String number, String direction, PageRequest of) { + public Page findSimilar( + String number, String direction, PageRequest of) { - - return repository.findSimilar(number.replaceAll("[^0-9a-zA-Z]", ""), direction, of).map(mapper::toDto); + return repository + .findSimilar(number.replaceAll("[^0-9a-zA-Z]", ""), direction, of) + .map(mapper::toDto); } @Autowired SmartCamCarDataService smartCamCarDataService; public SmartCamCarLicenseSnapshotDataDto humanModify(HumanModifyRequest request) { - SmartCamCarLicenseSnapshotDataEntity entity = repository.findById(request.getId()).orElseThrow(() -> new RuntimeException("not found")); - + SmartCamCarLicenseSnapshotDataEntity entity = + repository + .findById(request.getId()) + .orElseThrow(() -> new RuntimeException("not found")); if (StringUtils.isNotBlank(request.getNumber())) { entity.setNumber(request.getNumber()); @@ -690,7 +685,71 @@ public class SmartCamCarLicenseSnapshotDataService smartCamCarDataService.updateNumber(entity); return mapper.toDto(entity); + } + public void batchModify( + String number, + String oldNumber, + LocalDateTime startTime, + LocalDateTime endTime, + String organizationId) { + if (StringUtils.equals(number, oldNumber)) { + return; + } + + if (startTime == null && endTime != null) { + + startTime = endTime.minusHours(1); + } + + if (endTime == null && startTime != null) { + + endTime = startTime.plusHours(1); + } + + if (startTime == null && endTime == null) { + + startTime = LocalDateTime.now().minusHours(1); + endTime = LocalDateTime.now().plusHours(1); + } + + LocalDateTime finalEndTime = endTime; + LocalDateTime finalStartTime = startTime; + List all = + this.repository.findAll( + new Specification() { + @Override + public Predicate toPredicate( + Root root, + CriteriaQuery query, + CriteriaBuilder criteriaBuilder) { + + return criteriaBuilder.and( + criteriaBuilder.equal(root.get("number"), oldNumber), + criteriaBuilder.lessThanOrEqualTo( + root.get("infoTimeObj"), finalEndTime), + criteriaBuilder.greaterThanOrEqualTo( + root.get("infoTimeObj"), finalStartTime), + criteriaBuilder.equal( + root.get("organizationId"), organizationId)); + } + }); + + all.forEach( + entity -> { + try { + + HumanModifyRequest request = new HumanModifyRequest(); + + request.setId(entity.getId()); + request.setNumber(number); + + this.humanModify(request); + } catch (Exception e) { + + log.warn("batchModify error", e); + } + }); } }