package egovframework.scheduler;

import egovframework.service.impl.UnitSystServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@RequiredArgsConstructor
//@EnableScheduling
//@Component
public class UnitSystScheduler {

    private final UnitSystServiceImpl unitSystServiceImpl;

    public String getSysTime() {
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMddHHmmss");
        Date dtLog = new Date();
        return sdfDate.format(dtLog);
    }

    @Scheduled(cron = "15 * * * * *")  // 1분주기 작업 실행
    public void unitSystSttsSchedule() {
        log.info("unitSystSttsSchedule start: " + getSysTime());
        this.unitSystServiceImpl.updateUnitSystStts(true);
    }

    @PostConstruct
    private void init() {
        log.info("unitSystSttsSchedule init");
        this.unitSystServiceImpl.updateUnitSystStts(true);
    }

    @PreDestroy
    public void preDestroy() {
        log.info("unitSystSttsSchedule destroy");
        this.unitSystServiceImpl.updateUnitSystStts(false);
    }

}
