2be25df7d2
- P04: 修复 Withdrawal.TableName() 返回正确表名 withdrawals
- P03: 新增提现状态常量 (constant/withdrawal.go),消除魔法数字 0/1/2/3;
新增 CommissionTypeWithdrawCancel (338) 和 WithdrawalCancelForbidden 错误码
- P02: 迁移 02123 为 withdrawals 表添加 method/account/qr_code_url 列;
扩展 Withdrawal GORM 模型和 API 类型(CommissionWithdrawRequest、WithdrawalLog);
申请提现时增加收款方式校验逻辑
- P01: 新增用户取消提现接口 POST /v1/public/user/withdrawal_cancel;
仅可取消自己的待审核提现,退还佣金并写佣金日志
- P05: 管理端提现列表新增 method 过滤参数
Co-authored-by: multica-agent <github@multica.ai>
83 lines
3.4 KiB
Go
83 lines
3.4 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
logicCommon "github.com/perfect-panel/server/internal/logic/common"
|
|
"github.com/perfect-panel/server/internal/model/log"
|
|
usermodel "github.com/perfect-panel/server/internal/model/user"
|
|
"github.com/perfect-panel/server/internal/svc"
|
|
"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"
|
|
)
|
|
|
|
func approveWithdrawal(ctx context.Context, svcCtx *svc.ServiceContext, withdrawalID int64) error {
|
|
return svcCtx.DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
withdrawal, err := logicCommon.LoadPendingWithdrawalForUpdate(ctx, tx, withdrawalID)
|
|
if err != nil {
|
|
if err.Error() == "withdrawal status invalid" {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.WithdrawalStatusInvalid), "withdrawal %d already processed", withdrawalID)
|
|
}
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "load withdrawal failed: %v", err)
|
|
}
|
|
|
|
if err := tx.Model(&usermodel.Withdrawal{}).
|
|
Where("id = ? AND status = ?", withdrawalID, constant.WithdrawalStatusPending).
|
|
Updates(map[string]interface{}{
|
|
"status": constant.WithdrawalStatusApproved,
|
|
"reason": "",
|
|
}).Error; err != nil {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "approve withdrawal failed: %v", err)
|
|
}
|
|
|
|
if err := logicCommon.WriteCommissionLog(tx, withdrawal.UserId, log.CommissionTypeWithdraw, withdrawal.Amount, ""); err != nil {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "write commission log failed: %v", err)
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func rejectWithdrawal(ctx context.Context, svcCtx *svc.ServiceContext, withdrawalID int64, reason string) error {
|
|
reason = strings.TrimSpace(reason)
|
|
return svcCtx.DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
withdrawal, err := logicCommon.LoadPendingWithdrawalForUpdate(ctx, tx, withdrawalID)
|
|
if err != nil {
|
|
if err.Error() == "withdrawal status invalid" {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.WithdrawalStatusInvalid), "withdrawal %d already processed", withdrawalID)
|
|
}
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "load withdrawal failed: %v", err)
|
|
}
|
|
|
|
if err := tx.Model(&usermodel.Withdrawal{}).
|
|
Where("id = ? AND status = ?", withdrawalID, constant.WithdrawalStatusPending).
|
|
Updates(map[string]interface{}{
|
|
"status": constant.WithdrawalStatusRejected,
|
|
"reason": reason,
|
|
}).Error; err != nil {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "reject withdrawal failed: %v", err)
|
|
}
|
|
|
|
if err := svcCtx.UserModel.UpdateCommission(ctx, withdrawal.UserId, withdrawal.Amount, tx); err != nil {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "refund commission failed: %v", err)
|
|
}
|
|
|
|
if err := logicCommon.WriteCommissionLog(tx, withdrawal.UserId, log.CommissionTypeWithdrawReject, withdrawal.Amount, ""); err != nil {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "write commission log failed: %v", err)
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func isWithdrawalScene(remark string) bool {
|
|
normalized := strings.ToLower(strings.TrimSpace(remark))
|
|
return strings.Contains(normalized, "withdraw") || strings.Contains(normalized, "提现")
|
|
}
|
|
|
|
func logWithdrawalGuard(l logger.Logger, userID int64) {
|
|
l.Errorw("blocked commission overwrite in withdrawal scene", logger.Field("user_id", userID))
|
|
}
|