Browse Source

全路段双向最近一小时车流量

develop
王兴琳 6 months ago
parent
commit
39d086cc76
  1. 30
      zc-business/src/main/java/com/zc/business/controller/DcTrafficStatisticsController.java
  2. 52
      zc-business/src/main/java/com/zc/business/enums/StakeMarkRange.java
  3. 1
      zc-business/src/main/java/com/zc/business/service/IDcFacilityService.java
  4. 1
      zc-business/src/main/java/com/zc/business/service/IDcGantryStatisticsDataService.java
  5. 5
      zc-business/src/main/java/com/zc/business/service/IDcTrafficStatisticsService.java
  6. 13
      zc-business/src/main/java/com/zc/business/service/impl/DcFacilityServiceImpl.java
  7. 262
      zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java

30
zc-business/src/main/java/com/zc/business/controller/DcTrafficStatisticsController.java

@ -272,9 +272,9 @@ public AjaxResult sectionHourlyTrafficFlow(String startDate, String endDate) thr
return AjaxResult.success(jsonArray);
}/**
* 各收费站入口分车型小时车流量
* 各收费站入口分车型车流量
*/
@ApiOperation("各收费站入口分车型小时车流量")
@ApiOperation("各收费站入口分车型车流量")
@GetMapping("/history/trafficFlowAtTollStationEntrance")
public AjaxResult trafficFlowAtTollStationEntrance(String startDate, String endDate,String stationType) throws HttpException, IOException {
// 调用服务层方法,获取当前交通指标数据
@ -282,4 +282,30 @@ public AjaxResult trafficFlowAtTollStationEntrance(String startDate, String endD
// 将获取到的交通指标数据封装为成功的结果并返回
return AjaxResult.success(jsonArray);
}
/**
* 各收费站入口分车型小时车流量
*/
@ApiOperation("各收费站入口分车型小时车流量")
@GetMapping("/history/trafficFlowAtTollStationEntranceHour")
public AjaxResult trafficFlowAtTollStationEntranceHour(String startDate, String endDate,String stationType) throws HttpException, IOException {
// 调用服务层方法,获取当前交通指标数据
JSONArray jsonArray = dcTrafficStatisticsService.trafficFlowAtTollStationEntranceHour(startDate, endDate, stationType);
// 将获取到的交通指标数据封装为成功的结果并返回
return AjaxResult.success(jsonArray);
}
/**
* 全路段双向实时车流量
* @param startDate 时间
* @param direction 方向
*/
@ApiOperation("全路段双向实时车流量")
@GetMapping("/history/realTimeTrafficFlowHour")
public AjaxResult realTimeTrafficFlowHour(String startDate,Long direction) throws HttpException, IOException{
List<Map<String,Object>> mapList = dcTrafficStatisticsService.realTimeTrafficFlowHour(startDate,direction);
// 将查询结果封装为成功响应并返回
return AjaxResult.success(mapList);
}
}

52
zc-business/src/main/java/com/zc/business/enums/StakeMarkRange.java

@ -0,0 +1,52 @@
package com.zc.business.enums;
/**
*
*/
public enum StakeMarkRange {
ONE(54394, 59289, 1,"殷家林枢纽"),
TWO(59289, 72847,2, "大学城立交"),
THREE(72847, 83835,3, "长清立交"),
FOUR(83835, 86499,4, "松竹枢纽"),
FIVE(86499, 99750,5, "孝里立交"),
SIX(99750, 105904, 6,"平阴北立交"),
SEVEN(105904, 117878,7, "平阴立交"),
EIGHT(117878, 126233,8, "孔村枢纽"),
NINE(126233, 145933, 9,"平阴南立交"),
TEN(145933, 155652, 10,"东平立交"),
DONGPING_LAKE_HUB(155652,173950,11,"东平湖枢纽"),
LIANGSHANDONG_INTERCHANGE(173950,179396,12,"梁山东立交"),
LIANGSHAN_INTERCHANGE(179396,190495,13,"梁山立交"),
JIAXIANG_WEST_INTERCHANGE(190495,202979,14,"嘉祥西立交");
private final int stakeMark;
private final int endMark;
private final int identification;
private final String description;
public int getStakeMark() {
return stakeMark;
}
public int getEndMark() {
return endMark;
}
public String getDescription() {
return description;
}
public int getIdentification() {
return identification;
}
StakeMarkRange(int stakeMark, int endMark, int identification, String description) {
this.stakeMark = stakeMark;
this.endMark = endMark;
this.identification = identification;
this.description = description;
}
}

