package cn.iocoder.yudao.module.iot.service.thingmodel;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.ThingModelCommonPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.ThingModelCommonSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.ThingModelCommonDO;
import cn.iocoder.yudao.module.iot.dal.mysql.thingmodel.ThingModelCommonMapper;
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;

/**
 * IoT通用物模型 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ThingModelCommonServiceImpl implements ThingModelCommonService {

    @Resource
    private ThingModelCommonMapper thingModelCommonMapper;

    @Override
    public Long createThingModelCommon(ThingModelCommonSaveReqVO createReqVO) {
        // 插入
        ThingModelCommonDO thingModelCommon = BeanUtils.toBean(createReqVO, ThingModelCommonDO.class);
        thingModelCommon.setDeleted(false);
        thingModelCommonMapper.insert(thingModelCommon);
        // 返回
        return thingModelCommon.getId();
    }

    @Override
    public void updateThingModelCommon(ThingModelCommonSaveReqVO updateReqVO) {
        // 校验存在
        validateThingModelCommonExists(updateReqVO.getId());
        // 更新
        ThingModelCommonDO updateObj = BeanUtils.toBean(updateReqVO, ThingModelCommonDO.class);
        thingModelCommonMapper.updateById(updateObj);
    }

    @Override
    public void deleteThingModelCommon(Long id) {
        // 校验存在
        validateThingModelCommonExists(id);
        // 删除
        thingModelCommonMapper.deleteById(id);
    }

    private void validateThingModelCommonExists(Long id) {
        if (thingModelCommonMapper.selectById(id) == null) {
            throw exception(new ErrorCode(1,"不存在"));
        }
    }

    @Override
    public ThingModelCommonDO getThingModelCommon(Long id) {
        return thingModelCommonMapper.selectById(id);
    }

    @Override
    public PageResult<ThingModelCommonDO> getThingModelCommonPage(ThingModelCommonPageReqVO pageReqVO) {
        return thingModelCommonMapper.selectPage(pageReqVO);
    }

}