现有过车记录集合ListAnalysisJudgmentVehicleVOAnalysisJudgmentVehicleVO属性有hpzlhphmgcsj均为String类型。根据过车记录集合有统计条件如下开始时间例2023-04-01结束时间例2023-04-10时间段0600-1800为白天其余时间为晚上。昼伏夜出比例30 计算要求同一天同一辆车晚上有过车记录白天没有即该车辆次数+1同一天号牌
代码如下:
public List<AnalysisJudgmentVehicleVO> analyzeVehicleRecords(List<AnalysisJudgmentVehicleVO> vehicleRecords, String startTime, String endTime, String dayStart, String dayEnd, double dayNightRatio) {
// 将统计条件的字符串形式转换为日期时间类型
LocalDateTime dtStart = LocalDateTime.parse(startTime + " 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDateTime dtEnd = LocalDateTime.parse(endTime + " 23:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalTime timeDayStart = LocalTime.parse(dayStart);
LocalTime timeDayEnd = LocalTime.parse(dayEnd);
// 定义昼伏夜出的开始时间和结束时间
LocalDateTime dayStartDt = LocalDateTime.of(dtStart.toLocalDate(), timeDayStart);
LocalDateTime dayEndDt = LocalDateTime.of(dtStart.toLocalDate(), timeDayEnd);
LocalDateTime nightStartDt = LocalDateTime.of(dtStart.toLocalDate(), timeDayEnd.plusSeconds(1));
LocalDateTime nightEndDt = LocalDateTime.of(dtStart.toLocalDate().plusDays(1), timeDayStart);
// 统计每辆车的总次数和昼伏夜出次数
Map<String, AnalysisJudgmentVehicleVO> vehicleCountMap = new HashMap<>();
for (AnalysisJudgmentVehicleVO vehicle : vehicleRecords) {
// 跳过时间范围之外的记录
LocalDateTime recordDt = LocalDateTime.parse(vehicle.getGcsj(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
if (recordDt.isBefore(dtStart) || recordDt.isAfter(dtEnd)) {
continue;
}
// 判断记录是否在白天或晚上
boolean isDay = false;
if (recordDt.isAfter(dayStartDt) && recordDt.isBefore(dayEndDt)) {
isDay = true;
} else if (recordDt.isAfter(nightStartDt) && recordDt.isBefore(nightEndDt)) {
isDay = false;
} else {
continue;
}
// 统计车辆的总次数和昼伏夜出次数
String key = vehicle.getHpzl() + "_" + vehicle.getHphm();
AnalysisJudgmentVehicleVO countVehicle = vehicleCountMap.get(key);
if (countVehicle == null) {
countVehicle = new AnalysisJudgmentVehicleVO();
countVehicle.setHpzl(vehicle.getHpzl());
countVehicle.setHphm(vehicle.getHphm());
countVehicle.setTotalCount(1);
countVehicle.setDayNightCount(isDay ? 0 : 1);
vehicleCountMap.put(key, countVehicle);
} else {
countVehicle.setTotalCount(countVehicle.getTotalCount() + 1);
if (!isDay) {
countVehicle.setDayNightCount(countVehicle.getDayNightCount() + 1);
}
}
}
// 根据昼伏夜出比例筛选符合条件的记录
List<AnalysisJudgmentVehicleVO> newVehicleRecords = new ArrayList<>();
for (AnalysisJudgmentVehicleVO vehicle : vehicleCountMap.values()) {
// 计算昼伏夜出比例
double ratio = (double) vehicle.getDayNightCount() / (double) ChronoUnit.DAYS.between(dtStart.toLocalDate(), dtEnd.toLocalDate() + 1);
if (ratio > dayNightRatio) {
newVehicleRecords.add(vehicle);
}
}
return newVehicleRecords;
}
``
原文地址: https://www.cveoy.top/t/topic/eE4i 著作权归作者所有。请勿转载和采集!