package cn.iocoder.yudao.module.server.service.reverseproxy;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.server.controller.admin.reverseproxy.vo.*;
import cn.iocoder.yudao.module.server.dal.dataobject.reverseproxy.ReverseProxyDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.server.dal.mysql.reverseproxy.ReverseProxyMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.server.enums.ErrorCodeConstants.*;

/**
 * 反向代理配置 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ReverseProxyServiceImpl implements ReverseProxyService {

    @Resource
    private ReverseProxyMapper reverseProxyMapper;

    @Override
    public Long createReverseProxy(ReverseProxySaveReqVO createReqVO) {
        // 插入
        ReverseProxyDO reverseProxy = BeanUtils.toBean(createReqVO, ReverseProxyDO.class);
        reverseProxyMapper.insert(reverseProxy);
        // 返回
        return reverseProxy.getId();
    }

    @Override
    public void updateReverseProxy(ReverseProxySaveReqVO updateReqVO) {
        // 校验存在
        validateReverseProxyExists(updateReqVO.getId());
        // 更新
        ReverseProxyDO updateObj = BeanUtils.toBean(updateReqVO, ReverseProxyDO.class);
        reverseProxyMapper.updateById(updateObj);
    }

    @Override
    public void deleteReverseProxy(Long id) {
        // 校验存在
        validateReverseProxyExists(id);
        // 删除
        reverseProxyMapper.deleteById(id);
    }

    private void validateReverseProxyExists(Long id) {
        if (reverseProxyMapper.selectById(id) == null) {
            throw exception(REVERSE_PROXY_NOT_EXISTS);
        }
    }

    @Override
    public ReverseProxyDO getReverseProxy(Long id) {
        return reverseProxyMapper.selectById(id);
    }

    @Override
    public PageResult<ReverseProxyDO> getReverseProxyPage(ReverseProxyPageReqVO pageReqVO) {
        return reverseProxyMapper.selectPage(pageReqVO);
    }

}