Skip to content

Commit

Permalink
Fix negative wallet balance (#53)
Browse files Browse the repository at this point in the history
修复钱包余额小于扣款金额时,余额会变成负数的问题
  • Loading branch information
vatebur authored May 29, 2024
1 parent 9976d9e commit ab22138
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,19 @@ func GetGroupWalletOf(sortable bool, uids ...int64) (wallets []Wallet, err error
}

// InsertWalletOf 更新钱包(money > 0 增加,money < 0 减少)
func InsertWalletOf(uid int64, money int) error {
func (sql *Storage) InsertWalletOf(uid int64, money int) error {
sql.Lock()
defer sql.Unlock()
lastMoney := sdb.getWalletOf(uid)
return sdb.updateWalletOf(uid, lastMoney.Money+money)
newMoney := lastMoney.Money + money
if newMoney < 0 {
newMoney = 0
}
return sdb.updateWalletOf(uid, newMoney)
}

// 获取钱包数据
func (sql *Storage) getWalletOf(uid int64) (wallet Wallet) {
sql.RLock()
defer sql.RUnlock()
uidstr := strconv.FormatInt(uid, 10)
_ = sql.db.Find("storage", &wallet, "where uid is "+uidstr)
return
Expand Down Expand Up @@ -110,8 +114,6 @@ func (sql *Storage) getGroupWalletOf(sortable bool, uids ...int64) (wallets []Wa

// 更新钱包
func (sql *Storage) updateWalletOf(uid int64, money int) (err error) {
sql.Lock()
defer sql.Unlock()
return sql.db.Insert("storage", &Wallet{
UID: uid,
Money: money,
Expand Down

0 comments on commit ab22138

Please sign in to comment.