1、增加钱包余额

This commit is contained in:
2024-10-15 23:58:36 +08:00
parent 8609f8566d
commit fdfdbb5ff6
10 changed files with 313 additions and 285 deletions

View File

@@ -51,8 +51,14 @@ func (m *customWalletsModel) UpdateBalance(session sqlx.Session, ctx context.Con
return err
}
// 使用乐观锁更新余额
result, err := session.Exec("UPDATE wallets SET balance = balance + ?, version = version + 1 WHERE user_id = ? AND version = ?", amount, userId, wallet.Version)
// 检查当前版本是否达到了上限
newVersion := wallet.Version + 1
if newVersion > 100000000 { // 你可以根据实际需要设定上限,比如 UNSIGNED INT 的最大值
newVersion = 1 // 或者设置为其他重置值,比如 0
}
// 使用乐观锁更新余额和版本
result, err := session.Exec("UPDATE wallets SET balance = balance + ?, version = ? WHERE user_id = ? AND version = ?", amount, newVersion, userId, wallet.Version)
if err != nil {
return err
}