package com.its.pis.scheduler;

import com.its.app.utils.Elapsed;
import com.its.pis.config.ProcessConfig;
import com.its.pis.service.PisInfrService;
import com.its.pis.service.UnitSystService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;

@Slf4j
@AllArgsConstructor
@EnableScheduling
@Component
public class SchedulerTask {

    private final ProcessConfig processConfig;
    private final UnitSystService unitSystService;
    private final PisInfrService pisInfrService;

    @Scheduled(cron = "6 * * * * *")  // 1분주기 작업 실행(매분 6초)
    public void UnitSystSchedule() {
        if (!this.processConfig.isStartSchedule()) {
            return;
        }
        Elapsed elapsed = new Elapsed();
        log.info("          UnitSystSchedule: start. {}", Thread.currentThread().getName());
        // 프로세스 상태정보 업데이트
        this.unitSystService.updateUnitSystStts(true);
        // 주차정보시스템 및 주차장 상태정보 업데이트
        this.pisInfrService.updateCtlrStts(true);
        log.info("          UnitSystSchedule: ..end. {} ms. {}", elapsed.milliSeconds(), Thread.currentThread().getName());
    }

    @PreDestroy
    public void onShutDown() {
        this.unitSystService.updateUnitSystStts(false);
    }
}
