package cn.iocoder.yudao.module.device.service.cameradevice;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.device.controller.admin.cameradevice.vo.CameraDevicePageReqVO;
import cn.iocoder.yudao.module.device.controller.admin.cameradevice.vo.CameraDeviceSaveReqVO;
import cn.iocoder.yudao.module.device.dal.dataobject.cameradevice.CameraDeviceDO;
import cn.iocoder.yudao.module.device.dal.mysql.cameradevice.CameraDeviceMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.smartfactory.enums.ErrorCodeConstants.CAMERA_DEVICE_NOT_EXISTS;

/**
 * 设备-监控摄像头 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class CameraDeviceServiceImpl implements CameraDeviceService {

    @Resource
    private CameraDeviceMapper cameraDeviceMapper;

    @Override
    public Long createCameraDevice(CameraDeviceSaveReqVO createReqVO) {
        // 插入
        CameraDeviceDO cameraDevice = BeanUtils.toBean(createReqVO, CameraDeviceDO.class);
        cameraDeviceMapper.insert(cameraDevice);
        // 返回
        return cameraDevice.getId();
    }

    @Override
    public void updateCameraDevice(CameraDeviceSaveReqVO updateReqVO) {
        // 校验存在
        validateCameraDeviceExists(updateReqVO.getId());
        // 更新
        CameraDeviceDO updateObj = BeanUtils.toBean(updateReqVO, CameraDeviceDO.class);
        cameraDeviceMapper.updateById(updateObj);
    }

    @Override
    public void deleteCameraDevice(Long id) {
        // 校验存在
        validateCameraDeviceExists(id);
        // 删除
        cameraDeviceMapper.deleteById(id);
    }

    private void validateCameraDeviceExists(Long id) {
        if (cameraDeviceMapper.selectById(id) == null) {
            throw exception(CAMERA_DEVICE_NOT_EXISTS);
        }
    }

    @Override
    public CameraDeviceDO getCameraDevice(Long id) {
        return cameraDeviceMapper.selectById(id);
    }

    @Override
    public PageResult<CameraDeviceDO> getCameraDevicePage(CameraDevicePageReqVO pageReqVO) {
        return cameraDeviceMapper.selectPage(pageReqVO);
    }

}