package com.fastbee.deviceData.service.impl;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fastbee.deviceData.domain.dto.DeviceProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fastbee.deviceData.mapper.DeviceRealtimedataMoistureMapper;
import com.fastbee.deviceData.domain.DeviceRealtimedataMoisture;
import com.fastbee.deviceData.service.IDeviceRealtimedataMoistureService;

/**
 * 墒情实时数据Service业务层处理
 * 
 * @author kerwincui
 * @date 2024-11-14
 */
@Service
public class DeviceRealtimedataMoistureServiceImpl extends ServiceImpl<DeviceRealtimedataMoistureMapper,DeviceRealtimedataMoisture> implements IDeviceRealtimedataMoistureService
{
    @Autowired
    private DeviceRealtimedataMoistureMapper deviceRealtimedataMoistureMapper;

    /**
     * 查询墒情实时数据
     * 
     * @param id 墒情实时数据主键
     * @return 墒情实时数据
     */
    @Override
    public DeviceRealtimedataMoisture selectDeviceRealtimedataMoistureById(Long id)
    {
        return deviceRealtimedataMoistureMapper.selectDeviceRealtimedataMoistureById(id);
    }

    /**
     * 查询墒情实时数据列表
     * 
     * @param deviceRealtimedataMoisture 墒情实时数据
     * @return 墒情实时数据
     */
    @Override
    public List<DeviceRealtimedataMoisture> selectDeviceRealtimedataMoistureList(DeviceRealtimedataMoisture deviceRealtimedataMoisture)
    {
        return deviceRealtimedataMoistureMapper.selectDeviceRealtimedataMoistureList(deviceRealtimedataMoisture);
    }

    /**
     * 新增墒情实时数据
     * 
     * @param deviceRealtimedataMoisture 墒情实时数据
     * @return 结果
     */
    @Override
    public int insertDeviceRealtimedataMoisture(DeviceRealtimedataMoisture deviceRealtimedataMoisture)
    {
        return deviceRealtimedataMoistureMapper.insertDeviceRealtimedataMoisture(deviceRealtimedataMoisture);
    }

    /**
     * 修改墒情实时数据
     * 
     * @param deviceRealtimedataMoisture 墒情实时数据
     * @return 结果
     */
    @Override
    public int updateDeviceRealtimedataMoisture(DeviceRealtimedataMoisture deviceRealtimedataMoisture)
    {
        return deviceRealtimedataMoistureMapper.updateDeviceRealtimedataMoisture(deviceRealtimedataMoisture);
    }

    /**
     * 批量删除墒情实时数据
     * 
     * @param ids 需要删除的墒情实时数据主键
     * @return 结果
     */
    @Override
    public int deleteDeviceRealtimedataMoistureByIds(Long[] ids)
    {
        return deviceRealtimedataMoistureMapper.deleteDeviceRealtimedataMoistureByIds(ids);
    }

    /**
     * 删除墒情实时数据信息
     * 
     * @param id 墒情实时数据主键
     * @return 结果
     */
    @Override
    public int deleteDeviceRealtimedataMoistureById(Long id)
    {
        return deviceRealtimedataMoistureMapper.deleteDeviceRealtimedataMoistureById(id);
    }
    /**
     * 获取最新一条墒情数据
     * @return
     */
    public List<DeviceProperties> getLatestWeatherRealtimedata(String deviceId){
        System.err.println(deviceId);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("real_time");
        queryWrapper.last("limit 1");
        queryWrapper.eq("device_id",deviceId);
        DeviceRealtimedataMoisture moisture = deviceRealtimedataMoistureMapper.selectOne(queryWrapper);

        List<DeviceProperties> devicePropertiesList = new ArrayList<>();
        if(Objects.isNull(moisture)){
            return devicePropertiesList;
        }
        devicePropertiesList.add(DeviceProperties.builder().name("土壤温度").value(moisture.getSoilTemp().toString()).unit("°C").build());
        devicePropertiesList.add(DeviceProperties.builder().name("土壤湿度").value(moisture.getSoilHumi().toString()).unit("%").build());
        devicePropertiesList.add(DeviceProperties.builder().name("土壤EC").value(moisture.getSoilEc().toString()).unit("MS/cm").build());
        devicePropertiesList.add(DeviceProperties.builder().name("土壤Ph").value(moisture.getSoilPh().toString()).unit("").build());
        devicePropertiesList.add(DeviceProperties.builder().name("一层土壤温度").value(moisture.getSoilTemp1().toString()).unit("°C").build());
        devicePropertiesList.add(DeviceProperties.builder().name("一层土壤湿度").value(moisture.getSoilHumi1().toString()).unit("%").build());
        devicePropertiesList.add(DeviceProperties.builder().name("二层土壤温度").value(moisture.getSoilTemp2().toString()).unit("°C").build());
        devicePropertiesList.add(DeviceProperties.builder().name("二层土壤湿度").value(moisture.getSoilHumi2().toString()).unit("%").build());
        devicePropertiesList.add(DeviceProperties.builder().name("三层土壤温度").value(moisture.getSoilTemp3().toString()).unit("°C").build());
        devicePropertiesList.add(DeviceProperties.builder().name("三层土壤湿度").value(moisture.getSoilHumi3().toString()).unit("%").build());
        devicePropertiesList.add(DeviceProperties.builder().name("四层土壤温度").value(moisture.getSoilTemp4().toString()).unit("°C").build());
        devicePropertiesList.add(DeviceProperties.builder().name("四层土壤湿度").value(moisture.getSoilHumi4().toString()).unit("%").build());
        devicePropertiesList.add(DeviceProperties.builder().name("环境温度").value(moisture.getAirTemp().toString()).unit("°C").build());
        devicePropertiesList.add(DeviceProperties.builder().name("设备温度").value(moisture.getMppTemp().toString()).unit("%").build());
        devicePropertiesList.add(DeviceProperties.builder().name("电池电量").value(moisture.getBatteryCap().toString()).unit("mAh").build());
        devicePropertiesList.add(DeviceProperties.builder().name("电池电压").value(moisture.getBatteryVolt().toString()).unit("V").build());
        devicePropertiesList.add(DeviceProperties.builder().name("电池电流").value(moisture.getBatteryCur().toString()).unit("A").build());
        devicePropertiesList.add(DeviceProperties.builder().name("电池功率").value(moisture.getBatteryPower().toString()).unit("W").build());
        devicePropertiesList.add(DeviceProperties.builder().name("负载电压").value(moisture.getLoadVolt().toString()).unit("V").build());
        devicePropertiesList.add(DeviceProperties.builder().name("负载电流").value(moisture.getLoadCur().toString()).unit("A").build());
        devicePropertiesList.add(DeviceProperties.builder().name("负载功率").value(moisture.getLoadPower().toString()).unit("W").build());

        return devicePropertiesList;
    }
}
