package com.fastbee.deviceData.service.impl;

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

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.deviceData.domain.DeviceRealtimedataWorms;
import com.fastbee.deviceData.domain.dto.DeviceProperties;
import com.fastbee.deviceData.mapper.DeviceRealtimedataWormsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fastbee.deviceData.mapper.DeviceRealtimedataMeteorologyMapper;
import com.fastbee.deviceData.domain.DeviceRealtimedataMeteorology;
import com.fastbee.deviceData.service.IDeviceRealtimedataMeteorologyService;

/**
 * 气象设备实时数据Service业务层处理
 * 
 * @author kerwincui
 * @date 2024-11-14
 */
@Service
public class DeviceRealtimedataMeteorologyServiceImpl extends ServiceImpl<DeviceRealtimedataMeteorologyMapper, DeviceRealtimedataMeteorology> implements IDeviceRealtimedataMeteorologyService
{
    @Autowired
    private DeviceRealtimedataMeteorologyMapper deviceRealtimedataMeteorologyMapper;

    /**
     * 查询气象设备实时数据
     * 
     * @param id 气象设备实时数据主键
     * @return 气象设备实时数据
     */
    @Override
    public DeviceRealtimedataMeteorology selectDeviceRealtimedataMeteorologyById(Long id)
    {
        return deviceRealtimedataMeteorologyMapper.selectDeviceRealtimedataMeteorologyById(id);
    }

    /**
     * 查询气象设备实时数据列表
     * 
     * @param deviceRealtimedataMeteorology 气象设备实时数据
     * @return 气象设备实时数据
     */
    @Override
    public List<DeviceRealtimedataMeteorology> selectDeviceRealtimedataMeteorologyList(DeviceRealtimedataMeteorology deviceRealtimedataMeteorology)
    {
        return deviceRealtimedataMeteorologyMapper.selectDeviceRealtimedataMeteorologyList(deviceRealtimedataMeteorology);
    }

    /**
     * 新增气象设备实时数据
     * 
     * @param deviceRealtimedataMeteorology 气象设备实时数据
     * @return 结果
     */
    @Override
    public int insertDeviceRealtimedataMeteorology(DeviceRealtimedataMeteorology deviceRealtimedataMeteorology)
    {
        deviceRealtimedataMeteorology.setSaveTime(DateUtils.getNowDate());
        return deviceRealtimedataMeteorologyMapper.insertDeviceRealtimedataMeteorology(deviceRealtimedataMeteorology);
    }

    /**
     * 修改气象设备实时数据
     * 
     * @param deviceRealtimedataMeteorology 气象设备实时数据
     * @return 结果
     */
    @Override
    public int updateDeviceRealtimedataMeteorology(DeviceRealtimedataMeteorology deviceRealtimedataMeteorology)
    {
        deviceRealtimedataMeteorology.setSaveTime(DateUtils.getNowDate());
        return deviceRealtimedataMeteorologyMapper.updateDeviceRealtimedataMeteorology(deviceRealtimedataMeteorology);
    }

    /**
     * 批量删除气象设备实时数据
     * 
     * @param ids 需要删除的气象设备实时数据主键
     * @return 结果
     */
    @Override
    public int deleteDeviceRealtimedataMeteorologyByIds(Long[] ids)
    {
        return deviceRealtimedataMeteorologyMapper.deleteDeviceRealtimedataMeteorologyByIds(ids);
    }

    /**
     * 删除气象设备实时数据信息
     * 
     * @param id 气象设备实时数据主键
     * @return 结果
     */
    @Override
    public int deleteDeviceRealtimedataMeteorologyById(Long id)
    {
        return deviceRealtimedataMeteorologyMapper.deleteDeviceRealtimedataMeteorologyById(id);
    }

