fix: 优化过鱼设施逻辑

This commit is contained in:
tangwei 2026-06-05 14:23:54 +08:00
parent 4522383206
commit 94a587774f

View File

@ -40,6 +40,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Service @Service
@Slf4j @Slf4j
@ -213,9 +214,23 @@ public class FishImportServiceImpl implements IFishImportService {
} }
Map<String, SdRvcdDic> rvcdMap = new HashMap<>();
if (!allowedRvcdSet.isEmpty() || !directStcdSet.isEmpty()) { if (!allowedRvcdSet.isEmpty() || !directStcdSet.isEmpty()) {
if (!allowedRvcdSet.isEmpty()) { if (!allowedRvcdSet.isEmpty()) {
List<SdEngInfoBH> stationsFromHbrv = engInfoBHMapper.selectByReachcdList(new ArrayList<>(allowedRvcdSet)); List<SdRvcdDic> activeRvcdList = rvcdDicMapper.selectList(new LambdaQueryWrapper<SdRvcdDic>()
.eq(SdRvcdDic::getEnabled, 1)
.eq(SdRvcdDic::getIsDeleted, 0)
.select(SdRvcdDic::getRvcd, SdRvcdDic::getRvnm, SdRvcdDic::getPrvcd, SdRvcdDic::getOrderIndex));
if (activeRvcdList != null) {
for (SdRvcdDic rvcdDic : activeRvcdList) {
if (StrUtil.isNotBlank(rvcdDic.getRvcd())) {
rvcdMap.put(rvcdDic.getRvcd(), rvcdDic);
}
}
}
Set<String> expandedReachcdSet = expandReachcdScope(allowedRvcdSet, rvcdMap);
List<SdEngInfoBH> stationsFromHbrv = engInfoBHMapper.selectByReachcdList(new ArrayList<>(expandedReachcdSet));
for (SdEngInfoBH station : stationsFromHbrv) { for (SdEngInfoBH station : stationsFromHbrv) {
if (station.getStcd() != null) { if (station.getStcd() != null) {
directStcdSet.add(station.getStcd()); directStcdSet.add(station.getStcd());
@ -229,7 +244,7 @@ public class FishImportServiceImpl implements IFishImportService {
if (station.getStcd() != null) { if (station.getStcd() != null) {
directStcdSet.add(station.getStcd()); directStcdSet.add(station.getStcd());
} }
if (station.getRvcd() != null) { if (station.getReachcd() != null) {
allowedRvcdSet.add(station.getReachcd()); allowedRvcdSet.add(station.getReachcd());
} }
} }
@ -237,8 +252,34 @@ public class FishImportServiceImpl implements IFishImportService {
} }
if(!allowedRvcdSet.isEmpty()){ if(!allowedRvcdSet.isEmpty()){
List<SdRvcdDic> sdRvcdDics = rvcdDicMapper.selectList(new LambdaQueryWrapper<SdRvcdDic>().in(SdRvcdDic::getRvcd, allowedRvcdSet).select(SdRvcdDic::getRvcd, SdRvcdDic::getRvnm)); if (rvcdMap.isEmpty()) {
allowedRvcdList.addAll(sdRvcdDics); List<SdRvcdDic> activeRvcdList = rvcdDicMapper.selectList(new LambdaQueryWrapper<SdRvcdDic>()
.eq(SdRvcdDic::getEnabled, 1)
.eq(SdRvcdDic::getIsDeleted, 0)
.select(SdRvcdDic::getRvcd, SdRvcdDic::getRvnm, SdRvcdDic::getPrvcd, SdRvcdDic::getOrderIndex));
if (activeRvcdList != null) {
for (SdRvcdDic rvcdDic : activeRvcdList) {
if (StrUtil.isNotBlank(rvcdDic.getRvcd())) {
rvcdMap.put(rvcdDic.getRvcd(), rvcdDic);
}
}
}
}
Map<String, String> rvcdDisplayNameMap = new HashMap<>();
Map<String, String> rvcdOrderPathMap = new HashMap<>();
for (String rvcd : allowedRvcdSet) {
rvcdDisplayNameMap.put(rvcd, buildRvcdDisplayName(rvcd, rvcdMap, rvcdDisplayNameMap));
rvcdOrderPathMap.put(rvcd, buildRvcdOrderPath(rvcd, rvcdMap, rvcdOrderPathMap));
}
allowedRvcdList.addAll(allowedRvcdSet.stream()
.map(rvcdMap::get)
.filter(Objects::nonNull)
.peek(item -> item.setRvnm(rvcdDisplayNameMap.getOrDefault(item.getRvcd(), item.getRvnm())))
.sorted(Comparator.comparing((SdRvcdDic item) -> rvcdOrderPathMap.getOrDefault(item.getRvcd(), "~~~~~~~~~~"))
.thenComparing(SdRvcdDic::getRvcd, Comparator.nullsLast(String::compareTo)))
.toList());
} }
if (!directStcdSet.isEmpty()) { if (!directStcdSet.isEmpty()) {
@ -251,6 +292,20 @@ public class FishImportServiceImpl implements IFishImportService {
} }
Map<String, String> allowedRvcdNameToCodeMap = allowedRvcdList.stream()
.filter(item -> StrUtil.isNotBlank(item.getRvnm()) && StrUtil.isNotBlank(item.getRvcd()))
.collect(Collectors.toMap(SdRvcdDic::getRvnm, SdRvcdDic::getRvcd, (left, right) -> left, LinkedHashMap::new));
Map<String, String> allowedRvcdLastNameToCodeMap = buildAllowedRvcdLastNameToCodeMap(allowedRvcdList);
Map<String, String> allowedRvcdCodeToNameMap = allowedRvcdList.stream()
.filter(item -> StrUtil.isNotBlank(item.getRvcd()) && StrUtil.isNotBlank(item.getRvnm()))
.collect(Collectors.toMap(SdRvcdDic::getRvcd, SdRvcdDic::getRvnm, (left, right) -> left, LinkedHashMap::new));
Map<String, String> directStationNameToCodeMap = directStcdList.stream()
.filter(item -> StrUtil.isNotBlank(item.getEnnm()) && StrUtil.isNotBlank(item.getStcd()))
.collect(Collectors.toMap(SdEngInfoBH::getEnnm, SdEngInfoBH::getStcd, (left, right) -> left, LinkedHashMap::new));
Map<String, String> fpssNameToCodeMap = sdFpssList.stream()
.filter(item -> StrUtil.isNotBlank(item.getStnm()) && StrUtil.isNotBlank(item.getStcd()))
.collect(Collectors.toMap(SdFpssBH::getStnm, SdFpssBH::getStcd, (left, right) -> left, LinkedHashMap::new));
int totalRows = sheet.getLastRowNum(); int totalRows = sheet.getLastRowNum();
for (int i = 1; i <= totalRows; i++) { for (int i = 1; i <= totalRows; i++) {
Row row = sheet.getRow(i); Row row = sheet.getRow(i);
@ -258,7 +313,9 @@ public class FishImportServiceImpl implements IFishImportService {
continue; continue;
} }
FishImportResult.FishImportRow importRow = parseRow(result, row, columnIndexMap, i, uploadUserId,allowedRvcdList,directStcdList,sdFpssList); FishImportResult.FishImportRow importRow = parseRow(result, row, columnIndexMap, i, uploadUserId,
allowedRvcdNameToCodeMap, allowedRvcdLastNameToCodeMap, allowedRvcdCodeToNameMap,
directStationNameToCodeMap, fpssNameToCodeMap);
result.setTotalCount(result.getTotalCount() + 1); result.setTotalCount(result.getTotalCount() + 1);
if (importRow.getData() != null && importRow.getWarnings().isEmpty()) { if (importRow.getData() != null && importRow.getWarnings().isEmpty()) {
result.addSuccessRow(importRow); result.addSuccessRow(importRow);
@ -274,7 +331,12 @@ public class FishImportServiceImpl implements IFishImportService {
return result; return result;
} }
private FishImportResult.FishImportRow parseRow(FishImportResult result, Row row, Map<Integer, String> columnIndexMap, int rowIndex, String userId,List<SdRvcdDic> allowRvcdList,List<SdEngInfoBH> directStcdList,List<SdFpssBH> sdFpssList) { private FishImportResult.FishImportRow parseRow(FishImportResult result, Row row, Map<Integer, String> columnIndexMap,
int rowIndex, String userId, Map<String, String> allowRvcdNameToCodeMap,
Map<String, String> allowRvcdLastNameToCodeMap,
Map<String, String> allowRvcdCodeToNameMap,
Map<String, String> directStationNameToCodeMap,
Map<String, String> fpssNameToCodeMap) {
FishImportResult.FishImportRow importRow = new FishImportResult.FishImportRow(rowIndex); FishImportResult.FishImportRow importRow = new FishImportResult.FishImportRow(rowIndex);
@ -294,7 +356,7 @@ public class FishImportServiceImpl implements IFishImportService {
data.setEnnm(cellValue.trim()); data.setEnnm(cellValue.trim());
data.setRstcd(cellValue); data.setRstcd(cellValue);
} else { } else {
String stcd = directStcdList.stream().filter(sdEngInfoBH -> sdEngInfoBH.getEnnm().equals(cellValue.trim())).map(SdEngInfoBH::getStcd).findFirst().orElse( null); String stcd = directStationNameToCodeMap.get(cellValue.trim());
if (StrUtil.isBlank(stcd)) { if (StrUtil.isBlank(stcd)) {
importRow.getWarnings().add("rstcd"); importRow.getWarnings().add("rstcd");
data.setEnnm(cellValue.trim()); data.setEnnm(cellValue.trim());
@ -325,17 +387,17 @@ public class FishImportServiceImpl implements IFishImportService {
case "rvcd": case "rvcd":
if (!StringUtils.hasText(cellValue)) { if (!StringUtils.hasText(cellValue)) {
importRow.getWarnings().add("rvcd"); importRow.getWarnings().add("rvcd");
data.setHbrvcd(cellValue); data.setRvcd(cellValue);
data.setHbrvnm(cellValue); data.setRvnm(cellValue);
} else { } else {
String rvcdCode = allowRvcdList.stream().filter(sdRvcdDic -> sdRvcdDic.getRvnm().equals(cellValue.trim())).map(SdRvcdDic::getRvcd).findFirst().orElse( null); String rvcdCode = resolveAllowedRvcdCode(cellValue.trim(), allowRvcdNameToCodeMap, allowRvcdLastNameToCodeMap);
if (StrUtil.isBlank(rvcdCode)) { if (StrUtil.isBlank(rvcdCode)) {
importRow.getWarnings().add("rvcd"); importRow.getWarnings().add("rvcd");
data.setRvcd(cellValue.trim()); data.setRvcd(cellValue.trim());
data.setRvnm(cellValue.trim()); data.setRvnm(cellValue.trim());
} else { } else {
data.setRvcd(rvcdCode); data.setRvcd(rvcdCode);
data.setRvnm(cellValue.trim()); data.setRvnm(allowRvcdCodeToNameMap.getOrDefault(rvcdCode, cellValue.trim()));
} }
} }
break; break;
@ -536,7 +598,7 @@ public class FishImportServiceImpl implements IFishImportService {
importRow.getWarnings().add("stcd"); importRow.getWarnings().add("stcd");
data.setStcd(cellValue); data.setStcd(cellValue);
} else { } else {
String stcd = sdFpssList.stream().filter(sdFpssBH -> sdFpssBH.getStnm().equals(cellValue.trim())).map(SdFpssBH::getStcd).findFirst().orElse( null); String stcd = fpssNameToCodeMap.get(cellValue.trim());
if (StrUtil.isBlank(stcd)) { if (StrUtil.isBlank(stcd)) {
importRow.getWarnings().add("stcd"); importRow.getWarnings().add("stcd");
data.setStcd(cellValue.trim()); data.setStcd(cellValue.trim());
@ -561,6 +623,115 @@ public class FishImportServiceImpl implements IFishImportService {
return importRow; return importRow;
} }
private Map<String, String> buildAllowedRvcdLastNameToCodeMap(List<SdRvcdDic> allowedRvcdList) {
Map<String, String> uniqueMap = new LinkedHashMap<>();
Set<String> duplicateNames = new HashSet<>();
for (SdRvcdDic item : allowedRvcdList) {
if (item == null || StrUtil.isBlank(item.getRvnm()) || StrUtil.isBlank(item.getRvcd())) {
continue;
}
String lastName = extractLastRvcdName(item.getRvnm());
if (StrUtil.isBlank(lastName)) {
continue;
}
String existed = uniqueMap.putIfAbsent(lastName, item.getRvcd());
if (existed != null && !Objects.equals(existed, item.getRvcd())) {
duplicateNames.add(lastName);
}
}
duplicateNames.forEach(uniqueMap::remove);
return uniqueMap;
}
private String resolveAllowedRvcdCode(String inputName, Map<String, String> allowRvcdNameToCodeMap,
Map<String, String> allowRvcdLastNameToCodeMap) {
if (StrUtil.isBlank(inputName)) {
return null;
}
String exactCode = allowRvcdNameToCodeMap.get(inputName);
if (StrUtil.isNotBlank(exactCode)) {
return exactCode;
}
return allowRvcdLastNameToCodeMap.get(extractLastRvcdName(inputName));
}
private String extractLastRvcdName(String rvnm) {
if (StrUtil.isBlank(rvnm)) {
return null;
}
int separatorIndex = rvnm.lastIndexOf('-');
return separatorIndex >= 0 ? StrUtil.trim(rvnm.substring(separatorIndex + 1)) : StrUtil.trim(rvnm);
}
private Set<String> expandReachcdScope(Set<String> allowedRvcdSet, Map<String, SdRvcdDic> rvcdMap) {
if (allowedRvcdSet == null || allowedRvcdSet.isEmpty() || rvcdMap == null || rvcdMap.isEmpty()) {
return Collections.emptySet();
}
Set<String> result = new LinkedHashSet<>();
for (String rvcd : rvcdMap.keySet()) {
if (isInRvcdScope(rvcd, allowedRvcdSet, rvcdMap)) {
result.add(rvcd);
}
}
return result;
}
private boolean isInRvcdScope(String currentRvcd, Set<String> allowedRvcdSet, Map<String, SdRvcdDic> rvcdMap) {
String code = currentRvcd;
Set<String> visited = new HashSet<>();
while (StrUtil.isNotBlank(code) && visited.add(code)) {
if (allowedRvcdSet.contains(code)) {
return true;
}
SdRvcdDic current = rvcdMap.get(code);
if (current == null || StrUtil.isBlank(current.getPrvcd()) || "0".equals(current.getPrvcd())) {
break;
}
code = current.getPrvcd();
}
return false;
}
private String buildRvcdDisplayName(String rvcd, Map<String, SdRvcdDic> rvcdMap, Map<String, String> cache) {
if (StrUtil.isBlank(rvcd)) {
return null;
}
if (cache.containsKey(rvcd)) {
return cache.get(rvcd);
}
SdRvcdDic current = rvcdMap.get(rvcd);
if (current == null) {
return rvcd;
}
String currentName = StrUtil.blankToDefault(current.getRvnm(), rvcd);
String parentCode = current.getPrvcd();
if (StrUtil.isBlank(parentCode) || "0".equals(parentCode)) {
return currentName;
}
String parentName = buildRvcdDisplayName(parentCode, rvcdMap, cache);
return StrUtil.isBlank(parentName) ? currentName : parentName + "-" + currentName;
}
private String buildRvcdOrderPath(String rvcd, Map<String, SdRvcdDic> rvcdMap, Map<String, String> cache) {
if (StrUtil.isBlank(rvcd)) {
return null;
}
if (cache.containsKey(rvcd)) {
return cache.get(rvcd);
}
SdRvcdDic current = rvcdMap.get(rvcd);
if (current == null) {
return "~~~~~~~~~~/" + rvcd;
}
String currentSegment = String.format("%010d", current.getOrderIndex() == null ? 999999999 : current.getOrderIndex());
String parentCode = current.getPrvcd();
if (StrUtil.isBlank(parentCode) || "0".equals(parentCode)) {
return currentSegment;
}
String parentPath = buildRvcdOrderPath(parentCode, rvcdMap, cache);
return StrUtil.isBlank(parentPath) ? currentSegment : parentPath + "/" + currentSegment;
}
/** /**
* 解析鱼类体长范围 * 解析鱼类体长范围