package com.sf.service.gateway.service.impl;

import java.util.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.sf.common.utils.DateUtils;
import com.sf.common.utils.URLUtils;
import com.sf.service.gateway.domain.GatewayInterfaceInfo;
import com.sf.service.gateway.domain.GatewayServer;
import com.sf.service.gateway.enums.GatewayDataStatus;
import com.sf.service.gateway.enums.GatewayServiceModel;
import com.sf.service.gateway.mapper.GatewayServerMapper;
import com.sf.service.gateway.service.IGatewayInterfaceInfoService;
import com.sf.service.gateway.service.IGatewayServerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 服务管理Service业务层处理
 * 
 * @author zoukun
 * @date 2024-04-22
 */
@Slf4j
@Service
public class GatewayServerServiceImpl implements IGatewayServerService
{
    @Autowired
    private GatewayServerMapper gatewayServerMapper;

    @Autowired
    private IGatewayInterfaceInfoService gatewayInterfaceInfoService;

    /**
     * 查询服务管理
     * 
     * @param id 服务管理主键
     * @return 服务管理
     */
    @Override
    public GatewayServer selectGatewayServerById(Long id)
    {
        if (id == null) {
            return null;
        }
        return gatewayServerMapper.selectGatewayServerById(id);
    }

    /**
     * 查询服务管理列表
     * 
     * @param gatewayServer 服务管理
     * @return 服务管理
     */
    @Override
    public List<GatewayServer> selectGatewayServerList(GatewayServer gatewayServer)
    {
        return gatewayServerMapper.selectGatewayServerList(gatewayServer);
    }

    /**
     * 新增服务管理
     * 
     * @param gatewayServer 服务管理
     * @return 结果
     */
    @Override
    public int insertGatewayServer(GatewayServer gatewayServer)
    {
        checkServerModelConfig(gatewayServer);
        Date nowDate = DateUtils.getNowDate();
        gatewayServer.setCreateTime(nowDate);
        gatewayServer.setUpdateTime(nowDate);
        try {
            return gatewayServerMapper.insertGatewayServer(gatewayServer);
        }catch (DuplicateKeyException e){
            throw new IllegalArgumentException("服务名称不可重复!");
        }
    }

    private void checkServerModelConfig(GatewayServer gatewayServer) {
        GatewayServiceModel serviceModel = GatewayServiceModel.getByCode(gatewayServer.getServiceModel());
        Assert.notNull(serviceModel,"不支持的服务模式!");
        switch (serviceModel){
            case NORMAL:
                Assert.notBlank(gatewayServer.getServerAddress(),"服务地址不能为空");
                Assert.isTrue(URLUtils.isURL(gatewayServer.getServerAddress()),"请输入正确的URL服务地址，如:http://sac.zsmarter.com");
                break;
            case ROUTE:
                Assert.notNull(gatewayServer.getRouteId(),"请选择路由规则");
                break;
            default:
                break;
        }
    }

    /**
     * 修改服务管理
     * 
     * @param gatewayServer 服务管理
     * @return 结果
     */
    @Override
    public int updateGatewayServer(GatewayServer gatewayServer)
    {
        checkServerModelConfig(gatewayServer);
        gatewayServer.setUpdateTime(DateUtils.getNowDate());
        try {
            return gatewayServerMapper.updateGatewayServer(gatewayServer);
        }catch (DuplicateKeyException e){
            throw new IllegalArgumentException("服务名称不可重复!");
        }
    }

    /**
     * 批量删除服务管理
     * 
     * @param ids 需要删除的服务管理主键
     * @return 结果
     */
    @Override
    public int deleteGatewayServerByIds(Long[] ids)
    {
        if (ids == null || ids.length == 0) {
            return 0;
        }
        List<GatewayInterfaceInfo> gatewayInterfaceInfoList = gatewayInterfaceInfoService.selectGatewayInterfaceInfoByServerIds(CollUtil.newHashSet(ids));
        Assert.isTrue(CollUtil.isEmpty(gatewayInterfaceInfoList),"服务存在关联的接口，删除失败！");
        return gatewayServerMapper.deleteGatewayServerByIds(ids);
    }

    /**
     * 删除服务管理信息
     * 
     * @param id 服务管理主键
     * @return 结果
     */
    @Override
    public int deleteGatewayServerById(Long id)
    {
        List<GatewayInterfaceInfo> gatewayInterfaceInfoList = gatewayInterfaceInfoService.selectGatewayInterfaceInfoByServerIds(CollUtil.newHashSet(id));
        Assert.isTrue(CollUtil.isEmpty(gatewayInterfaceInfoList),"服务存在关联的接口，删除失败！");
        return gatewayServerMapper.deleteGatewayServerById(id);
    }

    @Override
    public int updateGatewayServerStatusByIds(Collection<Long> ids, GatewayDataStatus status) {
        if (CollectionUtils.isEmpty(ids) || status == null){
            log.warn("Insufficient update conditions");
            return 0;
        }
        return gatewayServerMapper.updateGatewayServerStatusByIds(ids,status.getCode());
    }

    @Override
    public List<GatewayServer> selectGatewayServerByIds(Collection<Long> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return Collections.emptyList();
        }
        return gatewayServerMapper.selectGatewayServerByIds(ids);
    }

    @Override
    public List<GatewayServer> selectGatewayServerByRouteIds(Collection<Long> routeIds) {
        if (CollectionUtils.isEmpty(routeIds)){
            return Collections.emptyList();
        }
        return gatewayServerMapper.selectGatewayServerByRouteIds(routeIds);
    }
}
