From 94a587774f61daaf59e890c46526f0c10a503a56 Mon Sep 17 00:00:00 2001 From: tangwei Date: Fri, 5 Jun 2026 14:23:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E8=BF=87=E9=B1=BC?= =?UTF-8?q?=E8=AE=BE=E6=96=BD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/FishImportServiceImpl.java | 195 ++++++++++++++++-- 1 file changed, 183 insertions(+), 12 deletions(-) diff --git a/backend/src/main/java/com/yfd/platform/qgc_data/service/impl/FishImportServiceImpl.java b/backend/src/main/java/com/yfd/platform/qgc_data/service/impl/FishImportServiceImpl.java index a0f59e16..f9629141 100644 --- a/backend/src/main/java/com/yfd/platform/qgc_data/service/impl/FishImportServiceImpl.java +++ b/backend/src/main/java/com/yfd/platform/qgc_data/service/impl/FishImportServiceImpl.java @@ -40,6 +40,7 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; @Service @Slf4j @@ -213,9 +214,23 @@ public class FishImportServiceImpl implements IFishImportService { } + Map rvcdMap = new HashMap<>(); if (!allowedRvcdSet.isEmpty() || !directStcdSet.isEmpty()) { if (!allowedRvcdSet.isEmpty()) { - List stationsFromHbrv = engInfoBHMapper.selectByReachcdList(new ArrayList<>(allowedRvcdSet)); + List activeRvcdList = rvcdDicMapper.selectList(new LambdaQueryWrapper() + .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 expandedReachcdSet = expandReachcdScope(allowedRvcdSet, rvcdMap); + List stationsFromHbrv = engInfoBHMapper.selectByReachcdList(new ArrayList<>(expandedReachcdSet)); for (SdEngInfoBH station : stationsFromHbrv) { if (station.getStcd() != null) { directStcdSet.add(station.getStcd()); @@ -229,7 +244,7 @@ public class FishImportServiceImpl implements IFishImportService { if (station.getStcd() != null) { directStcdSet.add(station.getStcd()); } - if (station.getRvcd() != null) { + if (station.getReachcd() != null) { allowedRvcdSet.add(station.getReachcd()); } } @@ -237,8 +252,34 @@ public class FishImportServiceImpl implements IFishImportService { } if(!allowedRvcdSet.isEmpty()){ - List sdRvcdDics = rvcdDicMapper.selectList(new LambdaQueryWrapper().in(SdRvcdDic::getRvcd, allowedRvcdSet).select(SdRvcdDic::getRvcd, SdRvcdDic::getRvnm)); - allowedRvcdList.addAll(sdRvcdDics); + if (rvcdMap.isEmpty()) { + List activeRvcdList = rvcdDicMapper.selectList(new LambdaQueryWrapper() + .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 rvcdDisplayNameMap = new HashMap<>(); + Map 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()) { @@ -251,6 +292,20 @@ public class FishImportServiceImpl implements IFishImportService { } + Map 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 allowedRvcdLastNameToCodeMap = buildAllowedRvcdLastNameToCodeMap(allowedRvcdList); + Map 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 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 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(); for (int i = 1; i <= totalRows; i++) { Row row = sheet.getRow(i); @@ -258,7 +313,9 @@ public class FishImportServiceImpl implements IFishImportService { 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); if (importRow.getData() != null && importRow.getWarnings().isEmpty()) { result.addSuccessRow(importRow); @@ -274,7 +331,12 @@ public class FishImportServiceImpl implements IFishImportService { return result; } - private FishImportResult.FishImportRow parseRow(FishImportResult result, Row row, Map columnIndexMap, int rowIndex, String userId,List allowRvcdList,List directStcdList,List sdFpssList) { + private FishImportResult.FishImportRow parseRow(FishImportResult result, Row row, Map columnIndexMap, + int rowIndex, String userId, Map allowRvcdNameToCodeMap, + Map allowRvcdLastNameToCodeMap, + Map allowRvcdCodeToNameMap, + Map directStationNameToCodeMap, + Map fpssNameToCodeMap) { FishImportResult.FishImportRow importRow = new FishImportResult.FishImportRow(rowIndex); @@ -294,7 +356,7 @@ public class FishImportServiceImpl implements IFishImportService { data.setEnnm(cellValue.trim()); data.setRstcd(cellValue); } 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)) { importRow.getWarnings().add("rstcd"); data.setEnnm(cellValue.trim()); @@ -325,17 +387,17 @@ public class FishImportServiceImpl implements IFishImportService { case "rvcd": if (!StringUtils.hasText(cellValue)) { importRow.getWarnings().add("rvcd"); - data.setHbrvcd(cellValue); - data.setHbrvnm(cellValue); + data.setRvcd(cellValue); + data.setRvnm(cellValue); } 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)) { importRow.getWarnings().add("rvcd"); data.setRvcd(cellValue.trim()); data.setRvnm(cellValue.trim()); } else { data.setRvcd(rvcdCode); - data.setRvnm(cellValue.trim()); + data.setRvnm(allowRvcdCodeToNameMap.getOrDefault(rvcdCode, cellValue.trim())); } } break; @@ -536,7 +598,7 @@ public class FishImportServiceImpl implements IFishImportService { importRow.getWarnings().add("stcd"); data.setStcd(cellValue); } 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)) { importRow.getWarnings().add("stcd"); data.setStcd(cellValue.trim()); @@ -561,6 +623,115 @@ public class FishImportServiceImpl implements IFishImportService { return importRow; } + private Map buildAllowedRvcdLastNameToCodeMap(List allowedRvcdList) { + Map uniqueMap = new LinkedHashMap<>(); + Set 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 allowRvcdNameToCodeMap, + Map 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 expandReachcdScope(Set allowedRvcdSet, Map rvcdMap) { + if (allowedRvcdSet == null || allowedRvcdSet.isEmpty() || rvcdMap == null || rvcdMap.isEmpty()) { + return Collections.emptySet(); + } + Set result = new LinkedHashSet<>(); + for (String rvcd : rvcdMap.keySet()) { + if (isInRvcdScope(rvcd, allowedRvcdSet, rvcdMap)) { + result.add(rvcd); + } + } + return result; + } + + private boolean isInRvcdScope(String currentRvcd, Set allowedRvcdSet, Map rvcdMap) { + String code = currentRvcd; + Set 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 rvcdMap, Map 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 rvcdMap, Map 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; + } + /** * 解析鱼类体长范围