Browse Source

收费站枚举

develop
王兴琳 9 months ago
parent
commit
ee4fd54751
  1. 55
      zc-business/src/main/java/com/zc/business/enums/LocationEnum.java
  2. 69
      zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java

55
zc-business/src/main/java/com/zc/business/enums/LocationEnum.java

@ -0,0 +1,55 @@
package com.zc.business.enums;
import java.util.Optional;
/**
*
*/
public enum LocationEnum {
CHANGQING_UNIVERSITY_CITY(1, "长清大学城"),
CHANGQING(2, "长清"),
XIAOLI(3, "孝里"),
PINGYIN_NORTH(4, "平阴北"),
PINGYIN(5, "平阴"),
PINGYIN_SOUTH(6, "平阴南"),
DONGPING(7, "东平"),
LIANGSHAN_EAST(8, "梁山东"),
LIANGSHAN(9, "梁山"),
JIAXIANG_WEST(10, "嘉祥西");
private final int order;
private final String name;
LocationEnum(int order, String name) {
this.order = order;
this.name = name;
}
public int getOrder() {
return order;
}
public String getName() {
return name;
}
// 通过序号查找枚举值,未找到时返回null
public static String findByOrder(int order) {
for (LocationEnum location : LocationEnum.values()) {
if (location.getOrder() == order) {
return location.getName();
}
}
return " "; // 未找到匹配的枚举值
}
// 查找序号,通过地点名称
public static int findOrderByName(String name) {
for (LocationEnum location : LocationEnum.values()) {
if (location.getName().equals(name)) {
return location.getOrder();
}
}
return -1; // 未找到匹配的名称
}
}

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

@ -1,4 +1,5 @@
package com.zc.business.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
@ -12,6 +13,7 @@ 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.LocationEnum;
import com.zc.business.enums.StakeMarkRange;
import com.zc.business.enums.UniversalEnum;
import com.zc.business.service.IDcFacilityService;
@ -39,6 +41,7 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Service
public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsService {
@ -790,33 +793,42 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
ResponseBody body = response.body();
if (body != null) {
String jsonString = body.string();
if (JSON.isValidArray(jsonString)) {
JSONArray jsonArray = JSON.parseArray(jsonString);
Map<String, Integer> sumByName = new LinkedHashMap<>();
List<Map<String, String>> list = new ArrayList();
for (Object item : jsonArray) { // 这里做了微调,直接遍历jsonArray的Object
JSONObject jsonObject = (JSONObject) item;
if (!JSON.isValidArray(jsonString)) return Collections.emptyList();
// 获取当前时间
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类型
JSONArray jsonArray = JSON.parseArray(jsonString);
sumByName.put(name, totalFlow);
}
}
// 正确创建新的映射对象并添加到list中
for (Map.Entry<String, Integer> entry : sumByName.entrySet()) {
Map<String, String> singleResult = new LinkedHashMap<>(); // 每次循环都创建一个新的映射
singleResult.put("name", entry.getKey());
singleResult.put("value", entry.getValue().toString());
list.add(singleResult);
}
return list;
}
// 从JSON数据创建原始数据映射,构造为String类型值
Map<String, Map<String, String>> originalDataMap = jsonArray.stream()
.filter(item -> ((JSONObject) item).getInteger("data_hour") == LocalTime.now().getHour())
.collect(Collectors.toMap(
item -> ((JSONObject) item).getString("ts_name"),
item -> {
JSONObject jsonObject = (JSONObject) item;
return new HashMap<String, String>() {{
put("value", String.valueOf(jsonObject.getInteger("total_flow")));
put("orderId", jsonObject.getString("order_id"));
}};
},
(oldVal, newVal) -> oldVal, // 保留旧值以解决键冲突
LinkedHashMap::new // 保持插入顺序
));
// 直接基于原始数据映射和LocationEnum构建最终结果列表
List<Map<String, String>> resultList = Arrays.stream(LocationEnum.values())
.map(location -> {
Map<String, String> defaultData = new HashMap<String, String>() {{
put("value", "0");
put("orderId", String.valueOf(LocationEnum.findOrderByName(location.getName())));
}};
return new HashMap<String, String>() {{
put("name", location.getName());
put("value", originalDataMap.getOrDefault(location.getName(), defaultData).get("value"));
put("orderId", originalDataMap.getOrDefault(location.getName(), defaultData).get("orderId"));
}};
})
.collect(Collectors.toList());
return resultList;
}
return new ArrayList();
} catch (IOException e) {
@ -860,7 +872,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
String jsonString = body.string();
if (JSON.isValidArray(jsonString)) {
return JSON.parseArray(jsonString);
}else {
} else {
return new JSONArray();
}
}
@ -995,7 +1007,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
} else {
return new JSONArray();
}
}else {
} else {
return new JSONArray();
}
} catch (IOException e) {
@ -1236,8 +1248,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
}
return totalFlow1 + totalFlow2;
}
catch (IOException e) {
} catch (IOException e) {
// 处理异常
e.printStackTrace();
return UniversalEnum.ZERO.getNumber();

Loading…
Cancel
Save