package com.fastbee.iot.service.impl;

import java.util.List;

import com.fastbee.common.core.mq.ota.OtaUpgradeBo;
import com.fastbee.common.enums.OTAUpgrade;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.iot.model.FirmwareTaskDetailInput;
import com.fastbee.iot.model.FirmwareTaskDetailOutput;
import com.fastbee.iot.model.FirmwareTaskDeviceStatistic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fastbee.iot.mapper.FirmwareTaskDetailMapper;
import com.fastbee.iot.domain.FirmwareTaskDetail;
import com.fastbee.iot.service.IFirmwareTaskDetailService;
import org.springframework.util.CollectionUtils;

/**
 * 固件升级任务详细Service业务层处理
 *
 * @author kerwincui
 * @date 2022-10-24
 */
@Service
public class FirmwareTaskDetailServiceImpl implements IFirmwareTaskDetailService {
    @Autowired
    private FirmwareTaskDetailMapper firmwareTaskDetailMapper;

    /**
     * 查询固件升级任务详细
     *
     * @param id 固件升级任务详细主键
     * @return 固件升级任务详细
     */
    @Override
    public FirmwareTaskDetail selectFirmwareTaskDetailById(Long id) {
        return firmwareTaskDetailMapper.selectFirmwareTaskDetailById(id);
    }

    /**
     * 查询固件升级任务详细列表
     *
     * @param firmwareTaskDetail 固件升级任务详细
     * @return 固件升级任务详细
     */
    @Override
    public List<FirmwareTaskDetail> selectFirmwareTaskDetailList(FirmwareTaskDetail firmwareTaskDetail) {
        return firmwareTaskDetailMapper.selectFirmwareTaskDetailList(firmwareTaskDetail);
    }

    /**
     * 新增固件升级任务详细
     *
     * @param firmwareTaskDetail 固件升级任务详细
     * @return 结果
     */
    @Override
    public int insertFirmwareTaskDetail(FirmwareTaskDetail firmwareTaskDetail) {
        firmwareTaskDetail.setCreateTime(DateUtils.getNowDate());
        return firmwareTaskDetailMapper.insertFirmwareTaskDetail(firmwareTaskDetail);
    }

    /**
     * 修改固件升级任务详细
     *
     * @param firmwareTaskDetail 固件升级任务详细
     * @return 结果
     */
    @Override
    public int updateFirmwareTaskDetail(FirmwareTaskDetail firmwareTaskDetail) {
        firmwareTaskDetail.setUpdateTime(DateUtils.getNowDate());
        return firmwareTaskDetailMapper.updateFirmwareTaskDetail(firmwareTaskDetail);
    }

    /**
     * 批量删除固件升级任务详细
     *
     * @param ids 需要删除的固件升级任务详细主键
     * @return 结果
     */
    @Override
    public int deleteFirmwareTaskDetailByIds(Long[] ids) {
        return firmwareTaskDetailMapper.deleteFirmwareTaskDetailByIds(ids);
    }

    /**
     * 删除固件升级任务详细信息
     *
     * @param id 固件升级任务详细主键
     * @return 结果
     */
    @Override
    public int deleteFirmwareTaskDetailById(Long id) {
        return firmwareTaskDetailMapper.deleteFirmwareTaskDetailById(id);
    }

    /**
     * OTA升级更新
     *
     * @param bo     升级返回对象
     * @param status 升级状态
     * @return 版本号
     */
    @Override
    public String update(OtaUpgradeBo bo, OTAUpgrade status) {
        //根据消息id查询 下发的OTA升级数据记录
        FirmwareTaskDetail taskDetail = new FirmwareTaskDetail();
        taskDetail.setMessageId(bo.getMessageId());
        List<FirmwareTaskDetail> taskDetailList = firmwareTaskDetailMapper.selectFirmwareALLDetailList(taskDetail);
        //如果OTA升级数据不存在，将数据插入到数据库
        if (CollectionUtils.isEmpty(taskDetailList)) {
            FirmwareTaskDetail insert = new FirmwareTaskDetail();
            insert.setSerialNumber(bo.getSerialNumber());
            insert.setTaskId(bo.getTaskId());
            insert.setUpgradeStatus(status.getStatus());
            insert.setDetailMsg(status.getDes());
            insert.setMessageId(bo.getMessageId());
            firmwareTaskDetailMapper.insertFirmwareTaskDetail(insert);
            return "";
        }
        //如果OTA升级记录存在，那么更新一下OTA升级的状态
        FirmwareTaskDetail update = taskDetailList.get(0);
        update.setSerialNumber(bo.getSerialNumber());
        update.setUpdateTime(DateUtils.getNowDate());
        update.setUpgradeStatus(status.getStatus());
        update.setDetailMsg(status.getDes());
        firmwareTaskDetailMapper.updateFirmwareTaskDetail(update);
        return update.getVersion();
    }

    /**
     * 根据固件id查询下属设备列表
     * @param firmwareTaskDetailInput
     * @return
     */
    @Override
    public List<FirmwareTaskDetailOutput> selectFirmwareTaskDetailListByFirmwareId(FirmwareTaskDetailInput firmwareTaskDetailInput) {
        return firmwareTaskDetailMapper.selectFirmwareTaskDetailListByFirmwareId(firmwareTaskDetailInput);
    }

    /**
     * 固件升级设备统计
     * @param firmwareTaskDetailInput
     * @return
     */
    @Override
    public List<FirmwareTaskDeviceStatistic> deviceStatistic(FirmwareTaskDetailInput firmwareTaskDetailInput) {
        return firmwareTaskDetailMapper.deviceStatistic(firmwareTaskDetailInput);
    }
}
