Files
hi-server/internal/logic/public/user/commissionWithdrawLogic.go
T
shanshanzhong147 d74803bcbe feat: implement withdrawal improvements P01-P05
- P04: fix Withdrawal.TableName() to return 'withdrawals'
- P03: add WithdrawalStatus* and WithdrawalMethod* constants; replace magic numbers; add CommissionTypeWithdrawCancel=338
- P01: add POST /v1/public/user/withdrawal_cancel to cancel pending withdrawals with ownership check, row lock, commission refund, and audit log
- P02: add method/account/qr_code_url fields to Withdrawal model and API; add input validation (qr_code_url required for Alipay/Wechat, account required for bank)
- P05: add method filter to admin GET /v1/admin/user/withdrawal/list

Co-authored-by: multica-agent <github@multica.ai>
2026-05-25 20:44:04 -07:00

117 lines
4.2 KiB
Go

package user
import (
"context"
"time"
logicCommon "github.com/perfect-panel/server/internal/logic/common"
"github.com/perfect-panel/server/internal/model/log"
"github.com/perfect-panel/server/internal/model/user"
"github.com/perfect-panel/server/internal/svc"
"github.com/perfect-panel/server/internal/types"
"github.com/perfect-panel/server/pkg/constant"
"github.com/perfect-panel/server/pkg/logger"
"github.com/perfect-panel/server/pkg/xerr"
"github.com/pkg/errors"
"gorm.io/gorm"
)
type CommissionWithdrawLogic struct {
logger.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// Commission Withdraw
func NewCommissionWithdrawLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommissionWithdrawLogic {
return &CommissionWithdrawLogic{
Logger: logger.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *CommissionWithdrawLogic) CommissionWithdraw(req *types.CommissionWithdrawRequest) (resp *types.WithdrawalLog, err error) {
u, ok := l.ctx.Value(constant.CtxKeyUser).(*user.User)
if !ok {
logger.Error("current user is not found in context")
return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "Invalid Access")
}
// Validate payment method fields
switch req.Method {
case user.WithdrawalMethodAlipay, user.WithdrawalMethodWechat:
if req.QrCodeUrl == "" {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "qr_code_url is required for method %d", req.Method)
}
case user.WithdrawalMethodBank:
if req.Account == "" {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "account is required for bank transfer")
}
default: // WithdrawalMethodOther
if req.Account == "" && req.Content == "" {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "account or content is required for other methods")
}
}
if u.Commission < req.Amount {
logger.Errorf("User %d has insufficient commission balance: %.2f, requested: %.2f", u.Id, float64(u.Commission)/100, float64(req.Amount)/100)
return nil, errors.Wrapf(xerr.NewErrCode(xerr.UserCommissionNotEnough), "User %d has insufficient commission balance", u.Id)
}
tx := l.svcCtx.DB.WithContext(l.ctx).Begin()
now := time.Now()
// Atomically deduct the requested amount so concurrent commission growth is preserved.
if err = l.svcCtx.DB.WithContext(l.ctx).
Model(&user.User{}).
Where("id = ? AND commission >= ?", u.Id, req.Amount).
UpdateColumn("commission", gorm.Expr("commission - ?", req.Amount)).Error; err != nil {
tx.Rollback()
l.Errorf("Failed to update user %d commission balance: %v", u.Id, err)
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "Failed to update user %d commission balance: %v", u.Id, err)
}
_ = l.svcCtx.UserModel.ClearUserCache(l.ctx, u)
// create withdrawal log
if err = logicCommon.WriteCommissionLog(tx, u.Id, log.CommissionTypeConvertBalance, req.Amount, ""); err != nil {
tx.Rollback()
l.Errorf("Failed to create commission log for user %d: %v", u.Id, err)
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "Failed to create commission log for user %d: %v", u.Id, err)
}
withdrawal := &user.Withdrawal{
UserId: u.Id,
Amount: req.Amount,
Content: req.Content,
Status: user.WithdrawalStatusPending,
Reason: "",
Method: req.Method,
Account: req.Account,
QrCodeUrl: req.QrCodeUrl,
}
if err = tx.Model(&user.Withdrawal{}).Create(withdrawal).Error; err != nil {
tx.Rollback()
l.Errorf("Failed to create withdrawal log for user %d: %v", u.Id, err)
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "Failed to create withdrawal log for user %d: %v", u.Id, err)
}
if err = tx.Commit().Error; err != nil {
l.Errorf("Transaction commit failed for user %d withdrawal: %v", u.Id, err)
return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "Transaction commit failed for user %d withdrawal: %v", u.Id, err)
}
return &types.WithdrawalLog{
Id: withdrawal.Id,
UserId: u.Id,
Amount: req.Amount,
Content: req.Content,
Status: user.WithdrawalStatusPending,
Reason: "",
Method: req.Method,
Account: req.Account,
QrCodeUrl: req.QrCodeUrl,
CreatedAt: now.UnixMilli(),
UpdatedAt: now.UnixMilli(),
}, nil
}