1
zc-business/src/main/java/com/zc/business/service/IDcFacilityService.java

@ -60,4 +60,5 @@ public interface IDcFacilityService extends IService<DcFacility> {
* @return 设备信息
*/
DcFacility getFacility(String id);
DcFacility getfacilityCode(String id);
}

1
zc-business/src/main/java/com/zc/business/service/IDcGantryStatisticsDataService.java

@ -39,4 +39,5 @@ public interface IDcGantryStatisticsDataService extends IService<DcGantryStatist
* 全路段双向实时车流量
*/
List<Map<String, String>> realTimeTrafficFlow(String startDate, String direction,String periodType );
}

5
zc-business/src/main/java/com/zc/business/service/IDcTrafficStatisticsService.java

@ -51,4 +51,9 @@ public interface IDcTrafficStatisticsService {
* 各收费站入口分车型小时车流量
*/
List<Map<String, String>>trafficFlowAtTollStationEntrance(String startDate, String endDate,String stationType) throws HttpException, IOException;
JSONArray trafficFlowAtTollStationEntranceHour(String startDate, String endDate, String stationType)throws HttpException, IOException;
List<Map<String, Object>> realTimeTrafficFlowHour(String startDate,Long direction) throws HttpException, IOException;
}

13
zc-business/src/main/java/com/zc/business/service/impl/DcFacilityServiceImpl.java

@ -8,6 +8,7 @@ import com.ruoyi.common.utils.PageUtils;
import com.zc.business.domain.DcFacility;
import com.zc.business.mapper.DcFacilityMapper;
import com.zc.business.service.IDcFacilityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@ -23,7 +24,8 @@ import java.util.Objects;
@Service
public class DcFacilityServiceImpl extends ServiceImpl<DcFacilityMapper, DcFacility> implements IDcFacilityService {
@Autowired
private DcFacilityMapper dcFacilityMapper;
public LambdaQueryWrapper<DcFacility> facilityQueryWrapper(DcFacility dcFacility) {
LambdaQueryWrapper<DcFacility> queryWrapper = new LambdaQueryWrapper<>();
@ -140,5 +142,14 @@ public class DcFacilityServiceImpl extends ServiceImpl<DcFacilityMapper, DcFacil
return getById(id);
}
@Override
public DcFacility getfacilityCode(String id) {
LambdaQueryWrapper<DcFacility> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(DcFacility::getFacilityCode, id);
return dcFacilityMapper.selectOne(queryWrapper);
}
}

262
zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java

