package com.ai.da.common.utils;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.ai.da.common.config.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 流量访问限制工具类
 */
@Slf4j
public class AccessLimitUtils {

    /**
     * 校验是否超过指定流量限制
     *
     * @param interfaceName
     * @return
     */
    public static void validate(String interfaceName, Integer count) {
        Integer useCount = LocalCacheUtils.getAidaInterfaceCurrentLimitingCache(interfaceName);
        if (useCount > count) {
            //系统繁忙
            throw new BusinessException("system.busy");
        } else {
            useCount++;
            LocalCacheUtils.setAidaInterfaceCurrentLimitingCache(interfaceName, useCount);
        }
    }

    /**
     * 校验过后 接口完毕 去掉限流
     *
     * @param interfaceName
     * @return
     */
    public static void validateOut(String interfaceName) {
        Integer useCount = LocalCacheUtils.getAidaInterfaceCurrentLimitingCache(interfaceName);
        useCount--;
        LocalCacheUtils.setAidaInterfaceCurrentLimitingCache(interfaceName, useCount);
    }

}
