package com.fastbee.iot.service.impl;

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

import com.fastbee.common.enums.OTAUpgrade;
import com.fastbee.common.exception.ServiceException;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.common.utils.bean.BeanUtils;
import com.fastbee.iot.domain.FirmwareTaskDetail;
import com.fastbee.iot.mapper.FirmwareTaskDetailMapper;
import com.fastbee.iot.model.FirmwareTaskInput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fastbee.iot.mapper.FirmwareTaskMapper;
import com.fastbee.iot.domain.FirmwareTask;
import com.fastbee.iot.service.IFirmwareTaskService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 固件升级任务Service业务层处理
 * 
 * @author kerwincui
 * @date 2022-10-24
 */
@Service
public class FirmwareTaskServiceImpl implements IFirmwareTaskService 
{
    @Autowired
    private FirmwareTaskMapper firmwareTaskMapper;
    @Autowired
    private FirmwareTaskDetailMapper firmwareTaskDetailMapper;

    /**
     * 查询固件升级任务
     * 
     * @param id 固件升级任务主键
     * @return 固件升级任务
     */
    @Override
    public FirmwareTask selectFirmwareTaskById(Long id)
    {
        return firmwareTaskMapper.selectFirmwareTaskById(id);
    }

    /**
     * 查询固件升级任务列表
     * 
     * @param firmwareTask 固件升级任务
     * @return 固件升级任务
     */
    @Override
    public List<FirmwareTask> selectFirmwareTaskList(FirmwareTask firmwareTask)
    {
        return firmwareTaskMapper.selectFirmwareTaskList(firmwareTask);
    }

    /**
     * 新增固件升级任务
     * 
     * @param firmwareTask 固件升级任务
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insertFirmwareTask(FirmwareTaskInput firmwareTaskInput)
    {
        //先查询任务是否存在，根据任务名查询
        int count = firmwareTaskMapper.selectCountFirmwareTaskByTaskName(firmwareTaskInput.getTaskName());
        if (count >0){
            throw new ServiceException("任务：" + firmwareTaskInput.getTaskName() +"已存在");
        }
        FirmwareTask firmwareTask = new FirmwareTask();
        BeanUtils.copyBeanProp(firmwareTask,firmwareTaskInput);
        firmwareTask.setCreateTime(DateUtils.getNowDate());
        // 插入固件升级任务主表
        firmwareTaskMapper.insertFirmwareTask(firmwareTask);
        List<String> deviceList = firmwareTaskInput.getDeviceList();
        List<FirmwareTaskDetail> detailList = new ArrayList<>(deviceList.size());
        for (String serialNumber : deviceList) {
            FirmwareTaskDetail detail = new FirmwareTaskDetail();
            detail.setTaskId(firmwareTask.getId());
            detail.setSerialNumber(serialNumber);
            detail.setUpgradeStatus(OTAUpgrade.AWAIT.getStatus());
            detail.setDetailMsg(OTAUpgrade.AWAIT.getSubMsg());
            detail.setTaskName(firmwareTask.getTaskName());
            detail.setCreateTime(DateUtils.getNowDate());
            detailList.add(detail);
        }
        // 批量插入明细表
        firmwareTaskDetailMapper.insertBatchFirmwareTaskDetail(detailList);
        return firmwareTask.getId();
    }

    /**
     * 修改固件升级任务
     * 
     * @param firmwareTask 固件升级任务
     * @return 结果
     */
    @Override
    public int updateFirmwareTask(FirmwareTask firmwareTask)
    {
        firmwareTask.setUpdateTime(DateUtils.getNowDate());
        return firmwareTaskMapper.updateFirmwareTask(firmwareTask);
    }

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

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