fix: 修复提现冻结逻辑错误导致数据库约束违反
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled
- 余额检查从 wallet.Balance 改为 wallet.GetAvailableBalance(), 防止 frozen_balance 已有值时误判为余额充足 - 冻结余额时只增加 frozen_balance,不再同时减少 balance, 与系统其他冻结操作保持一致(balance = 总额,frozen 为其子集) - 修复后 SQL 不再触发 chk_agent_wallet_frozen_balance 约束
This commit is contained in:
@@ -125,8 +125,8 @@ func (s *Service) CreateWithdrawalRequest(ctx context.Context, req *dto.CreateMy
|
||||
return nil, errors.New(errors.CodeInsufficientBalance, "钱包不存在")
|
||||
}
|
||||
|
||||
// 验证余额
|
||||
if req.Amount > wallet.Balance {
|
||||
// 验证可用余额(balance - frozen_balance),冻结中的金额不可再次提现
|
||||
if req.Amount > wallet.GetAvailableBalance() {
|
||||
return nil, errors.New(errors.CodeInsufficientBalance, "可提现余额不足")
|
||||
}
|
||||
|
||||
@@ -159,11 +159,12 @@ func (s *Service) CreateWithdrawalRequest(ctx context.Context, req *dto.CreateMy
|
||||
var withdrawalRequest *model.CommissionWithdrawalRequest
|
||||
|
||||
err = s.db.Transaction(func(tx *gorm.DB) error {
|
||||
// 冻结余额(使用条件更新 + RowsAffected 校验,防止并发重复提现)
|
||||
// 冻结余额:只增加 frozen_balance,balance(总额)不变
|
||||
// balance = 总余额,frozen_balance = 其中被锁定的子集,可用余额 = balance - frozen_balance
|
||||
// 使用条件更新 + RowsAffected 校验,防止并发重复提现
|
||||
result := tx.WithContext(ctx).Model(&model.AgentWallet{}).
|
||||
Where("id = ? AND balance >= ?", wallet.ID, req.Amount).
|
||||
Where("id = ? AND balance - frozen_balance >= ?", wallet.ID, req.Amount).
|
||||
Updates(map[string]interface{}{
|
||||
"balance": gorm.Expr("balance - ?", req.Amount),
|
||||
"frozen_balance": gorm.Expr("frozen_balance + ?", req.Amount),
|
||||
})
|
||||
if result.Error != nil {
|
||||
|
||||
Reference in New Issue
Block a user