package com.ruoyi.manage.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.entity.ParameterSet;
import com.ruoyi.manage.mapper.ParameterSetMapper;
import com.ruoyi.manage.service.IParameterSetService;
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-06-18
 */
@Service
public class ParameterSetServiceImpl implements IParameterSetService {
    @Autowired
    private ParameterSetMapper parameterSetMapper;
    @Autowired
    private MqttService mqttService;

    /**
     * 查询参数设置
     *
     * @param parameterId 参数设置主键
     * @return 参数设置
     */
    @Override
    public ParameterSet selectParameterSetByParameterId(Long parameterId) {
        return parameterSetMapper.selectParameterSetByParameterId(parameterId);
    }

    /**
     * 查询参数设置列表
     *
     * @param parameterSet 参数设置
     * @return 参数设置
     */
    @Override
    public List<ParameterSet> selectParameterSetList(ParameterSet parameterSet) {
        return parameterSetMapper.selectParameterSetList(parameterSet);
    }

    /**
     * 新增参数设置
     *
     * @param parameterSet 参数设置
     * @return 结果
     */
    @Override
    public AjaxResult insertParameterSet(ParameterSet parameterSet) {
        Long channelId = parameterSet.getChannelId();
        ParameterSet parameterSetOld = parameterSetMapper.selectParameterSetByChannelId(channelId);
        if (parameterSetOld != null) {
            return AjaxResult.error("当前通道已设置参数，请勿重复新增");
        }
        AjaxResult ajaxResult = checkParameter(parameterSet);
        if (ajaxResult.isError()) {
            return ajaxResult;
        }
        parameterSet.setCreateTime(DateUtils.getNowDate());
        parameterSetMapper.insertParameterSet(parameterSet);
        return AjaxResult.success();
    }

    //校验是否需要下发参数
    public AjaxResult checkParameter(ParameterSet parameterSet) {
        Long channelId = parameterSet.getChannelId();
        //下发指令
        String channelRange = parameterSet.getChannelRange();
        String leaveTime = parameterSet.getLeaveTime();
        String playTime = parameterSet.getPlayTime();
        String leaveRate = parameterSet.getLeaveRate();
        String playRate = parameterSet.getPlayRate();
        if (StringUtils.isNotEmpty(channelRange)) {
            try {
                CompletableFuture<Void> future = mqttService.publish(DETECTION_RECT, channelId+"_"+channelRange);
                future.get(10, TimeUnit.SECONDS);
            } catch (InterruptedException | ExecutionException | TimeoutException e) {
                return AjaxResult.error("发布消息失败:" + DETECTION_RECT);
            }
        } else if (StringUtils.isNotEmpty(leaveTime)) {
            try {
                CompletableFuture<Void> future = mqttService.publish(DETECTION_LEAVETIME, channelId+"_"+leaveTime);
                future.get(10, TimeUnit.SECONDS);
            } catch (InterruptedException | ExecutionException | TimeoutException e) {
                return AjaxResult.error("发布消息失败:" + DETECTION_LEAVETIME);
            }
        } else if (StringUtils.isNotEmpty(playTime)) {
            try {
                CompletableFuture<Void> future = mqttService.publish(DETECTION_PLAYTIME, channelId+"_"+playTime);
                future.get(10, TimeUnit.SECONDS);
            } catch (InterruptedException | ExecutionException | TimeoutException e) {
                return AjaxResult.error("发布消息失败:" + DETECTION_PLAYTIME);
            }
        } else if (StringUtils.isNotEmpty(leaveRate)) {
            try {
                CompletableFuture<Void> future = mqttService.publish(DETECTION_LEAVERATE, channelId+"_"+leaveRate);
                future.get(10, TimeUnit.SECONDS);
            } catch (InterruptedException | ExecutionException | TimeoutException e) {
                return AjaxResult.error("发布消息失败:" + DETECTION_LEAVERATE);
            }
        } else if (StringUtils.isNotEmpty(playRate)) {
            try {
                CompletableFuture<Void> future = mqttService.publish(DETECTION_PLAYRATE, channelId+"_"+playRate);
                future.get(10, TimeUnit.SECONDS);
            } catch (InterruptedException | ExecutionException | TimeoutException e) {
                return AjaxResult.error("发布消息失败:" + DETECTION_PLAYRATE);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 修改参数设置
     *
     * @param parameterSet 参数设置
     * @return 结果
     */
    @Override
    public AjaxResult updateParameterSet(ParameterSet parameterSet) {
        AjaxResult ajaxResult = checkParameter(parameterSet);
        if (ajaxResult.isError()) {
            return ajaxResult;
        }
        parameterSet.setUpdateTime(DateUtils.getNowDate());
        parameterSetMapper.updateParameterSet(parameterSet);
        return AjaxResult.success();
    }

    /**
     * 批量删除参数设置
     *
     * @param parameterIds 需要删除的参数设置主键
     * @return 结果
     */
    @Override
    public int deleteParameterSetByParameterIds(Long[] parameterIds) {
        return parameterSetMapper.deleteParameterSetByParameterIds(parameterIds);
    }

    /**
     * 删除参数设置信息
     *
     * @param parameterId 参数设置主键
     * @return 结果
     */
    @Override
    public int deleteParameterSetByParameterId(Long parameterId) {
        return parameterSetMapper.deleteParameterSetByParameterId(parameterId);
    }
}
