package com.skr.mdm.service.impl.equipment;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.skr.mdm.annotation.CmdHandler;
import com.skr.mdm.constant.CmdTypeConstants;
import com.skr.mdm.dao.DeviceEquipmentDao;
import com.skr.mdm.dao.SysSendMsgUserDao;
import com.skr.mdm.entity.SysSendMsgUserDO;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.service.CmdHandlerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 删除指令回调
 * @author nfq
 * @date 2021/4/9 15:48
 */
@Slf4j
@CmdHandler(cmdType = CmdTypeConstants.EQUIPMENT_DEL)
public class EquipmentDelCallBackServiceImpl implements CmdHandlerService {

    @Autowired
    private DeviceEquipmentDao equipmentDao;
    @Autowired
    private SysSendMsgUserDao sendMsgUserDao;

    @Override
    public ResponseResult cmdHandler(Integer equipmentId, String data) {
        try {
            equipmentDao.deleteById(equipmentId);
            sendMsgUserDao.delete(Wrappers.lambdaQuery(SysSendMsgUserDO.class).eq(SysSendMsgUserDO::getEquipmentId, equipmentId));
            log.info("设备删除回调成功：{}", equipmentId);
            return ResponseResult.success();
        } catch (Exception e) {
            log.error("设备删除回调异常:" + equipmentId + data, e);
            return ResponseResult.failure();
        }
    }
}
