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

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.protocol.vo.IotYfProtocolPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.protocol.vo.IotYfProtocolSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.protocol.IotYfProtocolDO;
import cn.iocoder.yudao.module.pms.dal.mysql.yanfan.protocol.IotYfProtocolMapper;
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 IotYfProtocolServiceImpl implements IotYfProtocolService {

    @Resource
    private IotYfProtocolMapper iotYfProtocolMapper;

    @Override
    public Long createIotProtocol(IotYfProtocolSaveReqVO createReqVO) {
        // 插入
        IotYfProtocolDO iotProtocol = BeanUtils.toBean(createReqVO, IotYfProtocolDO.class);
        iotYfProtocolMapper.insert(iotProtocol);
        // 返回
        return iotProtocol.getId();
    }

    @Override
    public void updateIotProtocol(IotYfProtocolSaveReqVO updateReqVO) {
        // 校验存在
        validateIotProtocolExists(updateReqVO.getId());
        // 更新
        IotYfProtocolDO updateObj = BeanUtils.toBean(updateReqVO, IotYfProtocolDO.class);
        iotYfProtocolMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotProtocol(Long id) {
        // 校验存在
        validateIotProtocolExists(id);
        // 删除
        iotYfProtocolMapper.deleteById(id);
    }

    private void validateIotProtocolExists(Long id) {
        if (iotYfProtocolMapper.selectById(id) == null) {
            throw exception(new ErrorCode(888,"不存在"));
        }
    }

    @Override
    public IotYfProtocolDO getIotProtocol(Long id) {
        return iotYfProtocolMapper.selectById(id);
    }

    @Override
    public PageResult<IotYfProtocolDO> getIotProtocolPage(IotYfProtocolPageReqVO pageReqVO) {
        return iotYfProtocolMapper.selectPage(pageReqVO);
    }

}
