package incheon.ags.dss.under.batch;

import incheon.ags.dss.under.service.UrbUdgdSnkgHistService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class UrbUdgdSnkgBatch {

    @Autowired
    private UrbUdgdSnkgHistService urbUdgdSnkgHistService;

    /**
     * [주기 설정 선택] 
     * 현재는 '매일 새벽 2시' 실행으로 설정
     * 테스트용 5초: 0/5 * * * * *
     * 1시간: "0 0 * * * *"
     * 12시간: "0 0 0/12 * * *"
     * 24시간(새벽0시): "0 0 0 * * *"
     */
//    @Scheduled(cron = "0 0 * * * *")
    @Scheduled(cron = "0 0 2 * * *")
//    @Scheduled(cron = "0/5 * * * * *")
    public void executeSyncBatch() {
        // 서비스 주입 여부 체크
        if (urbUdgdSnkgHistService == null) {
            log.error(">>> [배치 에러] UrbUdgdSnkgHistService가 주입되지 않았습니다.");
            return;
        }

        log.info("▶▶▶ [배치 시작] 지반침하 데이터 동기화 (실행시각: {})", LocalDateTime.now());
        
        try {
            // 서비스 로직 호출
            urbUdgdSnkgHistService.syncIncheonSubsidenceData();
            log.info("▶▶▶ [배치 완료] 데이터 동기화가 성공적으로 끝났습니다.");
        } catch (Exception e) {
            log.error("▶▶▶ [배치 실패] 실행 중 예외 발생: ", e);
        }
    }
}