package cn.iocoder.yudao.module.system.service.devicewarn;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.devicewarn.vo.DeviceWarnPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.devicewarn.vo.DeviceWarnSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.deviceInfo.DeviceInfoDO;
import cn.iocoder.yudao.module.system.dal.dataobject.devicewarn.DeviceWarnDO;
import cn.iocoder.yudao.module.system.dal.mysql.deviceInfo.DeviceInfoMapper;
import cn.iocoder.yudao.module.system.dal.mysql.devicewarn.DeviceWarnMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.DEVICE_WARN_NOT_EXISTS;

/**
 * 设备报警 Service 实现类
 *
 * @author 我是秦俊旗
 */
@Service
@Validated
public class DeviceWarnServiceImpl implements DeviceWarnService {

    @Resource
    private DeviceWarnMapper deviceWarnMapper;
    @Resource
    private DeviceInfoMapper deviceInfoMapper;
    @Resource
    private HttpServletRequest httpServletRequest;

    @Override
    public Long createDeviceWarn(DeviceWarnSaveReqVO createReqVO) {
        // 插入
        DeviceWarnDO deviceWarn = BeanUtils.toBean(createReqVO, DeviceWarnDO.class);
        String mac = this.getHearder();
        DeviceInfoDO deviceInfoDO = deviceInfoMapper.selectOne(new LambdaQueryWrapper<DeviceInfoDO>()
                .eq(DeviceInfoDO::getDeviceIp, mac)
                .last("limit 1"));
        deviceWarn.setDeviceSn(mac);
        deviceWarn.setStoreId(deviceInfoDO.getCarteenId());
        deviceWarn.setDeviceName(deviceInfoDO.getDeviceName());
        deviceWarnMapper.insert(deviceWarn);
        // 返回
        return deviceWarn.getId();
    }

    @Override
    public void updateDeviceWarn(DeviceWarnSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceWarnExists(updateReqVO.getId());
        // 更新
        DeviceWarnDO updateObj = BeanUtils.toBean(updateReqVO, DeviceWarnDO.class);
        deviceWarnMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceWarn(Long id) {
        // 校验存在
        validateDeviceWarnExists(id);
        // 删除
        deviceWarnMapper.deleteById(id);
    }

    private void validateDeviceWarnExists(Long id) {
        if (deviceWarnMapper.selectById(id) == null) {
            throw exception(DEVICE_WARN_NOT_EXISTS);
        }
    }

    @Override
    public DeviceWarnDO getDeviceWarn(Long id) {
        return deviceWarnMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceWarnDO> getDeviceWarnPage(DeviceWarnPageReqVO pageReqVO) {
        return deviceWarnMapper.selectPage(pageReqVO);
    }
    public String getHearder(){
        try {
            return httpServletRequest.getHeader("Authorization");
        } catch (Exception e) {
            throw exception(2000_10_001,"无法获取设备编码");
        }
    }
}