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

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.iotmodel.vo.IotModelPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmodel.vo.IotModelSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmodel.IotModelDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotmodel.IotModelMapper;
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_MODEL_NOT_EXISTS;


/**
 * PMS 功能优化 规格型号 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class IotModelServiceImpl implements IotModelService {

    @Resource
    private IotModelMapper iotModelMapper;

    @Override
    public Long createIotModel(IotModelSaveReqVO createReqVO) {
        // 插入
        IotModelDO iotModel = BeanUtils.toBean(createReqVO, IotModelDO.class);
        iotModelMapper.insert(iotModel);
        // 返回
        return iotModel.getId();
    }

    @Override
    public void updateIotModel(IotModelSaveReqVO updateReqVO) {
        // 校验存在
        validateIotModelExists(updateReqVO.getId());
        // 更新
        IotModelDO updateObj = BeanUtils.toBean(updateReqVO, IotModelDO.class);
        iotModelMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotModel(Long id) {
        // 校验存在
        validateIotModelExists(id);
        // 删除
        iotModelMapper.deleteById(id);
    }

    private void validateIotModelExists(Long id) {
        if (iotModelMapper.selectById(id) == null) {
            throw exception(IOT_MODEL_NOT_EXISTS);
        }
    }

    @Override
    public IotModelDO getIotModel(Long id) {
        return iotModelMapper.selectById(id);
    }

    @Override
    public PageResult<IotModelDO> getIotModelPage(IotModelPageReqVO pageReqVO) {
        return iotModelMapper.selectPage(pageReqVO);
    }

}