package egovframework.service.impl;

import egovframework.service.UnitSystService;
import egovframework.service.mapper.UnitSystSttsMapper;
import lombok.RequiredArgsConstructor;
import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Calendar;

@RequiredArgsConstructor
@Service
public class UnitSystServiceImpl  extends EgovAbstractServiceImpl implements UnitSystService {

    private final UnitSystSttsMapper unitSystSttsMapper;

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

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

    @Override
    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분 정각에 이력으로 저장
            insertUnitSystSttsHs(SYST_STTS_CD);
        }
    }

}
