package cn.iocoder.yudao.module.product.service.slavedevice;

import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.controller.admin.slavedevice.vo.SlaveDevicePageReqVO;
import cn.iocoder.yudao.module.product.dal.dataobject.slavedevice.SlaveDeviceDO;
import cn.iocoder.yudao.module.product.dal.mysql.slavedevice.SlaveDeviceMapper;
import cn.iocoder.yudao.module.product.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

/**
 * 从(子)设备信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SlaveDeviceServiceImpl implements SlaveDeviceService {

    @Resource
    private SlaveDeviceMapper slaveDeviceMapper;

    @Override
    public void deleteSlaveDevice(Long id) {
        // 校验存在
        validateSlaveDeviceExists(id);
        // 删除
        slaveDeviceMapper.deleteById(id);
    }

    private void validateSlaveDeviceExists(Long id) {
        if (slaveDeviceMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.SLAVE_DEVICE_NOT_EXISTS);
        }
    }

    @Override
    public SlaveDeviceDO getSlaveDevice(Long id) {
        return slaveDeviceMapper.selectById(id);
    }

    @Override
    public PageResult<SlaveDeviceDO> getSlaveDevicePage(SlaveDevicePageReqVO pageReqVO) {
        return slaveDeviceMapper.selectPage(pageReqVO);
    }
}
