package cn.iocoder.yudao.module.pms.service.iotcarzhbd;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotcarzhbd.vo.IotCarZhbdPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotcarzhbd.vo.IotCarZhbdSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotcarzhbd.IotCarZhbdDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotcarzhbd.IotCarZhbdMapper;
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.pms.enums.ErrorCodeConstant.IOT_CAR_ZHBD_NOT_EXISTS;

/**
 * 中航北斗车辆表 Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotCarZhbdServiceImpl implements IotCarZhbdService {

    @Resource
    private IotCarZhbdMapper iotCarZhbdMapper;

    @Override
    public Long createIotCarZhbd(IotCarZhbdSaveReqVO createReqVO) {
        // 插入
        IotCarZhbdDO iotCarZhbd = BeanUtils.toBean(createReqVO, IotCarZhbdDO.class);
        iotCarZhbdMapper.insert(iotCarZhbd);
        // 返回
        return iotCarZhbd.getId();
    }

    @Override
    public void updateIotCarZhbd(IotCarZhbdSaveReqVO updateReqVO) {
        // 校验存在
        validateIotCarZhbdExists(updateReqVO.getId());
        // 更新
        IotCarZhbdDO updateObj = BeanUtils.toBean(updateReqVO, IotCarZhbdDO.class);
        iotCarZhbdMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotCarZhbd(Long id) {
        // 校验存在
        validateIotCarZhbdExists(id);
        // 删除
        iotCarZhbdMapper.deleteById(id);
    }

    private void validateIotCarZhbdExists(Long id) {
        if (iotCarZhbdMapper.selectById(id) == null) {
            throw exception(IOT_CAR_ZHBD_NOT_EXISTS);
        }
    }

    @Override
    public IotCarZhbdDO getIotCarZhbd(Long id) {
        return iotCarZhbdMapper.selectById(id);
    }

    @Override
    public PageResult<IotCarZhbdDO> getIotCarZhbdPage(IotCarZhbdPageReqVO pageReqVO) {
        return iotCarZhbdMapper.selectPage(pageReqVO);
    }

}