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

import java.util.List;
import com.shzg.common.utils.DateUtils;
import com.shzg.project.worn.sensor.config.MqttClientConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shzg.project.worn.mapper.MqttTopicConfigMapper;
import com.shzg.project.worn.domain.MqttTopicConfig;
import com.shzg.project.worn.service.IMqttTopicConfigService;

/**
 * MQTT主题配置Service业务层处理
 * 
 * @author shzg
 * @date 2026-04-08
 */
@Service
public class MqttTopicConfigServiceImpl implements IMqttTopicConfigService 
{
    @Autowired
    private MqttTopicConfigMapper mqttTopicConfigMapper;

    @Autowired
    private MqttClientConfig mqttClientConfig;


    /**
     * 查询MQTT主题配置
     * 
     * @param id MQTT主题配置主键
     * @return MQTT主题配置
     */
    @Override
    public MqttTopicConfig selectMqttTopicConfigById(Long id)
    {
        return mqttTopicConfigMapper.selectMqttTopicConfigById(id);
    }

    /**
     * 查询MQTT主题配置列表
     * 
     * @param mqttTopicConfig MQTT主题配置
     * @return MQTT主题配置
     */
    @Override
    public List<MqttTopicConfig> selectMqttTopicConfigList(MqttTopicConfig mqttTopicConfig)
    {
        return mqttTopicConfigMapper.selectMqttTopicConfigList(mqttTopicConfig);
    }

    /**
     * 查询所有启用的 MQTT 主题配置
     *
     * @return MQTT 主题配置集合
     */
    @Override
    public List<MqttTopicConfig> selectEnabledMqttTopicConfigList()
    {
        return mqttTopicConfigMapper.selectEnabledMqttTopicConfigList();
    }

    /**
     * 新增MQTT主题配置
     * 
     * @param mqttTopicConfig MQTT主题配置
     * @return 结果
     */
    @Override
    public int insertMqttTopicConfig(MqttTopicConfig mqttTopicConfig)
    {
        mqttTopicConfig.setCreateTime(DateUtils.getNowDate());
        int rows = mqttTopicConfigMapper.insertMqttTopicConfig(mqttTopicConfig);

        mqttClientConfig.refreshSubscribe();

        return rows;
    }

    /**
     * 修改MQTT主题配置
     * 
     * @param mqttTopicConfig MQTT主题配置
     * @return 结果
     */
    @Override
    public int updateMqttTopicConfig(MqttTopicConfig mqttTopicConfig)
    {
        mqttTopicConfig.setUpdateTime(DateUtils.getNowDate());
        int rows = mqttTopicConfigMapper.updateMqttTopicConfig(mqttTopicConfig);

        mqttClientConfig.refreshSubscribe();

        return rows;
    }

    /**
     * 批量删除MQTT主题配置
     * 
     * @param ids 需要删除的MQTT主题配置主键
     * @return 结果
     */
    @Override
    public int deleteMqttTopicConfigByIds(Long[] ids)
    {
        int rows = mqttTopicConfigMapper.deleteMqttTopicConfigByIds(ids);

        mqttClientConfig.refreshSubscribe();

        return rows;
    }
    /**
     * 删除MQTT主题配置信息
     * 
     * @param id MQTT主题配置主键
     * @return 结果
     */
    @Override
    public int deleteMqttTopicConfigById(Long id)
    {
        return mqttTopicConfigMapper.deleteMqttTopicConfigById(id);
    }

    @Override
    public MqttTopicConfig selectByDeptId(Long deptId) {
        return mqttTopicConfigMapper.selectByDeptId(deptId);
    }
}