    /**
     * 获取最新的一条气象数据
     * @return
     */
    @Override
    public Map<String,Object> getLatestWeatherRealtimedata(){
        Map<String,Object> rest= new HashMap<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("real_time");
        queryWrapper.last("limit 1");
        DeviceRealtimedataMeteorology meteorology = deviceRealtimedataMeteorologyMapper.selectOne(queryWrapper);

        List<DeviceProperties> devicePropertiesList = new ArrayList<>();
        rest.put("props",devicePropertiesList);
        if(Objects.isNull(meteorology)){
            return rest;
        }
        devicePropertiesList.add(DeviceProperties.builder().name("空气温度").value(meteorology.getAirTemp().toString()).unit("°C").build());
        devicePropertiesList.add(DeviceProperties.builder().name("空气湿度").value(meteorology.getAirHumi().toString()).unit("%").build());
        devicePropertiesList.add(DeviceProperties.builder().name("风速").value(meteorology.getWindSpeed().toString()).unit("m/s").build());
        devicePropertiesList.add(DeviceProperties.builder().name("风向").value(windDirMap(meteorology.getWindDir())).unit("风").build());
        devicePropertiesList.add(DeviceProperties.builder().name("大气压力").value(meteorology.getAirPre().toString()).unit("hPa").build());
        devicePropertiesList.add(DeviceProperties.builder().name("光照度").value(meteorology.getGuangzhao().toString()).unit("lx").build());
        devicePropertiesList.add(DeviceProperties.builder().name("光合有效辐射").value(meteorology.getFushe().toString()).unit("Lux").build());
        devicePropertiesList.add(DeviceProperties.builder().name("累积雨量").value(meteorology.getSumRain().toString()).unit("mm").build());
        devicePropertiesList.add(DeviceProperties.builder().name("瞬时雨量").value(meteorology.getInsRain().toString()).unit("mm").build());
        devicePropertiesList.add(DeviceProperties.builder().name("日雨量").value(meteorology.getDayRain().toString()).unit("mm").build());
        devicePropertiesList.add(DeviceProperties.builder().name("蒸发量").value(meteorology.getZhengfa().toString()).unit("t/h").build());
        devicePropertiesList.add(DeviceProperties.builder().name("土壤温度").value(meteorology.getSoilTemp().toString()).unit("°C").build());
        devicePropertiesList.add(DeviceProperties.builder().name("土壤湿度").value(meteorology.getSoilHumi().toString()).unit("%").build());

        Map<String,Object> info=new HashMap<>();
        info.put("deviceEncoding", meteorology.getDeviceId());
        rest.put("info",info);
        return rest;
    }


    /**
     * 风向数据映射
     */
    private String windDirMap(BigDecimal windDir){
        //8个风向
        if (windDir.compareTo(new BigDecimal("0")) >= 0 && windDir.compareTo(new BigDecimal("22.5")) < 0) {
            return "北";
        } else if (windDir.compareTo(new BigDecimal("22.5")) >= 0 && windDir.compareTo(new BigDecimal("67.5")) < 0) {
            return "东北";
        } else if (windDir.compareTo(new BigDecimal("67.5")) >= 0 && windDir.compareTo(new BigDecimal("112.5")) < 0) {
            return "东";
        }  else if (windDir.compareTo(new BigDecimal("112.5")) >= 0 && windDir.compareTo(new BigDecimal("157.5")) < 0) {
            return "东南";
        } else if (windDir.compareTo(new BigDecimal("157.5")) >= 0 && windDir.compareTo(new BigDecimal("202.5")) < 0) {
            return "南";
        } else if (windDir.compareTo(new BigDecimal("202.5")) >= 0 && windDir.compareTo(new BigDecimal("247.5")) < 0) {
            return "西南";
        } else if (windDir.compareTo(new BigDecimal("247.5")) >= 0 && windDir.compareTo(new BigDecimal("292.5")) < 0) {
            return "西";
        } else if (windDir.compareTo(new BigDecimal("292.5")) >= 0 && windDir.compareTo(new BigDecimal("337.5")) < 0) {
            return "西北";
        } else if (windDir.compareTo(new BigDecimal("337.5")) >= 0 && windDir.compareTo(new BigDecimal("360")) < 0) {
            return "北";
        } else {
            return "无数据";
        }
    }
}
