package Nredis

import (
	"fmt"
	. "proxy/MsgDef"
	l4g "proxy/logger"
	"strconv"
)

// "Acct:"+acctData.ServiceNbr
func RdbDelAcctRecord(serviceNbr string) {
	var cursor uint64= 0
	for {
		keys, cursorNew, err := rdb.SScan(ctx, "Acct:"+serviceNbr, cursor, "", 0).Result()
		if err != nil {
			l4g.RedisLog.Errorf("SScan Acct:%s, err: %v", serviceNbr, err)
			break
		}

		//fmt.Printf("\nfound %d keys\n", len(keys))
		for _, key := range keys {
			acctKey := "Acct:"+serviceNbr+":"+key
			err := rdb.Del(ctx, acctKey).Err()
			if err != nil {
				l4g.RedisLog.Errorf("Del %s, err: %v", acctKey, err)
			} else {
				//subFields := strings.Split(key, ":")
				//if len(subFields) == 3 && subFields[2] != "" {
					_ = rdb.SRem(ctx, "Acct:"+serviceNbr, key)
					//return srem.Err()
				//}
			}
		}
		if cursorNew == 0 {
			break
		} else {
			cursor = cursorNew
		}
	}
	_ = rdb.Del(ctx, "Acct:"+serviceNbr).Err()

	return
}

func RdbDelAcctOfr(serviceNbr string, ofrId int) error {
	acctKey := "Acct:"+serviceNbr+":"+strconv.Itoa(ofrId)
	err := rdb.Del(ctx, acctKey).Err()
	if err != nil {
		l4g.RedisLog.Errorf("Del %s, err: %v", acctKey, err)
	} else {
		_ = rdb.SRem(ctx, "Acct:"+serviceNbr, strconv.Itoa(ofrId))
	}

	return err
}

func RdbSetAcctRecord(acctData *AcctData) error {
	//&prd_inst_id, &service_nbr, &prd_inst_stas_id, &acct_id, &cust_id
	key := "Acct:"+acctData.ServiceNbr+":"+strconv.Itoa(acctData.OfrId)
	err := rdb.HMSet(ctx, key, "acctType", acctData.AcctType, "prdInstId", acctData.PrdInstId, "serviceNbr", acctData.ServiceNbr,
		"prdInstStasId", acctData.PrdInstStasId, "acctId", acctData.AcctId, "custId", acctData.CustId, "ofrId", acctData.OfrId, "ofrInstId", acctData.OfrInstId,
		"effTime", acctData.EffTime, "expTime", acctData.ExpTime).Err()
	if err != nil {
		l4g.RedisLog.Errorf("HMSet Acct:%s, err: %v", acctData.ServiceNbr, err)
	} else {
		sadd := rdb.SAdd(ctx, "Acct:"+acctData.ServiceNbr, strconv.Itoa(acctData.OfrId))
		if sadd.Err() != nil {
			l4g.RedisLog.Errorf("SAdd Acct:%s %d, err: %v", acctData.ServiceNbr, acctData.OfrId, sadd.Err())
			return sadd.Err()
		} else {
			l4g.RedisLog.Debugf("SAdd Acct:%s %d", acctData.ServiceNbr, acctData.OfrId)
		}
	}

	return err
}

func RdbGetAcctRecordByServiceNbr(serviceNbr string) (rsp string, err error) {
	var cursor uint64= 0
	for {
		keys, cursorNew, err := rdb.SScan(ctx, "Acct:"+serviceNbr, cursor, "", 0).Result()
		if err != nil {
			l4g.RedisLog.Errorf("SScan Acct:%s, err: %v", serviceNbr, err)
			break
		}

		//fmt.Printf("\nfound %d keys\n", len(keys))
		for _, key := range keys {
			acctKey := "Acct:"+serviceNbr+":"+key
			res := rdb.HGetAll(ctx, acctKey)
			if res.Err() != nil {
				l4g.RedisLog.Errorf("HGetAll %s, err: %v", acctKey, err)
			} else {
				var acctData AcctData
				if err = res.Scan(&acctData); err != nil {
					l4g.RedisLog.Errorf("HGetAll Scan %s, err: %v", acctKey, err)
				} else {
					l4g.RedisLog.Debugf("Get %s, [%v]", acctKey, acctData)
					rsp += fmt.Sprintf("%s:***************************************\r\nPrdInstId: %d\r\nPrdInstStasId: %d\r\nCustId: %d\r\nAcctId: %d\r\n",
						acctKey, acctData.PrdInstId, acctData.PrdInstStasId, acctData.CustId, acctData.AcctId)
					rsp += fmt.Sprintf("OfrId: %d\r\nOfrInstId: %d\r\nEffTime: %s\r\nExpTime: %s\r\n",
						acctData.OfrId, acctData.OfrInstId, acctData.EffTime, acctData.ExpTime)
					return rsp, err
				}
			}
		}
		if cursorNew == 0 {
			break
		} else {
			cursor = cursorNew
		}
	}

	return "", err
}
