package egovframework.com.its.web.server.service;

import egovframework.com.its.web.server.mapper.UnitSystSttsMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Calendar;

@RequiredArgsConstructor
@Service
public class UnitSystService {

    private final UnitSystSttsMapper unitSystSttsMapper;

    private int updateUnitSystStts(String stts) {
        return this.unitSystSttsMapper.updateUnitSystStts(stts);
    }

    private int addUnitSystSttsHs(String stts) {
        return this.unitSystSttsMapper.insertUnitSystSttsHs(stts);
    }

    public void updateUnitSystStts(boolean isRun) {
        boolean insHs = false;
        Calendar cal = Calendar.getInstance();
        int min = cal.get(Calendar.MINUTE);
        if ((min % 5) == 0) {
            insHs = true;
        }

        String SYST_STTS_CD = isRun ? "SPS1" : "SPC3";
        updateUnitSystStts(SYST_STTS_CD);
        if (insHs) {
            // 매 5분 정각에 이력으로 저장
            addUnitSystSttsHs(SYST_STTS_CD);
        }
    }

}
