package cn.iocoder.yudao.module.pms.service.yanfan.thingsmodeltemplate;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.thingsmodeltemplate.vo.IotYfThingsModelTranslatePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.thingsmodeltemplate.vo.IotYfThingsModelTranslateSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.thingsmodeltemplate.IotYfThingsModelTranslateDO;
import cn.iocoder.yudao.module.pms.dal.mysql.yanfan.thingsmodel.IotYfThingsModelTranslateMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * yf物模型翻译 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotThingsModelTranslateServiceImpl implements IotThingsModelTranslateService {

    @Resource
    private IotYfThingsModelTranslateMapper iotYfThingsModelTranslateMapper;

    @Override
    public Long createIotThingsModelTranslate(IotYfThingsModelTranslateSaveReqVO createReqVO) {
        // 插入
        IotYfThingsModelTranslateDO iotThingsModelTranslate = BeanUtils.toBean(createReqVO, IotYfThingsModelTranslateDO.class);
        iotYfThingsModelTranslateMapper.insert(iotThingsModelTranslate);
        // 返回
        return iotThingsModelTranslate.getId();
    }

    @Override
    public void updateIotThingsModelTranslate(IotYfThingsModelTranslateSaveReqVO updateReqVO) {
        // 校验存在
        validateIotThingsModelTranslateExists(updateReqVO.getId());
        // 更新
        IotYfThingsModelTranslateDO updateObj = BeanUtils.toBean(updateReqVO, IotYfThingsModelTranslateDO.class);
        iotYfThingsModelTranslateMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotThingsModelTranslate(Long id) {
        // 校验存在
        validateIotThingsModelTranslateExists(id);
        // 删除
        iotYfThingsModelTranslateMapper.deleteById(id);
    }

    private void validateIotThingsModelTranslateExists(Long id) {
        if (iotYfThingsModelTranslateMapper.selectById(id) == null) {
            throw exception(new ErrorCode(666,"不存在"));
        }
    }

    @Override
    public IotYfThingsModelTranslateDO getIotThingsModelTranslate(Long id) {
        return iotYfThingsModelTranslateMapper.selectById(id);
    }

    @Override
    public PageResult<IotYfThingsModelTranslateDO> getIotThingsModelTranslatePage(IotYfThingsModelTranslatePageReqVO pageReqVO) {
        return iotYfThingsModelTranslateMapper.selectPage(pageReqVO);
    }

}
