This commit is contained in:
Mrx
2026-03-23 17:13:49 +08:00
parent e10c69fe30
commit c4c9722250
8 changed files with 229 additions and 174 deletions

View File

@@ -7,6 +7,8 @@ import (
"qnc-server/common/ctxdata"
"qnc-server/common/xerr"
"qnc-server/pkg/lzkit/lzUtils"
"regexp"
"strings"
"time"
"github.com/google/uuid"
@@ -25,6 +27,11 @@ type ApplyWithdrawalLogic struct {
svcCtx *svc.ServiceContext
}
const (
withdrawMethodAlipay int64 = 1
withdrawMethodBankCard int64 = 2
)
func NewApplyWithdrawalLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ApplyWithdrawalLogic {
return &ApplyWithdrawalLogic{
Logger: logx.WithContext(ctx),
@@ -65,6 +72,32 @@ func (l *ApplyWithdrawalLogic) ApplyWithdrawal(req *types.ApplyWithdrawalReq) (r
if req.Amount <= 0 {
return nil, errors.Wrapf(xerr.NewErrMsg("提现金额必须大于0"), "")
}
if req.WithdrawMethod != withdrawMethodAlipay && req.WithdrawMethod != withdrawMethodBankCard {
return nil, errors.Wrapf(xerr.NewErrMsg("提现方式不支持"), "")
}
if strings.TrimSpace(req.PayeeName) == "" {
return nil, errors.Wrapf(xerr.NewErrMsg("收款人姓名不能为空"), "")
}
if req.WithdrawMethod == withdrawMethodAlipay {
if strings.TrimSpace(req.PayeeAccount) == "" {
return nil, errors.Wrapf(xerr.NewErrMsg("支付宝账号不能为空"), "")
}
} else {
if strings.TrimSpace(req.BankName) == "" {
return nil, errors.Wrapf(xerr.NewErrMsg("开户行不能为空"), "")
}
bankCardNo := strings.TrimSpace(req.BankCardNo)
if bankCardNo == "" {
return nil, errors.Wrapf(xerr.NewErrMsg("银行卡号不能为空"), "")
}
if !regexp.MustCompile(`^\d{13,19}$`).MatchString(bankCardNo) {
return nil, errors.Wrapf(xerr.NewErrMsg("银行卡号格式不正确"), "")
}
if !regexp.MustCompile(`^1[3-9]\d{9}$`).MatchString(strings.TrimSpace(req.BankReservedMobile)) {
return nil, errors.Wrapf(xerr.NewErrMsg("银行预留手机号格式不正确"), "")
}
}
// 4. 获取钱包信息
wallet, err := l.svcCtx.AgentWalletModel.FindOneByAgentId(l.ctx, agent.Id)
@@ -85,9 +118,9 @@ func (l *ApplyWithdrawalLogic) ApplyWithdrawal(req *types.ApplyWithdrawalReq) (r
}
// 7. 生成提现单号
withdrawNo := fmt.Sprintf("WD%d%d", time.Now().Unix(), agent.Id)
withdrawNo := fmt.Sprintf("WD%d%s", time.Now().Unix(), agent.Id)
// 8. 使用事务处理提现申请
// 8. 使用事务处理提现申请(申请时冻结余额,审核通过后扣减冻结余额)
var withdrawalId string
err = l.svcCtx.AgentWalletModel.Trans(l.ctx, func(transCtx context.Context, session sqlx.Session) error {
// 8.1 冻结余额
@@ -99,15 +132,19 @@ func (l *ApplyWithdrawalLogic) ApplyWithdrawal(req *types.ApplyWithdrawalReq) (r
// 8.2 创建提现记录
withdrawal := &model.AgentWithdrawal{
Id: uuid.New().String(),
AgentId: agent.Id,
WithdrawNo: withdrawNo,
PayeeAccount: req.PayeeAccount,
PayeeName: req.PayeeName,
Amount: req.Amount,
ActualAmount: taxInfo.ActualAmount,
TaxAmount: taxInfo.TaxAmount,
Status: 1, // 处理中(待审核)
Id: uuid.New().String(),
AgentId: agent.Id,
WithdrawNo: withdrawNo,
WithdrawMethod: req.WithdrawMethod,
PayeeAccount: strings.TrimSpace(req.PayeeAccount),
PayeeName: strings.TrimSpace(req.PayeeName),
BankName: strings.TrimSpace(req.BankName),
BankCardNo: strings.TrimSpace(req.BankCardNo),
BankReservedMobile: strings.TrimSpace(req.BankReservedMobile),
Amount: req.Amount,
ActualAmount: taxInfo.ActualAmount,
TaxAmount: taxInfo.TaxAmount,
Status: 1, // 待审核
}
_, err := l.svcCtx.AgentWithdrawalModel.Insert(transCtx, session, withdrawal)