package com.ruoyi.system.service.impl;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.system.domain.SiteConfig;
import com.ruoyi.system.domain.SiteInfo;
import com.ruoyi.system.dto.SiteConfigDTO;
import com.ruoyi.system.dto.SiteConfigMqttDTO;
import com.ruoyi.system.dto.SiteInfoDTO;
import com.ruoyi.system.mapper.SiteConfigMapper;
import com.ruoyi.system.mapper.SiteInfoMapper;
import com.ruoyi.system.paramet.SiteInfoQuery;
import com.ruoyi.system.service.MqttGateWayService;
import com.ruoyi.system.service.SiteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;



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

    @Resource
    private SiteInfoMapper siteInfoMapper;

    @Resource
    private SiteConfigMapper siteConfigMapper;

    @Resource
    private MqttGateWayService mqttGateWayService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insert(SiteInfoDTO siteInfoDTO) {
        try {
            SiteInfo siteInfo = new SiteInfo();
            BeanUtils.copyProperties(siteInfoDTO, siteInfo);
            siteInfo.setSiteId(0L);
            siteInfo.setCreatTime(new Date());
            siteInfo.setIsDel(0);
            return siteInfoMapper.insert(siteInfo) > 0;
        } catch (Exception e){
            log.error("新增站点错误", e);
            throw new BaseException("新增站点错误");
        }
    }

    @Override
    public Page<SiteInfo> query(SiteInfoQuery siteInfoQuery) {
        try {
            if (siteInfoQuery.getPage() == null || siteInfoQuery.getSize() == null){
                siteInfoQuery.setPage(1L);
                siteInfoQuery.setSize(10L);
            }
            Page<SiteInfo> page = new Page<>(siteInfoQuery.getPage(),siteInfoQuery.getSize());
            return siteInfoMapper.queryAllByLimit(siteInfoQuery,page);
        } catch (Exception e){
            log.error("查询站点错误", e);
            throw new BaseException("查询站点错误");
        }

    }

    @Override
    public SiteInfo queryById(Long id) {
        return siteInfoMapper.queryById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean update(SiteInfoDTO siteInfoDTO) {
        try {
            if (siteInfoDTO.getSiteId() == null) {
                throw new BaseException("站点id不能为空");
            }
            SiteInfo siteInfo;
            siteInfo = siteInfoMapper.queryById(siteInfoDTO.getSiteId());
            if (siteInfo == null) {
                throw new BaseException("站点id不存在");
            }
            siteInfo = new SiteInfo();
            BeanUtils.copyProperties(siteInfoDTO, siteInfo);
            return siteInfoMapper.update(siteInfo) > 0;
        } catch (BaseException e){
            throw new BaseException(e.getMessage());
        } catch (Exception e){
            log.error("更新站点错误", e);
            throw new BaseException("更新站点错误");
        }
    }

    @Override
    public boolean delete(Long SiteId) {
        try {
            return siteInfoMapper.deleteById(SiteId) > 0;
        } catch (Exception e){
            log.error("删除站点错误", e);
            throw new BaseException("删除站点错误");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insertConfig(SiteConfigDTO siteConfigDTO) {
        try {
            SiteConfig siteConfig;
            siteConfig = siteConfigMapper.queryBySiteId(siteConfigDTO.getSiteId());
            if (siteConfig != null){
                throw new BaseException("新增失败，该站点已存在配置信息");
            }
            SiteInfo siteInfo = siteInfoMapper.queryById(siteConfigDTO.getSiteId());
            if (siteInfo == null){
                throw new BaseException("新增失败，SiteId错误");
            }
            siteConfig = new SiteConfig();
            BeanUtils.copyProperties(siteConfigDTO, siteConfig);
            siteConfig.setConfigId(0L);
            boolean res = siteConfigMapper.insert(siteConfig) > 0;
            //TODO 下发MQTT消息
            String topic = "down/" + siteInfo.getDeviceId() + "/settings";
            SiteConfigMqttDTO siteConfigMqttDTO = new SiteConfigMqttDTO();
            BeanUtils.copyProperties(siteConfig, siteConfigMqttDTO);
            String data = JSONUtil.toJsonStr(siteConfigMqttDTO);
            log.info("mqtt发送数据：topic:{} data:{}",topic,data);
            mqttGateWayService.sendMessageToMqtt(data, topic);
            return res;
        } catch (BaseException e){
            throw new BaseException(e.getMessage());
        } catch (Exception e){
            log.error("更新站点信息错误", e);
            throw new BaseException("更新站点信息错误");
        }
    }

    @Override
    public SiteConfig queryConfig(Long SiteId) {
        try {
            return siteConfigMapper.queryBySiteId(SiteId);
        } catch (Exception e){
            log.error("查询站点信息错误", e);
            throw new BaseException("查询站点信息错误");
        }
    }


    @Override
    public boolean updateConfig(SiteConfigDTO siteConfigDTO) {
        try {
            if (siteConfigDTO.getSiteId() == null) {
                throw new BaseException("站点id不能为空");
            }
            SiteInfo siteInfo = siteInfoMapper.queryById(siteConfigDTO.getSiteId());
            if (siteInfo == null){
                throw new BaseException("新增失败，SiteId错误");
            }
            SiteConfig siteConfig;
            siteConfig = siteConfigMapper.queryBySiteId(siteConfigDTO.getSiteId());
            if (siteConfig == null) {
                throw new BaseException("站点id不存在");
            }
            Long configId = siteConfig.getConfigId();
            siteConfig = new SiteConfig();
            BeanUtils.copyProperties(siteConfigDTO, siteConfig);
            siteConfig.setConfigId(configId);
            boolean res = siteConfigMapper.update(siteConfig) > 0;
            //TODO 下发MQTT消息测试
            String topic = "down/" + siteInfo.getDeviceId() + "/settings";
            SiteConfigMqttDTO siteConfigMqttDTO = new SiteConfigMqttDTO();
            BeanUtils.copyProperties(siteConfig, siteConfigMqttDTO);
            String data = JSONUtil.toJsonStr(siteConfigMqttDTO);
            log.info("mqtt发送数据：topic:{} data:{}",topic,data);
            mqttGateWayService.sendMessageToMqtt(data, topic);
            return res;
        } catch (BaseException e){
            throw new BaseException(e.getMessage());
        } catch (Exception e){
            log.error("更新站点信息错误", e);
            throw new BaseException("更新站点信息错误");
        }
    }



}
