t
This commit is contained in:
@@ -61,6 +61,10 @@ service main {
|
||||
// 代理会员配置编辑
|
||||
@handler AdminUpdateAgentMembershipConfig
|
||||
post /agent-membership-config/update (AdminUpdateAgentMembershipConfigReq) returns (AdminUpdateAgentMembershipConfigResp)
|
||||
|
||||
// 银行卡提现审核(确认/拒绝)
|
||||
@handler AdminReviewBankCardWithdrawal
|
||||
post /agent-withdrawal/bank-card/review (AdminReviewBankCardWithdrawalReq) returns (AdminReviewBankCardWithdrawalResp)
|
||||
}
|
||||
|
||||
type (
|
||||
@@ -193,6 +197,10 @@ type (
|
||||
PayeeAccount string `json:"payee_account"` // 收款账户
|
||||
Remark string `json:"remark"` // 备注
|
||||
CreateTime string `json:"create_time"` // 创建时间
|
||||
WithdrawType int64 `json:"withdraw_type"` // 提现类型:1-支付宝,2-银行卡
|
||||
BankCardNo string `json:"bank_card_no"` // 银行卡号
|
||||
BankName string `json:"bank_name"` // 开户支行
|
||||
PayeeName string `json:"payee_name"` // 收款人姓名
|
||||
}
|
||||
|
||||
// 代理提现分页查询响应
|
||||
@@ -383,4 +391,16 @@ type (
|
||||
AdminUpdateAgentMembershipConfigResp {
|
||||
Success bool `json:"success"` // 是否成功
|
||||
}
|
||||
|
||||
// 银行卡提现审核请求
|
||||
AdminReviewBankCardWithdrawalReq {
|
||||
WithdrawalId int64 `json:"withdrawal_id"` // 提现记录ID
|
||||
Action int64 `json:"action"` // 操作:1-确认,2-拒绝
|
||||
Remark string `json:"remark"` // 备注(拒绝时必填)
|
||||
}
|
||||
|
||||
// 银行卡提现审核响应
|
||||
AdminReviewBankCardWithdrawalResp {
|
||||
Success bool `json:"success"` // 是否成功
|
||||
}
|
||||
)
|
||||
@@ -278,6 +278,14 @@ service main {
|
||||
|
||||
@handler GetAgentWithdrawalTaxExemption
|
||||
get /withdrawal/tax/exemption (GetWithdrawalTaxExemptionReq) returns (GetWithdrawalTaxExemptionResp)
|
||||
|
||||
// 银行卡提现申请
|
||||
@handler BankCardWithdrawal
|
||||
post /withdrawal/bank-card (BankCardWithdrawalReq) returns (WithdrawalResp)
|
||||
|
||||
// 获取历史银行卡信息
|
||||
@handler GetBankCardInfo
|
||||
get /withdrawal/bank-card/info (GetBankCardInfoReq) returns (GetBankCardInfoResp)
|
||||
}
|
||||
|
||||
type (
|
||||
@@ -383,6 +391,21 @@ type (
|
||||
RemainingExemptionAmount float64 `json:"remaining_exemption_amount"`
|
||||
TaxRate float64 `json:"tax_rate"`
|
||||
}
|
||||
// 银行卡提现申请请求
|
||||
BankCardWithdrawalReq {
|
||||
BankCardNo string `json:"bank_card_no"` // 银行卡号
|
||||
BankName string `json:"bank_name"` // 开户支行
|
||||
Amount float64 `json:"amount"` // 提现金额
|
||||
}
|
||||
// 获取历史银行卡信息请求
|
||||
GetBankCardInfoReq {}
|
||||
// 获取历史银行卡信息响应
|
||||
GetBankCardInfoResp {
|
||||
BankCardNo string `json:"bank_card_no"` // 银行卡号
|
||||
BankName string `json:"bank_name"` // 开户支行
|
||||
PayeeName string `json:"payee_name"` // 收款人姓名
|
||||
IdCard string `json:"id_card"` // 身份证号
|
||||
}
|
||||
)
|
||||
|
||||
@server (
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package admin_agent
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"tydata-server/app/main/api/internal/logic/admin_agent"
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/common/result"
|
||||
"tydata-server/pkg/lzkit/validator"
|
||||
)
|
||||
|
||||
func AdminReviewBankCardWithdrawalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.AdminReviewBankCardWithdrawalReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
result.ParamErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
if err := validator.Validate(req); err != nil {
|
||||
result.ParamValidateErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
l := admin_agent.NewAdminReviewBankCardWithdrawalLogic(r.Context(), svcCtx)
|
||||
resp, err := l.AdminReviewBankCardWithdrawal(&req)
|
||||
result.HttpResult(r, w, resp, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"tydata-server/app/main/api/internal/logic/agent"
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/common/result"
|
||||
"tydata-server/pkg/lzkit/validator"
|
||||
)
|
||||
|
||||
func BankCardWithdrawalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.BankCardWithdrawalReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
result.ParamErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
if err := validator.Validate(req); err != nil {
|
||||
result.ParamValidateErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
l := agent.NewBankCardWithdrawalLogic(r.Context(), svcCtx)
|
||||
resp, err := l.BankCardWithdrawal(&req)
|
||||
result.HttpResult(r, w, resp, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"tydata-server/app/main/api/internal/logic/agent"
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/common/result"
|
||||
"tydata-server/pkg/lzkit/validator"
|
||||
)
|
||||
|
||||
func GetBankCardInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.GetBankCardInfoReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
result.ParamErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
if err := validator.Validate(req); err != nil {
|
||||
result.ParamValidateErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
l := agent.NewGetBankCardInfoLogic(r.Context(), svcCtx)
|
||||
resp, err := l.GetBankCardInfo(&req)
|
||||
result.HttpResult(r, w, resp, err)
|
||||
}
|
||||
}
|
||||
@@ -87,6 +87,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
Path: "/agent-reward/list",
|
||||
Handler: admin_agent.AdminGetAgentRewardListHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/agent-withdrawal/bank-card/review",
|
||||
Handler: admin_agent.AdminReviewBankCardWithdrawalHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/agent-withdrawal/list",
|
||||
@@ -765,6 +770,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
Path: "/withdrawal",
|
||||
Handler: agent.AgentWithdrawalHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/withdrawal/bank-card",
|
||||
Handler: agent.BankCardWithdrawalHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/withdrawal/bank-card/info",
|
||||
Handler: agent.GetBankCardInfoHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/withdrawal/tax/exemption",
|
||||
|
||||
@@ -49,6 +49,12 @@ func (l *AdminGetAgentWithdrawalListLogic) AdminGetAgentWithdrawalList(req *type
|
||||
item.Remark = v.Remark.String
|
||||
}
|
||||
item.CreateTime = v.CreateTime.Format("2006-01-02 15:04:05")
|
||||
|
||||
// 设置银行卡信息(如果模型有这些字段,copier会自动复制;如果没有,这里手动设置默认值)
|
||||
// 注意:如果数据库还没有迁移,这些字段可能不存在,需要先注释
|
||||
// 如果模型有这些字段,copier会自动复制,这里不需要手动设置
|
||||
// 如果模型没有这些字段,需要等数据库迁移后重新生成模型
|
||||
|
||||
items = append(items, item)
|
||||
}
|
||||
resp = &types.AdminGetAgentWithdrawalListResp{
|
||||
|
||||
@@ -0,0 +1,188 @@
|
||||
package admin_agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
"tydata-server/app/main/model"
|
||||
"tydata-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
// 审核操作常量
|
||||
const (
|
||||
ReviewActionApprove = 1 // 确认
|
||||
ReviewActionReject = 2 // 拒绝
|
||||
)
|
||||
|
||||
// 状态常量
|
||||
const (
|
||||
StatusPending = 1 // 申请中/处理中
|
||||
StatusSuccess = 2 // 成功
|
||||
StatusFailed = 3 // 失败
|
||||
)
|
||||
|
||||
// 提现类型常量
|
||||
const (
|
||||
WithdrawTypeAlipay = 1 // 支付宝提现
|
||||
WithdrawTypeBankCard = 2 // 银行卡提现
|
||||
)
|
||||
|
||||
type AdminReviewBankCardWithdrawalLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminReviewBankCardWithdrawalLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminReviewBankCardWithdrawalLogic {
|
||||
return &AdminReviewBankCardWithdrawalLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminReviewBankCardWithdrawalLogic) AdminReviewBankCardWithdrawal(req *types.AdminReviewBankCardWithdrawalReq) (resp *types.AdminReviewBankCardWithdrawalResp, err error) {
|
||||
// 验证操作类型
|
||||
if req.Action != ReviewActionApprove && req.Action != ReviewActionReject {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("操作类型不正确"), "操作类型验证失败")
|
||||
}
|
||||
|
||||
// 拒绝操作必须填写备注
|
||||
if req.Action == ReviewActionReject && req.Remark == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("拒绝提现必须填写拒绝原因"), "拒绝原因验证失败")
|
||||
}
|
||||
|
||||
resp = &types.AdminReviewBankCardWithdrawalResp{
|
||||
Success: false,
|
||||
}
|
||||
|
||||
// 使用事务处理审核操作
|
||||
err = l.svcCtx.AgentModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error {
|
||||
// 获取提现记录
|
||||
record, err := l.svcCtx.AgentWithdrawalModel.FindOne(ctx, req.WithdrawalId)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return errors.Wrapf(xerr.NewErrMsg("提现记录不存在"), "提现记录不存在")
|
||||
}
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询提现记录失败: %v", err)
|
||||
}
|
||||
|
||||
// 验证提现记录状态必须是申请中
|
||||
if record.Status != StatusPending {
|
||||
return errors.Wrapf(xerr.NewErrMsg("该提现记录已处理,无法重复操作"), "状态验证失败")
|
||||
}
|
||||
|
||||
// 验证提现类型(如果模型有WithdrawType字段)
|
||||
// 注意:如果数据库还没有迁移,可以先注释这个验证
|
||||
// if record.WithdrawType != WithdrawTypeBankCard {
|
||||
// return errors.Wrapf(xerr.NewErrMsg("该记录不是银行卡提现,无法审核"), "提现类型验证失败")
|
||||
// }
|
||||
|
||||
if req.Action == ReviewActionApprove {
|
||||
// 确认提现
|
||||
return l.approveWithdrawal(ctx, session, record)
|
||||
} else {
|
||||
// 拒绝提现
|
||||
return l.rejectWithdrawal(ctx, session, record, req.Remark)
|
||||
}
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp.Success = true
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// 确认提现
|
||||
func (l *AdminReviewBankCardWithdrawalLogic) approveWithdrawal(ctx context.Context, session sqlx.Session, record *model.AgentWithdrawal) error {
|
||||
// 更新提现记录状态为成功
|
||||
record.Status = StatusSuccess
|
||||
record.Remark = sql.NullString{String: "管理员确认提现", Valid: true}
|
||||
if _, err := l.svcCtx.AgentWithdrawalModel.Update(ctx, session, record); err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新提现记录失败: %v", err)
|
||||
}
|
||||
|
||||
// 解冻资金并扣除(FrozenBalance -= amount, Balance不变)
|
||||
wallet, err := l.svcCtx.AgentWalletModel.FindOneByAgentId(ctx, record.AgentId)
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询钱包失败: %v", err)
|
||||
}
|
||||
|
||||
wallet.FrozenBalance -= record.Amount
|
||||
if err := l.svcCtx.AgentWalletModel.UpdateWithVersion(ctx, session, wallet); err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新钱包失败: %v", err)
|
||||
}
|
||||
|
||||
// 更新扣税记录状态为成功
|
||||
taxModel, err := l.svcCtx.AgentWithdrawalTaxModel.FindOneByWithdrawalId(ctx, record.Id)
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询扣税记录失败: %v", err)
|
||||
}
|
||||
if taxModel.TaxStatus == model.TaxStatusPending {
|
||||
taxModel.TaxStatus = model.TaxStatusSuccess // 扣税状态 = 成功
|
||||
taxModel.TaxTime = sql.NullTime{Time: time.Now(), Valid: true}
|
||||
if err := l.svcCtx.AgentWithdrawalTaxModel.UpdateWithVersion(ctx, session, taxModel); err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新扣税记录失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 提现成功后,给上级代理发放提现奖励
|
||||
withdrawRewardErr := l.svcCtx.AgentService.GiveWithdrawReward(ctx, record.AgentId, record.Amount, session)
|
||||
if withdrawRewardErr != nil {
|
||||
l.Logger.Errorf("发放提现奖励失败,代理ID:%d,提现金额:%f,错误:%+v", record.AgentId, record.Amount, withdrawRewardErr)
|
||||
// 提现奖励失败不影响主流程,只记录日志
|
||||
} else {
|
||||
l.Logger.Infof("发放提现奖励成功,代理ID:%d,提现金额:%f", record.AgentId, record.Amount)
|
||||
}
|
||||
|
||||
l.Logger.Infof("银行卡提现确认成功 withdrawalId:%d amount:%f", record.Id, record.Amount)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 拒绝提现
|
||||
func (l *AdminReviewBankCardWithdrawalLogic) rejectWithdrawal(ctx context.Context, session sqlx.Session, record *model.AgentWithdrawal, remark string) error {
|
||||
// 更新提现记录状态为失败
|
||||
record.Status = StatusFailed
|
||||
record.Remark = sql.NullString{String: "管理员拒绝:" + remark, Valid: true}
|
||||
if _, err := l.svcCtx.AgentWithdrawalModel.Update(ctx, session, record); err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新提现记录失败: %v", err)
|
||||
}
|
||||
|
||||
// 解冻资金(FrozenBalance -= amount, Balance += amount)
|
||||
wallet, err := l.svcCtx.AgentWalletModel.FindOneByAgentId(ctx, record.AgentId)
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询钱包失败: %v", err)
|
||||
}
|
||||
|
||||
wallet.Balance += record.Amount
|
||||
wallet.FrozenBalance -= record.Amount
|
||||
if err := l.svcCtx.AgentWalletModel.UpdateWithVersion(ctx, session, wallet); err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新钱包失败: %v", err)
|
||||
}
|
||||
|
||||
// 更新扣税记录状态为失败
|
||||
taxModel, err := l.svcCtx.AgentWithdrawalTaxModel.FindOneByWithdrawalId(ctx, record.Id)
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询扣税记录失败: %v", err)
|
||||
}
|
||||
if taxModel.TaxStatus == model.TaxStatusPending {
|
||||
taxModel.TaxStatus = model.TaxStatusFailed // 扣税状态 = 失败
|
||||
taxModel.TaxTime = sql.NullTime{Time: time.Now(), Valid: true}
|
||||
if err := l.svcCtx.AgentWithdrawalTaxModel.UpdateWithVersion(ctx, session, taxModel); err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新扣税记录失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
l.Logger.Infof("银行卡提现拒绝 withdrawalId:%d amount:%f reason:%s", record.Id, record.Amount, remark)
|
||||
return nil
|
||||
}
|
||||
198
app/main/api/internal/logic/agent/bankcardwithdrawallogic.go
Normal file
198
app/main/api/internal/logic/agent/bankcardwithdrawallogic.go
Normal file
@@ -0,0 +1,198 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"regexp"
|
||||
"time"
|
||||
"tydata-server/app/main/model"
|
||||
"tydata-server/common/ctxdata"
|
||||
"tydata-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
// 提现类型常量
|
||||
const (
|
||||
WithdrawTypeAlipay = 1 // 支付宝提现
|
||||
WithdrawTypeBankCard = 2 // 银行卡提现
|
||||
)
|
||||
|
||||
type BankCardWithdrawalLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewBankCardWithdrawalLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BankCardWithdrawalLogic {
|
||||
return &BankCardWithdrawalLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *BankCardWithdrawalLogic) BankCardWithdrawal(req *types.BankCardWithdrawalReq) (resp *types.WithdrawalResp, err error) {
|
||||
var (
|
||||
outBizNo string
|
||||
withdrawRes = &types.WithdrawalResp{}
|
||||
)
|
||||
var finalWithdrawAmount float64 // 实际到账金额
|
||||
|
||||
// 验证银行卡号格式(16-19位数字)
|
||||
bankCardNoRegex := regexp.MustCompile(`^\d{16,19}$`)
|
||||
if !bankCardNoRegex.MatchString(req.BankCardNo) {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("银行卡号格式不正确,请输入16-19位数字"), "银行卡号格式验证失败")
|
||||
}
|
||||
|
||||
// 验证开户支行不能为空
|
||||
if req.BankName == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("开户支行不能为空"), "开户支行验证失败")
|
||||
}
|
||||
|
||||
// 使用事务处理核心操作
|
||||
err = l.svcCtx.AgentModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error {
|
||||
userID, err := ctxdata.GetUidFromCtx(l.ctx)
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取用户ID失败: %v", err)
|
||||
}
|
||||
|
||||
// 查询代理信息
|
||||
agentModel, err := l.svcCtx.AgentModel.FindOneByUserId(l.ctx, userID)
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询代理信息失败: %v", err)
|
||||
}
|
||||
|
||||
// 查询实名认证信息
|
||||
agentRealName, err := l.svcCtx.AgentRealNameModel.FindOneByAgentId(l.ctx, agentModel.Id)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return errors.Wrapf(xerr.NewErrMsg("您未进行实名认证, 无法提现"), "您未进行实名认证")
|
||||
}
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "查询代理实名信息失败: %v", err)
|
||||
}
|
||||
if agentRealName.Status != model.AgentRealNameStatusApproved {
|
||||
return errors.Wrapf(xerr.NewErrMsg("您的实名认证未通过, 无法提现"), "您的实名认证未通过")
|
||||
}
|
||||
|
||||
// 查询钱包
|
||||
agentWallet, err := l.svcCtx.AgentWalletModel.FindOneByAgentId(l.ctx, agentModel.Id)
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询代理钱包失败: %v", err)
|
||||
}
|
||||
|
||||
// 校验可提现金额
|
||||
if req.Amount > agentWallet.Balance {
|
||||
return errors.Wrapf(xerr.NewErrMsg("您可提现的余额不足"), "余额不足")
|
||||
}
|
||||
|
||||
// 最低提现金额验证
|
||||
if req.Amount < 50 {
|
||||
return errors.Wrapf(xerr.NewErrMsg("提现金额不能低于50元"), "金额验证失败")
|
||||
}
|
||||
|
||||
// 生成交易号
|
||||
outBizNo = "BC_" + l.svcCtx.AlipayService.GenerateOutTradeNo()
|
||||
|
||||
// 冻结资金(事务内操作)
|
||||
if err = l.freezeFunds(session, agentWallet, req.Amount); err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "资金冻结失败: %v", err)
|
||||
}
|
||||
|
||||
yearMonth := int64(time.Now().Year()*100 + int(time.Now().Month()))
|
||||
// 统一按6%收取税收
|
||||
taxRate := l.svcCtx.Config.TaxConfig.TaxRate
|
||||
var (
|
||||
taxAmount float64 // 应缴税费
|
||||
taxDeductionPart float64 // 应税金额
|
||||
TaxStatus int64 // 扣税状态
|
||||
exemptionAmount float64 // 免税金额(固定为0)
|
||||
)
|
||||
|
||||
// 统一扣税逻辑:所有提现都按6%收取税收
|
||||
exemptionAmount = 0 // 免税金额 = 0
|
||||
TaxStatus = model.TaxStatusPending // 扣税状态 = 待扣税
|
||||
taxDeductionPart = req.Amount // 应税金额 = 提现金额
|
||||
taxAmount = taxDeductionPart * taxRate // 应缴税费 = 应税金额 * 税率
|
||||
finalWithdrawAmount = req.Amount - taxAmount // 实际到账金额 = 提现金额 - 应缴税费
|
||||
|
||||
// 创建提现记录(初始状态为申请中,提现类型为银行卡)
|
||||
withdrawalID, err := l.createBankCardWithdrawalRecord(session, agentModel.Id, req.BankCardNo, req.BankName, agentRealName.Name, req.Amount, finalWithdrawAmount, taxAmount, outBizNo)
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建提现记录失败: %v", err)
|
||||
}
|
||||
|
||||
// 扣税记录
|
||||
taxModel := &model.AgentWithdrawalTax{
|
||||
AgentId: agentModel.Id,
|
||||
YearMonth: yearMonth,
|
||||
WithdrawalId: withdrawalID,
|
||||
WithdrawalAmount: req.Amount,
|
||||
ExemptionAmount: exemptionAmount,
|
||||
TaxableAmount: taxDeductionPart,
|
||||
TaxRate: taxRate,
|
||||
TaxAmount: taxAmount,
|
||||
ActualAmount: finalWithdrawAmount,
|
||||
TaxStatus: TaxStatus,
|
||||
Remark: sql.NullString{String: "银行卡提现申请,待审核扣税", Valid: true},
|
||||
ExemptionRecordId: 0, // 不再使用免税额度记录
|
||||
}
|
||||
_, err = l.svcCtx.AgentWithdrawalTaxModel.Insert(ctx, session, taxModel)
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建扣税记录失败: %v", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 银行卡提现不需要调用支付接口,直接返回申请中状态
|
||||
withdrawRes.Status = WithdrawStatusProcessing
|
||||
withdrawRes.FailMsg = ""
|
||||
|
||||
l.Logger.Infof("银行卡提现申请成功 outBizNo:%s agentId:%d amount:%f", outBizNo, 0, req.Amount)
|
||||
return withdrawRes, nil
|
||||
}
|
||||
|
||||
// 创建银行卡提现记录(事务内操作)
|
||||
func (l *BankCardWithdrawalLogic) createBankCardWithdrawalRecord(session sqlx.Session, agentID int64, bankCardNo string, bankName string, payeeName string, amount float64, finalWithdrawAmount float64, taxAmount float64, outBizNo string) (int64, error) {
|
||||
record := &model.AgentWithdrawal{
|
||||
AgentId: agentID,
|
||||
WithdrawNo: outBizNo,
|
||||
PayeeAccount: bankCardNo, // 银行卡号存储在PayeeAccount字段
|
||||
Amount: amount,
|
||||
ActualAmount: finalWithdrawAmount,
|
||||
TaxAmount: taxAmount,
|
||||
Status: StatusProcessing, // 申请中状态
|
||||
// 注意:以下字段需要在数据库迁移后添加,如果模型还没有这些字段,需要先更新模型
|
||||
// WithdrawType: WithdrawTypeBankCard,
|
||||
// BankCardNo: sql.NullString{String: bankCardNo, Valid: true},
|
||||
// BankName: sql.NullString{String: bankName, Valid: true},
|
||||
// PayeeName: sql.NullString{String: payeeName, Valid: true},
|
||||
}
|
||||
|
||||
result, err := l.svcCtx.AgentWithdrawalModel.Insert(l.ctx, session, record)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.LastInsertId()
|
||||
}
|
||||
|
||||
// 冻结资金(事务内操作)
|
||||
func (l *BankCardWithdrawalLogic) freezeFunds(session sqlx.Session, wallet *model.AgentWallet, amount float64) error {
|
||||
wallet.Balance -= amount
|
||||
wallet.FrozenBalance += amount
|
||||
err := l.svcCtx.AgentWalletModel.UpdateWithVersion(l.ctx, session, wallet)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
87
app/main/api/internal/logic/agent/getbankcardinfologic.go
Normal file
87
app/main/api/internal/logic/agent/getbankcardinfologic.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"tydata-server/app/main/model"
|
||||
"tydata-server/common/ctxdata"
|
||||
"tydata-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetBankCardInfoLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetBankCardInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetBankCardInfoLogic {
|
||||
return &GetBankCardInfoLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetBankCardInfoLogic) GetBankCardInfo(req *types.GetBankCardInfoReq) (resp *types.GetBankCardInfoResp, err error) {
|
||||
userID, err := ctxdata.GetUidFromCtx(l.ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取用户ID失败: %v", err)
|
||||
}
|
||||
|
||||
// 查询代理信息
|
||||
agentModel, err := l.svcCtx.AgentModel.FindOneByUserId(l.ctx, userID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询代理信息失败: %v", err)
|
||||
}
|
||||
|
||||
// 查询实名认证信息
|
||||
agentRealName, err := l.svcCtx.AgentRealNameModel.FindOneByAgentId(l.ctx, agentModel.Id)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("您未进行实名认证"), "您未进行实名认证")
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "查询代理实名信息失败: %v", err)
|
||||
}
|
||||
|
||||
// 查询最近一次银行卡提现记录,用于自动填充
|
||||
// 注意:这里需要查询 withdraw_type = 2 的记录
|
||||
// 如果数据库还没有迁移,可以先返回空值
|
||||
resp = &types.GetBankCardInfoResp{
|
||||
PayeeName: agentRealName.Name,
|
||||
IdCard: agentRealName.IdCard,
|
||||
BankCardNo: "",
|
||||
BankName: "",
|
||||
}
|
||||
|
||||
// 查询最近一次成功的银行卡提现记录
|
||||
// 这里使用SelectBuilder查询,但由于模型可能还没有withdraw_type字段,先注释
|
||||
// builder := l.svcCtx.AgentWithdrawalModel.SelectBuilder()
|
||||
// builder = builder.Where(squirrel.Eq{"agent_id": agentModel.Id})
|
||||
// builder = builder.Where(squirrel.Eq{"withdraw_type": WithdrawTypeBankCard})
|
||||
// builder = builder.Where(squirrel.Eq{"status": StatusSuccess})
|
||||
// builder = builder.OrderBy("create_time DESC")
|
||||
// builder = builder.Limit(1)
|
||||
//
|
||||
// list, err := l.svcCtx.AgentWithdrawalModel.FindAll(l.ctx, builder, "create_time DESC")
|
||||
// if err == nil && len(list) > 0 {
|
||||
// lastRecord := list[0]
|
||||
// if lastRecord.BankCardNo.Valid {
|
||||
// resp.BankCardNo = lastRecord.BankCardNo.String
|
||||
// }
|
||||
// if lastRecord.BankName.Valid {
|
||||
// resp.BankName = lastRecord.BankName.String
|
||||
// }
|
||||
// }
|
||||
|
||||
// 临时方案:从PayeeAccount字段读取(如果之前有银行卡提现记录)
|
||||
// 注意:这个方案不够准确,因为PayeeAccount也可能存储支付宝账号
|
||||
// 建议数据库迁移后使用上面的方案
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
@@ -692,6 +692,16 @@ type AdminRetryAgentProcessResp struct {
|
||||
ProcessedAt string `json:"processed_at"` // 处理时间
|
||||
}
|
||||
|
||||
type AdminReviewBankCardWithdrawalReq struct {
|
||||
WithdrawalId int64 `json:"withdrawal_id"` // 提现记录ID
|
||||
Action int64 `json:"action"` // 操作:1-确认,2-拒绝
|
||||
Remark string `json:"remark"` // 备注(拒绝时必填)
|
||||
}
|
||||
|
||||
type AdminReviewBankCardWithdrawalResp struct {
|
||||
Success bool `json:"success"` // 是否成功
|
||||
}
|
||||
|
||||
type AdminRoleApiInfo struct {
|
||||
Id int64 `json:"id"`
|
||||
RoleId int64 `json:"role_id"`
|
||||
@@ -1129,6 +1139,10 @@ type AgentWithdrawalListItem struct {
|
||||
PayeeAccount string `json:"payee_account"` // 收款账户
|
||||
Remark string `json:"remark"` // 备注
|
||||
CreateTime string `json:"create_time"` // 创建时间
|
||||
WithdrawType int64 `json:"withdraw_type"` // 提现类型:1-支付宝,2-银行卡
|
||||
BankCardNo string `json:"bank_card_no"` // 银行卡号
|
||||
BankName string `json:"bank_name"` // 开户支行
|
||||
PayeeName string `json:"payee_name"` // 收款人姓名
|
||||
}
|
||||
|
||||
type AuthorizationDocumentInfo struct {
|
||||
@@ -1144,6 +1158,12 @@ type AuthorizationDocumentInfo struct {
|
||||
CreateTime string `json:"createTime"` // 创建时间
|
||||
}
|
||||
|
||||
type BankCardWithdrawalReq struct {
|
||||
BankCardNo string `json:"bank_card_no"` // 银行卡号
|
||||
BankName string `json:"bank_name"` // 开户支行
|
||||
Amount float64 `json:"amount"` // 提现金额
|
||||
}
|
||||
|
||||
type BindMobileReq struct {
|
||||
Mobile string `json:"mobile" validate:"required,mobile"`
|
||||
Code string `json:"code" validate:"required"`
|
||||
@@ -1156,10 +1176,9 @@ type BindMobileResp struct {
|
||||
}
|
||||
|
||||
type Commission struct {
|
||||
ProductName string `json:"product_name"`
|
||||
Amount float64 `json:"amount"`
|
||||
CreateTime string `json:"create_time"`
|
||||
QueryParams map[string]interface{} `json:"query_params,omitempty"`
|
||||
ProductName string `json:"product_name"`
|
||||
Amount float64 `json:"amount"`
|
||||
CreateTime string `json:"create_time"`
|
||||
}
|
||||
|
||||
type CreateMenuReq struct {
|
||||
@@ -1328,6 +1347,16 @@ type GetAuthorizationDocumentResp struct {
|
||||
CreateTime string `json:"createTime"` // 创建时间
|
||||
}
|
||||
|
||||
type GetBankCardInfoReq struct {
|
||||
}
|
||||
|
||||
type GetBankCardInfoResp struct {
|
||||
BankCardNo string `json:"bank_card_no"` // 银行卡号
|
||||
BankName string `json:"bank_name"` // 开户支行
|
||||
PayeeName string `json:"payee_name"` // 收款人姓名
|
||||
IdCard string `json:"id_card"` // 身份证号
|
||||
}
|
||||
|
||||
type GetCommissionReq struct {
|
||||
Page int64 `form:"page"` // 页码
|
||||
PageSize int64 `form:"page_size"` // 每页数据量
|
||||
|
||||
Reference in New Issue
Block a user