package com.fastbee.deviceData.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.deviceData.domain.DeviceRealtimedataWorms;
import com.fastbee.deviceData.domain.dto.DeviceProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fastbee.deviceData.mapper.DeviceRealtimedataMiaoqingMapper;
import com.fastbee.deviceData.domain.DeviceRealtimedataMiaoqing;
import com.fastbee.deviceData.service.IDeviceRealtimedataMiaoqingService;

/**
 * 苗青设备实时数据Service业务层处理
 * 
 * @author kerwincui
 * @date 2024-11-14
 */
@Service
public class DeviceRealtimedataMiaoqingServiceImpl implements IDeviceRealtimedataMiaoqingService 
{
    @Autowired
    private DeviceRealtimedataMiaoqingMapper deviceRealtimedataMiaoqingMapper;

    /**
     * 查询苗青设备实时数据
     * 
     * @param id 苗青设备实时数据主键
     * @return 苗青设备实时数据
     */
    @Override
    public DeviceRealtimedataMiaoqing selectDeviceRealtimedataMiaoqingById(Long id)
    {
        return deviceRealtimedataMiaoqingMapper.selectDeviceRealtimedataMiaoqingById(id);
    }

    /**
     * 查询苗青设备实时数据列表
     * 
     * @param deviceRealtimedataMiaoqing 苗青设备实时数据
     * @return 苗青设备实时数据
     */
    @Override
    public List<DeviceRealtimedataMiaoqing> selectDeviceRealtimedataMiaoqingList(DeviceRealtimedataMiaoqing deviceRealtimedataMiaoqing)
    {
        return deviceRealtimedataMiaoqingMapper.selectDeviceRealtimedataMiaoqingList(deviceRealtimedataMiaoqing);
    }

    /**
     * 新增苗青设备实时数据
     * 
     * @param deviceRealtimedataMiaoqing 苗青设备实时数据
     * @return 结果
     */
    @Override
    public int insertDeviceRealtimedataMiaoqing(DeviceRealtimedataMiaoqing deviceRealtimedataMiaoqing)
    {
        return deviceRealtimedataMiaoqingMapper.insertDeviceRealtimedataMiaoqing(deviceRealtimedataMiaoqing);
    }

    /**
     * 修改苗青设备实时数据
     * 
     * @param deviceRealtimedataMiaoqing 苗青设备实时数据
     * @return 结果
     */
    @Override
    public int updateDeviceRealtimedataMiaoqing(DeviceRealtimedataMiaoqing deviceRealtimedataMiaoqing)
    {
        return deviceRealtimedataMiaoqingMapper.updateDeviceRealtimedataMiaoqing(deviceRealtimedataMiaoqing);
    }

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

    /**
     * 删除苗青设备实时数据信息
     * 
     * @param id 苗青设备实时数据主键
     * @return 结果
     */
    @Override
    public int deleteDeviceRealtimedataMiaoqingById(Long id)
    {
        return deviceRealtimedataMiaoqingMapper.deleteDeviceRealtimedataMiaoqingById(id);
    }

    /**
     * 获取最新一条苗青数据
     * @param deviceId
     * @return
     */
    @Override
    public List<DeviceProperties> getLatestMiaoqingRealtimedata(String deviceId){
        System.err.println(deviceId);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("real_time");
        queryWrapper.last("limit 1");
        queryWrapper.eq("device_addr",deviceId);
        DeviceRealtimedataMiaoqing miaoqing = deviceRealtimedataMiaoqingMapper.selectOne(queryWrapper);
        if(Objects.isNull(miaoqing)){
//            return devicePropertiesList;
        }
        return null;
    }
}
