This commit is contained in:
2026-04-22 17:49:20 +08:00
parent a2fad4a095
commit f545aee45e
15 changed files with 268 additions and 140 deletions

View File

@@ -1,14 +1,14 @@
package agent
import (
"context"
"database/sql"
"fmt"
"time"
"bdrp-server/app/main/model"
"bdrp-server/common/ctxdata"
"bdrp-server/common/xerr"
"bdrp-server/pkg/lzkit/lzUtils"
"context"
"database/sql"
"fmt"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/pkg/errors"
@@ -56,6 +56,8 @@ func (l *AgentWithdrawalLogic) AgentWithdrawal(req *types.WithdrawalReq) (*types
agentID int64
)
var finalWithdrawAmount float64 // 实际到账金额
withdrawAmount := roundMoney(req.Amount)
// 使用事务处理核心操作
err := l.svcCtx.AgentModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error {
userID, err := ctxdata.GetUidFromCtx(l.ctx)
@@ -89,7 +91,7 @@ func (l *AgentWithdrawalLogic) AgentWithdrawal(req *types.WithdrawalReq) (*types
}
// 校验可提现金额
if req.Amount > agentWallet.Balance {
if withdrawAmount > roundMoney(agentWallet.Balance) {
return errors.Wrapf(xerr.NewErrMsg("您可提现的余额不足"), "获取用户ID失败")
}
@@ -97,7 +99,7 @@ func (l *AgentWithdrawalLogic) AgentWithdrawal(req *types.WithdrawalReq) (*types
outBizNo = "W_" + l.svcCtx.AlipayService.GenerateOutTradeNo()
// 冻结资金(事务内操作)
if err = l.freezeFunds(session, agentWallet, req.Amount); err != nil {
if err = l.freezeFunds(session, agentWallet, withdrawAmount); err != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "资金冻结失败: %v", err)
}
yearMonth := int64(time.Now().Year()*100 + int(time.Now().Month()))
@@ -111,14 +113,14 @@ func (l *AgentWithdrawalLogic) AgentWithdrawal(req *types.WithdrawalReq) (*types
)
// 统一扣税逻辑所有提现都按6%收取税收
exemptionAmount = 0 // 免税金额 = 0
TaxStatus = model.TaxStatusPending // 扣税状态 = 待扣税
taxDeductionPart = req.Amount // 应税金额 = 提现金额
taxAmount = taxDeductionPart * taxRate // 应缴税费 = 应税金额 * 税率
finalWithdrawAmount = req.Amount - taxAmount // 实际到账金额 = 提现金额 - 应缴税费
exemptionAmount = 0 // 免税金额 = 0
TaxStatus = model.TaxStatusPending // 扣税状态 = 待扣税
taxDeductionPart = withdrawAmount // 应税金额 = 提现金额
taxAmount = roundMoney(taxDeductionPart * taxRate) // 应缴税费 = 应税金额 * 税率
finalWithdrawAmount = roundMoney(withdrawAmount - taxAmount) // 实际到账金额 = 提现金额 - 应缴税费
// 创建提现记录(初始状态为处理中)
withdrawalID, err := l.createWithdrawalRecord(session, agentModel.Id, req.PayeeAccount, req.PayeeName, req.Amount, finalWithdrawAmount, taxAmount, outBizNo)
withdrawalID, err := l.createWithdrawalRecord(session, agentModel.Id, req.PayeeAccount, req.PayeeName, withdrawAmount, finalWithdrawAmount, taxAmount, outBizNo)
if err != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建提现记录失败: %v", err)
}
@@ -127,7 +129,7 @@ func (l *AgentWithdrawalLogic) AgentWithdrawal(req *types.WithdrawalReq) (*types
AgentId: agentModel.Id,
YearMonth: yearMonth,
WithdrawalId: withdrawalID,
WithdrawalAmount: req.Amount,
WithdrawalAmount: withdrawAmount,
ExemptionAmount: exemptionAmount,
TaxableAmount: taxDeductionPart,
TaxRate: taxRate,
@@ -153,7 +155,7 @@ func (l *AgentWithdrawalLogic) AgentWithdrawal(req *types.WithdrawalReq) (*types
withdrawRes.Status = WithdrawStatusProcessing
withdrawRes.FailMsg = ""
l.Logger.Infof("支付宝提现申请成功 outBizNo:%s agentId:%d amount:%f", outBizNo, agentID, req.Amount)
l.Logger.Infof("支付宝提现申请成功 outBizNo:%s agentId:%d amount:%f", outBizNo, agentID, withdrawAmount)
return withdrawRes, nil
}
@@ -193,6 +195,7 @@ func (l *AgentWithdrawalLogic) mapAlipayError(code string) string {
if msg, ok := errorMapping[code]; ok {
return msg
}
l.Logger.Infof("未匹配到支付宝错误码 code:%s", code)
return "系统错误,请联系客服"
}
@@ -204,9 +207,9 @@ func (l *AgentWithdrawalLogic) createWithdrawalRecord(session sqlx.Session, agen
WithdrawNo: outBizNo,
PayeeAccount: payeeAccount,
PayeeName: sql.NullString{String: payeeName, Valid: true}, // 设置收款人姓名
Amount: amount,
ActualAmount: finalWithdrawAmount,
TaxAmount: taxAmount,
Amount: roundMoney(amount),
ActualAmount: roundMoney(finalWithdrawAmount),
TaxAmount: roundMoney(taxAmount),
Status: StatusProcessing,
}
@@ -219,8 +222,8 @@ func (l *AgentWithdrawalLogic) createWithdrawalRecord(session sqlx.Session, agen
// 冻结资金(事务内操作)
func (l *AgentWithdrawalLogic) freezeFunds(session sqlx.Session, wallet *model.AgentWallet, amount float64) error {
wallet.Balance -= amount
wallet.FrozenBalance += amount
wallet.Balance = roundMoney(wallet.Balance - amount)
wallet.FrozenBalance = roundMoney(wallet.FrozenBalance + amount)
err := l.svcCtx.AgentWalletModel.UpdateWithVersion(l.ctx, session, wallet)
if err != nil {
return err
@@ -296,8 +299,8 @@ func (l *AgentWithdrawalLogic) updateWithdrawalStatus(outBizNo string, status in
return err
}
wallet.Balance += record.Amount
wallet.FrozenBalance -= record.Amount
wallet.Balance = roundMoney(wallet.Balance + record.Amount)
wallet.FrozenBalance = roundMoney(wallet.FrozenBalance - record.Amount)
if err := l.svcCtx.AgentWalletModel.UpdateWithVersion(ctx, session, wallet); err != nil {
return err
}
@@ -320,7 +323,7 @@ func (l *AgentWithdrawalLogic) updateWithdrawalStatus(outBizNo string, status in
return err
}
wallet.FrozenBalance -= record.Amount
wallet.FrozenBalance = roundMoney(wallet.FrozenBalance - record.Amount)
if err := l.svcCtx.AgentWalletModel.UpdateWithVersion(ctx, session, wallet); err != nil {
return err
}

View File

@@ -1,13 +1,13 @@
package agent
import (
"bdrp-server/app/main/model"
"bdrp-server/common/ctxdata"
"bdrp-server/common/xerr"
"context"
"database/sql"
"regexp"
"time"
"bdrp-server/app/main/model"
"bdrp-server/common/ctxdata"
"bdrp-server/common/xerr"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/stores/sqlx"
@@ -57,6 +57,8 @@ func (l *BankCardWithdrawalLogic) BankCardWithdrawal(req *types.BankCardWithdraw
return nil, errors.Wrapf(xerr.NewErrMsg("开户支行不能为空"), "开户支行验证失败")
}
withdrawAmount := roundMoney(req.Amount)
// 使用事务处理核心操作
err = l.svcCtx.AgentModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error {
userID, err := ctxdata.GetUidFromCtx(l.ctx)
@@ -90,12 +92,12 @@ func (l *BankCardWithdrawalLogic) BankCardWithdrawal(req *types.BankCardWithdraw
}
// 校验可提现金额
if req.Amount > agentWallet.Balance {
if withdrawAmount > roundMoney(agentWallet.Balance) {
return errors.Wrapf(xerr.NewErrMsg("您可提现的余额不足"), "余额不足")
}
// 最低提现金额验证
if req.Amount < 50 {
if withdrawAmount < 50 {
return errors.Wrapf(xerr.NewErrMsg("提现金额不能低于50元"), "金额验证失败")
}
@@ -103,7 +105,7 @@ func (l *BankCardWithdrawalLogic) BankCardWithdrawal(req *types.BankCardWithdraw
outBizNo = "BC_" + l.svcCtx.AlipayService.GenerateOutTradeNo()
// 冻结资金(事务内操作)
if err = l.freezeFunds(session, agentWallet, req.Amount); err != nil {
if err = l.freezeFunds(session, agentWallet, withdrawAmount); err != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "资金冻结失败: %v", err)
}
@@ -118,14 +120,14 @@ func (l *BankCardWithdrawalLogic) BankCardWithdrawal(req *types.BankCardWithdraw
)
// 统一扣税逻辑所有提现都按6%收取税收
exemptionAmount = 0 // 免税金额 = 0
TaxStatus = model.TaxStatusPending // 扣税状态 = 待扣税
taxDeductionPart = req.Amount // 应税金额 = 提现金额
taxAmount = taxDeductionPart * taxRate // 应缴税费 = 应税金额 * 税率
finalWithdrawAmount = req.Amount - taxAmount // 实际到账金额 = 提现金额 - 应缴税费
exemptionAmount = 0 // 免税金额 = 0
TaxStatus = model.TaxStatusPending // 扣税状态 = 待扣税
taxDeductionPart = withdrawAmount // 应税金额 = 提现金额
taxAmount = roundMoney(taxDeductionPart * taxRate) // 应缴税费 = 应税金额 * 税率
finalWithdrawAmount = roundMoney(withdrawAmount - taxAmount) // 实际到账金额 = 提现金额 - 应缴税费
// 创建提现记录(初始状态为申请中,提现类型为银行卡)
withdrawalID, err := l.createBankCardWithdrawalRecord(session, agentModel.Id, req.BankCardNo, req.BankName, agentRealName.Name, req.Amount, finalWithdrawAmount, taxAmount, outBizNo)
withdrawalID, err := l.createBankCardWithdrawalRecord(session, agentModel.Id, req.BankCardNo, req.BankName, agentRealName.Name, withdrawAmount, finalWithdrawAmount, taxAmount, outBizNo)
if err != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建提现记录失败: %v", err)
}
@@ -135,7 +137,7 @@ func (l *BankCardWithdrawalLogic) BankCardWithdrawal(req *types.BankCardWithdraw
AgentId: agentModel.Id,
YearMonth: yearMonth,
WithdrawalId: withdrawalID,
WithdrawalAmount: req.Amount,
WithdrawalAmount: withdrawAmount,
ExemptionAmount: exemptionAmount,
TaxableAmount: taxDeductionPart,
TaxRate: taxRate,
@@ -160,7 +162,7 @@ func (l *BankCardWithdrawalLogic) BankCardWithdrawal(req *types.BankCardWithdraw
withdrawRes.Status = WithdrawStatusProcessing
withdrawRes.FailMsg = ""
l.Logger.Infof("银行卡提现申请成功 outBizNo:%s agentId:%d amount:%f", outBizNo, agentID, req.Amount)
l.Logger.Infof("银行卡提现申请成功 outBizNo:%s agentId:%d amount:%f", outBizNo, agentID, withdrawAmount)
return withdrawRes, nil
}
@@ -171,9 +173,9 @@ func (l *BankCardWithdrawalLogic) createBankCardWithdrawalRecord(session sqlx.Se
WithdrawType: WithdrawTypeBankCard, // 银行卡提现
WithdrawNo: outBizNo,
PayeeAccount: bankCardNo, // 银行卡号存储在PayeeAccount字段
Amount: amount,
ActualAmount: finalWithdrawAmount,
TaxAmount: taxAmount,
Amount: roundMoney(amount),
ActualAmount: roundMoney(finalWithdrawAmount),
TaxAmount: roundMoney(taxAmount),
Status: StatusProcessing, // 申请中状态
BankCardNo: sql.NullString{String: bankCardNo, Valid: true},
BankName: sql.NullString{String: bankName, Valid: true},
@@ -194,8 +196,8 @@ func (l *BankCardWithdrawalLogic) freezeFunds(session sqlx.Session, wallet *mode
frozenBalanceBefore := wallet.FrozenBalance
// 更新钱包余额
wallet.Balance -= amount
wallet.FrozenBalance += amount
wallet.Balance = roundMoney(wallet.Balance - amount)
wallet.FrozenBalance = roundMoney(wallet.FrozenBalance + amount)
err := l.svcCtx.AgentWalletModel.UpdateWithVersion(l.ctx, session, wallet)
if err != nil {
return err
@@ -207,7 +209,7 @@ func (l *BankCardWithdrawalLogic) freezeFunds(session sqlx.Session, wallet *mode
session,
wallet.AgentId,
model.WalletTransactionTypeFreeze,
amount, // 变动金额
roundMoney(amount), // 变动金额
balanceBefore, // 变动前余额
wallet.Balance, // 变动后余额
frozenBalanceBefore, // 变动前冻结余额

View File

@@ -5,6 +5,7 @@ import (
"bdrp-server/app/main/model"
"bdrp-server/common/ctxdata"
"bdrp-server/common/xerr"
"math"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/mr"
@@ -32,6 +33,10 @@ func NewGetAgentProductConfigLogic(ctx context.Context, svcCtx *svc.ServiceConte
type AgentProductConfigResp struct {
}
func roundMoney(v float64) float64 {
return math.Round(v*100) / 100
}
func (l *GetAgentProductConfigLogic) GetAgentProductConfig() (resp *types.AgentProductConfigResp, err error) {
userID, err := ctxdata.GetUidFromCtx(l.ctx)
if err != nil {
@@ -81,10 +86,10 @@ func (l *GetAgentProductConfigLogic) GetAgentProductConfig() (resp *types.AgentP
return
}
agentProductConfig.ProductID = config.ProductId
agentProductConfig.CostPrice = agentProductConfigModel.CostPrice
agentProductConfig.PriceRangeMin = agentProductConfigModel.PriceRangeMin
agentProductConfig.PriceRangeMax = agentProductConfigModel.PriceRangeMax
agentProductConfig.PPricingStandard = agentProductConfigModel.PricingStandard
agentProductConfig.CostPrice = roundMoney(agentProductConfigModel.CostPrice)
agentProductConfig.PriceRangeMin = roundMoney(agentProductConfigModel.PriceRangeMin)
agentProductConfig.PriceRangeMax = roundMoney(agentProductConfigModel.PriceRangeMax)
agentProductConfig.PPricingStandard = roundMoney(agentProductConfigModel.PricingStandard)
agentProductConfig.POverpricingRatio = agentProductConfigModel.OverpricingRatio
// 看推广人是否有上级,上级是否有这个配置权限,上级是否有相关配置
@@ -119,10 +124,10 @@ func (l *GetAgentProductConfigLogic) GetAgentProductConfig() (resp *types.AgentP
cancel(membershipConfigErr)
return
}
agentProductConfig.CostPrice += membershipUserConfigModel.PriceIncreaseAmount
agentProductConfig.PriceRangeMin += membershipUserConfigModel.PriceIncreaseAmount
agentProductConfig.APricingStandard = membershipUserConfigModel.PriceRangeFrom
agentProductConfig.APricingEnd = membershipUserConfigModel.PriceRangeTo
agentProductConfig.CostPrice = roundMoney(agentProductConfig.CostPrice + membershipUserConfigModel.PriceIncreaseAmount)
agentProductConfig.PriceRangeMin = roundMoney(agentProductConfig.PriceRangeMin + membershipUserConfigModel.PriceIncreaseAmount)
agentProductConfig.APricingStandard = roundMoney(membershipUserConfigModel.PriceRangeFrom)
agentProductConfig.APricingEnd = roundMoney(membershipUserConfigModel.PriceRangeTo)
agentProductConfig.AOverpricingRatio = membershipUserConfigModel.PriceRatio
}
}