package com.ruoyi.manage.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.ChannelNumber;
import com.ruoyi.common.core.domain.entity.ParameterSet;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.manage.domain.ChannelNumberVo;
import com.ruoyi.manage.domain.EquipmentManage;
import com.ruoyi.manage.mapper.ChannelNumberMapper;
import com.ruoyi.manage.mapper.EquipmentManageMapper;
import com.ruoyi.manage.mapper.ParameterSetMapper;
import com.ruoyi.manage.service.IChannelNumberService;
import com.ruoyi.mqtt.service.MqttService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static com.ruoyi.common.constant.Constants.*;


/**
 * 通道管理Service业务层处理
 *
 * @author boman
 * @date 2025-05-07
 */
@Service
public class ChannelNumberServiceImpl implements IChannelNumberService {
    @Autowired
    private ChannelNumberMapper channelNumberMapper;

    @Autowired
    private ParameterSetMapper parameterSetMapper;
    @Autowired
    private EquipmentManageMapper equipmentManageMapper;

    @Autowired
    private MqttService mqttService;

    /**
     * 查询通道管理
     *
     * @param channelId 通道管理主键
     * @return 通道管理
     */
    @Override
    public ChannelNumber selectChannelNumberByChannelId(Long channelId) {
        ChannelNumber channelNumber = channelNumberMapper.selectChannelNumberByChannelId(channelId);
        channelNumber.setIsChannel(N);
        //判断是否设置过参数
        ParameterSet parameterSet = parameterSetMapper.selectParameterSetByChannelId(channelId);
        if (parameterSet != null) {
            channelNumber.setIsChannel(Y);
            channelNumber.setParameterSet(parameterSet);
        }
        Long equipmentId = channelNumber.getEquipmentId();
        EquipmentManage equipmentManage = equipmentManageMapper.selectEquipmentManageByEquipmentId(equipmentId);
        if (equipmentManage != null) {
            channelNumber.setEquipmentIp(equipmentManage.getEquipmentIp());
        }
        return channelNumber;
    }

    /**
     * 查询通道管理列表
     *
     * @param channelNumber 通道管理
     * @return 通道管理
     */
    @Override
    public List<ChannelNumber> selectChannelNumberList(ChannelNumber channelNumber) {
        List<ChannelNumber> channelNumbers = channelNumberMapper.selectChannelNumberList(channelNumber);
        if (channelNumbers != null && !channelNumbers.isEmpty()) {
            ChannelNumber channelNumberOne = channelNumbers.get(0);
            Long channelId = channelNumberOne.getChannelId();
            //判断是否设置过参数
            ParameterSet parameterSet = parameterSetMapper.selectParameterSetByChannelId(channelId);
            channelNumberOne.setIsChannel(N);
            if (parameterSet != null) {
                channelNumberOne.setIsChannel(Y);
                channelNumberOne.setParameterSet(parameterSet);
            }
        }
        return channelNumbers;
    }

    /**
     * 新增通道管理
     *
     * @param channelNumber 通道管理
     * @return 结果
     */
    @Override
    public AjaxResult insertChannelNumber(ChannelNumber channelNumber) {
        ChannelNumber channelNumberOld = channelNumberMapper.selectChannelNumberByChannelNum(channelNumber);
        if (channelNumberOld != null) {
            return AjaxResult.error("设备已存在当前通道编号");
        }
        channelNumber.setCreateTime(DateUtils.getNowDate());
        channelNumberMapper.insertChannelNumber(channelNumber);
        Long channelId = channelNumber.getChannelId();
        String videoAddress = channelNumber.getVideoAddress();
        if (StringUtils.isNotEmpty(videoAddress)) {
            try {
                CompletableFuture<Void> future = mqttService.publish(DETECTION_RTSP, channelId + "_" + videoAddress);
                future.get(10, TimeUnit.SECONDS);
            } catch (InterruptedException | ExecutionException | TimeoutException e) {
                return AjaxResult.error("发布消息失败:" + DETECTION_RTSP);
            }
        }
        return AjaxResult.success();
    }


    /**
     * 修改通道管理
     *
     * @param channelNumber 通道管理
     * @return 结果
     */

    public void updateChannelNumberByChannelId(ChannelNumberVo channelNumber) {
        channelNumberMapper.updateChannelNumberByChannelId(channelNumber);
    }

    /**
     * 修改通道管理
     *
     * @param channelNumber 通道管理
     * @return 结果
     */
    @Override
    public AjaxResult updateChannelNumber(ChannelNumber channelNumber) {
        Long channelId = channelNumber.getChannelId();
        ChannelNumber channelNumberOld = channelNumberMapper.selectChannelNumberByChannelNum(channelNumber);
        if (channelNumberOld != null && !channelNumber.getChannelId().equals(channelNumberOld.getChannelId())) {
            return AjaxResult.error("设备已存在当前通道编号");
        }
        String videoAddress = channelNumber.getVideoAddress();
        if (StringUtils.isNotEmpty(videoAddress)) {
            try {
                CompletableFuture<Void> future = mqttService.publish(DETECTION_RTSP, channelId + "_" + videoAddress);
                future.get(10, TimeUnit.SECONDS);
            } catch (InterruptedException | ExecutionException | TimeoutException e) {
                return AjaxResult.error("发布消息失败:" + DETECTION_RTSP);
            }
        }
        channelNumber.setUpdateTime(DateUtils.getNowDate());
        channelNumberMapper.updateChannelNumber(channelNumber);
        return AjaxResult.success();
    }

    /**
     * 批量删除通道管理
     *
     * @param channelIds 需要删除的通道管理主键
     * @return 结果
     */
    @Override
    public int deleteChannelNumberByChannelIds(Long[] channelIds) {
        return channelNumberMapper.deleteChannelNumberByChannelIds(channelIds);
    }

    /**
     * 删除通道管理信息
     *
     * @param channelId 通道管理主键
     * @return 结果
     */
    @Override
    public int deleteChannelNumberByChannelId(Long channelId) {
        return channelNumberMapper.deleteChannelNumberByChannelId(channelId);
    }
}
