From 4af693ec1df675931414071ffc33623d3bc8c6c4 Mon Sep 17 00:00:00 2001 From: lilloo04 Date: Fri, 19 Sep 2025 13:12:28 +0900 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=EC=A7=80=EC=97=AD=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EB=A7=A4=EC=B9=AD=20=EB=A1=9C=EC=A7=81=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0=20=EB=B0=8F=20=EC=A0=95=EA=B7=9C=ED=99=94=20=EC=A7=80?= =?UTF-8?q?=EC=9B=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../place/service/TatsCnctrService.java | 88 +++++++++++++++++-- .../region/repository/RegionRepository.java | 5 +- 2 files changed, 83 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/comma/soomteum/domain/place/service/TatsCnctrService.java b/src/main/java/com/comma/soomteum/domain/place/service/TatsCnctrService.java index 421fdab..0e1a1ad 100644 --- a/src/main/java/com/comma/soomteum/domain/place/service/TatsCnctrService.java +++ b/src/main/java/com/comma/soomteum/domain/place/service/TatsCnctrService.java @@ -50,15 +50,57 @@ public Mono getCnctrRate( log.info("[TatsCnctr] 혼잡도 조회 시작: title='{}', area={}, sigungu={}", req.getTitle(), req.getAreacode(), req.getSigungucode()); - return Mono.fromCallable(() -> - regionRepository.findByKorAreaCodeAndKorSigunguCode( - req.getAreacode(), req.getSigungucode()) - .orElseThrow(() -> { - log.error("[TatsCnctr] 지역 코드 조회 실패: area={}, sigungu={}", - req.getAreacode(), req.getSigungucode()); - return new CustomException(ErrorCode.REGION_NOT_FOUND); - }) - ) + return Mono.fromCallable(() -> { + String areaCode = req.getAreacode(); + String sigunguCode = req.getSigungucode(); + + log.debug("[TatsCnctr] 입력 코드: areacode='{}', sigungucode='{}'", areaCode, sigunguCode); + + // 1. 먼저 정확한 area/sigungu 코드로 조회 + var region = regionRepository.findByKorAreaCodeAndKorSigunguCode(areaCode, sigunguCode); + if (region.isPresent()) { + log.debug("[TatsCnctr] 정확한 매칭 성공: {}", region.get().getName()); + return region.get(); + } + + // 2. area 코드만으로 조회 시도 + log.warn("[TatsCnctr] 정확한 지역 코드 매칭 실패, area 코드만으로 재시도: area={}, sigungu={}", + areaCode, sigunguCode); + + var regionByArea = regionRepository.findByKorAreaCode(areaCode); + if (regionByArea.isPresent()) { + log.info("[TatsCnctr] area 코드로 지역 찾음: {}", regionByArea.get().getName()); + return regionByArea.get(); + } + + // 3. 코드 변환 시도 (예: "32" -> "1", "01" -> "1" 등) + String normalizedAreaCode = normalizeAreaCode(areaCode); + String normalizedSigunguCode = normalizeSigunguCode(sigunguCode); + + if (!areaCode.equals(normalizedAreaCode) || !sigunguCode.equals(normalizedSigunguCode)) { + log.info("[TatsCnctr] 코드 정규화 시도: {}→{}, {}→{}", + areaCode, normalizedAreaCode, sigunguCode, normalizedSigunguCode); + + var normalizedRegion = regionRepository.findByKorAreaCodeAndKorSigunguCode( + normalizedAreaCode, normalizedSigunguCode); + if (normalizedRegion.isPresent()) { + log.info("[TatsCnctr] 정규화 코드로 지역 찾음: {}", normalizedRegion.get().getName()); + return normalizedRegion.get(); + } + + // area 코드만으로도 시도 + var normalizedRegionByArea = regionRepository.findByKorAreaCode(normalizedAreaCode); + if (normalizedRegionByArea.isPresent()) { + log.info("[TatsCnctr] 정규화 area 코드로 지역 찾음: {}", normalizedRegionByArea.get().getName()); + return normalizedRegionByArea.get(); + } + } + + // 4. 모든 시도 실패 + log.error("[TatsCnctr] 지역 코드 조회 완전 실패: area={}, sigungu={}", + areaCode, sigunguCode); + throw new CustomException(ErrorCode.REGION_NOT_FOUND); + }) .subscribeOn(Schedulers.boundedElastic()) .flatMap(region -> { @@ -162,4 +204,32 @@ private static boolean isRetryable(Throwable t) { private static void qpIfPresent(UriBuilder b, String name, String value) { if (value != null && !value.isBlank()) b.queryParam(name, value); } + + /** + * 지역 코드를 정규화합니다. + * 예: "32" -> "1", "01" -> "1" + */ + private String normalizeAreaCode(String areaCode) { + if (areaCode == null) return null; + + // 서울: 1 + if ("01".equals(areaCode)) return "1"; + // 인천: 2 (실제 매핑은 데이터에 따라 조정 필요) + if ("02".equals(areaCode)) return "2"; + // 강원: 32 -> 1 (예시, 실제 데이터에 맞게 조정) + if ("32".equals(areaCode)) return "1"; + + // 기본적으로 앞의 0을 제거하고 반환 + return areaCode.replaceFirst("^0+", ""); + } + + /** + * 시군구 코드를 정규화합니다. + */ + private String normalizeSigunguCode(String sigunguCode) { + if (sigunguCode == null) return null; + + // 기본적으로 앞의 0을 제거하고 반환 + return sigunguCode.replaceFirst("^0+", ""); + } } diff --git a/src/main/java/com/comma/soomteum/domain/region/repository/RegionRepository.java b/src/main/java/com/comma/soomteum/domain/region/repository/RegionRepository.java index d1936d9..9d76aa8 100644 --- a/src/main/java/com/comma/soomteum/domain/region/repository/RegionRepository.java +++ b/src/main/java/com/comma/soomteum/domain/region/repository/RegionRepository.java @@ -9,6 +9,9 @@ public interface RegionRepository extends JpaRepository { @EntityGraph(attributePaths = "cnctrSigungus") Optional findByKorAreaCodeAndKorSigunguCode(String korAreaCode, String korSigunguCode); - + + @EntityGraph(attributePaths = "cnctrSigungus") + Optional findByKorAreaCode(String korAreaCode); + Optional findByName(String name); } From 82aae4000bd78e81d055bb38cdf7368a63d5954a Mon Sep 17 00:00:00 2001 From: lilloo04 Date: Fri, 19 Sep 2025 13:20:15 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20=EC=A7=80=EC=97=AD=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=A0=95=EA=B7=9C=ED=99=94=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../place/service/TatsCnctrService.java | 52 +------------------ 1 file changed, 1 insertion(+), 51 deletions(-) diff --git a/src/main/java/com/comma/soomteum/domain/place/service/TatsCnctrService.java b/src/main/java/com/comma/soomteum/domain/place/service/TatsCnctrService.java index 0e1a1ad..99aa533 100644 --- a/src/main/java/com/comma/soomteum/domain/place/service/TatsCnctrService.java +++ b/src/main/java/com/comma/soomteum/domain/place/service/TatsCnctrService.java @@ -73,30 +73,7 @@ public Mono getCnctrRate( return regionByArea.get(); } - // 3. 코드 변환 시도 (예: "32" -> "1", "01" -> "1" 등) - String normalizedAreaCode = normalizeAreaCode(areaCode); - String normalizedSigunguCode = normalizeSigunguCode(sigunguCode); - - if (!areaCode.equals(normalizedAreaCode) || !sigunguCode.equals(normalizedSigunguCode)) { - log.info("[TatsCnctr] 코드 정규화 시도: {}→{}, {}→{}", - areaCode, normalizedAreaCode, sigunguCode, normalizedSigunguCode); - - var normalizedRegion = regionRepository.findByKorAreaCodeAndKorSigunguCode( - normalizedAreaCode, normalizedSigunguCode); - if (normalizedRegion.isPresent()) { - log.info("[TatsCnctr] 정규화 코드로 지역 찾음: {}", normalizedRegion.get().getName()); - return normalizedRegion.get(); - } - - // area 코드만으로도 시도 - var normalizedRegionByArea = regionRepository.findByKorAreaCode(normalizedAreaCode); - if (normalizedRegionByArea.isPresent()) { - log.info("[TatsCnctr] 정규화 area 코드로 지역 찾음: {}", normalizedRegionByArea.get().getName()); - return normalizedRegionByArea.get(); - } - } - - // 4. 모든 시도 실패 + // 3. 모든 시도 실패 log.error("[TatsCnctr] 지역 코드 조회 완전 실패: area={}, sigungu={}", areaCode, sigunguCode); throw new CustomException(ErrorCode.REGION_NOT_FOUND); @@ -205,31 +182,4 @@ private static void qpIfPresent(UriBuilder b, String name, String value) { if (value != null && !value.isBlank()) b.queryParam(name, value); } - /** - * 지역 코드를 정규화합니다. - * 예: "32" -> "1", "01" -> "1" - */ - private String normalizeAreaCode(String areaCode) { - if (areaCode == null) return null; - - // 서울: 1 - if ("01".equals(areaCode)) return "1"; - // 인천: 2 (실제 매핑은 데이터에 따라 조정 필요) - if ("02".equals(areaCode)) return "2"; - // 강원: 32 -> 1 (예시, 실제 데이터에 맞게 조정) - if ("32".equals(areaCode)) return "1"; - - // 기본적으로 앞의 0을 제거하고 반환 - return areaCode.replaceFirst("^0+", ""); - } - - /** - * 시군구 코드를 정규화합니다. - */ - private String normalizeSigunguCode(String sigunguCode) { - if (sigunguCode == null) return null; - - // 기본적으로 앞의 0을 제거하고 반환 - return sigunguCode.replaceFirst("^0+", ""); - } }