diff --git a/internal/service/my_commission/service.go b/internal/service/my_commission/service.go index a2b39fa..64c5a72 100644 --- a/internal/service/my_commission/service.go +++ b/internal/service/my_commission/service.go @@ -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 {