package com.gonghang.h5clientapp.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gonghang.h5clientapp.beans.dto.EnvDataDTO;
import com.gonghang.h5clientapp.beans.dto.EnvSettingDTO;
import com.gonghang.h5clientapp.beans.vo.AlarmResultVO;
import com.gonghang.h5clientapp.common.enumData.AlarmLevelEnum;
import com.gonghang.h5clientapp.common.enumData.EnvTypeEnum;
import com.gonghang.h5clientapp.dao.EnvSettingMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by shiwn on 2022/8/3 14:44
 */
@Service
public class EnvSettingServiceImpl extends ServiceImpl<EnvSettingMapper, EnvSettingDTO> implements EnvSettingService {

    @Override
    public Map getData() {
        List<EnvSettingDTO> list = this.list();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        Map<String, List<EnvSettingDTO>> collect = list.stream().sorted(Comparator.comparing(EnvSettingDTO::getId)).collect(Collectors.groupingBy(EnvSettingDTO::getEnvType));
        Map<String, EnvSettingDTO> result = new HashMap<>();
        if (!CollectionUtils.isEmpty(collect)) {
            collect.entrySet().forEach(t -> {
                        if (!CollectionUtils.isEmpty(t.getValue())) {
                            result.put(t.getKey(), t.getValue().get(0));
                        }
                    }
            );
        }
        return result;
    }

    @Override
    public List<AlarmResultVO> envDataIsAlarm(EnvDataDTO envData, Map<String, EnvSettingDTO> envSettingMap) {
        List<AlarmResultVO> alarmResult = new ArrayList<>(15);
        //co
        AlarmResultVO COalarmResult = checkEnvValue(envSettingMap.get(EnvTypeEnum.CO.toString()), String.valueOf(envData.getCo()));
        COalarmResult.setDataId(envData.getId());
        alarmResult.add(COalarmResult);
        //co2
        AlarmResultVO CO2alarmResult = checkEnvValue(envSettingMap.get(EnvTypeEnum.CO2.toString()), String.valueOf(envData.getCo2()));
        CO2alarmResult.setDataId(envData.getId());
        alarmResult.add(CO2alarmResult);

        //SF6
        AlarmResultVO SF6alarmResult = checkEnvValue(envSettingMap.get(EnvTypeEnum.SF6.toString()), String.valueOf(envData.getSf6()));
        SF6alarmResult.setDataId(envData.getId());
        alarmResult.add(SF6alarmResult);

        //o3
        AlarmResultVO O3alarmResult = checkEnvValue(envSettingMap.get(EnvTypeEnum.O3.toString()), String.valueOf(envData.getO3()));
        O3alarmResult.setDataId(envData.getId());
        alarmResult.add(O3alarmResult);

        //temperature
        AlarmResultVO temperatureResult = checkEnvValue(envSettingMap.get(EnvTypeEnum.TEMPERATURE.toString()), String.valueOf(envData.getTemperature()));
        temperatureResult.setDataId(envData.getId());
        alarmResult.add(temperatureResult);

        //humidity
        AlarmResultVO humidityResult = checkEnvValue(envSettingMap.get(EnvTypeEnum.HUMIDITY.toString()), String.valueOf(envData.getHumidity()));
        humidityResult.setDataId(envData.getId());
        alarmResult.add(humidityResult);
        return alarmResult;
    }


