package Nredis

import (
	l4g "proxy/logger"
	"strconv"
	"time"
)

func CheckIfBundleLimitSent(planId uint32, rate int) bool {
	n, err := rdb.Exists(ctx, "BundleLimit:"+strconv.Itoa(int(planId))+":"+strconv.Itoa(rate)).Result()
	if err != nil {
		l4g.RedisLog.Errorf("Check Exists PlanExpiry:%d, err: %v", planId, err)
		return false
	} else {
		if n >= 1 {
			return true
		} else {
			return false
		}
	}
}

func SetPlanExpire(planId uint32, rate int) error {
	key := "BundleLimit:"+strconv.Itoa(int(planId))+":"+strconv.Itoa(rate)
	err := rdb.Set(ctx, key, "1", time.Hour * 24 * 15).Err()
	if err != nil {
		l4g.RedisLog.Errorf("Set BundleLimit:%d:%d, err: %v", planId, rate, err)
	} else {
		l4g.RedisLog.Debugf("Set BundleLimit:%d:%d, succ", planId, rate)
	}

	return err
}
