Browse Source

首页交通流报错

develop
王兴琳 5 months ago
parent
commit
25fefe027b
  1. 223
      zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java

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

@ -5,17 +5,16 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.sun.xml.bind.v2.TODO;
import com.zc.business.constant.RedisKeyConstants;
import com.zc.business.domain.*;
import com.zc.business.domain.DcCongestionSection;
import com.zc.business.domain.DcFacility;
import com.zc.business.domain.DcRoadSection;
import com.zc.business.domain.DcRoadSectionCongestion;
import com.zc.business.enums.StakeMarkRange;
import com.zc.business.enums.UniversalEnum;
import com.zc.business.service.IDcFacilityService;
import com.zc.business.service.IDcRoadConditionInformationService;
import com.zc.business.service.IDcRoadSectionService;
import com.zc.business.service.IDcTrafficStatisticsService;
import com.zc.business.utils.StakeMarkUtils;
@ -28,7 +27,6 @@ import okhttp3.Response;
import okhttp3.ResponseBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
@ -119,7 +117,6 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
}
*/
public static CompletableFuture<String> refreshAccessToken() {
CompletableFuture<String> future = new CompletableFuture<>();
OkHttp okHttp = new OkHttp();
@ -208,7 +205,12 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
ResponseBody body = response.body();
if (body != null) {
return JSON.parseArray(body.string());
String jsonString = body.string();
if (JSON.isValidArray(jsonString)) {
return JSON.parseArray(jsonString);
} else {
return new JSONArray();
}
}
return new JSONArray();
@ -318,7 +320,12 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
ResponseBody body = response.body();
if (body != null) {
return JSON.parseArray(body.string());
String jsonString = body.string();
if (JSON.isValidArray(jsonString)) {
return JSON.parseArray(jsonString);
} else {
return new JSONArray();
}
}
return new JSONArray();
}
@ -563,7 +570,12 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
ResponseBody body = response.body();
if (body != null) {
return JSON.parseArray(body.string());
String jsonString = body.string();
if (JSON.isValidArray(jsonString)) {
return JSON.parseArray(jsonString);
} else {
return new JSONArray();
}
}
return new JSONArray();
@ -604,7 +616,12 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
ResponseBody body = response.body();
if (body != null) {
return JSON.parseArray(body.string());
String jsonString = body.string();
if (JSON.isValidArray(jsonString)) {
return JSON.parseArray(jsonString);
} else {
return new JSONArray();
}
}
return new JSONArray();
@ -642,7 +659,12 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
ResponseBody body = response.body();
if (body != null) {
return JSON.parseArray(body.string());
String jsonString = body.string();
if (JSON.isValidArray(jsonString)) {
return JSON.parseArray(jsonString);
}else {
return new JSONArray();
}
}
return new JSONArray();
@ -684,12 +706,12 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
if (body != null) {
JSONArray jsonArray = JSON.parseArray(body.string());
Map<String, Integer> sumByName = new LinkedHashMap<>();
Integer totalFlow1=0;
Integer totalFlow1 = 0;
List<Map<String, String>> list = new ArrayList();
for (Object item : jsonArray) { // 这里做了微调,直接遍历jsonArray的Object
JSONObject jsonObject = (JSONObject) item;
totalFlow1=totalFlow1+ jsonObject.getInteger("total_flow");
totalFlow1 = totalFlow1 + jsonObject.getInteger("total_flow");
// 获取当前时间
LocalTime now = LocalTime.now();
@ -702,7 +724,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
sumByName.put(name, totalFlow);
}
}
System.out.println("总数===:"+totalFlow1);
System.out.println("总数===:" + totalFlow1);
// 正确创建新的映射对象并添加到list中
for (Map.Entry<String, Integer> entry : sumByName.entrySet()) {
Map<String, String> singleResult = new LinkedHashMap<>(); // 每次循环都创建一个新的映射
@ -746,7 +768,12 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
ResponseBody body = response.body();
if (body != null) {
return JSON.parseArray(body.string());
String jsonString = body.string();
if (JSON.isValidArray(jsonString)) {
return JSON.parseArray(jsonString);
}else {
return new JSONArray();
}
}
return new JSONArray();
@ -866,8 +893,15 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
.data(requestParams) // 请求参数
.post(); // 请求方法
ResponseBody body = response.body();
JSONArray jsonArray = JSON.parseArray(body.string());
return jsonArray;
if (body != null) {
String jsonString = body.string();
if (JSON.isValidArray(jsonString)) {
return JSON.parseArray(jsonString);
}else {
return new JSONArray();
}
}
return new JSONArray();
}
//格式化桩号
@ -933,8 +967,9 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
ResponseBody body = response.body();
if (body != null) {
JSONArray jsonArray = JSON.parseArray(body.string());
System.out.println(jsonArray);
String jsonString = body.string();
if (JSON.isValidArray(jsonString)) {
JSONArray jsonArray = JSON.parseArray(jsonString);
for (Object item : jsonArray) {
JSONObject jsonObject = (JSONObject) item;
if (jsonObject.getString("gantry_id").equals(nearestFacility.getFacilityCode())) {
@ -946,7 +981,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
mapList.add(map);
}
}
}
// 输出结果
return mapList;
@ -982,100 +1017,110 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
ResponseBody body = response.body();
if (body != null) {
JSONArray jsonArray = JSON.parseArray(body.string());
for (Object object : jsonArray) {
JSONObject jsonObject = (JSONObject) object;
Integer asOneRoad = jsonObject.getInteger("as_one_road");
if (jsonObject.getDouble("end_pile_no")!=null){
double endPileNo = jsonObject.getDouble("end_pile_no");
String endMark = formatNumber(endPileNo);
jsonObject.put("endMark", endMark);//结束桩号
}
double startPileNo = jsonObject.getDouble("start_pile_no");
String stakeMark = formatNumber(startPileNo);
jsonObject.put("stakeMark", stakeMark);//
//as_one_road
//是否为同一条路,1-是,0-否
if (asOneRoad == UniversalEnum.ZERO.getNumber()) {
String string = jsonObject.getString("list_link_info");
JSONArray jsonlist = JSON.parseArray(string);
JSONObject o = (JSONObject) jsonlist.get(UniversalEnum.ZERO.getNumber());
String string1 = o.getString("firstPoint");
String string2 = o.getString("lastPoint");
String lngLats = o.getString("lngLats");
String[] lngLatsString = lngLats.split(";");
double s = Double.parseDouble(lngLatsString[UniversalEnum.ZERO.getNumber()].split(",")[UniversalEnum.ZERO.getNumber()]);
double s2 = Double.parseDouble(lngLatsString[UniversalEnum.ZERO.getNumber()].split(",")[UniversalEnum.ONE.getNumber()]);
double s3 = Double.parseDouble(lngLatsString[lngLatsString.length - UniversalEnum.ONE.getNumber()].split(",")[UniversalEnum.ZERO.getNumber()]);
double s4 = Double.parseDouble(lngLatsString[lngLatsString.length - UniversalEnum.ONE.getNumber()].split(",")[UniversalEnum.ONE.getNumber()]);
double distanceM = calculateDistance(s2, s, s4, s3);
// System.out.printf("最大距离为: %.2f 米%n", distanceM);
jsonObject.put("max_jam_dist", distanceM);//最大拥堵距离
double endMake = startPileNo +Double.parseDouble(String.format("%.3f", distanceM));
String endmark = formatNumber(endMake);
jsonObject.put("endMark", endmark);//结束桩号
String[] split = string1.split(",");
double firstLon = Double.parseDouble(split[UniversalEnum.ZERO.getNumber()]);
double firstLat = Double.parseDouble(split[UniversalEnum.ONE.getNumber()]);
String[] split2 = string2.split(",");
double lastLon = Double.parseDouble(split2[UniversalEnum.ZERO.getNumber()]);
double lastLat = Double.parseDouble(split2[UniversalEnum.ONE.getNumber()]);
//double firstLat = 35.38524527319016;
// double firstLon = 118.39808642864227;
//double lastLat = 35.386351346969604;
// double lastLon = 118.4038907289505;
double distanceKm = calculateDistance(firstLat, firstLon, lastLat, lastLon);
double distanceMeters = distanceKm * UniversalEnum.THOUSAND.getNumber(); // 将距离转换为米
//System.out.printf("两点之间的距离为: %.2f 米%n", distanceMeters);
jsonObject.put("jam_dist", distanceMeters);//当前拥堵距离
String jsonString = body.string();
if (JSON.isValidArray(jsonString)) {
JSONArray jsonArray = JSON.parseArray(jsonString);
for (Object object : jsonArray) {
JSONObject jsonObject = (JSONObject) object;
Integer asOneRoad = jsonObject.getInteger("as_one_road");
if (jsonObject.getDouble("end_pile_no") != null) {
double endPileNo = jsonObject.getDouble("end_pile_no");
String endMark = formatNumber(endPileNo);
jsonObject.put("endMark", endMark);//结束桩号
}
double startPileNo = jsonObject.getDouble("start_pile_no");
String stakeMark = formatNumber(startPileNo);
jsonObject.put("stakeMark", stakeMark);//
//as_one_road
//是否为同一条路,1-是,0-否
if (asOneRoad == UniversalEnum.ZERO.getNumber()) {
String string = jsonObject.getString("list_link_info");
JSONArray jsonlist = JSON.parseArray(string);
JSONObject o = (JSONObject) jsonlist.get(UniversalEnum.ZERO.getNumber());
String string1 = o.getString("firstPoint");
String string2 = o.getString("lastPoint");
String lngLats = o.getString("lngLats");
String[] lngLatsString = lngLats.split(";");
double s = Double.parseDouble(lngLatsString[UniversalEnum.ZERO.getNumber()].split(",")[UniversalEnum.ZERO.getNumber()]);
double s2 = Double.parseDouble(lngLatsString[UniversalEnum.ZERO.getNumber()].split(",")[UniversalEnum.ONE.getNumber()]);
double s3 = Double.parseDouble(lngLatsString[lngLatsString.length - UniversalEnum.ONE.getNumber()].split(",")[UniversalEnum.ZERO.getNumber()]);
double s4 = Double.parseDouble(lngLatsString[lngLatsString.length - UniversalEnum.ONE.getNumber()].split(",")[UniversalEnum.ONE.getNumber()]);
double distanceM = calculateDistance(s2, s, s4, s3);
// System.out.printf("最大距离为: %.2f 米%n", distanceM);
jsonObject.put("max_jam_dist", distanceM);//最大拥堵距离
double endMake = startPileNo + Double.parseDouble(String.format("%.3f", distanceM));
String endmark = formatNumber(endMake);
jsonObject.put("endMark", endmark);//结束桩号
String[] split = string1.split(",");
double firstLon = Double.parseDouble(split[UniversalEnum.ZERO.getNumber()]);
double firstLat = Double.parseDouble(split[UniversalEnum.ONE.getNumber()]);
String[] split2 = string2.split(",");
double lastLon = Double.parseDouble(split2[UniversalEnum.ZERO.getNumber()]);
double lastLat = Double.parseDouble(split2[UniversalEnum.ONE.getNumber()]);
//double firstLat = 35.38524527319016;
// double firstLon = 118.39808642864227;
//double lastLat = 35.386351346969604;
// double lastLon = 118.4038907289505;
double distanceKm = calculateDistance(firstLat, firstLon, lastLat, lastLon);
double distanceMeters = distanceKm * UniversalEnum.THOUSAND.getNumber(); // 将距离转换为米
//System.out.printf("两点之间的距离为: %.2f 米%n", distanceMeters);
jsonObject.put("jam_dist", distanceMeters);//当前拥堵距离
}
}
}
return jsonArray;
return jsonArray;
}
}
return new JSONArray();
}
/**
* 各收费站日累计车流辆
*
* @return
*/
@Override
public int trafficFlowAtToll()throws HttpException, IOException {
public int trafficFlowAtToll() throws HttpException, IOException {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(UniversalEnum.TIME_FORMAT_YEARS_MONTH_DAY.getValue());
LocalDate currentDate = LocalDate.now();
String startDate = currentDate.format(formatter);
Response response = getResponseTrafficFlowAtToll(startDate,UniversalEnum.ONE.getValue());
Response responseTwo = getResponseTrafficFlowAtToll(startDate,UniversalEnum.TWO.getValue());
Response response = getResponseTrafficFlowAtToll(startDate, UniversalEnum.ONE.getValue());
Response responseTwo = getResponseTrafficFlowAtToll(startDate, UniversalEnum.TWO.getValue());
ResponseBody body = response.body();
ResponseBody bodyTwo = responseTwo.body();
Integer totalFlow1=UniversalEnum.ZERO.getNumber();
Integer totalFlow2=UniversalEnum.ZERO.getNumber();
Integer totalFlow1 = UniversalEnum.ZERO.getNumber();
Integer totalFlow2 = UniversalEnum.ZERO.getNumber();
if (body != null) {
JSONArray jsonArray = JSON.parseArray(body.string());
for (Object item : jsonArray) {
JSONObject jsonObject = (JSONObject) item;
totalFlow1=totalFlow1+ jsonObject.getInteger("total_flow");
String jsonString = body.string();
if (JSON.isValidArray(jsonString)) {
JSONArray jsonArray = JSON.parseArray(jsonString);
for (Object item : jsonArray) {
JSONObject jsonObject = (JSONObject) item;
totalFlow1 = totalFlow1 + jsonObject.getInteger("total_flow");
}
}
}
if (bodyTwo != null) {
JSONArray jsonArray = JSON.parseArray(bodyTwo.string());
for (Object item : jsonArray) {
JSONObject jsonObject = (JSONObject) item;
totalFlow2=totalFlow2+ jsonObject.getInteger("total_flow");
String jsonString = bodyTwo.string();
if (JSON.isValidArray(jsonString)) {
JSONArray jsonArray = JSON.parseArray(jsonString);
for (Object item : jsonArray) {
JSONObject jsonObject = (JSONObject) item;
totalFlow2 = totalFlow2 + jsonObject.getInteger("total_flow");
}
}
}
return totalFlow1+totalFlow2;
return totalFlow1 + totalFlow2;
}
private Response getResponseTrafficFlowAtToll(String startDate,String stationType) throws HttpException, IOException {
private Response getResponseTrafficFlowAtToll(String startDate, String stationType) throws HttpException, IOException {
OkHttp okHttp = new OkHttp();
RequestParams requestParams = new RequestParams();

Loading…
Cancel
Save