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

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.group.vo.IotYfGroupPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.group.vo.IotYfGroupSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.group.IotYfGroupDO;
import cn.iocoder.yudao.module.pms.dal.mysql.yanfan.group.IotYfGroupMapper;
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 IotYfGroupServiceImpl implements IotYfGroupService {

    @Resource
    private IotYfGroupMapper iotYfGroupMapper;

    @Override
    public Long createIotGroup(IotYfGroupSaveReqVO createReqVO) {
        // 插入
        IotYfGroupDO iotGroup = BeanUtils.toBean(createReqVO, IotYfGroupDO.class);
        iotYfGroupMapper.insert(iotGroup);
        // 返回
        return iotGroup.getGroupId();
    }

    @Override
    public void updateIotGroup(IotYfGroupSaveReqVO updateReqVO) {
        // 校验存在
        validateIotGroupExists(updateReqVO.getGroupId());
        // 更新
        IotYfGroupDO updateObj = BeanUtils.toBean(updateReqVO, IotYfGroupDO.class);
        iotYfGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotGroup(Long id) {
        // 校验存在
        validateIotGroupExists(id);
        // 删除
        iotYfGroupMapper.deleteById(id);
    }

    private void validateIotGroupExists(Long id) {
        if (iotYfGroupMapper.selectById(id) == null) {
            throw exception(new ErrorCode(333,"不存在"));
        }
    }

    @Override
    public IotYfGroupDO getIotGroup(Long id) {
        return iotYfGroupMapper.selectById(id);
    }

    @Override
    public PageResult<IotYfGroupDO> getIotGroupPage(IotYfGroupPageReqVO pageReqVO) {
        return iotYfGroupMapper.selectPage(pageReqVO);
    }

}
