package io.company.localhost.service; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import io.company.localhost.common.dto.MapDto; import io.company.localhost.mapper.localvacaMapper; import lombok.RequiredArgsConstructor; @Service @RequiredArgsConstructor public class localvacaService { private final localvacaMapper localvacaMapper; private final RestTemplate restTemplate = new RestTemplate(); @Value("${api.public-holiday.key}") private String serviceKey; public void insertVacation(MapDto map) { localvacaMapper.insertVacation(map); } public List getVacationList(int year, int month) { return localvacaMapper.findVacations(year, month); } /** * πŸ”Ή νŠΉμ • 연월에 λŒ€ν•œ 곡휴일 데이터 쑰회 */ public List getHolidays(int year, int month) { // βœ… ServiceKeyλ₯Ό λ””μ½”λ”©ν•΄μ„œ μ‚¬μš© String decodedServiceKey = URLDecoder.decode(serviceKey, StandardCharsets.UTF_8); System.out.println("πŸ“Œ λ””μ½”λ”©λœ ServiceKey: " + decodedServiceKey); // βœ… URIλ₯Ό 직접 λ¬Έμžμ—΄λ‘œ κ΅¬μ„±ν•˜μ—¬ ServiceKeyκ°€ λ‹€μ‹œ μΈμ½”λ”©λ˜μ§€ μ•Šλ„λ‘ 함 String url = "http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getRestDeInfo" + "?solYear=" + year + "&solMonth=" + String.format("%02d", month) + "&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()); } /** * πŸ”Ή 곡휴일 데이터λ₯Ό MapDto둜 λ³€ν™˜ */ private List parseResponse(Map response) { List holidays = new ArrayList<>(); if (response == null || !response.containsKey("response")) { System.out.println("πŸ“Œ 응닡이 λΉ„μ–΄ 있음."); return holidays; } Map responseBody = (Map) response.get("response"); System.out.println("πŸ“Œ responseBody: " + responseBody); if (responseBody == null || !responseBody.containsKey("body")) { System.out.println("πŸ“Œ API 응닡 λ°μ΄ν„°μ—μ„œ 'body' ν•„λ“œ μ—†μŒ."); return holidays; } Map body = (Map) responseBody.get("body"); System.out.println("πŸ“Œ body: " + body); if (body == null || !body.containsKey("items")) { System.out.println("πŸ“Œ API 응닡 λ°μ΄ν„°μ—μ„œ 'items' ν•„λ“œ μ—†μŒ."); return holidays; } Object items = body.get("items"); System.out.println("πŸ“Œ items: " + items); // βœ… 'items'κ°€ Map νƒ€μž…μΈμ§€ 확인 ν›„ 처리 if (items instanceof Map) { Map itemMap = (Map) items; if (itemMap.containsKey("item")) { Object itemData = itemMap.get("item"); System.out.println("πŸ“Œ itemData: " + itemData); // βœ… 'item'이 λ¦¬μŠ€νŠΈμΈμ§€ 단일 객체인지 확인 ν›„ λ³€ν™˜ if (itemData instanceof List) { for (Map item : (List>) itemData) { holidays.add(convertToMapDto(item)); } } else if (itemData instanceof Map) { holidays.add(convertToMapDto((Map) itemData)); } } } System.out.println("πŸ“Œ μ΅œμ’… holidays: " + holidays); return holidays; } /** * πŸ”Ή 곡휴일 데이터λ₯Ό MapDto둜 λ³€ν™˜ */ private MapDto convertToMapDto(Map item) { MapDto dto = new MapDto(); // βœ… locdateλ₯Ό μ•ˆμ „ν•˜κ²Œ λ³€ν™˜ (int, long, double λ“± λ‹€μ–‘ν•œ ν˜•νƒœ λŒ€μ‘) String locdateStr = String.valueOf(item.get("locdate")); if (locdateStr.contains(".")) { locdateStr = locdateStr.split("\\.")[0]; // μ†Œμˆ˜μ  제거 } // βœ… YYYY-MM-DD ν˜•μ‹μœΌλ‘œ λ³€ν™˜ if (locdateStr.length() == 8) { String formattedDate = locdateStr.substring(0, 4) + "-" + locdateStr.substring(4, 6) + "-" + locdateStr.substring(6, 8); dto.put("date", formattedDate); } else { dto.put("date", locdateStr); // λ³€ν™˜ λΆˆκ°€λŠ₯ν•œ 경우 원본 μ €μž₯ } dto.put("name", item.get("dateName")); return dto; } /** * λ‚΄ μ—°μ°¨ μ‚¬μš© λ‚΄μ—­ 쑰회 (μ‚¬μš©ν•œ μ—°μ°¨ & 받은 μ—°μ°¨) */ public Map> getUserVacationHistory(Long userId) { List usedVacations = localvacaMapper.getUsedVacations(userId); List receivedVacations = localvacaMapper.getReceivedVacations(userId); Map> history = new HashMap<>(); history.put("usedVacations", usedVacations); history.put("receivedVacations", receivedVacations); return history; } }