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 ...@@ -92,28 +92,29 @@ func (d *Dao) GetBalanceSubCharge(users []string) (map[string]decimal.Decimal, e
break break
} }
//fmt.Println("l", l, "idx", idx, "l+idx", l+idx, "len(redisValueRes)", len(redisValueRes), "v", v, "redisValueRes[l+idx]", redisValueRes[l+idx]) 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 v != nil && redisValueRes[l+idx] != nil {
balanceStr, ok := v.(string) if balanceStr, ok := v.(string); ok {
if ok {
chargeStr, ok := redisValueRes[l+idx].(string)
if ok {
balanceDec, err := decimal.NewFromString(balanceStr) balanceDec, err := decimal.NewFromString(balanceStr)
if err != nil { if err != nil {
fmt.Println("user balance error", users[idx], err.Error()) fmt.Println("user balance error", users[idx], err.Error())
res[users[idx]] = decimal.NewFromInt(0) res[users[idx]] = decimal.NewFromInt(0)
continue 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 { if err != nil {
fmt.Println("user charge error", users[idx], err.Error()) fmt.Println("user charge error", users[idx], err.Error())
res[users[idx]] = decimal.NewFromInt(0) res[users[idx]] = decimal.NewFromInt(0)
continue continue
} }
} else {
chargeDec = decimal.NewFromInt(0)
}
if balanceDec.Cmp(chargeDec) == 1 { if balanceDec.Cmp(chargeDec) == 1 {
realBalance := balanceDec.Sub(chargeDec) realBalance := balanceDec.Sub(chargeDec)
...@@ -125,8 +126,6 @@ func (d *Dao) GetBalanceSubCharge(users []string) (map[string]decimal.Decimal, e ...@@ -125,8 +126,6 @@ func (d *Dao) GetBalanceSubCharge(users []string) (map[string]decimal.Decimal, e
} }
continue continue
}
}
} }
fmt.Println("user balance or charge is nil", "user", users[idx], "balance", v, "charge", redisValueRes[l+idx]) 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) { ...@@ -36,30 +36,30 @@ func TestRedis(t *testing.T) {
} }
func TestChargeIncreaseBy(t *testing.T) { // func TestChargeIncreaseBy(t *testing.T) {
d := &Dao{ // d := &Dao{
redis.NewClient(&redis.Options{ // redis.NewClient(&redis.Options{
Addr: "localhost:6379", // Addr: "localhost:6379",
Password: "", // no password set // Password: "", // no password set
DB: 0, // use default DB // DB: 0, // use default DB
Protocol: 3, // specify 2 for RESP 2 or 3 for RESP 3 // Protocol: 3, // specify 2 for RESP 2 or 3 for RESP 3
}), // }),
} // }
usersFee := make([]UserFee, 0, 100) // usersFee := make([]UserFee, 0, 100)
for i := 0; i < 10; i++ { // for i := 0; i < 10; i++ {
usersFee = append(usersFee, UserFee{ // usersFee = append(usersFee, UserFee{
User: fmt.Sprintf("user-%v", i), // User: fmt.Sprintf("user-%v", i),
Fee: decimal.NewFromInt(int64(i)), // Fee: decimal.NewFromInt(int64(i)),
}) // })
} // }
if err := d.ChargeIncrby(context.Background(), usersFee); err != nil { // if err := d.ChargeIncrby(context.Background(), usersFee); err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
} // }
func TestBalanceIncreaseBy(t *testing.T) { func TestBalanceIncreaseBy(t *testing.T) {
...@@ -77,7 +77,7 @@ func TestBalanceIncreaseBy(t *testing.T) { ...@@ -77,7 +77,7 @@ func TestBalanceIncreaseBy(t *testing.T) {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
usersFee = append(usersFee, UserFee{ usersFee = append(usersFee, UserFee{
User: fmt.Sprintf("user-%v", i), 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