package com.fastbee.iot.service.impl;

import com.alibaba.fastjson2.JSON;
import com.fastbee.common.core.mq.message.MqttBo;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.iot.domain.SimulateLog;
import com.fastbee.iot.mapper.SimulateLogMapper;
import com.fastbee.iot.service.ISimulateLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 模拟设备日志Service业务层处理
 * @author gsb
 * @date 2023/4/6 17:04
 */
@Service
public class SimulateLogServiceImpl implements ISimulateLogService {

    @Autowired
    private SimulateLogMapper simulateLogMapper;

    /**
     * 查询模拟设备日志
     *
     * @param logId 模拟设备日志主键
     * @return 模拟设备日志
     */
    @Override
    public SimulateLog selectSimulateLogByLogId(Long logId)
    {
        return simulateLogMapper.selectSimulateLogByLogId(logId);
    }

    /**
     * 查询模拟设备日志列表
     *
     * @param simulateLog 模拟设备日志
     * @return 模拟设备日志
     */
    @Override
    public List<MqttBo> selectSimulateLogList(SimulateLog simulateLog)
    {
        List<SimulateLog> list = simulateLogMapper.selectSimulateLogList(simulateLog);
        List<MqttBo> result = new ArrayList<>();
        for (SimulateLog log : list) {
            MqttBo send = JSON.parseObject(log.getSendData(), MqttBo.class);
            send.setTs(log.getCreateTime());
            send.setDirection("send");
            MqttBo receive = JSON.parseObject(log.getCallbackData(), MqttBo.class);
            receive.setTs(log.getCreateTime());
            receive.setDirection("receive");
            result.add(receive);
            result.add(send);
        }
        Collections.reverse(result);
        return result;
    }

    /**
     * 新增模拟设备日志
     *
     * @param simulateLog 模拟设备日志
     * @return 结果
     */
    @Override
    public int insertSimulateLog(SimulateLog simulateLog)
    {
        simulateLog.setCreateTime(DateUtils.getNowDate());
        return simulateLogMapper.insertSimulateLog(simulateLog);
    }

    /**
     * 修改模拟设备日志
     *
     * @param simulateLog 模拟设备日志
     * @return 结果
     */
    @Override
    public int updateSimulateLog(SimulateLog simulateLog)
    {
        return simulateLogMapper.updateSimulateLog(simulateLog);
    }

    /**
     * 批量删除模拟设备日志
     *
     * @param logIds 需要删除的模拟设备日志主键
     * @return 结果
     */
    @Override
    public int deleteSimulateLogByLogIds(Long[] logIds)
    {
        return simulateLogMapper.deleteSimulateLogByLogIds(logIds);
    }

    /**
     * 删除模拟设备日志信息
     *
     * @param logId 模拟设备日志主键
     * @return 结果
     */
    @Override
    public int deleteSimulateLogByLogId(Long logId)
    {
        return simulateLogMapper.deleteSimulateLogByLogId(logId);
    }

    /**
     * 根据设备编号删除模拟设备日志信息
     *
     * @param serialNumber 设备编号
     * @return 结果
     */
    @Override
    public int deleteSimulateLogByDeviceNumber(String serialNumber)
    {
        return simulateLogMapper.deleteSimulateLogBySerialNumber(serialNumber);
    }
}
