mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-07-25 23:27:07 +08:00
refactor(smartCam): 优化车牌号更新逻辑
- 在 SmartCamCarDataService 中添加车牌号更新逻辑 - 在 SmartCamCarLicenseSnapshotDataService 中实现批量修改功能- 优化代码格式和可读性
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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<Object> bucket = redissonClient.getBucket(entity.getOrganizationId() + ":" + entity.getNumber() + ":" + entity.getSmartCam().getDirection());
|
||||
RBucket<Object> 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<SmartCamCarDataEntity> notFinished = smartCamCarDataRepository.findAll(
|
||||
new Specification<SmartCamCarDataEntity>() {
|
||||
@Override
|
||||
public Predicate toPredicate(
|
||||
Root<SmartCamCarDataEntity> 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<SmartCamCarDataEntity> notFinished =
|
||||
smartCamCarDataRepository.findAll(
|
||||
new Specification<SmartCamCarDataEntity>() {
|
||||
@Override
|
||||
public Predicate toPredicate(
|
||||
Root<SmartCamCarDataEntity> 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<SmartCamCarDataEntity> notFinished =
|
||||
smartCamCarDataRepository.findAll(
|
||||
new Specification<SmartCamCarDataEntity>() {
|
||||
@Override
|
||||
public Predicate toPredicate(
|
||||
Root<SmartCamCarDataEntity> 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<SmartCamCarDataEntity> list =
|
||||
notFinished.stream()
|
||||
.sorted(Comparator.comparing(SmartCamCarDataEntity::getCreateTime))
|
||||
.toList();
|
||||
|
||||
|
||||
|
||||
|
||||
}else {
|
||||
|
||||
List<SmartCamCarDataEntity> notFinished = smartCamCarDataRepository.findAll(
|
||||
new Specification<SmartCamCarDataEntity>() {
|
||||
@Override
|
||||
public Predicate toPredicate(
|
||||
Root<SmartCamCarDataEntity> 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<SmartCamCarDataEntity> 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<SmartCamCarLicenseSnapshotDataDto> findSimilar(String number, String direction, PageRequest of) {
|
||||
public Page<SmartCamCarLicenseSnapshotDataDto> 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<SmartCamCarLicenseSnapshotDataEntity> all =
|
||||
this.repository.findAll(
|
||||
new Specification<SmartCamCarLicenseSnapshotDataEntity>() {
|
||||
@Override
|
||||
public Predicate toPredicate(
|
||||
Root<SmartCamCarLicenseSnapshotDataEntity> 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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user