package Nredis

import (
	"proxy/MsgDef"
	. "proxy/MsgDef"
	l4g "proxy/logger"
	"strings"
	"time"
)

func CheckIfRrExpired(rr *MsgDef.RrData) bool {
	if rr == nil {
		return false
	}

	if rr.MeasureDomain == "01" || rr.MeasureDomain == "03" {
		if rr.UsedValue >= rr.FreeValue {
			return true
		}
		nowStr := time.Now().Format("20060102150405")
		if strings.Compare(nowStr, rr.EndTime) > 0 {
			return true
		}
		return false
	}

	if rr.MeasureDomain == "02" {
		nowStr := time.Now().Format("20060102150405")
		if strings.Compare(nowStr, rr.EndTime) > 0 {
			return true
		}
	}

	return false
}

func RdbGetRrByKey(key string) (*RrData, error) {
	res := rdb.HGetAll(ctx, key)
	err := res.Err()
	if err != nil {
		l4g.RedisLog.Errorf("HGetAll %s, err: %v", key, err)
		return nil, err
	}

	var rr RrData
	if err = res.Scan(&rr); err != nil {
		l4g.RedisLog.Errorf("HGetAll Scan %s, err: %v", key, err)
		return nil, err
	} else {
		//l4g.RedisLog.Debugf("Get %s, [%v]", key, rr)
	}

	return &rr, err
}

func RdbClrExpireRrRecord() error {
	l4g.RedisLog.Warnf("cron clear expire rr!")
	var cursor uint64= 0
	var err error
	for {
		var keys []string
		keys, cursor, err = rdb.Scan(ctx, cursor, "Rr:*", 30).Result()
		if err != nil {
			l4g.RedisLog.Errorf("Scan Rr:*, err: %v", err)
			break
		}

		for _, key := range keys {
			subFields := strings.Split(key, ":")
			if len(subFields) == 4 {
				rr, err1 := RdbGetRrByKey(key)
				if rr != nil && err1 == nil && CheckIfRrExpired(rr) {
					err1 = rdb.Del(ctx, key).Err()
					if err1 != nil {
						l4g.RedisLog.Errorf("Del Rr[%s], err: %v", key, err1)
					} else {
						l4g.RedisLog.Warnf("Del Rr[%s] succ", key)

						l4g.RedisLog.Warnf("SRem RrSet:%s:%s %s", subFields[1], subFields[2], subFields[3])
						_ = rdb.SRem(ctx, "RrSet:"+subFields[1]+":"+subFields[2], subFields[3])
						l4g.RedisLog.Warnf("Del RrId:%s", subFields[3])
						_ = rdb.Del(ctx, "RrId:"+subFields[3]).Err()
						//return srem.Err()
					}
				}
			}
		}
		if cursor == 0 {
			break
		}
	}

	return err
}

func RdbGetAcctPlanByKey(key string) (*AcctData, error) {
	res := rdb.HGetAll(ctx, key)
	err := res.Err()
	if err != nil {
		l4g.RedisLog.Errorf("HGetAll %s, err: %v", key, err)
		return nil, err
	}

	var acct AcctData
	if err = res.Scan(&acct); err != nil {
		l4g.RedisLog.Errorf("HGetAll Scan %s, err: %v", key, err)
		return nil, err
	} else {
		//l4g.RedisLog.Debugf("Get %s, [%v]", key, acct)
	}

	return &acct, err
}

func CheckIfAcctExpired(acct *MsgDef.AcctData) bool {
	if acct == nil {
		return false
	}

	nowStr := time.Now().Format("2006-01-02 15:04:05")
	if strings.Compare(nowStr, acct.ExpTime[:len(nowStr)]) > 0 {
		return true
	}

	return false
}

func RdbClrExpirePlanRecord() error {
	l4g.RedisLog.Warnf("cron clear expire plan!")
	var cursor uint64= 0
	var err error
	for {
		var keys []string
		keys, cursor, err = rdb.Scan(ctx, cursor, "Acct:*", 30).Result()
		if err != nil {
			l4g.RedisLog.Errorf("Scan Acct:*, err: %v", err)
			break
		}

		for _, key := range keys {
			subFields := strings.Split(key, ":")
			if len(subFields) == 3 {
				acct, err1 := RdbGetAcctPlanByKey(key)
				if acct != nil && err1 == nil && CheckIfAcctExpired(acct) {
					err1 = rdb.Del(ctx, key).Err()
					if err1 != nil {
						l4g.RedisLog.Errorf("Del Acct Plan[%s], err: %v", key, err1)
					} else {
						l4g.RedisLog.Warnf("Del Acct Plan[%s] succ", key)

						l4g.RedisLog.Warnf("SRem Acct:%s %s", subFields[1], subFields[2])
						_ = rdb.SRem(ctx, "Acct:"+subFields[1], subFields[2])
						//return srem.Err()
					}
				}
			}
		}
		if cursor == 0 {
			break
		}
	}

	return err
}

func ClrExpRes() {
	_ = RdbClrExpireRrRecord()
	_ = RdbClrExpirePlanRecord()
}