    /**
     * 私有:判断环境告警等级
     *
     * @param: envSetting 阈值设置
     * @param: checkValue 检测数据
     * @return:
     * @author machao
     * @date: 2021/7/16 11:25
     */
    private AlarmResultVO checkEnvValue(EnvSettingDTO envSetting, String checkValue) {
        //初始化告警返回值
        AlarmResultVO envAlarmResult = new AlarmResultVO();
        envAlarmResult.setTargetId(envSetting.getId());
        envAlarmResult.setAlarmFor(envSetting.getEnvType());
        envAlarmResult.setDiscernResult(StringUtils.isEmpty(checkValue) ? "" : Float.valueOf(checkValue).toString());
        envAlarmResult.setAlarmLevel(String.valueOf(AlarmLevelEnum.NORMAL.getCode()));
        if (envSetting == null || StringUtils.isEmpty(checkValue)) {
            return envAlarmResult;
        }
        BigDecimal value = new BigDecimal(checkValue);

        //若值在安全范围类返回正常
        if (!StringUtils.isEmpty(envSetting.getSafeMin()) && !StringUtils.isEmpty(envSetting.getSafeMax())) {
            if (new BigDecimal(envSetting.getSafeMin()).compareTo(value) <= 0
                    && new BigDecimal(envSetting.getSafeMax()).compareTo(value) > 0) {
                envAlarmResult.setAlarmLevel(String.valueOf(AlarmLevelEnum.NORMAL.getCode()));
                return envAlarmResult;
            }
        } else {
            if (new BigDecimal(envSetting.getSafeMax()).compareTo(value) >= 0) {
                envAlarmResult.setAlarmLevel(String.valueOf(AlarmLevelEnum.NORMAL.getCode()));
                return envAlarmResult;
            }
        }
        //危急告警
        if (!StringUtils.isEmpty(envSetting.getSeriousWarningMin()) &&
                new BigDecimal(envSetting.getSeriousWarningMin()).compareTo(value) <= 0) {
            envAlarmResult.setAlarmLevel(String.valueOf(AlarmLevelEnum.TYPE_4.getCode()));
            return envAlarmResult;
        }

        if (!StringUtils.isEmpty(envSetting.getSeriousWarningMax()) &&
                new BigDecimal(envSetting.getSeriousWarningMax()).compareTo(value) >= 0) {
            envAlarmResult.setAlarmLevel(String.valueOf(AlarmLevelEnum.TYPE_4.getCode()));
            return envAlarmResult;
        }

        //一般告警 双段式 先判断去区间值的情况 若有区间值则比较区间值 否则比较单边值
        if (!StringUtils.isEmpty(envSetting.getEarlyWarningMin1()) && !StringUtils.isEmpty(envSetting.getEarlyWarningMax1())) {
            if (new BigDecimal(envSetting.getEarlyWarningMin1()).compareTo(value) <= 0
                    && new BigDecimal(envSetting.getEarlyWarningMax1()).compareTo(value) > 0) {
                envAlarmResult.setAlarmLevel(String.valueOf(AlarmLevelEnum.TYPE_2.getCode()));
                return envAlarmResult;
            }
        } else {

            if (!StringUtils.isEmpty(envSetting.getEarlyWarningMin1())
                    && new BigDecimal(envSetting.getEarlyWarningMin1()).compareTo(value) <= 0) {
                envAlarmResult.setAlarmLevel(String.valueOf(AlarmLevelEnum.TYPE_2.getCode()));
                return envAlarmResult;
            }

            if (!StringUtils.isEmpty(envSetting.getEarlyWarningMax1())
                    && new BigDecimal(envSetting.getEarlyWarningMax1()).compareTo(value) >= 0) {
                envAlarmResult.setAlarmLevel(String.valueOf(AlarmLevelEnum.TYPE_2.getCode()));
                return envAlarmResult;
            }
        }
        if (!StringUtils.isEmpty(envSetting.getEarlyWarningMin2()) && !StringUtils.isEmpty(envSetting.getEarlyWarningMax2())) {
            if (new BigDecimal(envSetting.getEarlyWarningMin2()).compareTo(value) <= 0
                    && new BigDecimal(envSetting.getEarlyWarningMax2()).compareTo(value) > 0) {
                envAlarmResult.setAlarmLevel(String.valueOf(AlarmLevelEnum.TYPE_2.getCode()));
                return envAlarmResult;
            }
        } else {
            if (!StringUtils.isEmpty(envSetting.getEarlyWarningMin2())
                    && new BigDecimal(envSetting.getEarlyWarningMin1()).compareTo(value) <= 0) {
                envAlarmResult.setAlarmLevel(String.valueOf(AlarmLevelEnum.TYPE_2.getCode()));
                return envAlarmResult;
            }

            if (!StringUtils.isEmpty(envSetting.getEarlyWarningMax2())
                    && new BigDecimal(envSetting.getEarlyWarningMax1()).compareTo(value) >= 0) {
                envAlarmResult.setAlarmLevel(String.valueOf(AlarmLevelEnum.TYPE_2.getCode()));
                return envAlarmResult;
            }

        }

        return envAlarmResult;
    }
}
