package com.skr.mdm.api.cmd.handler;

import com.skr.mdm.api.cmd.CmdAdminProcessHandler;
import com.skr.mdm.api.cmd.annotation.CmdAdminHandler;
import com.skr.mdm.bean.bo.CmdInfoBO;
import com.skr.mdm.bean.bo.InstructMessageBO;
import com.skr.mdm.bean.qo.cmd.CmdQO;
import com.skr.mdm.bean.vo.equipment.SendCmdMessageInfoVO;
import com.skr.mdm.constant.SendCmdTypeConstants;
import com.skr.mdm.dao.DeviceEquipmentDao;
import com.skr.mdm.dao.DeviceInstructRestoreFactoryDao;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.enums.CmdAdminEnum;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author nfq
 * @date 2021/10/28 17:50
 */
@CmdAdminHandler(cmdType = {CmdAdminEnum.ONLY_RESTORE_FACTORY})
public class CmdFactoryResetHandler implements CmdAdminProcessHandler {

    @Autowired
    private DeviceInstructRestoreFactoryDao deviceInstructRestoreFactoryDao;
    @Autowired
    private DeviceEquipmentDao equipmentDao;

    @Override
    public Long saveCmd(CmdInfoBO cmdInfoBO, LoginAdminDTO loginAdminDTO) {
        InstructMessageBO instructMessageBO = getInstructMessageBO(cmdInfoBO, loginAdminDTO);
        deviceInstructRestoreFactoryDao.insertInstructRestoreFactory(instructMessageBO);
        List<Integer> inactiveDeviceIdList = equipmentDao.selectInactiveDeviceIdList(cmdInfoBO.getEquipmentIdList(), 0);
        if (SendCmdTypeConstants.DELETE_FACTORY.equals(instructMessageBO.getCmdType()) && inactiveDeviceIdList.size() > 0) {
            cmdInfoBO.getEquipmentIdList().removeAll(inactiveDeviceIdList);
            equipmentDao.deleteBatchIds(inactiveDeviceIdList);
        }
        return instructMessageBO.getMessageId();
    }

    @Override
    public ResponseResult cmdUserInfo(CmdQO cmdQO) {
        List<SendCmdMessageInfoVO> sendCmdMessageInfoVOList = deviceInstructRestoreFactoryDao.selectSendInfoList(cmdQO);
        return ResponseResult.success(PageUtil.getPageResult(sendCmdMessageInfoVOList));
    }
}
