package cn.iocoder.yudao.module.safe.service.camera;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.safe.controller.admin.camera.vo.AppSafeCameraPageReqVO;
import cn.iocoder.yudao.module.safe.controller.admin.camera.vo.AppSafeCameraSaveReqVO;
import cn.iocoder.yudao.module.safe.dal.dataobject.camera.SafeCameraDO;
import cn.iocoder.yudao.module.safe.dal.mysql.camera.SafeCameraMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.safe.enums.ErrorCodeConstants.CAMERA_NOT_EXISTS;

/**
 * 安防前端设备 Service 实现类
 *
 * @author caoyang
 */
@Service
@Validated
public class SafeCameraServiceImpl implements SafeCameraService {

    @Resource
    private SafeCameraMapper cameraMapper;

    @Override
    public Long createCamera(AppSafeCameraSaveReqVO createReqVO) {
        // 插入
        SafeCameraDO camera = BeanUtils.toBean(createReqVO, SafeCameraDO.class);
        cameraMapper.insert(camera);
        // 返回
        return camera.getId();
    }

    @Override
    public void updateCamera(AppSafeCameraSaveReqVO updateReqVO) {
        // 校验存在
        validateCameraExists(updateReqVO.getId());
        // 更新
        SafeCameraDO updateObj = BeanUtils.toBean(updateReqVO, SafeCameraDO.class);
        cameraMapper.updateById(updateObj);
    }

    @Override
    public void deleteCamera(Long id) {
        // 校验存在
        validateCameraExists(id);
        // 删除
        cameraMapper.deleteById(id);
    }

    private void validateCameraExists(Long id) {
        if (cameraMapper.selectById(id) == null) {
            throw exception(CAMERA_NOT_EXISTS);
        }
    }

    @Override
    public SafeCameraDO getCamera(Long id) {
        return cameraMapper.selectById(id);
    }

    @Override
    public PageResult<SafeCameraDO> getCameraPage(AppSafeCameraPageReqVO pageReqVO) {
        return cameraMapper.selectPage(pageReqVO);
    }

}
