package com.tax.file.service.async.strategy;

import com.yxcheng.token.common.cache.RedisService;
import com.yxcheng.token.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ExecuteTimesLimitStrategy {
    @Autowired
    private RedisService redisService;

    private int times = 10;
    private int liveTime = 30*60;

    public boolean isAccept(String key){
        String timesStr = redisService.get(key);
        if(StringUtil.isNotEmpty(timesStr) && Integer.valueOf(timesStr) >=times ){
            return false;
        }
        return true;
    }

    public boolean isExist(String key){
        String timesStr = redisService.get(key);
        if(StringUtil.isNotEmpty(timesStr)){
            return false;
        }
        return true;
    }

    public boolean isAccept(String key,int times){
        String timesStr = redisService.get(key);
        if(StringUtil.isNotEmpty(timesStr) && Integer.valueOf(timesStr) >=times ){
            return false;
        }
        return true;
    }

    public void addTime(String key){
        addTime(key,liveTime);
    }

    public void addTime(String key,int liveTime){
        String timesStr = redisService.get(key);
        if(StringUtil.isEmpty(timesStr))timesStr = "0";
        if(Integer.valueOf(timesStr) < times ){
            int redisTimes = Integer.valueOf(timesStr) + 1;
            redisService.set(key,String.valueOf(redisTimes),liveTime);
        }
    }


    public void addTime(String key,int liveTime,int times){
        String timesStr = redisService.get(key);
        if(StringUtil.isEmpty(timesStr)) {
            timesStr = "0";
        }
        if(Integer.valueOf(timesStr) < times ){
            int redisTimes = Integer.valueOf(timesStr) + 1;
            redisService.set(key,String.valueOf(redisTimes),liveTime);
        }
    }

}
