package cn.iocoder.yudao.module.pms.service.yanfan.sip.relation;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.sip.relation.vo.IotYfSipRelationPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.sip.relation.vo.IotYfSipRelationSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.sip.relation.IotYfSipRelationDO;
import cn.iocoder.yudao.module.pms.dal.mysql.yanfan.sip.relation.IotYfSipRelationMapper;
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 java.util.Collections;
import java.util.List;

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

/**
 * yf监控设备关联 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotYfSipRelationServiceImpl implements IotYfSipRelationService {

    @Resource
    private IotYfSipRelationMapper iotYfSipRelationMapper;

    @Override
    public List<IotYfSipRelationDO> selectSipRelationList(IotYfSipRelationPageReqVO sipRelation) {
        return iotYfSipRelationMapper.selectSipRelationList(sipRelation);
    }

    @Override
    public Long createIotSipRelation(IotYfSipRelationSaveReqVO createReqVO) {
        // 插入
        IotYfSipRelationDO iotSipRelation = BeanUtils.toBean(createReqVO, IotYfSipRelationDO.class);
        iotYfSipRelationMapper.insert(iotSipRelation);
        // 返回
        return iotSipRelation.getId();
    }

    @Override
    public void updateIotSipRelation(IotYfSipRelationSaveReqVO updateReqVO) {
        // 校验存在
        validateIotSipRelationExists(updateReqVO.getId());
        // 更新
        IotYfSipRelationDO updateObj = BeanUtils.toBean(updateReqVO, IotYfSipRelationDO.class);
        iotYfSipRelationMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotSipRelation(Long id) {
        // 校验存在
        validateIotSipRelationExists(id);
        // 删除
        iotYfSipRelationMapper.deleteById(id);
    }

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

    @Override
    public IotYfSipRelationDO getIotSipRelation(Long id) {
        return iotYfSipRelationMapper.selectById(id);
    }

    @Override
    public PageResult<IotYfSipRelationDO> getIotSipRelationPage(IotYfSipRelationPageReqVO pageReqVO) {
        return iotYfSipRelationMapper.selectPage(pageReqVO);
    }

}
