Commit a862e4a6 authored by wujipeng's avatar wujipeng
Browse files

管径优化

parent cedc7ea3
......@@ -14,6 +14,7 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
......@@ -60,10 +61,16 @@ public class BasicServiceImpl extends ServiceImpl<BasicMapper, BasicEntity> impl
@Override
public Map<String, Integer> roadSummary(String type, String region) {
Map<String, Integer> result = new HashMap<>();
//获取基础信息
List<BasicEntity> basicList = list(new LambdaQueryWrapper<>(BasicEntity.class).eq(BasicEntity::getRegion, region));
Map<String, String> basicMap = new HashMap<>();
/*
* LinkedHashMap 可以保留道路的处理顺序。后续遇到拆分后的重复道路名时,
* 通过写入前检查 key 是否存在,只保留第一次写入的数量,避免后续记录覆盖它。
*/
Map<String, Integer> result = new LinkedHashMap<>();
// 按主键升序获取基础信息,使“第一次出现”的判定结果稳定可预期。
List<BasicEntity> basicList = list(new LambdaQueryWrapper<>(BasicEntity.class)
.eq(BasicEntity::getRegion, region)
.orderByAsc(BasicEntity::getId));
Map<String, String> basicMap = new LinkedHashMap<>();
for (BasicEntity basicEntity : basicList) {
/*
* roadName 在后续会被作为返回结果 Map 的 key,即 JSON 对象的字段名。
......@@ -88,9 +95,9 @@ public class BasicServiceImpl extends ServiceImpl<BasicMapper, BasicEntity> impl
basicMap.forEach((key, value) -> {
if (defectMap.containsKey(key)) {
Integer i = defectMap.get(key);
result.put(value, i);
putSplitRoadNames(result, value, i);
} else {
result.put(value, 0);
putSplitRoadNames(result, value, 0);
}
});
} else if ("修复".equals(type)) {
......@@ -106,12 +113,33 @@ public class BasicServiceImpl extends ServiceImpl<BasicMapper, BasicEntity> impl
basicMap.forEach((key, value) -> {
if (repairMap.containsKey(key)) {
Integer i = repairMap.get(key);
result.put(value, i);
putSplitRoadNames(result, value, i);
} else {
result.put(value, 0);
putSplitRoadNames(result, value, 0);
}
});
}
return result;
}
/**
* 将可能由中文顿号连接的组合道路名拆分后写入汇总结果。
* <p>
* 例如:“道路A、道路B”且数量为 4,会分别写入“道路A=4”和“道路B=4”。
* 如果拆分后的道路名已经存在,则保留第一次写入的数量;同时忽略空白片段,
* 避免返回空字符串 key。
*
* @param result 接口最终返回的道路数量映射
* @param roadName 数据库中的道路名,可能包含多个以“、”分隔的名称
* @param count 原组合道路对应的数量,拆分后每个道路都使用该数量
*/
private void putSplitRoadNames(Map<String, Integer> result, String roadName, Integer count) {
String[] roadNames = roadName.split("、");
for (String name : roadNames) {
String normalizedName = name.trim();
if (!normalizedName.isEmpty() && !result.containsKey(normalizedName)) {
result.put(normalizedName, count);
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment