package com.ruoyi.system.service.impl;


import cn.hutool.json.JSONUtil;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.system.domain.SiteInfo;
import com.ruoyi.system.domain.TaskNotice;
import com.ruoyi.system.domain.TaskResult;
import com.ruoyi.system.dto.ManualMeasureDTO;
import com.ruoyi.system.mapper.SiteInfoMapper;
import com.ruoyi.system.mapper.TaskNoticeMapper;
import com.ruoyi.system.mapper.TaskResultMapper;
import com.ruoyi.system.service.DirectiveService;
import com.ruoyi.system.service.MqttGateWayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;


/**
 * classname: 站点信息
 * description  服务实现类
 */
@Service
@Slf4j
public class DirectiveServiceImpl implements DirectiveService {

    @Resource
    private SiteInfoMapper siteInfoMapper;

    @Resource
    MqttGateWayService mqttGateWayService;

    @Resource
    private TaskNoticeMapper taskNoticeMapper;

    @Resource
    private TaskResultMapper taskResultMapper;

    @Override
    public boolean manualMeasure(ManualMeasureDTO manualMeasureDTO) {
        SiteInfo siteInfo = siteInfoMapper.queryById(manualMeasureDTO.getSiteId());
        if (siteInfo == null) {
            throw new BaseException("站点不存在");
        }
        try {
            log.info("手动下发任务 SiteId:{}", manualMeasureDTO.getSiteId());
            HashMap<String, Object> requestMap = new HashMap<>();
            requestMap.put("id",siteInfo.getId());
            requestMap.put("type",manualMeasureDTO.getType());
            if (manualMeasureDTO.getType() == 1){
                requestMap.put("positions",null);
            } else {
                requestMap.put("positions",manualMeasureDTO.getPositions());
            }
            //TODO 发送mqtt
            String topic = "down/" + siteInfo.getDeviceId() + "/manualMeasure";
            String data = JSONUtil.toJsonStr(requestMap);
            log.info("mqtt发送数据：topic:{} data:{}",topic,data);
            mqttGateWayService.sendMessageToMqtt(data, topic);
        } catch (Exception e){
            log.error("手动测量下发异常", e);
            throw new BaseException("手动测量下发异常");
        }
        return true;
    }


    @Override
    public boolean taskStop(Long siteId,Long action) {
        SiteInfo siteInfo = siteInfoMapper.queryById(siteId);
        if (siteInfo == null) {
            throw new BaseException("站点不存在");
        }
        try {
            TaskNotice taskNotice = new TaskNotice();
            taskNotice.setStartId(0L);
            taskNotice.setCreateTime(new Date());
            taskNotice.setId(siteInfo.getId());
            taskNotice.setSiteId(siteId);
            TaskResult taskResul = taskResultMapper.queryBySiteIdOne(siteId);
            if (taskResul == null) {
                throw new BaseException("当前站点没有任务");
            }
            if (action == 0) {
                taskNotice.setRemark("手动停止任务");
                if (taskResul.getStatus() == 0 || taskResul.getStatus() == 3) {
                    taskResul.setStatus(1);
                } else {
                    throw new BaseException("当前站点任务状态不能停止任务");
                }
            } else if (action == 1) {
                taskNotice.setRemark("手动暂停任务");
                if (taskResul.getStatus() == 0) {
                    taskResul.setStatus(3);
                } else {
                    throw new BaseException("当前站点任务状态不能暂停任务");
                }
            } else if (action == 2) {
                taskNotice.setRemark("手动继续任务");
                if (taskResul.getStatus() == 3) {
                    taskResul.setStatus(0);
                } else {
                    throw new BaseException("当前站点任务状态不能继续任务");
                }
            } else {
                throw new BaseException("action参数错误");
            }
            taskResultMapper.update(taskResul);
            taskNoticeMapper.insert(taskNotice);
            log.info("手动停止任务 SiteId:{} Remark:{}", siteId, taskNotice.getRemark());
            //TODO 发送mqtt
            HashMap<String, Object> requestMap = new HashMap<>();
            requestMap.put("id", siteInfo.getId());
            requestMap.put("action", action);
            String topic = "down/" + siteInfo.getDeviceId() + "/taskStop";
            String data = JSONUtil.toJsonStr(requestMap);
            log.info("mqtt发送数据：topic:{} data:{}",topic,data);
            mqttGateWayService.sendMessageToMqtt(data, topic);
        }catch (BaseException e){
            throw e;
        } catch (Exception e){
            log.error("手动测量下发异常", e);
            throw new BaseException("手动测量下发异常");
        }
        return true;
    }






}
