|
@ -31,7 +31,9 @@ import org.springframework.stereotype.Service; |
|
|
import javax.annotation.PostConstruct; |
|
|
import javax.annotation.PostConstruct; |
|
|
import javax.annotation.Resource; |
|
|
import javax.annotation.Resource; |
|
|
import java.io.IOException; |
|
|
import java.io.IOException; |
|
|
|
|
|
import java.time.LocalDate; |
|
|
import java.time.LocalTime; |
|
|
import java.time.LocalTime; |
|
|
|
|
|
import java.time.format.DateTimeFormatter; |
|
|
import java.util.*; |
|
|
import java.util.*; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
import java.util.regex.Matcher; |
|
|
import java.util.regex.Matcher; |
|
@ -632,35 +634,27 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi |
|
|
ResponseBody body = response.body(); |
|
|
ResponseBody body = response.body(); |
|
|
if (body != null) { |
|
|
if (body != null) { |
|
|
JSONArray jsonArray = JSON.parseArray(body.string()); |
|
|
JSONArray jsonArray = JSON.parseArray(body.string()); |
|
|
|
|
|
Map<String, Integer> sumByName = new LinkedHashMap<>(); |
|
|
// 使用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
|
|
|
for (Object item : jsonArray) { // 这里做了微调,直接遍历jsonArray的Object
|
|
|
JSONObject jsonObject = (JSONObject) item; |
|
|
JSONObject jsonObject = (JSONObject) item; |
|
|
// 获取当前时间
|
|
|
// 获取当前时间
|
|
|
LocalTime now = LocalTime.now(); |
|
|
LocalTime now = LocalTime.now(); |
|
|
|
|
|
|
|
|
// 获取当前小时数(24小时制)
|
|
|
// 获取当前小时数(24小时制)
|
|
|
int currentHour = now.getHour(); |
|
|
int currentHour = now.getHour(); |
|
|
if (jsonObject.getInteger("data_hour") == currentHour) { |
|
|
if (jsonObject.getInteger("data_hour") == currentHour) { |
|
|
String name = jsonObject.getString("ts_name"); // 更安全的取值方式
|
|
|
String name = jsonObject.getString("ts_name"); // 更安全的取值方式
|
|
|
int totalFlow = jsonObject.getInteger("total_flow"); // 专门针对Integer类型
|
|
|
int totalFlow = jsonObject.getInteger("total_flow"); // 专门针对Integer类型
|
|
|
sumByName.put(name, totalFlow); |
|
|
sumByName.put(name, totalFlow); |
|
|
// sumByName.put(name, sumByName.getOrDefault(name, 0) + totalFlow);
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 输出结果
|
|
|
|
|
|
// 输出结果
|
|
|
|
|
|
// 正确创建新的映射对象并添加到list中
|
|
|
// 正确创建新的映射对象并添加到list中
|
|
|
for (Map.Entry<String, Integer> entry : sumByName.entrySet()) { |
|
|
for (Map.Entry<String, Integer> entry : sumByName.entrySet()) { |
|
|
Map<String, String> singleResult = new HashMap<>(); // 每次循环都创建一个新的映射
|
|
|
Map<String, String> singleResult = new LinkedHashMap<>(); // 每次循环都创建一个新的映射
|
|
|
singleResult.put("name", entry.getKey()); |
|
|
singleResult.put("name", entry.getKey()); |
|
|
singleResult.put("value", entry.getValue().toString()); |
|
|
singleResult.put("value", entry.getValue().toString()); |
|
|
list.add(singleResult); |
|
|
list.add(singleResult); |
|
|
//System.out.println(entry.getKey() + " 的 total_flow 总和为: " + entry.getValue());
|
|
|
|
|
|
} |
|
|
} |
|
|
return list; |
|
|
return list; |
|
|
} |
|
|
} |
|
@ -705,22 +699,107 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public List<Map<String, Object>> realTimeTrafficFlowHour(String startDate, Long direction) throws HttpException, IOException { |
|
|
public Map<String,List<Map<String, Object>>> realTimeTrafficFlowHour(String startDate, Long direction) throws HttpException, IOException { |
|
|
OkHttp okHttp = new OkHttp(); |
|
|
|
|
|
|
|
|
|
|
|
RequestParams requestParams = new RequestParams(); |
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
|
|
|
|
|
LocalDate currentDate = LocalDate.now(); |
|
|
|
|
|
String nowYear = currentDate.format(formatter); |
|
|
|
|
|
//String nowYear =startDate;
|
|
|
|
|
|
// 获取一年前的日期
|
|
|
|
|
|
LocalDate oneYearAgo = currentDate.minusYears(1); |
|
|
|
|
|
String lastYear = oneYearAgo.format(formatter); |
|
|
|
|
|
|
|
|
|
|
|
OkHttp okHttp = new OkHttp(); |
|
|
|
|
|
RequestParams requestParams = new RequestParams(); |
|
|
requestParams.put("sysid", sysid); |
|
|
requestParams.put("sysid", sysid); |
|
|
|
|
|
//求情地址 startDate 时间
|
|
|
|
|
|
JSONArray body = getResponseBody(nowYear, requestParams, okHttp); |
|
|
|
|
|
|
|
|
|
|
|
JSONArray bodylast = getResponseBody(lastYear, requestParams, okHttp); |
|
|
|
|
|
Map<String,List<Map<String, Object>>> map = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
|
|
if (body != null) { |
|
|
|
|
|
List<Map<String, Object>> mapList = getMaps("1", body); |
|
|
|
|
|
map.put("1",mapList); |
|
|
|
|
|
List<Map<String, Object>> mapList2 = getMaps("3", body); |
|
|
|
|
|
map.put("2",mapList2); |
|
|
|
|
|
} |
|
|
|
|
|
if (bodylast != null) { |
|
|
|
|
|
List<Map<String, Object>> mapList = getMaps("1", bodylast); |
|
|
|
|
|
map.put("3",mapList); |
|
|
|
|
|
List<Map<String, Object>> mapList2 = getMaps("3", bodylast); |
|
|
|
|
|
map.put("4",mapList2); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return map; |
|
|
|
|
|
} |
|
|
|
|
|
//处理 接口响应数据
|
|
|
|
|
|
private List<Map<String, Object>> getMaps(String direction, JSONArray jsonArray) throws IOException { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取当前时间
|
|
|
|
|
|
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(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", getDescriptionByIdentification(i)); |
|
|
|
|
|
map.put("totalFlow", flowCounts.get(i)); |
|
|
|
|
|
mapList.add(map); |
|
|
|
|
|
} |
|
|
|
|
|
return mapList; |
|
|
|
|
|
} |
|
|
|
|
|
// 获取车流量接口 返回 jsonArray
|
|
|
|
|
|
private JSONArray getResponseBody(String startDate, RequestParams requestParams, OkHttp okHttp) throws HttpException, IOException { |
|
|
JSONObject parameters = new JSONObject() { |
|
|
JSONObject parameters = new JSONObject() { |
|
|
{ |
|
|
{ |
|
|
put("start_date", startDate); |
|
|
put("start_date", startDate); |
|
|
put("end_date", startDate); |
|
|
put("end_date", startDate); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
requestParams.put("parameters", parameters.toJSONString()); |
|
|
requestParams.put("parameters", parameters.toJSONString()); |
|
|
|
|
|
|
|
|
Map<String, String> headers = new HashMap<>(); |
|
|
Map<String, String> headers = new HashMap<>(); |
|
|
headers.put("Authorization", getAccessToken()); |
|
|
headers.put("Authorization", getAccessToken()); |
|
|
|
|
|
|
|
@ -731,65 +810,8 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi |
|
|
.data(requestParams) // 请求参数
|
|
|
.data(requestParams) // 请求参数
|
|
|
.post(); // 请求方法
|
|
|
.post(); // 请求方法
|
|
|
ResponseBody body = response.body(); |
|
|
ResponseBody body = response.body(); |
|
|
if (body != null) { |
|
|
JSONArray jsonArray = JSON.parseArray(body.string()); |
|
|
|
|
|
return jsonArray; |
|
|
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", getDescriptionByIdentification(i)); |
|
|
|
|
|
map.put("totalFlow", flowCounts.get(i)); |
|
|
|
|
|
mapList.add(map); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return mapList; |
|
|
|
|
|
} |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -815,8 +837,19 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi |
|
|
} |
|
|
} |
|
|
return ""; |
|
|
return ""; |
|
|
} |
|
|
} |
|
|
/* |
|
|
|
|
|
public JSONArray realTimeTrafficFlowHour2(String startDate,Long direction) throws HttpException, IOException { |
|
|
public List<Map<String,Object>> queryTheGantryDataByPileNumber(String startDate,String stakeMark) throws HttpException, IOException { |
|
|
|
|
|
|
|
|
|
|
|
if (stakeMark != null) { |
|
|
|
|
|
//处理URl地址栏获取参数+号消失
|
|
|
|
|
|
stakeMark= stakeMark.replace(" ", "+"); |
|
|
|
|
|
} |
|
|
|
|
|
List<Map<String,Object>> mapList =new ArrayList<>(); |
|
|
|
|
|
DcFacility dcFacilityAll = new DcFacility(); |
|
|
|
|
|
dcFacilityAll.setFacilityType(10); |
|
|
|
|
|
List<DcFacility> dcFacilityList = facilityService.listFacility(dcFacilityAll); |
|
|
|
|
|
DcFacility nearestFacility = findNearestFacility(dcFacilityList,stakeMark); |
|
|
|
|
|
|
|
|
OkHttp okHttp = new OkHttp(); |
|
|
OkHttp okHttp = new OkHttp(); |
|
|
|
|
|
|
|
|
RequestParams requestParams = new RequestParams(); |
|
|
RequestParams requestParams = new RequestParams(); |
|
@ -845,41 +878,51 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi |
|
|
ResponseBody body = response.body(); |
|
|
ResponseBody body = response.body(); |
|
|
if (body != null) { |
|
|
if (body != null) { |
|
|
JSONArray jsonArray = JSON.parseArray(body.string()); |
|
|
JSONArray jsonArray = JSON.parseArray(body.string()); |
|
|
|
|
|
System.out.println(jsonArray); |
|
|
// 使用HashMap来分组并求和
|
|
|
for (Object item : jsonArray) { |
|
|
List<Map<String, Object>> list = new ArrayList(); |
|
|
|
|
|
|
|
|
|
|
|
for (Object item : jsonArray) { // 这里做了微调,直接遍历jsonArray的Object
|
|
|
|
|
|
JSONObject jsonObject = (JSONObject) item; |
|
|
JSONObject jsonObject = (JSONObject) item; |
|
|
// 获取当前时间
|
|
|
if (jsonObject.getString("gantry_id").equals(nearestFacility.getFacilityCode())) { |
|
|
LocalTime now = LocalTime.now(); |
|
|
Map<String, Object> map = new HashMap<>(); |
|
|
|
|
|
int totalFlow = jsonObject.getInteger("total_flow"); |
|
|
// 获取当前小时数(24小时制)
|
|
|
int data_hour = jsonObject.getInteger("data_hour"); |
|
|
int currentHour = now.getHour(); |
|
|
map.put("hour",data_hour); |
|
|
if (jsonObject.getInteger("data_hour") == 16) { |
|
|
map.put("totalFlow",totalFlow); |
|
|
|
|
|
mapList.add(map); |
|
|
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 jsonArray; |
|
|
return mapList; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return null; |
|
|
return null; |
|
|
} |
|
|
} |
|
|
*/ |
|
|
public DcFacility findNearestFacility(List<DcFacility> dcFacilityList, String stakeMarkCode) { |
|
|
|
|
|
// 将目标桩号转换为整数,以便比较
|
|
|
|
|
|
int targetCodeAsInt = Integer.parseInt(extract(stakeMarkCode)); |
|
|
|
|
|
|
|
|
|
|
|
DcFacility nearestFacility = null; |
|
|
|
|
|
int minDistance = Integer.MAX_VALUE; // 初始化为最大整数值,确保任何实际距离都会小于这个值
|
|
|
|
|
|
|
|
|
|
|
|
for (DcFacility facility : dcFacilityList) { |
|
|
|
|
|
// 假设每个DcFacility对象中有一个方法或直接字段可以获取处理后的桩号字符串
|
|
|
|
|
|
String stakeMark = facility.getStakeMark(); // 请替换为实际的getter方法名
|
|
|
|
|
|
|
|
|
|
|
|
// 将当前设施的桩号转换为整数
|
|
|
|
|
|
int currentCodeAsInt = Integer.parseInt(extract(stakeMark)); |
|
|
|
|
|
|
|
|
|
|
|
// 计算与目标桩号的距离(差值的绝对值)
|
|
|
|
|
|
int distance = Math.abs(targetCodeAsInt - currentCodeAsInt); |
|
|
|
|
|
|
|
|
|
|
|
// 如果当前桩号比之前记录的最近桩号更近,则更新最近桩号
|
|
|
|
|
|
if (distance < minDistance) { |
|
|
|
|
|
minDistance = distance; |
|
|
|
|
|
nearestFacility = facility; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return nearestFacility; // 返回最近的桩号信息
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|