Commit 8d3acd93 authored by Your Name's avatar Your Name

fix charge is 0

parent 5d23461f
......@@ -92,28 +92,29 @@ func (d *Dao) GetBalanceSubCharge(users []string) (map[string]decimal.Decimal, e
break
}
//fmt.Println("l", l, "idx", idx, "l+idx", l+idx, "len(redisValueRes)", len(redisValueRes), "v", v, "redisValueRes[l+idx]", redisValueRes[l+idx])
if v != nil && redisValueRes[l+idx] != nil {
balanceStr, ok := v.(string)
if ok {
chargeStr, ok := redisValueRes[l+idx].(string)
if ok {
fmt.Println("l", l, "idx", idx, "l+idx", l+idx, "len(redisValueRes)", len(redisValueRes), "v", v, "redisValueRes[l+idx]", redisValueRes[l+idx])
//if v != nil && redisValueRes[l+idx] != nil {
if balanceStr, ok := v.(string); ok {
balanceDec, err := decimal.NewFromString(balanceStr)
if err != nil {
fmt.Println("user balance error", users[idx], err.Error())
res[users[idx]] = decimal.NewFromInt(0)
continue
}
chargeDec, err := decimal.NewFromString(chargeStr)
var chargeDec decimal.Decimal
if chargeStr, ok := redisValueRes[l+idx].(string); ok {
var err error
chargeDec, err = decimal.NewFromString(chargeStr)
if err != nil {
fmt.Println("user charge error", users[idx], err.Error())
res[users[idx]] = decimal.NewFromInt(0)
continue
}
} else {
chargeDec = decimal.NewFromInt(0)
}
if balanceDec.Cmp(chargeDec) == 1 {
realBalance := balanceDec.Sub(chargeDec)
......@@ -125,8 +126,6 @@ func (d *Dao) GetBalanceSubCharge(users []string) (map[string]decimal.Decimal, e
}
continue
}
}
}
fmt.Println("user balance or charge is nil", "user", users[idx], "balance", v, "charge", redisValueRes[l+idx])
......
......@@ -36,30 +36,30 @@ func TestRedis(t *testing.T) {
}
func TestChargeIncreaseBy(t *testing.T) {
d := &Dao{
redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
Protocol: 3, // specify 2 for RESP 2 or 3 for RESP 3
}),
}
usersFee := make([]UserFee, 0, 100)
for i := 0; i < 10; i++ {
usersFee = append(usersFee, UserFee{
User: fmt.Sprintf("user-%v", i),
Fee: decimal.NewFromInt(int64(i)),
})
}
if err := d.ChargeIncrby(context.Background(), usersFee); err != nil {
t.Fatal(err)
}
}
// func TestChargeIncreaseBy(t *testing.T) {
// d := &Dao{
// redis.NewClient(&redis.Options{
// Addr: "localhost:6379",
// Password: "", // no password set
// DB: 0, // use default DB
// Protocol: 3, // specify 2 for RESP 2 or 3 for RESP 3
// }),
// }
// usersFee := make([]UserFee, 0, 100)
// for i := 0; i < 10; i++ {
// usersFee = append(usersFee, UserFee{
// User: fmt.Sprintf("user-%v", i),
// Fee: decimal.NewFromInt(int64(i)),
// })
// }
// if err := d.ChargeIncrby(context.Background(), usersFee); err != nil {
// t.Fatal(err)
// }
// }
func TestBalanceIncreaseBy(t *testing.T) {
......@@ -77,7 +77,7 @@ func TestBalanceIncreaseBy(t *testing.T) {
for i := 0; i < 10; i++ {
usersFee = append(usersFee, UserFee{
User: fmt.Sprintf("user-%v", i),
Fee: decimal.NewFromInt(int64(i * 10)),
Fee: decimal.NewFromInt(int64(i * 10000000)),
})
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment