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

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
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.DeviceEquipmentDao;
import com.skr.mdm.dao.DeviceInstructUserStatusDao;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.entity.DeviceEquipmentDO;
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.LOSS_EQ_CLEAR_DATA, CmdAdminEnum.LOSS_EQ_DATA, CmdAdminEnum.BACK_EQ, CmdAdminEnum.RESTORE_FACTORY})
public class CmdUserStatusHandler implements CmdAdminProcessHandler {

    @Autowired
    private DeviceInstructUserStatusDao deviceInstructUserStatusDao;
    @Autowired
    private DeviceEquipmentDao equipmentDao;

    @Override
    public Long saveCmd(CmdInfoBO cmdInfoBO, LoginAdminDTO loginAdminDTO) {
        InstructMessageBO instructMessageBO = getInstructMessageBO(cmdInfoBO, loginAdminDTO);
        deviceInstructUserStatusDao.insertInstructUserStatusList(instructMessageBO);
        // 修改设备状态
        Integer equipmentState;
        switch (instructMessageBO.getCmdType()) {
            case 1:
            case 2:
                equipmentState = 2;
                break;
            case 4:
                equipmentState = 3;
                break;
            default:
                equipmentState = 0;
                break;
        }
        new LambdaUpdateChainWrapper<>(equipmentDao)
                .set(DeviceEquipmentDO::getState, equipmentState)
                .in(DeviceEquipmentDO::getId, cmdInfoBO.getEquipmentIdList())
                .update();
        return instructMessageBO.getMessageId();
    }

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