feat(smartCamCarData):优化车辆数据处理逻辑

- 添加 finished 字段用于标记数据是否完成
- 修改 findByNumber 查询,增加 finished 参数
- 优化车辆入场和出场的处理逻辑
-增加车牌号格式校验
- 修复可能的并发问题,通过 Specification 查询未完成的数据
This commit is contained in:
2025-04-20 16:49:40 +08:00
parent 64cbd2e848
commit 237e83f5c8
3 changed files with 131 additions and 26 deletions

View File

@@ -35,5 +35,8 @@ public class SmartCamCarDataEntity extends OrgCommonEntity {
@ManyToOne
private SmartCamCarLicenseSnapshotDataEntity exit;
private Boolean finished;
}

View File

@@ -13,6 +13,6 @@ public interface SmartCamCarDataRepository extends BaseRepository<SmartCamCarDat
@Query("select false")
boolean isLinked(List<String> ids);
@Query("select d from SmartCamCarDataEntity d where d.number = :number and d.organizationId = :organizationId order by d.createTime desc limit 1")
SmartCamCarDataEntity findByNumber(String organizationId, String number);
@Query("select d from SmartCamCarDataEntity d where d.number = :number and d.organizationId = :organizationId and d.finished = :finished order by d.createTime desc limit 1")
SmartCamCarDataEntity findByNumber(String organizationId, String number, Boolean finished);
}

View File

@@ -34,6 +34,7 @@ import jakarta.persistence.criteria.Root;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
@@ -49,6 +50,9 @@ import org.springframework.transaction.annotation.Transactional;
import java.io.ByteArrayInputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;
@Service
@Slf4j
@@ -312,43 +316,141 @@ public class SmartCamCarLicenseSnapshotDataService
return;
}
SmartCamCarDataEntity byNumber =
smartCamCarDataRepository.findByNumber(
entity.getOrganizationId(), 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());
return;
}
boolean entry = StringUtils.equals(entity.getSmartCam().getDirection(), "0");
if (byNumber == null) {
if (entry){
// 把上次的未结束的数据置为结束
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());
smartCamCarDataRepository.save(smartCamCarDataEntity);
}
SmartCamCarDataEntity smartCamCarDataEntity = new SmartCamCarDataEntity();
smartCamCarDataEntity.setNumber(entity.getNumber());
if (entry) {
smartCamCarDataEntity.setEntry(entity);
smartCamCarDataEntity.setEntryTime(entity.getInfoTimeObj());
smartCamCarDataEntity.setEntry(entity);
smartCamCarDataEntity.setEntryTime(entity.getInfoTimeObj());
} else {
smartCamCarDataEntity.setOrganizationId(entity.getOrganizationId());
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();
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);
last.setExitTime(entity.getInfoTimeObj());
smartCamCarDataRepository.save(last);
}else {
// 没有找到进入的记录,创建一个
SmartCamCarDataEntity smartCamCarDataEntity = new SmartCamCarDataEntity();
smartCamCarDataEntity.setNumber(entity.getNumber());
smartCamCarDataEntity.setExit(entity);
smartCamCarDataEntity.setExitTime(entity.getInfoTimeObj());
smartCamCarDataEntity.setOrganizationId(entity.getOrganizationId());
smartCamCarDataEntity.setFinished(true);
smartCamCarDataRepository.save(smartCamCarDataEntity);
}
smartCamCarDataEntity.setOrganizationId(entity.getOrganizationId());
smartCamCarDataRepository.save(smartCamCarDataEntity);
} else {
if (entry) {
log.info("重复的车牌号数据 {} {}", byNumber.getId(), byNumber.getNumber());
} else {
byNumber.setExit(entity);
byNumber.setExitTime(entity.getInfoTimeObj());
byNumber.setOrganizationId(entity.getOrganizationId());
smartCamCarDataRepository.save(byNumber);
}
}
} catch (Exception e) {
log.error("保存数据异常", e);
}