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

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.iotwebtopoprojectdevice.vo.IotWebtopoProjectDevicePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotwebtopoprojectdevice.vo.IotWebtopoProjectDeviceSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotwebtopoprojectdevice.IotWebtopoProjectDeviceDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotwebtopoprojectdevice.IotWebtopoProjectDeviceMapper;
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_WEBTOPO_PROJECT_DEVICE_NOT_EXISTS;

/**
 * 组态项目关联设备 Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotWebtopoProjectDeviceServiceImpl implements IotWebtopoProjectDeviceService {

    @Resource
    private IotWebtopoProjectDeviceMapper iotWebtopoProjectDeviceMapper;

    @Override
    public Long createIotWebtopoProjectDevice(IotWebtopoProjectDeviceSaveReqVO createReqVO) {
        // 插入
        IotWebtopoProjectDeviceDO iotWebtopoProjectDevice = BeanUtils.toBean(createReqVO, IotWebtopoProjectDeviceDO.class);
        iotWebtopoProjectDeviceMapper.insert(iotWebtopoProjectDevice);
        // 返回
        return iotWebtopoProjectDevice.getId();
    }

    @Override
    public void updateIotWebtopoProjectDevice(IotWebtopoProjectDeviceSaveReqVO updateReqVO) {
        // 校验存在
        validateIotWebtopoProjectDeviceExists(updateReqVO.getId());
        // 更新
        IotWebtopoProjectDeviceDO updateObj = BeanUtils.toBean(updateReqVO, IotWebtopoProjectDeviceDO.class);
        iotWebtopoProjectDeviceMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotWebtopoProjectDevice(Long id) {
        // 校验存在
        validateIotWebtopoProjectDeviceExists(id);
        // 删除
        iotWebtopoProjectDeviceMapper.deleteById(id);
    }

    private void validateIotWebtopoProjectDeviceExists(Long id) {
        if (iotWebtopoProjectDeviceMapper.selectById(id) == null) {
            throw exception(IOT_WEBTOPO_PROJECT_DEVICE_NOT_EXISTS);
        }
    }

    @Override
    public IotWebtopoProjectDeviceDO getIotWebtopoProjectDevice(Long id) {
        return iotWebtopoProjectDeviceMapper.selectById(id);
    }

    @Override
    public PageResult<IotWebtopoProjectDeviceDO> getIotWebtopoProjectDevicePage(IotWebtopoProjectDevicePageReqVO pageReqVO) {
        return iotWebtopoProjectDeviceMapper.selectPage(pageReqVO);
    }

}