|
|
@ -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 ""; |
|
|
|
} |
|
|
|
} |
|
|
|