diff --git a/internal/dao/jinzhu/wallet.go b/internal/dao/jinzhu/wallet.go index 148d24a1..54a3cd61 100644 --- a/internal/dao/jinzhu/wallet.go +++ b/internal/dao/jinzhu/wallet.go @@ -71,16 +71,21 @@ func (d *walletServant) HandleRechargeSuccess(recharge *core.WalletRecharge, tra return d.db.Transaction(func(tx *gorm.DB) error { //获取当前时间戳 currentTimestamp := time.Now().Unix() + balance := user.Balance + newBalance := balance + recharge.Amount*864 if balance < currentTimestamp { balance = currentTimestamp + if err := tx.Model(user).Update("balance", newBalance).Error; err != nil { + // 返回任何错误都会回滚事务 + return err + } + } else { + // 扣除金额 + if err := tx.Model(user).Update("balance", gorm.Expr("balance + ?", recharge.Amount*864)).Error; err != nil { + // 返回任何错误都会回滚事务 + return err + } } - // 扣除金额 - if err := tx.Model(user).Update("balance", gorm.Expr("balance + ?", recharge.Amount*864)).Error; err != nil { - // 返回任何错误都会回滚事务 - return err - } - balance := user.Balance - // 新增账单 if err := tx.Create(&dbr.WalletStatement{ UserID: user.ID,