@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zc.business.domain.*;
import com.zc.business.enums.StakeMarkRange;
import com.zc.business.service.IDcFacilityService;
import com.zc.business.service.IDcRoadSectionService;
import com.zc.business.service.IDcTrafficStatisticsService;
@ -23,7 +24,11 @@ import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalTime;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Service
public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsService {
@ -31,7 +36,6 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
// 智慧高速平台token
private JSONObject token = null;
private final String sysid = "sdgs_it_hs_jihe";
private String baseUrl = "http://10.166.139.16:8080";
@Resource
@ -79,7 +83,8 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
.post(new Callback() {
// 请求失败时的回调处理,此处未实现具体逻辑
@Override
public void onFailure(Call call, IOException e) {}
public void onFailure(Call call, IOException e) {
}
// 请求成功时的回调处理
@Override
@ -267,13 +272,13 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
*
* @return List<DcRoadSectionCongestion> 当前路段拥堵情况列表
* @throws HttpException 抛出HttpException异常当HTTP请求发生错误时
* @throws IOException 抛出IOException异常当发生输入/输出错误时
* @throws IOException 抛出IOException异常当发生输入/输出错误时
*/
@Override
public List<DcRoadSectionCongestion> currentSectionCongested() throws HttpException, IOException {
// 调用方法获取当前拥堵事件信息
JSONArray currentEventCongested = currentEventCongested();
JSONArray currentEventCongested = currentEventCongested();
// 如果没有拥堵事件,则直接返回空列表
if (currentEventCongested == null || currentEventCongested.isEmpty()) {
@ -297,7 +302,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
dcRoadSectionCongestion.setDownCongestionSections(new ArrayList<>());
// 遍历当前拥堵事件,计算每个路段的拥堵情况
for (Object object :currentEventCongested) {
for (Object object : currentEventCongested) {
if (object instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) object;
calculateSectionCongestion(jsonObject, dcRoadSectionCongestion);
@ -315,10 +320,10 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
/**
* 根据给定的JSON对象计算路段的拥堵情况
*
* @param jsonObject 包含拥堵信息的JSON对象包括拥堵开始桩号结束桩号和拥堵距离
* @param jsonObject 包含拥堵信息的JSON对象包括拥堵开始桩号结束桩号和拥堵距离
* @param dcRoadSectionCongestion 包含路段基本信息和拥堵情况的对象需要根据计算结果更新拥堵信息
*/
private void calculateSectionCongestion(JSONObject jsonObject, DcRoadSectionCongestion dcRoadSectionCongestion){
private void calculateSectionCongestion(JSONObject jsonObject, DcRoadSectionCongestion dcRoadSectionCongestion) {
// 根据方向,计算对应方向的路段拥堵情况
String dirCode = jsonObject.getString("dir_code");
@ -408,7 +413,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
// 路段包含结束拥堵桩号不包含开始拥堵桩号
if (endPileNo >= startStakeMark && endPileNo <= endStakeMark && startPileNo <= startStakeMark) {
congestionSection.setCongestionStartStakeMark(startStakeMark);
congestionSection.setCongestionEndStakeMark((int)endPileNo);
congestionSection.setCongestionEndStakeMark((int) endPileNo);
congestionSection.setCongestionDistance((endStakeMark - startStakeMark));
congestionSections.add(congestionSection);
@ -543,11 +548,12 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
return new JSONArray();
}
/**
/**
* 各收费站入口分车型小时车流量
*/
@Override
public List<Map<String, String>> trafficFlowAtTollStationEntrance(String startDate, String endDate,String stationType) throws HttpException, IOException {
public List<Map<String, String>> trafficFlowAtTollStationEntrance(String startDate, String endDate, String stationType) throws HttpException, IOException {
OkHttp okHttp = new OkHttp();
RequestParams requestParams = new RequestParams();
@ -580,14 +586,21 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
// 使用HashMap来分组并求和
Map<String, Integer> sumByName = new HashMap<>();
List<Map<String, String>> list = new ArrayList();
List<Map<String, String>> list = new ArrayList();
for (Object item : jsonArray) { // 这里做了微调,直接遍历jsonArray的Object
JSONObject jsonObject = (JSONObject) item;
String name = jsonObject.getString("ts_name"); // 更安全的取值方式
int totalFlow = jsonObject.getInteger("total_flow"); // 专门针对Integer类型
sumByName.put(name, sumByName.getOrDefault(name, 0) + totalFlow);
// 获取当前时间
LocalTime now = LocalTime.now();
// 获取当前小时数(24小时制)
int currentHour = now.getHour();
if (jsonObject.getInteger("data_hour") == currentHour) {
String name = jsonObject.getString("ts_name"); // 更安全的取值方式
int totalFlow = jsonObject.getInteger("total_flow"); // 专门针对Integer类型
sumByName.put(name, totalFlow);
// sumByName.put(name, sumByName.getOrDefault(name, 0) + totalFlow);
}
}
// 输出结果
@ -598,7 +611,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
singleResult.put("name", entry.getKey());
singleResult.put("value", entry.getValue().toString());
list.add(singleResult);
System.out.println(entry.getKey() + " 的 total_flow 总和为: " + entry.getValue());
//System.out.println(entry.getKey() + " 的 total_flow 总和为: " + entry.getValue());
}
return list;
}
@ -606,4 +619,221 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
return new ArrayList();
}
@Override
public JSONArray trafficFlowAtTollStationEntranceHour(String startDate, String endDate, String stationType) throws HttpException, IOException {
OkHttp okHttp = new OkHttp();
RequestParams requestParams = new RequestParams();
requestParams.put("sysid", sysid);
JSONObject parameters = new JSONObject() {
{
put("start_date", startDate);
put("end_date", endDate);
put("station_type", stationType);
}
};
requestParams.put("parameters", parameters.toJSONString());
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", getAccessToken());
Response response // 请求响应
= okHttp
.headers(headers)
.url(baseUrl + "/api/dc/query/ts_jihe_d_vehtypeflowbyhourstation") // 请求地址
.data(requestParams) // 请求参数
.post(); // 请求方法
ResponseBody body = response.body();
if (body != null) {
return JSON.parseArray(body.string());
}
return new JSONArray();
}
@Override
public List<Map<String, Object>> realTimeTrafficFlowHour(String startDate,Long direction) throws HttpException, IOException {
OkHttp okHttp = new OkHttp();
RequestParams requestParams = new RequestParams();
requestParams.put("sysid", sysid);
JSONObject parameters = new JSONObject() {
{
put("start_date", startDate);
put("end_date", startDate);
}
};
requestParams.put("parameters", parameters.toJSONString());
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", getAccessToken());
Response response // 请求响应
= okHttp
.headers(headers)
.url(baseUrl + "/api/dc/query/gan_jihe_d_vehtypeflow") // 请求地址
.data(requestParams) // 请求参数
.post(); // 请求方法
ResponseBody body = response.body();
if (body != null) {
JSONArray jsonArray = JSON.parseArray(body.string());
// 获取当前时间
LocalTime now = LocalTime.now();
// 获取当前小时数(24小时制)
int currentHour = now.getHour();
// 初始化计数器和描述映射
Map<Integer, Integer> flowCounts = new HashMap<>();
Map<Integer, String> descriptions = new HashMap<>();
for (int i = 1; i <= 14; i++) {
flowCounts.put(i, 0);
descriptions.put(i, "");
}
for (Object item : jsonArray) {
JSONObject jsonObject = (JSONObject) item;
if (jsonObject.getInteger("data_hour") == currentHour) {
int totalFlow = jsonObject.getInteger("total_flow");
String gantryId = jsonObject.getString("gantry_id");
DcFacility dcFacilityAll = new DcFacility();
dcFacilityAll.setFacilityType(10);
dcFacilityAll.setDirection(String.valueOf(direction));
List<DcFacility> dcFacilityList = facilityService.listFacility(dcFacilityAll);
DcFacility dcFacility2 = facilityService.getfacilityCode(gantryId);
if (dcFacility2 != null) {
String stakeMark = dcFacility2.getStakeMark();
boolean exists = dcFacilityList.stream()
.anyMatch(dcFacility -> dcFacility.getId().equals(dcFacility2.getId()));
if (exists) {
int extractedNumber = Integer.parseInt(extract(stakeMark));
int identification = Arrays.stream(StakeMarkRange.values())
.filter(smRange -> extractedNumber >= smRange.getStakeMark() && extractedNumber <= smRange.getEndMark())
.mapToInt(StakeMarkRange::getIdentification)
.findFirst()
.orElse(0);
String description = getDescriptionByIdentification(identification);
descriptions.put(identification, description);
flowCounts.put(identification, flowCounts.get(identification) + totalFlow);
}
}
}
}
// 构建结果列表
List<Map<String, Object>> mapList = new ArrayList<>();
for (int i = 1; i <= 14; i++) {
Map<String, Object> map = new HashMap<>();
map.put("name", descriptions.get(i));
map.put("totalFlow", flowCounts.get(i));
mapList.add(map);
}
return mapList;
}
return null;
}
/**
*
* @param startDate
* @param direction
* @return
* @throws HttpException
* @throws IOException
*/
public List<Map<String, Object>> realTimeTrafficFlowHour2(String startDate,Long direction) throws HttpException, IOException {
OkHttp okHttp = new OkHttp();
RequestParams requestParams = new RequestParams();
requestParams.put("sysid", sysid);
JSONObject parameters = new JSONObject() {
{
put("start_date", startDate);
put("end_date", startDate);
}
};
requestParams.put("parameters", parameters.toJSONString());
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", getAccessToken());
Response response // 请求响应
= okHttp
.headers(headers)
.url(baseUrl + "/api/dc/query/gan_jihe_d_vehtypeflow") // 请求地址
.data(requestParams) // 请求参数
.post(); // 请求方法
ResponseBody body = response.body();
if (body != null) {
JSONArray jsonArray = JSON.parseArray(body.string());
// 使用HashMap来分组并求和
List<Map<String, Object>> list = new ArrayList();
for (Object item : jsonArray) { // 这里做了微调,直接遍历jsonArray的Object
JSONObject jsonObject = (JSONObject) item;
// 获取当前时间
LocalTime now = LocalTime.now();
// 获取当前小时数(24小时制)
int currentHour = now.getHour();
if (jsonObject.getInteger("data_hour") == 16) {
Map<String,Object> sumByName = new HashMap<>();
String name = jsonObject.getString("gantry_name"); // 更安全的取值方式
int totalFlow = jsonObject.getInteger("total_flow"); // 专门针对Integer类型
String gantryId = jsonObject.getString("gantry_id"); // 专门针对Integer类型
String data_hour = jsonObject.getString("data_hour"); // 专门针对Integer类型
sumByName.put("naame", name);
sumByName.put("gantryId", gantryId);
sumByName.put("totalFlow", totalFlow);
sumByName.put("data_hour", data_hour);
list.add(sumByName);
}
}
// 输出结果
return list;
}
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 "";
}
}

Loading…
Cancel
Save