package com.djhk.common.enums;

import lombok.Getter;

public enum IpRequestTimeCheckEnum {

    ONE_SECONDS(1,30L,"一秒钟限制30次"),
    FIVE_SECONDS(15,300L,"五秒钟限制100次"),
    THIRTY_SECONDS(30,500L,"30秒钟限制500次"),
    ;

    @Getter
    private Integer seconds;

    @Getter
    private Long times;

    @Getter
    private String desc;

    IpRequestTimeCheckEnum(Integer seconds, Long times, String desc) {
        this.seconds = seconds;
        this.times = times;
        this.desc = desc;
    }

    public static IpRequestTimeCheckEnum getBySeconds(Integer seconds) {
        if (seconds == null) {
            return null;
        }

        for (IpRequestTimeCheckEnum checkEnum : IpRequestTimeCheckEnum.values()) {
            if (seconds.equals(checkEnum.getSeconds())) {
                return checkEnum;
            }
        }

        return null;
    }

    public boolean checkTimes(Integer seconds,Long times) {
        IpRequestTimeCheckEnum checkEnum = getBySeconds(seconds);
        if (checkEnum != null) {
            return checkEnum.getTimes().compareTo(times) < 0;
        }

        return false;
    }
}
