package com.shzg.project.worn.service.impl;

import java.util.List;
import com.shzg.common.utils.DateUtils;
import com.shzg.project.worn.unit.MqttDeviceCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shzg.project.worn.mapper.MqttSensorDeviceMapper;
import com.shzg.project.worn.domain.MqttSensorDevice;
import com.shzg.project.worn.service.IMqttSensorDeviceService;

/**
 * MQTT设备Service业务层处理
 * 
 * @author shzg
 * @date 2026-04-01
 */
@Service
public class MqttSensorDeviceServiceImpl implements IMqttSensorDeviceService 
{
    @Autowired
    private MqttSensorDeviceMapper mqttSensorDeviceMapper;

    @Autowired
    private MqttDeviceCache deviceCache;

    /**
     * 查询MQTT设备
     * 
     * @param id MQTT设备主键
     * @return MQTT设备
     */
    @Override
    public MqttSensorDevice selectMqttSensorDeviceById(Long id)
    {
        return mqttSensorDeviceMapper.selectMqttSensorDeviceById(id);
    }

    /**
     * 查询MQTT设备列表
     * 
     * @param mqttSensorDevice MQTT设备
     * @return MQTT设备
     */
    @Override
    public List<MqttSensorDevice> selectMqttSensorDeviceList(MqttSensorDevice mqttSensorDevice)
    {
        return mqttSensorDeviceMapper.selectMqttSensorDeviceList(mqttSensorDevice);
    }

    @Override
    public List<MqttSensorDevice> selectAllForMqttCache()
    {
        return mqttSensorDeviceMapper.selectAllForMqttCache();
    }

    /**
     * 新增MQTT设备
     * 
     * @param mqttSensorDevice MQTT设备
     * @return 结果
     */
    @Override
    public int insertMqttSensorDevice(MqttSensorDevice mqttSensorDevice)
    {
        mqttSensorDevice.setCreateTime(DateUtils.getNowDate());
        int rows = mqttSensorDeviceMapper.insertMqttSensorDevice(mqttSensorDevice);

        // 👉 刷新缓存
        deviceCache.refresh();

        return rows;
    }

    /**
     * 修改MQTT设备
     * 
     * @param mqttSensorDevice MQTT设备
     * @return 结果
     */
    @Override
    public int updateMqttSensorDevice(MqttSensorDevice mqttSensorDevice)
    {
        mqttSensorDevice.setUpdateTime(DateUtils.getNowDate());
        int rows = mqttSensorDeviceMapper.updateMqttSensorDevice(mqttSensorDevice);

        // 👉 刷新缓存
        deviceCache.refresh();

        return rows;
    }

    /**
     * 批量删除MQTT设备
     * 
     * @param ids 需要删除的MQTT设备主键
     * @return 结果
     */
    @Override
    public int deleteMqttSensorDeviceByIds(Long[] ids)
    {
        int rows = mqttSensorDeviceMapper.deleteMqttSensorDeviceByIds(ids);

        // 👉 刷新缓存
        deviceCache.refresh();

        return rows;
    }

    /**
     * 删除MQTT设备信息
     * 
     * @param id MQTT设备主键
     * @return 结果
     */
    @Override
    public int deleteMqttSensorDeviceById(Long id)
    {
        return mqttSensorDeviceMapper.deleteMqttSensorDeviceById(id);
    }

    /**
     * 根据DevEui查询设备
     * @param devEui
     * @return
     */
    @Override
    public MqttSensorDevice selectByDevEui(String devEui) {

        if (devEui == null || devEui.isEmpty()) {
            return null;
        }

        return mqttSensorDeviceMapper.selectByDevEui(devEui.toLowerCase());
    }

    @Override
    public int updateRuntimeStatus(Long deviceId, String status)
    {
        if (deviceId == null || status == null || status.isEmpty()) {
            return 0;
        }

        MqttSensorDevice update = new MqttSensorDevice();
        update.setId(deviceId);
        update.setStatus(status);
        update.setUpdateTime(DateUtils.getNowDate());

        return mqttSensorDeviceMapper.updateRuntimeStatus(update);
    }
}
