package com.tsi.api.server.scheduler;

import com.tsi.api.server.mybatis.TsiDatabaseService;
import com.tsi.app.common.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;

@Slf4j
@EnableScheduling
@Component
public class TsiScheduler {

    private final TsiDatabaseService tsiDatabaseService;

    public TsiScheduler(TsiDatabaseService tsiDatabaseService) {
        this.tsiDatabaseService = tsiDatabaseService;
    }

    @PreDestroy
    public void onShutDown() {
    }

    @Async
    @Scheduled(cron = "0 * * * * *")  // 1분 주기 작업 실행
    public void jobLoadDatabase() {
        long startTime = System.nanoTime();
        int res = this.tsiDatabaseService.loadAlarmConfig();
        this.tsiDatabaseService.updateProcessState(1);
        log.info("      loadAlarmConfig: {} EA, {} {}.", res, TimeUtils.elapsedTime(startTime), Thread.currentThread().getName());
    }

    @Async
    @Scheduled(cron = "0/10 * * * * *")  // 10 초 주기 작업 실행
    public void jobUpdateSystemStatus() {
        long startTime = System.nanoTime();
        int res = this.tsiDatabaseService.updateSystemStatus();
        log.info("jobUpdateSystemStatus: {} EA, {} {}.", res, TimeUtils.elapsedTime(startTime), Thread.currentThread().getName());
    }
}
