package com.huimv.datacollection.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.huimv.datacollection.entity.IndoorEnvironmentEntity;
import com.huimv.datacollection.entity.WarningInfoEntity;
import com.huimv.datacollection.feign.ManagementFeignService;
import com.huimv.datacollection.service.HandleDataPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 数据包处理serviceImpl
 * </p>
 *
 * @author yinhao
 * @date 2021/7/7 17:36
 */
@Service
public class HandleDataPackageServiceImpl implements HandleDataPackageService {

    @Autowired
    private ManagementFeignService managementFeignService;

    @Override
    public void handleTemperature(IndoorEnvironmentEntity indoorEnvironmentEntity, double value, Integer farmId, Integer pigstyId, Map<String, Double> map) {
        //获取室内阈值信息
        Double highTemp = map.get("高温");
        double highTempThreshold = highTemp == null ? 40.0 : highTemp;
        Double lowTemp = map.get("低温");
        double lowTempThreshold = lowTemp == null ? 15.0 : lowTemp;

        if (value > highTempThreshold) {
            WarningInfoEntity warningInfoEntity = createWarningInfo(farmId, pigstyId, value);
            indoorEnvironmentEntity.setAbnormalStatus(true);
            insertWarningInfo(warningInfoEntity, "高温");
        } else if (value < lowTempThreshold) {
            indoorEnvironmentEntity.setAbnormalStatus(true);
            WarningInfoEntity warningInfoEntity = createWarningInfo(farmId, pigstyId, value);
            insertWarningInfo(warningInfoEntity, "低温");
        }

        managementFeignService.saveIndoorEnvironment(indoorEnvironmentEntity);
    }


    @Override
    public void handleHumidity(IndoorEnvironmentEntity indoorEnvironmentEntity, double value, Integer farmId, Integer pigstyId, Map<String, Double> map) {
        Double highHumidity = map.get("湿度过高");
        double highHumidityThreshold = highHumidity == null ? 90.0 : highHumidity;
        Double lowHumidity = map.get("湿度过低");
        double lowHumidityThreshold = lowHumidity == null ? 25.0 : lowHumidity;

        if (value > highHumidityThreshold) {
            WarningInfoEntity warningInfoEntity = createWarningInfo(farmId, pigstyId, value);
            indoorEnvironmentEntity.setAbnormalStatus(true);
            insertWarningInfo(warningInfoEntity, "湿度过高");
        } else if (value < lowHumidityThreshold) {
            indoorEnvironmentEntity.setAbnormalStatus(true);
            WarningInfoEntity warningInfoEntity = createWarningInfo(farmId, pigstyId, value);
            insertWarningInfo(warningInfoEntity, "湿度过低");
        }

        managementFeignService.saveIndoorEnvironment(indoorEnvironmentEntity);
    }

    @Override
    public void handleGas(IndoorEnvironmentEntity indoorEnvironmentEntity, double value, Integer farmId, Integer pigstyId, Map<String, Double> map) {
        Double highPpm = map.get("氨气浓度过高");
        double highPpmThreshold = highPpm == null ? 50.0 : highPpm;

        if (value > highPpmThreshold) {
            WarningInfoEntity warningInfoEntity = createWarningInfo(farmId, pigstyId, value);
            indoorEnvironmentEntity.setAbnormalStatus(true);
            insertWarningInfo(warningInfoEntity, "氨气浓度过高");
        }
    }

    private void insertWarningInfo(WarningInfoEntity warningInfoEntity, String warningType) {
        warningInfoEntity.setWarningType(warningType);
        warningInfoEntity.setBelongTo((short) 1);
        managementFeignService.saveWarningInfo(warningInfoEntity);
    }

    private WarningInfoEntity createWarningInfo(Integer farmId, Integer pigstyId, Double value) {
        WarningInfoEntity warningInfoEntity = new WarningInfoEntity();
        DateTime date = DateUtil.date();
        warningInfoEntity.setNowDate(DateUtil.beginOfDay(date));
        warningInfoEntity.setFarmId(farmId);
        warningInfoEntity.setPigstyId(pigstyId);
        warningInfoEntity.setNowValue(value);
        warningInfoEntity.setWarningTime(date);
        return warningInfoEntity;
    }
}
