package com.zksy.environment.utils;

import com.zksy.api.domain.AlarmData;
import com.zksy.api.service.AlarmDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Component
public class AlarmUtil {

    @Autowired
    private AlarmDataService alarmDataService;

    public void checkAndSaveAlarm(String deviceName, String deviceCode, String deviceType,
                                    String warningType, String warningCode, Double warningValue,
                                    BigDecimal actualValue, String remark) {
        if (warningValue == null || actualValue == null) {
            return;
        }

        if (actualValue.doubleValue() > warningValue) {
            saveAlarm(deviceName, deviceCode, deviceType, warningType, warningCode,
                    BigDecimal.valueOf(warningValue), actualValue, remark);
        }
    }

    public void saveAlarm(String deviceName, String deviceCode, String deviceType,
                           String warningType, String warningCode, BigDecimal warningValue,
                           BigDecimal actualValue, String remark) {
        try {
            AlarmData alarmData = new AlarmData();
            alarmData.setDeviceName(deviceName);
            alarmData.setDeviceCode(deviceCode);
            alarmData.setDeviceType(deviceType);
            alarmData.setWarningType(warningType);
            alarmData.setWarningCode(warningCode);
            alarmData.setWarningValue(warningValue);
            alarmData.setActualValue(actualValue);
            alarmData.setAlarmStatus(0);
            alarmData.setAlarmTime(LocalDateTime.now());
            alarmData.setRemark(remark);
            alarmData.setCreateTime(LocalDateTime.now());

            alarmDataService.saveAlarmData(alarmData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
