From 078cf7379106e0d58ef1d12d0741ab5756aaf7c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=85=B4=E7=90=B3?= <1911390090@qq.com> Date: Wed, 12 Jun 2024 08:28:34 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E8=B7=AF=E6=AE=B5=E4=BA=A4=E9=80=9A?= =?UTF-8?q?=E6=8C=87=E6=A0=87=E5=88=86=E6=9E=90=E3=80=82=20=E4=BA=A4?= =?UTF-8?q?=E9=80=9A=E6=8C=87=E6=A0=87=E6=97=B6=E9=97=B4=E5=88=86=E5=B8=83?= =?UTF-8?q?=EF=BC=8C=E4=BA=A4=E9=80=9A=E6=8C=87=E6=A0=87=E6=8E=92=E5=90=8D?= =?UTF-8?q?=EF=BC=8C=E4=BA=A4=E9=80=9A=E6=8C=87=E6=A0=87=E9=9B=B7=E8=BE=BE?= =?UTF-8?q?=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DcTrafficStatisticsController.java | 43 +++ .../java/com/zc/business/domain/DcEvent.java | 5 +- .../DcGantryMetricsStatisticsDataMapper.java | 5 + .../mapper/DcGantryStatisticsDataMapper.java | 2 + ...IDcGantryMetricsStatisticsDataService.java | 7 + .../IDcGantryStatisticsDataService.java | 6 + .../service/impl/DcEventServiceImpl.java | 11 +- .../impl/DcGantryStatisticsDataImpl.java | 42 ++- .../impl/DcTrafficStatisticsServiceImpl.java | 52 ++-- ...antryMetricsStatisticsDataServiceImpl.java | 270 +++++++++++++++++- .../DcGantryMetricsStatisticsDataMapper.xml | 31 +- .../business/DcGantryStatisticsDataMapper.xml | 38 +++ 12 files changed, 464 insertions(+), 48 deletions(-) diff --git a/zc-business/src/main/java/com/zc/business/controller/DcTrafficStatisticsController.java b/zc-business/src/main/java/com/zc/business/controller/DcTrafficStatisticsController.java index abbcc879..fd7e61ac 100644 --- a/zc-business/src/main/java/com/zc/business/controller/DcTrafficStatisticsController.java +++ b/zc-business/src/main/java/com/zc/business/controller/DcTrafficStatisticsController.java @@ -175,6 +175,49 @@ public AjaxResult realTimeTrafficFlow(String startDate, String direction,String // 将查询结果封装为成功响应并返回 return AjaxResult.success(mapList); } + /** + * 车流量时段分析 + * @param startDate 时间 + * @param direction 方向 + * @param periodType 时间粒子 + */ + @ApiOperation("车流量时段分析") + @GetMapping("/history/trafficPeriodAnalysis") + public AjaxResult trafficPeriodAnalysis(String startDate, String direction,String periodType ){ + List> mapList = dcGantryStatisticsDataService.trafficPeriodAnalysis(startDate,direction,periodType); + // 将查询结果封装为成功响应并返回 + return AjaxResult.success(mapList); + } + /** + * sectionTrafficIndexAnalysis + * @param startDate 时间 + * @param direction 方向 + * @param periodType 时间粒子 + * @param ranking 排名 1 饱和度 2拥挤度 3交通特征指数 + * + */ + @ApiOperation("路段交通指标分析") + @GetMapping("/history/sectionTrafficIndexAnalysis") + public AjaxResult sectionTrafficIndexAnalysis(String startDate, String direction,String periodType,int ranking ){ + List> mapList = dcGantryMetricsStatisticsDataService.sectionTrafficIndexAnalysis(startDate,direction,periodType,ranking); + // 将查询结果封装为成功响应并返回 + return AjaxResult.success(mapList); + } + @ApiOperation("通指标时间分布") + @GetMapping("/history/passIndicatorTimeDistribution") + public AjaxResult passIndicatorTimeDistribution(String startDate, String direction,String periodType ){ + List> mapList = dcGantryMetricsStatisticsDataService.passIndicatorTimeDistribution(startDate,direction,periodType); + // 将查询结果封装为成功响应并返回 + return AjaxResult.success(mapList); + } + @ApiOperation("交通指标雷达图") + @GetMapping("/history/radarMapOfTrafficIndicators") + public AjaxResult radarMapOfTrafficIndicators(String startDate, String direction,String periodType ){ + Map map = dcGantryMetricsStatisticsDataService.radarMapOfTrafficIndicators(startDate,direction,periodType); + // 将查询结果封装为成功响应并返回 + return AjaxResult.success(map); + } + /** diff --git a/zc-business/src/main/java/com/zc/business/domain/DcEvent.java b/zc-business/src/main/java/com/zc/business/domain/DcEvent.java index 14f05f49..8c626a5f 100644 --- a/zc-business/src/main/java/com/zc/business/domain/DcEvent.java +++ b/zc-business/src/main/java/com/zc/business/domain/DcEvent.java @@ -58,7 +58,7 @@ public class DcEvent { * 2-中 * 3-下 */ - @Excel(name = "方向", readConverterExp = "1=菏泽方向,3=济南方向") + @Excel(name = "方向") //@Excel(name = "方向") @ApiModelProperty("方向") private String direction; @@ -69,6 +69,7 @@ public class DcEvent { @ApiModelProperty("处理人员") private Long userId; + @Excel(name = "事件子类") @ApiModelProperty("事件子类") private String eventSubclassName; @@ -136,7 +137,7 @@ public class DcEvent { * 3-3 立交封闭和限行 * 3-4 服务区封闭和限行 */ - @Excel(name = "事件类型字符串") + @ApiModelProperty("事件子类 例:事件子类:* 1-1 追尾* 1-2 侧翻* 1-3 撞护栏* 1-4 自然* 1-5 其他事故* 2-1 车辆故障") private String eventSubclass; diff --git a/zc-business/src/main/java/com/zc/business/mapper/DcGantryMetricsStatisticsDataMapper.java b/zc-business/src/main/java/com/zc/business/mapper/DcGantryMetricsStatisticsDataMapper.java index 51d4288b..bcd75b82 100644 --- a/zc-business/src/main/java/com/zc/business/mapper/DcGantryMetricsStatisticsDataMapper.java +++ b/zc-business/src/main/java/com/zc/business/mapper/DcGantryMetricsStatisticsDataMapper.java @@ -3,8 +3,10 @@ package com.zc.business.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.zc.business.domain.DcGantryMetricsStatisticsData; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; import java.util.List; +import java.util.Map; /** * 门架指标数据统计Mapper接口 @@ -29,4 +31,7 @@ public interface DcGantryMetricsStatisticsDataMapper extends BaseMapper gantryTrafficMetricsDataList); + + List> sectionTrafficIndexAnalysis(@Param("startDate") String startDate, @Param("direction") String direction,@Param("periodType") String periodType); + } diff --git a/zc-business/src/main/java/com/zc/business/mapper/DcGantryStatisticsDataMapper.java b/zc-business/src/main/java/com/zc/business/mapper/DcGantryStatisticsDataMapper.java index 749d803f..1909a0ed 100644 --- a/zc-business/src/main/java/com/zc/business/mapper/DcGantryStatisticsDataMapper.java +++ b/zc-business/src/main/java/com/zc/business/mapper/DcGantryStatisticsDataMapper.java @@ -45,5 +45,7 @@ public interface DcGantryStatisticsDataMapper extends BaseMapper> realTimeTrafficFlow(@Param("startDate") String startDate, @Param("direction")String direction, @Param("periodType")String periodType ); + + List> trafficPeriodAnalysis(@Param("startDate") String startDate, @Param("direction")String direction, @Param("periodType")String periodType); } diff --git a/zc-business/src/main/java/com/zc/business/service/IDcGantryMetricsStatisticsDataService.java b/zc-business/src/main/java/com/zc/business/service/IDcGantryMetricsStatisticsDataService.java index b6a9a76e..69a30014 100644 --- a/zc-business/src/main/java/com/zc/business/service/IDcGantryMetricsStatisticsDataService.java +++ b/zc-business/src/main/java/com/zc/business/service/IDcGantryMetricsStatisticsDataService.java @@ -6,6 +6,7 @@ import com.zc.business.domain.DcGantryMetricsStatisticsData; import com.zc.business.domain.DcGantryMetricsStatisticsData; import java.util.List; +import java.util.Map; /** * 门架指标统计服务接口,提供处理交通数据的相关方法 @@ -39,4 +40,10 @@ public interface IDcGantryMetricsStatisticsDataService extends IService gantryMetricsData(DcGantryMetricsStatisticsData request); + + List> sectionTrafficIndexAnalysis(String startDate, String direction, String periodType,int ranking); + + List> passIndicatorTimeDistribution(String startDate, String direction, String periodType); + + Map radarMapOfTrafficIndicators(String startDate, String direction, String periodType); } diff --git a/zc-business/src/main/java/com/zc/business/service/IDcGantryStatisticsDataService.java b/zc-business/src/main/java/com/zc/business/service/IDcGantryStatisticsDataService.java index 256f970d..a0712aa4 100644 --- a/zc-business/src/main/java/com/zc/business/service/IDcGantryStatisticsDataService.java +++ b/zc-business/src/main/java/com/zc/business/service/IDcGantryStatisticsDataService.java @@ -47,5 +47,11 @@ public interface IDcGantryStatisticsDataService extends IService> realTimeTrafficFlow(String startDate, String direction,String periodType ); + /** + * 车流量时段分析 + */ + List> trafficPeriodAnalysis(String startDate, String direction, String periodType); + + } diff --git a/zc-business/src/main/java/com/zc/business/service/impl/DcEventServiceImpl.java b/zc-business/src/main/java/com/zc/business/service/impl/DcEventServiceImpl.java index 46786c06..78608989 100644 --- a/zc-business/src/main/java/com/zc/business/service/impl/DcEventServiceImpl.java +++ b/zc-business/src/main/java/com/zc/business/service/impl/DcEventServiceImpl.java @@ -229,9 +229,7 @@ public class DcEventServiceImpl extends ServiceImpl impl dcEvent.getDcEventAccident().setId(uuid); int i1 = dcEventAccidentMapper.insertDcEventAccident(dcEvent.getDcEventAccident()); - if (dcEvent.getEventState() ==1){ - updateDcEventState(uuid,1);//直接确认 - } + } break; //车辆事故 @@ -239,9 +237,6 @@ public class DcEventServiceImpl extends ServiceImpl impl if (dcEvent.getDcEventVehicleAccident() != null) { dcEvent.getDcEventVehicleAccident().setId(uuid); int i5 = dcEventVehicleAccidentMapper.insertDcEventVehicleAccident(dcEvent.getDcEventVehicleAccident()); - if (dcEvent.getEventState() ==1){ - updateDcEventState(uuid,1);//直接确认 - } } break; //交通管制 @@ -1224,10 +1219,10 @@ public class DcEventServiceImpl extends ServiceImpl impl return 1; } - //事件状态已被修改 返回成功 +/* //事件状态已被修改 返回成功 if (dcEvent1.getEventState() == 1) { return 1; - } + }*/ int i = dcEventMapper.updateDcEventState(id, state); diff --git a/zc-business/src/main/java/com/zc/business/service/impl/DcGantryStatisticsDataImpl.java b/zc-business/src/main/java/com/zc/business/service/impl/DcGantryStatisticsDataImpl.java index d40b9185..cef92d43 100644 --- a/zc-business/src/main/java/com/zc/business/service/impl/DcGantryStatisticsDataImpl.java +++ b/zc-business/src/main/java/com/zc/business/service/impl/DcGantryStatisticsDataImpl.java @@ -21,9 +21,9 @@ import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import javax.annotation.Resource; -import java.util.Date; -import java.util.List; -import java.util.Map; +import java.time.LocalDateTime; +import java.util.*; +import java.util.stream.Collectors; /** * 门架数据统计服务实现类 @@ -206,4 +206,40 @@ public class DcGantryStatisticsDataImpl extends ServiceImpl> realTimeTrafficFlow(String startDate, String direction, String periodType ) { return dcGantryStatisticsDataMapper.realTimeTrafficFlow(startDate,direction,periodType); } + /** + * 车流量时段分析 + */ + @Override + public List> trafficPeriodAnalysis(String startDate, String direction, String periodType) { + + List> mapList = dcGantryStatisticsDataMapper.trafficPeriodAnalysis(startDate, direction, periodType); + List> aggregatedList = aggregateTrafficVolume(mapList); + return aggregatedList; + } + + public List> aggregateTrafficVolume(List> dataList) { + // 创建一个Map,用于存储整点时间的流量数据 + Map aggregatedData = new HashMap<>(); + + // 遍历接口返回的数据列表 + for (Map data : dataList) { + LocalDateTime statisticalDate = (LocalDateTime) data.get("statisticalDate"); + int hour = statisticalDate.getHour(); + // 获取该整点时间的流量数据并累加 + int trafficVolume = (int) data.get("trafficVolume"); + aggregatedData.put(hour, + aggregatedData.getOrDefault(hour, 0) + trafficVolume); + } + + // 将整点时间的流量数据重新构建为列表 + List> aggregatedList = new ArrayList<>(); + for (Map.Entry entry : aggregatedData.entrySet()) { + Map aggregatedItem = new HashMap<>(); + aggregatedItem.put("statisticalHour", String.valueOf(entry.getKey())); // 整点时间 + aggregatedItem.put("trafficVolume", String.valueOf(entry.getValue())); // 流量数据 + aggregatedList.add(aggregatedItem); + } + + return aggregatedList; + } } diff --git a/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java b/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java index 1fc41ff2..5f9de246 100644 --- a/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java +++ b/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java @@ -778,14 +778,28 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi return null; } - /** - * - * @param startDate - * @param direction - * @return - * @throws HttpException - * @throws IOException - */ + + private static String extract(String input) { + Pattern pattern = Pattern.compile("K(\\d{3})\\+(\\d{3})"); + Matcher matcher = pattern.matcher(input); + + if (matcher.find()) { + String part1 = matcher.group(1); + String part2 = matcher.group(2); + // 直接拼接两部分数字,无需格式化,这样得到的字符串自然没有前导零 + return part1 + part2; + } else { + return "N/A"; + } + } + public static String getDescriptionByIdentification(int identification) { + for (StakeMarkRange range : StakeMarkRange.values()) { + if (range.getIdentification() == identification) { + return range.getDescription(); + } + } + return ""; + } public List> realTimeTrafficFlowHour2(String startDate,Long direction) throws HttpException, IOException { OkHttp okHttp = new OkHttp(); @@ -851,26 +865,4 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi return new ArrayList(); } - private static String extract(String input) { - Pattern pattern = Pattern.compile("K(\\d{3})\\+(\\d{3})"); - Matcher matcher = pattern.matcher(input); - - if (matcher.find()) { - String part1 = matcher.group(1); - String part2 = matcher.group(2); - // 直接拼接两部分数字,无需格式化,这样得到的字符串自然没有前导零 - return part1 + part2; - } else { - return "N/A"; - } - } - public static String getDescriptionByIdentification(int identification) { - for (StakeMarkRange range : StakeMarkRange.values()) { - if (range.getIdentification() == identification) { - return range.getDescription(); - } - } - return ""; - } - } diff --git a/zc-business/src/main/java/com/zc/business/service/impl/IDcGantryMetricsStatisticsDataServiceImpl.java b/zc-business/src/main/java/com/zc/business/service/impl/IDcGantryMetricsStatisticsDataServiceImpl.java index 4df08393..5fc6ea39 100644 --- a/zc-business/src/main/java/com/zc/business/service/impl/IDcGantryMetricsStatisticsDataServiceImpl.java +++ b/zc-business/src/main/java/com/zc/business/service/impl/IDcGantryMetricsStatisticsDataServiceImpl.java @@ -8,8 +8,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.common.exception.ServiceException; import com.zc.business.domain.DcGantryMetricsStatisticsData; -import com.zc.business.domain.DcGantryStatisticsData; import com.zc.business.enums.ChannelCongestionLevelEnum; +import com.zc.business.enums.StakeMarkRange; import com.zc.business.enums.TrafficCompositionRateEnum; import com.zc.business.enums.TrafficDataPeriodTypeEnum; import com.zc.business.mapper.DcGantryMetricsStatisticsDataMapper; @@ -28,9 +28,11 @@ import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import javax.annotation.Resource; import java.io.IOException; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; +import java.time.LocalDateTime; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; /** * 门架指标统计服务实现类,负责处理实时设备消息、缓存数据、定时任务以及数据保存等功能。 @@ -162,6 +164,226 @@ public class IDcGantryMetricsStatisticsDataServiceImpl return list(queryWrapper); } + /** + * 路段交通指标分析 + * + * @param startDate + * @param direction + * @param periodType + * @return + */ + @Override + public List> sectionTrafficIndexAnalysis(String startDate, String direction, String periodType, int ranking) { + List> mapListOne = gantryMetricsStatisticsDataMapper.sectionTrafficIndexAnalysis(startDate, direction, periodType); + List> mapList = new ArrayList<>(); + Map>> result = mapListOne.stream() + .collect(Collectors.groupingBy(map -> { + String stakeMark = map.get("stakeMark").toString(); + int extractedNumber = Integer.parseInt(extract(stakeMark)); + return Arrays.stream(StakeMarkRange.values()) + .filter(smRange -> extractedNumber >= smRange.getStakeMark() && extractedNumber <= smRange.getEndMark()) + .mapToInt(StakeMarkRange::getIdentification) + .findFirst() + .orElse(0); + })); +// 处理每个组 + for (Map.Entry>> entry : result.entrySet()) { + Integer groupId = entry.getKey(); + List> groupData = entry.getValue(); + + // 计算crowdingRate和trafficCompositionRate的众数,以及saturationRate的平均值 + int[] rates = {0, 0}; // 用于存储众数,第一个元素为crowdingRate众数,第二个为trafficCompositionRate众数 + float sumSaturationRate = 0; + int count = groupData.size(); + String id = ""; + Map crowdingRateCounts = new HashMap<>(); + Map trafficCompositionRateCounts = new HashMap<>(); + + for (Map item : groupData) { + Integer crowdingRate = (Integer) item.get("crowdingRate"); + Integer trafficCompositionRate = (Integer) item.get("trafficCompositionRate"); + Float saturationRate = (Float) item.get("saturationRate"); + id = (String) item.get("id"); + + // Double saturationRate = (Double) item.get("saturationRate"); + + sumSaturationRate += saturationRate; + + crowdingRateCounts.put(crowdingRate, crowdingRateCounts.getOrDefault(crowdingRate, 0) + 1); + trafficCompositionRateCounts.put(trafficCompositionRate, trafficCompositionRateCounts.getOrDefault(trafficCompositionRate, 0) + 1); + } + + // 找到众数 + rates[0] = findMaxCountKey(crowdingRateCounts); + rates[1] = findMaxCountKey(trafficCompositionRateCounts); + + // 计算平均saturationRate + double avgSaturationRate = count > 0 ? sumSaturationRate / count : 0; + +// 格式化保留4位小数 + String formattedAvgSaturationRate = String.format("%.4f", avgSaturationRate); + Map map = new HashMap<>(); + String description = getDescriptionByIdentification(groupId); + + map.put("groupId", groupId); + map.put("Id", id); + map.put("groupName", description); + map.put("crowdingRate", rates[0]); + map.put("trafficCompositionRate", rates[1]); + map.put("saturationRate", formattedAvgSaturationRate); + mapList.add(map); + // System.out.println("集团: " + groupId); + // System.out.println("拥堵度" + rates[0]); + // System.out.println("交通组成特征指数" + rates[1]); + // System.out.println("平均饱和度:" + formattedAvgSaturationRate); + + } + if (ranking == 1) { + mapList.sort(Comparator.comparingInt(map -> (Integer) ((Map) map).get("saturationRate")).reversed()); + } + if (ranking == 2) { + mapList.sort(Comparator.comparingInt(map -> (Integer) ((Map) map).get("crowdingRate")).reversed()); + } + if (ranking == 3) { + mapList.sort(Comparator.comparingInt(map -> (Integer) ((Map) map).get("trafficCompositionRate")).reversed()); + } + return mapList; + } + + /** + * 交通指标时间分布 + * + * @param startDate + * @param direction + * @param periodType + * @return + */ + @Override + public List> passIndicatorTimeDistribution(String startDate, String direction, String periodType) { + List> mapList = gantryMetricsStatisticsDataMapper.sectionTrafficIndexAnalysis(startDate, direction, periodType); + List> mapTime = new ArrayList<>(); + +// 使用 Java流按照相同时间分组 + Map>> result = mapList.stream() + .collect(Collectors.groupingBy( + map -> (LocalDateTime) map.get("statisticalDate"), + Collectors.toList() + )); + // 处理每个组 + for (Map.Entry>> entry : result.entrySet()) { + LocalDateTime groupId = entry.getKey(); + List> groupData = entry.getValue(); + + // 计算crowdingRate和trafficCompositionRate的众数,以及saturationRate的平均值 + int[] rates = {0, 0}; // 用于存储众数,第一个元素为crowdingRate众数,第二个为trafficCompositionRate众数 + float sumSaturationRate = 0; + int count = groupData.size(); + String id = ""; + Map crowdingRateCounts = new HashMap<>(); + Map trafficCompositionRateCounts = new HashMap<>(); + + for (Map item : groupData) { + Integer crowdingRate = (Integer) item.get("crowdingRate"); + Integer trafficCompositionRate = (Integer) item.get("trafficCompositionRate"); + Float saturationRate = (Float) item.get("saturationRate"); + id = (String) item.get("id"); + + // Double saturationRate = (Double) item.get("saturationRate"); + + sumSaturationRate += saturationRate; + + crowdingRateCounts.put(crowdingRate, crowdingRateCounts.getOrDefault(crowdingRate, 0) + 1); + trafficCompositionRateCounts.put(trafficCompositionRate, trafficCompositionRateCounts.getOrDefault(trafficCompositionRate, 0) + 1); + } + + // 找到众数 + rates[0] = findMaxCountKey(crowdingRateCounts); + rates[1] = findMaxCountKey(trafficCompositionRateCounts); + + // 计算平均saturationRate + double avgSaturationRate = count > 0 ? sumSaturationRate / count : 0; + +// 格式化保留4位小数 + String formattedAvgSaturationRate = String.format("%.4f", avgSaturationRate); + Map map = new HashMap<>(); + map.put("groupTime", groupId); + map.put("Id", id); + map.put("crowdingRate", rates[0]); + map.put("trafficCompositionRate", rates[1]); + map.put("saturationRate", formattedAvgSaturationRate); + mapTime.add(map); + } + + // 输出结果 + result.forEach((date, data) -> { + System.out.println("统计日期:" + date); + data.forEach(entry -> { + entry.forEach((key, value) -> { + System.out.println(key + ": " + value); + }); + }); + }); + return mapTime; + } + + /** + * 交通指标雷达图 + * @param startDate + * @param direction + * @param periodType + * @return + */ + @Override + public Map radarMapOfTrafficIndicators(String startDate, String direction, String periodType) { + List> mapOne = gantryMetricsStatisticsDataMapper.sectionTrafficIndexAnalysis(startDate, direction, periodType); + // 计算crowdingRate和trafficCompositionRate的众数,以及saturationRate的平均值 + int[] rates = {0, 0}; // 用于存储众数,第一个元素为crowdingRate众数,第二个为trafficCompositionRate众数 + float sumSaturationRate = 0; + int count = mapOne.size(); + Map crowdingRateCounts = new HashMap<>(); + Map trafficCompositionRateCounts = new HashMap<>(); + + for (Map item : mapOne) { + Integer crowdingRate = (Integer) item.get("crowdingRate"); + Integer trafficCompositionRate = (Integer) item.get("trafficCompositionRate"); + Float saturationRate = (Float) item.get("saturationRate"); + // Double saturationRate = (Double) item.get("saturationRate"); + sumSaturationRate += saturationRate; + + crowdingRateCounts.put(crowdingRate, crowdingRateCounts.getOrDefault(crowdingRate, 0) + 1); + trafficCompositionRateCounts.put(trafficCompositionRate, trafficCompositionRateCounts.getOrDefault(trafficCompositionRate, 0) + 1); + } + // 找到众数 + rates[0] = findMaxCountKey(crowdingRateCounts); + rates[1] = findMaxCountKey(trafficCompositionRateCounts); + // 计算平均saturationRate + double avgSaturationRate = count > 0 ? sumSaturationRate / count : 0; + +// 格式化保留4位小数 + String formattedAvgSaturationRate = String.format("%.4f", avgSaturationRate); + Map map = new HashMap<>(); + map.put("crowdingRate", rates[0]); + map.put("trafficCompositionRate", rates[1]); + map.put("saturationRate", formattedAvgSaturationRate); + return map; + } + + + private static int findMaxCountKey(Map counts) { + // 初始化一个 Pair 类型来存储当前的最大计数及对应的键 + Pair maxPair = null; + + for (Map.Entry entry : counts.entrySet()) { + if (maxPair == null || entry.getValue() > maxPair.getValue() || + (entry.getValue().equals(maxPair.getValue()) && entry.getKey() > maxPair.getKey())) { + maxPair = new Pair<>(entry.getKey(), entry.getValue()); + } + } + + // 确保maxPair不为null,这里假设输入的counts非空且至少有一个元素 + return maxPair.getKey(); + } + /** * 恢复小时缓存数据的方法 * 该方法首先会获取当前月份的门架入口数据, @@ -272,4 +494,44 @@ public class IDcGantryMetricsStatisticsDataServiceImpl } + private static String extract(String input) { + Pattern pattern = Pattern.compile("K(\\d{3})\\+(\\d{3})"); + Matcher matcher = pattern.matcher(input); + + if (matcher.find()) { + String part1 = matcher.group(1); + String part2 = matcher.group(2); + // 直接拼接两部分数字,无需格式化,这样得到的字符串自然没有前导零 + return part1 + part2; + } else { + return "N/A"; + } + } + + public static String getDescriptionByIdentification(int identification) { + for (StakeMarkRange range : StakeMarkRange.values()) { + if (range.getIdentification() == identification) { + return range.getDescription(); + } + } + return ""; + } + + public static class Pair { + private K key; + private V value; + + public Pair(K key, V value) { + this.key = key; + this.value = value; + } + + public K getKey() { + return key; + } + + public V getValue() { + return value; + } + } } diff --git a/zc-business/src/main/resources/mapper/business/DcGantryMetricsStatisticsDataMapper.xml b/zc-business/src/main/resources/mapper/business/DcGantryMetricsStatisticsDataMapper.xml index f72efc5f..76ea7a78 100644 --- a/zc-business/src/main/resources/mapper/business/DcGantryMetricsStatisticsDataMapper.xml +++ b/zc-business/src/main/resources/mapper/business/DcGantryMetricsStatisticsDataMapper.xml @@ -3,7 +3,17 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> - + + + + + + + + + + + INSERT INTO @@ -74,4 +84,23 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" update_time = NOW() + + + + diff --git a/zc-business/src/main/resources/mapper/business/DcGantryStatisticsDataMapper.xml b/zc-business/src/main/resources/mapper/business/DcGantryStatisticsDataMapper.xml index 3ea240b8..8efa1702 100644 --- a/zc-business/src/main/resources/mapper/business/DcGantryStatisticsDataMapper.xml +++ b/zc-business/src/main/resources/mapper/business/DcGantryStatisticsDataMapper.xml @@ -7,6 +7,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + + + + + + + + INSERT INTO @@ -213,4 +221,34 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + + + + From 20b6bcbcf80cc1dc46ff4bf1a3762fef812d5a46 Mon Sep 17 00:00:00 2001 From: lau572 <1010031226@qq.com> Date: Wed, 12 Jun 2024 09:17:59 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E9=A6=96=E9=A1=B5=E5=A4=84=E7=90=86?= =?UTF-8?q?=E4=B8=AD=E4=BA=8B=E4=BB=B6=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/resources/mapper/business/DcTrafficIncidentsMapper.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zc-business/src/main/resources/mapper/business/DcTrafficIncidentsMapper.xml b/zc-business/src/main/resources/mapper/business/DcTrafficIncidentsMapper.xml index eaa01cd6..a4dfb940 100644 --- a/zc-business/src/main/resources/mapper/business/DcTrafficIncidentsMapper.xml +++ b/zc-business/src/main/resources/mapper/business/DcTrafficIncidentsMapper.xml @@ -57,7 +57,7 @@ +