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.dao.DeviceInstructLockscreenDao;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.enums.CmdAdminEnum;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.util.HmacShaUtils;
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.LOCK_SCREEN, CmdAdminEnum.UNLOCK_SCREEN})
public class CmdLockHandler implements CmdAdminProcessHandler {

    @Autowired
    private DeviceInstructLockscreenDao deviceInstructLockscreenDao;

    @Override
    public Long saveCmd(CmdInfoBO cmdInfoBO, LoginAdminDTO loginAdminDTO) {
        InstructMessageBO instructMessageBO = getInstructMessageBO(cmdInfoBO, loginAdminDTO);
        deviceInstructLockscreenDao.insertInstructLockScreeList(instructMessageBO);
        return instructMessageBO.getMessageId();
    }

    @Override
    public ResponseResult cmdUserInfo(CmdQO cmdQO) {
        List<SendCmdMessageInfoVO> sendCmdMessageInfoVOList = deviceInstructLockscreenDao.selectSendInfoList(cmdQO);
        // 锁屏类型时 更改离线解锁码
        if (CmdAdminEnum.LOCK_SCREEN.getCmdValue().equals(cmdQO.getCmdType())) {
            for (SendCmdMessageInfoVO sendCmdMessageInfoVO : sendCmdMessageInfoVOList) {
                // 随机码转换为离线解锁码
                sendCmdMessageInfoVO.setOfflineUnlockCode(HmacShaUtils.getCode(sendCmdMessageInfoVO.getOfflineUnlockCode()));
            }
        }
        return ResponseResult.success(PageUtil.getPageResult(sendCmdMessageInfoVOList));
    }
}