From f37a281f6ccccfdc8cd0561f390ed02855bb7b32 Mon Sep 17 00:00:00 2001 From: dyhj625 Date: Thu, 20 Mar 2025 10:14:04 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B3=B5=ED=9C=B4=EC=9D=BC=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=EC=9E=90=20=EC=A7=80=EC=A0=95=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../localhost/service/localvacaService.java | 66 ++++++++++++++----- 1 file changed, 51 insertions(+), 15 deletions(-) diff --git a/src/main/java/io/company/localhost/service/localvacaService.java b/src/main/java/io/company/localhost/service/localvacaService.java index bb061b5..656bf43 100644 --- a/src/main/java/io/company/localhost/service/localvacaService.java +++ b/src/main/java/io/company/localhost/service/localvacaService.java @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Value; @@ -48,6 +49,22 @@ public class localvacaService { @Value("${api.public-holiday.key}") private String serviceKey; + + private static final List CUSTOM_HOLIDAYS = new ArrayList<>(); + + // 초기 사용자 지정 공휴일 추가 + static { + MapDto customHoliday1 = new MapDto(); + customHoliday1.put("monthDay", "05-01"); + customHoliday1.put("name", "근로자의 날"); + CUSTOM_HOLIDAYS.add(customHoliday1); + +// MapDto customHoliday2 = new MapDto(); +// customHoliday2.put("date", "2025-03-21"); +// customHoliday2.put("name", "사용자 지정 예시"); +// +// CUSTOM_HOLIDAYS.add(customHoliday2); + } public void insertVacation(MapDto vacation) { // 필요한 경우 데이터 검증/전처리 후 매퍼 호출 @@ -86,37 +103,56 @@ public class localvacaService { } } - public List selectVacationList(int year, int month) { return localvacaMapper.selectVacations(year, month); } - - + public List selectHolidays(int year, int month) { - // ✅ ServiceKey를 디코딩해서 사용 - String decodedServiceKey = URLDecoder.decode(serviceKey, StandardCharsets.UTF_8); - System.out.println("📌 디코딩된 ServiceKey: " + decodedServiceKey); + // API에서 공휴일 가져오기 + List apiHolidays = fetchHolidaysFromApi(year, month); - // ✅ URI를 직접 문자열로 구성하여 ServiceKey가 다시 인코딩되지 않도록 함 + // 사용자 정의 공휴일 추가 (monthDay: 전체 연도 / date: 특정 연도) + List userDefinedHolidays = CUSTOM_HOLIDAYS.stream() + .filter(holiday -> + Optional.ofNullable(holiday.getString("date")) + .map(d -> d.startsWith(year + "-")) // 특정 연도의 공휴일 + .orElse(false) // date가 있으면 연도 매칭 후 포함 + + || Optional.ofNullable(holiday.getString("monthDay")) + .map(md -> md.startsWith(String.format("%02d-", month))) // 모든 연도에 적용되는 monthDay + .orElse(false) + ) + .map(holiday -> { + MapDto newHoliday = new MapDto(); + // 특정 연도 공휴일이면 그대로 사용, 모든 연도 공휴일이면 해당 연도 추가 + newHoliday.put("date", holiday.containsKey("date") ? holiday.getString("date") : year + "-" + holiday.getString("monthDay")); + newHoliday.put("name", holiday.getString("name")); + return newHoliday; + }) + .collect(Collectors.toList()); + + // API 공휴일과 사용자 정의 공휴일 합치기 + List combinedHolidays = new ArrayList<>(); + combinedHolidays.addAll(apiHolidays); + combinedHolidays.addAll(userDefinedHolidays); + + return combinedHolidays; + } + + private List fetchHolidaysFromApi(int year, int month) { + String decodedServiceKey = URLDecoder.decode(serviceKey, StandardCharsets.UTF_8); String url = "http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getRestDeInfo" + "?solYear=" + year + "&solMonth=" + String.format("%02d", month) - + "&ServiceKey=" + decodedServiceKey // ✅ 디코딩된 상태로 직접 추가 + + "&ServiceKey=" + decodedServiceKey + "&_type=json"; - System.out.println("📌 API 요청 URL: " + url); - - // ✅ API 요청 헤더 추가 (User-Agent 포함) HttpHeaders headers = new HttpHeaders(); headers.set(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"); HttpEntity entity = new HttpEntity<>(headers); - - // ✅ `exchange` 메서드를 사용하여 요청 ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, entity, Map.class); - System.out.println("📌 API 응답 데이터: " + response.getBody()); - return parseResponse(response.getBody()); }