2025-12-10 15:59:47 +09:00
|
|
|
package com.snp.batch.service;
|
2026-01-08 15:12:06 +09:00
|
|
|
|
2025-12-10 15:59:47 +09:00
|
|
|
import com.snp.batch.global.repository.BatchLastExecutionRepository;
|
2026-01-08 15:12:06 +09:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2025-12-10 15:59:47 +09:00
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
2026-01-12 14:41:08 +09:00
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.ZoneId;
|
|
|
|
|
import java.time.ZoneOffset;
|
2025-12-24 14:15:13 +09:00
|
|
|
import java.time.format.DateTimeFormatter;
|
2026-01-08 15:12:06 +09:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
@Slf4j
|
2025-12-10 15:59:47 +09:00
|
|
|
@Service
|
|
|
|
|
public class BatchDateService {
|
|
|
|
|
private final BatchLastExecutionRepository repository;
|
|
|
|
|
|
|
|
|
|
public BatchDateService(BatchLastExecutionRepository repository) {
|
|
|
|
|
this.repository = repository;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-08 15:12:06 +09:00
|
|
|
public Map<String, String> getDateRangeWithoutTimeParams(String apiKey) {
|
|
|
|
|
return repository.findDateRangeByApiKey(apiKey)
|
|
|
|
|
.map(projection -> {
|
|
|
|
|
Map<String, String> params = new HashMap<>();
|
2025-12-10 15:59:47 +09:00
|
|
|
|
2026-01-08 15:12:06 +09:00
|
|
|
LocalDateTime fromTarget = (projection.getRangeFromDate() != null)
|
|
|
|
|
? projection.getRangeFromDate()
|
|
|
|
|
: projection.getLastSuccessDate();
|
2025-12-10 15:59:47 +09:00
|
|
|
|
2026-01-08 15:12:06 +09:00
|
|
|
LocalDateTime toTarget = (projection.getRangeToDate() != null)
|
|
|
|
|
? projection.getRangeToDate()
|
|
|
|
|
: LocalDateTime.now();
|
2025-12-10 15:59:47 +09:00
|
|
|
|
2026-01-08 15:12:06 +09:00
|
|
|
// 2. 파라미터 맵에 날짜 정보 매핑
|
|
|
|
|
putDateParams(params, "from", fromTarget);
|
|
|
|
|
putDateParams(params, "to", toTarget);
|
2025-12-10 15:59:47 +09:00
|
|
|
|
2026-01-08 15:12:06 +09:00
|
|
|
// 3. 고정 값 설정
|
|
|
|
|
params.put("shipsCategory", "0");
|
2025-12-10 15:59:47 +09:00
|
|
|
|
2026-01-08 15:12:06 +09:00
|
|
|
return params;
|
|
|
|
|
})
|
|
|
|
|
.orElseGet(() -> {
|
|
|
|
|
log.warn("해당 apiKey에 대한 데이터를 찾을 수 없습니다: {}", apiKey);
|
|
|
|
|
return new HashMap<>();
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-12-10 15:59:47 +09:00
|
|
|
|
2026-01-08 15:12:06 +09:00
|
|
|
/**
|
|
|
|
|
* LocalDateTime에서 연, 월, 일을 추출하여 Map에 담는 헬퍼 메소드
|
|
|
|
|
*/
|
|
|
|
|
private void putDateParams(Map<String, String> params, String prefix, LocalDateTime dateTime) {
|
|
|
|
|
if (dateTime != null) {
|
|
|
|
|
params.put(prefix + "Year", String.valueOf(dateTime.getYear()));
|
|
|
|
|
params.put(prefix + "Month", String.valueOf(dateTime.getMonthValue()));
|
|
|
|
|
params.put(prefix + "Day", String.valueOf(dateTime.getDayOfMonth()));
|
|
|
|
|
}
|
2025-12-10 15:59:47 +09:00
|
|
|
}
|
|
|
|
|
|
2026-01-08 15:12:06 +09:00
|
|
|
public Map<String, String> getDateRangeWithTimezoneParams(String apiKey) {
|
|
|
|
|
return repository.findDateRangeByApiKey(apiKey)
|
|
|
|
|
.map(projection -> {
|
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
|
|
// 'Z'를 문자열 리터럴이 아닌 실제 타임존 기호(X)로 처리
|
|
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSX");
|
2025-12-24 14:15:13 +09:00
|
|
|
|
2026-01-08 15:12:06 +09:00
|
|
|
// 한국 시간을 UTC로 변환하는 헬퍼 메소드 (아래 정의)
|
|
|
|
|
params.put("fromDate", formatToUtc(projection.getRangeFromDate() != null ?
|
|
|
|
|
projection.getRangeFromDate() : projection.getLastSuccessDate(), formatter));
|
2025-12-24 14:15:13 +09:00
|
|
|
|
2026-01-08 15:12:06 +09:00
|
|
|
LocalDateTime toDateTime = projection.getRangeToDate() != null ?
|
|
|
|
|
projection.getRangeToDate() : LocalDateTime.now();
|
2025-12-24 14:15:13 +09:00
|
|
|
|
2026-01-08 15:12:06 +09:00
|
|
|
params.put("toDate", formatToUtc(toDateTime, formatter));
|
2025-12-24 14:15:13 +09:00
|
|
|
|
2026-01-08 15:12:06 +09:00
|
|
|
return params;
|
|
|
|
|
})
|
|
|
|
|
.orElseGet(() -> {
|
|
|
|
|
log.warn("해당 apiKey에 대한 데이터를 찾을 수 없습니다: {}", apiKey);
|
|
|
|
|
return new HashMap<>();
|
|
|
|
|
});
|
2025-12-24 14:15:13 +09:00
|
|
|
}
|
|
|
|
|
|
2026-01-08 15:12:06 +09:00
|
|
|
// 한국 시간(LocalDateTime)을 UTC 문자열로 변환하는 로직
|
|
|
|
|
private String formatToUtc(LocalDateTime localDateTime, DateTimeFormatter formatter) {
|
|
|
|
|
if (localDateTime == null) return null;
|
|
|
|
|
// 1. 한국 시간대(KST)임을 명시
|
|
|
|
|
// 2. UTC로 시간대를 변경 (9시간 빠짐)
|
|
|
|
|
// 3. 포맷팅 (끝에 Z가 자동으로 붙음)
|
|
|
|
|
return localDateTime.atZone(ZoneId.of("Asia/Seoul"))
|
|
|
|
|
.withZoneSameInstant(ZoneOffset.UTC)
|
|
|
|
|
.format(formatter);
|
2025-12-10 15:59:47 +09:00
|
|
|
}
|
2026-01-08 15:12:06 +09:00
|
|
|
|
2025-12-10 15:59:47 +09:00
|
|
|
}
|