공휴일 사용자 지정 추가
This commit is contained in:
parent
1643a5fc7c
commit
f37a281f6c
@ -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<MapDto> 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<MapDto> selectVacationList(int year, int month) {
|
||||
return localvacaMapper.selectVacations(year, month);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<MapDto> selectHolidays(int year, int month) {
|
||||
// ✅ ServiceKey를 디코딩해서 사용
|
||||
String decodedServiceKey = URLDecoder.decode(serviceKey, StandardCharsets.UTF_8);
|
||||
System.out.println("📌 디코딩된 ServiceKey: " + decodedServiceKey);
|
||||
// API에서 공휴일 가져오기
|
||||
List<MapDto> apiHolidays = fetchHolidaysFromApi(year, month);
|
||||
|
||||
// ✅ URI를 직접 문자열로 구성하여 ServiceKey가 다시 인코딩되지 않도록 함
|
||||
// 사용자 정의 공휴일 추가 (monthDay: 전체 연도 / date: 특정 연도)
|
||||
List<MapDto> 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<MapDto> combinedHolidays = new ArrayList<>();
|
||||
combinedHolidays.addAll(apiHolidays);
|
||||
combinedHolidays.addAll(userDefinedHolidays);
|
||||
|
||||
return combinedHolidays;
|
||||
}
|
||||
|
||||
private List<MapDto> 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<String> entity = new HttpEntity<>(headers);
|
||||
|
||||
// ✅ `exchange` 메서드를 사용하여 요청
|
||||
ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.GET, entity, Map.class);
|
||||
|
||||
System.out.println("📌 API 응답 데이터: " + response.getBody());
|
||||
|
||||
return parseResponse(response.getBody());
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user