优化提现问题
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"hfb_sys/backend/internal/modules/wallet"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// 用户创建提现申请
|
||||
@@ -146,22 +147,22 @@ func (r *Repository) FindByID(ctx context.Context, userID, id uint64) (*Withdraw
|
||||
|
||||
// 用户取消提现
|
||||
func (r *Repository) Cancel(ctx context.Context, userID, id uint64) error {
|
||||
db := r.db.WithContext(ctx)
|
||||
var withdrawal model.WithdrawalRequest
|
||||
if err := db.Where("id = ? AND user_id = ?", id, userID).
|
||||
First(&withdrawal).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrWithdrawalNotFound
|
||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var withdrawal model.WithdrawalRequest
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("id = ? AND user_id = ?", id, userID).
|
||||
First(&withdrawal).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrWithdrawalNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// 只有待审核状态可以取消
|
||||
if withdrawal.Status != "pending" {
|
||||
return ErrWithdrawalLocked
|
||||
}
|
||||
// 事务内加行锁后重新校验,只有待审核状态可以取消,避免与管理员审核并发重复解冻
|
||||
if withdrawal.Status != "pending" {
|
||||
return ErrWithdrawalLocked
|
||||
}
|
||||
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
// 更新状态
|
||||
if err := tx.Model(&withdrawal).Update("status", "cancelled").Error; err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user