package cn.iocoder.yudao.module.system.service.permission;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuTenantRelatePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuTenantRelateSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuTenantRelateDO;
import cn.iocoder.yudao.module.system.dal.mysql.permission.MenuTenantRelateMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.TENANT_RELATE_NOT_EXISTS;

/**
 * 菜单租户关系 Service 实现类
 *
 * @author zhaopq
 */
@Service
@Validated
public class MenuTenantRelateServiceImpl implements MenuTenantRelateService {

    @Resource
    private MenuTenantRelateMapper tenantRelateMapper;

    @Override
    public Long createTenantRelate(MenuTenantRelateSaveReqVO createReqVO) {
        // 插入
        MenuTenantRelateDO tenantRelate = BeanUtils.toBean(createReqVO, MenuTenantRelateDO.class);
        tenantRelateMapper.insert(tenantRelate);
        // 返回
        return tenantRelate.getId();
    }

    @Override
    public void updateTenantRelate(MenuTenantRelateSaveReqVO updateReqVO) {
        // 校验存在
        validateTenantRelateExists(updateReqVO.getId());
        // 更新
        MenuTenantRelateDO updateObj = BeanUtils.toBean(updateReqVO, MenuTenantRelateDO.class);
        tenantRelateMapper.updateById(updateObj);
    }

    @Override
    public void deleteTenantRelate(Long id) {
        // 校验存在
        validateTenantRelateExists(id);
        // 删除
        tenantRelateMapper.deleteById(id);
    }

    private void validateTenantRelateExists(Long id) {
        if (tenantRelateMapper.selectById(id) == null) {
            throw exception(TENANT_RELATE_NOT_EXISTS);
        }
    }

    @Override
    public MenuTenantRelateDO getTenantRelate(Long id) {
        return tenantRelateMapper.selectById(id);
    }

    @Override
    public PageResult<MenuTenantRelateDO> getTenantRelatePage(MenuTenantRelatePageReqVO pageReqVO) {
        return tenantRelateMapper.selectPage(pageReqVO);
    }

}