package me.zhengjie.modules.security.config.bean;

import lombok.RequiredArgsConstructor;
import me.zhengjie.utils.RedisUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
@RequiredArgsConstructor
public class CheckLoginNumber {

    private final RedisUtils redisUtils;
    
    public Integer checkLoginTimes(String key, Integer errLimit) {
        Object value = redisUtils.get(key);
        Integer number = 0;
        //第一次登陆失败
        if (ObjectUtils.isEmpty(value)) {
            //设置key
            redisUtils.set(key, number);
        } else {
            number = (Integer) value;
        }
        //登陆失败未操作次数限制
        System.out.println(number);
        if (number <  errLimit) {
            number++;
            redisUtils.set(key, number);
        }
        //设置过期时间
        redisUtils.expire(key, 1800);
        return errLimit - number;
    }

}
