feat(distribution): 分销站点消息中心与钱包提现多租户独立分支隔离
- 新增 000070 数据库迁移:支持 chat_conversations、wallet_accounts、wallet_ledger、withdrawal_requests 按 distribution_site_id 隔离 - 消息中心:会话列表、未读数统计、站点专属客服及订单群聊与主站实现双向完全隔离 - 资金账户与提现:独立按 (user_id, distribution_site_id) 建账,分销订单自动结算至对应分站钱包,提现与流水相互独立 - 前台界面:Axios 自动透传 X-Distribution-Site-Code,钱包、提现与消息页面呈现分站专属标牌与独立说明
This commit is contained in:
@@ -106,21 +106,23 @@ func (r *Repository) Review(ctx context.Context, adminID, id uint64, req ReviewW
|
||||
// 如果拒绝,解冻余额
|
||||
if !approved {
|
||||
if err := wallet.AppendEntries(tx, wallet.Entry{
|
||||
UserID: withdrawal.UserID,
|
||||
Direction: "out",
|
||||
AmountCent: withdrawal.AmountCent,
|
||||
BalanceType: "frozen",
|
||||
BizType: "withdraw_reject",
|
||||
BizNo: withdrawal.WithdrawNo,
|
||||
Remark: fmt.Sprintf("提现被拒绝: %s", req.Remark),
|
||||
UserID: withdrawal.UserID,
|
||||
DistributionSiteID: withdrawal.DistributionSiteID,
|
||||
Direction: "out",
|
||||
AmountCent: withdrawal.AmountCent,
|
||||
BalanceType: "frozen",
|
||||
BizType: "withdraw_reject",
|
||||
BizNo: withdrawal.WithdrawNo,
|
||||
Remark: fmt.Sprintf("提现被拒绝: %s", req.Remark),
|
||||
}, wallet.Entry{
|
||||
UserID: withdrawal.UserID,
|
||||
Direction: "in",
|
||||
AmountCent: withdrawal.AmountCent,
|
||||
BalanceType: "available",
|
||||
BizType: "withdraw_reject",
|
||||
BizNo: withdrawal.WithdrawNo,
|
||||
Remark: fmt.Sprintf("提现被拒绝: %s", req.Remark),
|
||||
UserID: withdrawal.UserID,
|
||||
DistributionSiteID: withdrawal.DistributionSiteID,
|
||||
Direction: "in",
|
||||
AmountCent: withdrawal.AmountCent,
|
||||
BalanceType: "available",
|
||||
BizType: "withdraw_reject",
|
||||
BizNo: withdrawal.WithdrawNo,
|
||||
Remark: fmt.Sprintf("提现被拒绝: %s", req.Remark),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -166,13 +168,14 @@ func (r *Repository) ConfirmPayment(ctx context.Context, adminID, id uint64, req
|
||||
|
||||
// 扣除冻结余额
|
||||
if err := wallet.AppendEntries(tx, wallet.Entry{
|
||||
UserID: withdrawal.UserID,
|
||||
Direction: "out",
|
||||
AmountCent: withdrawal.AmountCent,
|
||||
BalanceType: "frozen",
|
||||
BizType: "withdraw_complete",
|
||||
BizNo: withdrawal.WithdrawNo,
|
||||
Remark: "提现完成",
|
||||
UserID: withdrawal.UserID,
|
||||
DistributionSiteID: withdrawal.DistributionSiteID,
|
||||
Direction: "out",
|
||||
AmountCent: withdrawal.AmountCent,
|
||||
BalanceType: "frozen",
|
||||
BizType: "withdraw_complete",
|
||||
BizNo: withdrawal.WithdrawNo,
|
||||
Remark: "提现完成",
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -6,31 +6,33 @@ import (
|
||||
|
||||
// 用户端 DTO
|
||||
type WithdrawalDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
WithdrawNo string `json:"withdraw_no"`
|
||||
UserID uint64 `json:"user_id"`
|
||||
AmountCent int64 `json:"amount_cent"`
|
||||
FeeCent int64 `json:"fee_cent"`
|
||||
ActualAmountCent int64 `json:"actual_amount_cent"`
|
||||
AccountType string `json:"account_type"`
|
||||
AccountName string `json:"account_name"`
|
||||
AccountNo string `json:"account_no"` // 脱敏
|
||||
BankName string `json:"bank_name"`
|
||||
Status string `json:"status"`
|
||||
ReviewRemark string `json:"review_remark"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ReviewedAt *time.Time `json:"reviewed_at"`
|
||||
PaidAt *time.Time `json:"paid_at"`
|
||||
ID uint64 `json:"id"`
|
||||
WithdrawNo string `json:"withdraw_no"`
|
||||
UserID uint64 `json:"user_id"`
|
||||
DistributionSiteID uint64 `json:"distribution_site_id"`
|
||||
AmountCent int64 `json:"amount_cent"`
|
||||
FeeCent int64 `json:"fee_cent"`
|
||||
ActualAmountCent int64 `json:"actual_amount_cent"`
|
||||
AccountType string `json:"account_type"`
|
||||
AccountName string `json:"account_name"`
|
||||
AccountNo string `json:"account_no"` // 脱敏
|
||||
BankName string `json:"bank_name"`
|
||||
Status string `json:"status"`
|
||||
ReviewRemark string `json:"review_remark"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ReviewedAt *time.Time `json:"reviewed_at"`
|
||||
PaidAt *time.Time `json:"paid_at"`
|
||||
}
|
||||
|
||||
// 管理员端详细 DTO
|
||||
type WithdrawalDetailDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
WithdrawNo string `json:"withdraw_no"`
|
||||
UserID uint64 `json:"user_id"`
|
||||
UserNickname string `json:"user_nickname"`
|
||||
UserPhone string `json:"user_phone"`
|
||||
ID uint64 `json:"id"`
|
||||
WithdrawNo string `json:"withdraw_no"`
|
||||
UserID uint64 `json:"user_id"`
|
||||
DistributionSiteID uint64 `json:"distribution_site_id"`
|
||||
UserNickname string `json:"user_nickname"`
|
||||
UserPhone string `json:"user_phone"`
|
||||
AmountCent int64 `json:"amount_cent"`
|
||||
FeeCent int64 `json:"fee_cent"`
|
||||
ActualAmountCent int64 `json:"actual_amount_cent"`
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package withdrawal
|
||||
|
||||
import (
|
||||
"hfb_sys/backend/pkg/response"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"hfb_sys/backend/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -15,6 +17,20 @@ func NewHandler(service *Service) *Handler {
|
||||
return &Handler{service: service}
|
||||
}
|
||||
|
||||
func parseSiteCode(c *gin.Context) string {
|
||||
for _, val := range []string{
|
||||
c.Query("distribution_site_code"),
|
||||
c.Query("site_code"),
|
||||
c.GetHeader("X-Distribution-Site-Code"),
|
||||
c.GetHeader("X-Site-Code"),
|
||||
} {
|
||||
if trimmed := strings.TrimSpace(val); trimmed != "" {
|
||||
return trimmed
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// ========== 用户端接口 ==========
|
||||
|
||||
func (h *Handler) Create(c *gin.Context) {
|
||||
@@ -30,7 +46,8 @@ func (h *Handler) Create(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
withdrawal, err := h.service.Create(c.Request.Context(), userID, req)
|
||||
siteCode := parseSiteCode(c)
|
||||
withdrawal, err := h.service.Create(c.Request.Context(), userID, req, siteCode)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
@@ -49,7 +66,8 @@ func (h *Handler) List(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||||
|
||||
result, err := h.service.List(c.Request.Context(), userID, page, pageSize)
|
||||
siteCode := parseSiteCode(c)
|
||||
result, err := h.service.List(c.Request.Context(), userID, page, pageSize, siteCode)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
|
||||
@@ -10,22 +10,23 @@ import (
|
||||
// 转换为用户DTO
|
||||
func toDTO(w model.WithdrawalRequest) WithdrawalDTO {
|
||||
return WithdrawalDTO{
|
||||
ID: w.ID,
|
||||
WithdrawNo: w.WithdrawNo,
|
||||
UserID: w.UserID,
|
||||
AmountCent: w.AmountCent,
|
||||
FeeCent: w.FeeCent,
|
||||
ActualAmountCent: w.ActualAmountCent,
|
||||
AccountType: w.AccountType,
|
||||
AccountName: w.AccountName,
|
||||
AccountNo: w.AccountNo,
|
||||
BankName: w.BankName,
|
||||
Status: w.Status,
|
||||
ReviewRemark: w.ReviewRemark,
|
||||
CreatedAt: w.CreatedAt,
|
||||
UpdatedAt: w.UpdatedAt,
|
||||
ReviewedAt: w.ReviewedAt,
|
||||
PaidAt: w.PaidAt,
|
||||
ID: w.ID,
|
||||
WithdrawNo: w.WithdrawNo,
|
||||
UserID: w.UserID,
|
||||
DistributionSiteID: w.DistributionSiteID,
|
||||
AmountCent: w.AmountCent,
|
||||
FeeCent: w.FeeCent,
|
||||
ActualAmountCent: w.ActualAmountCent,
|
||||
AccountType: w.AccountType,
|
||||
AccountName: w.AccountName,
|
||||
AccountNo: w.AccountNo,
|
||||
BankName: w.BankName,
|
||||
Status: w.Status,
|
||||
ReviewRemark: w.ReviewRemark,
|
||||
CreatedAt: w.CreatedAt,
|
||||
UpdatedAt: w.UpdatedAt,
|
||||
ReviewedAt: w.ReviewedAt,
|
||||
PaidAt: w.PaidAt,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,10 +76,11 @@ func (r *Repository) toDetailDTO(ctx context.Context, w model.WithdrawalRequest)
|
||||
}
|
||||
|
||||
return &WithdrawalDetailDTO{
|
||||
ID: w.ID,
|
||||
WithdrawNo: w.WithdrawNo,
|
||||
UserID: w.UserID,
|
||||
UserNickname: user.Nickname,
|
||||
ID: w.ID,
|
||||
WithdrawNo: w.WithdrawNo,
|
||||
UserID: w.UserID,
|
||||
DistributionSiteID: w.DistributionSiteID,
|
||||
UserNickname: user.Nickname,
|
||||
UserPhone: user.Phone,
|
||||
AmountCent: w.AmountCent,
|
||||
FeeCent: w.FeeCent,
|
||||
|
||||
@@ -32,7 +32,7 @@ func NewService(repo *Repository) *Service {
|
||||
}
|
||||
|
||||
// 用户端方法
|
||||
func (s *Service) Create(ctx context.Context, userID uint64, req CreateWithdrawalRequest) (*WithdrawalDTO, error) {
|
||||
func (s *Service) Create(ctx context.Context, userID uint64, req CreateWithdrawalRequest, siteCode ...string) (*WithdrawalDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
@@ -45,10 +45,10 @@ func (s *Service) Create(ctx context.Context, userID uint64, req CreateWithdrawa
|
||||
return nil, ErrMaxWithdrawalAmount
|
||||
}
|
||||
|
||||
return s.repo.Create(ctx, userID, req)
|
||||
return s.repo.Create(ctx, userID, req, siteCode...)
|
||||
}
|
||||
|
||||
func (s *Service) List(ctx context.Context, userID uint64, page, pageSize int) (*PaginatedResult, error) {
|
||||
func (s *Service) List(ctx context.Context, userID uint64, page, pageSize int, siteCode ...string) (*PaginatedResult, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
@@ -58,7 +58,7 @@ func (s *Service) List(ctx context.Context, userID uint64, page, pageSize int) (
|
||||
if pageSize < 1 || pageSize > 100 {
|
||||
pageSize = 20
|
||||
}
|
||||
return s.repo.List(ctx, userID, page, pageSize)
|
||||
return s.repo.List(ctx, userID, page, pageSize, siteCode...)
|
||||
}
|
||||
|
||||
func (s *Service) FindByID(ctx context.Context, userID, id uint64) (*WithdrawalDTO, error) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package withdrawal
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/wallet"
|
||||
@@ -11,8 +12,23 @@ import (
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
func (r *Repository) resolveSiteID(ctx context.Context, siteCode string) (uint64, error) {
|
||||
siteCode = strings.TrimSpace(siteCode)
|
||||
if siteCode == "" {
|
||||
return 0, nil
|
||||
}
|
||||
var site model.DistributionSite
|
||||
if err := r.db.WithContext(ctx).Where("site_code = ? AND status = 'active'", siteCode).First(&site).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return 0, errors.New("distribution site unavailable")
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
return site.ID, nil
|
||||
}
|
||||
|
||||
// 用户创建提现申请
|
||||
func (r *Repository) Create(ctx context.Context, userID uint64, req CreateWithdrawalRequest) (*WithdrawalDTO, error) {
|
||||
func (r *Repository) Create(ctx context.Context, userID uint64, req CreateWithdrawalRequest, siteCode ...string) (*WithdrawalDTO, error) {
|
||||
db := r.db.WithContext(ctx)
|
||||
// 验证收款账号
|
||||
var paymentAccount model.UserPaymentAccount
|
||||
@@ -24,6 +40,15 @@ func (r *Repository) Create(ctx context.Context, userID uint64, req CreateWithdr
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var code string
|
||||
if len(siteCode) > 0 {
|
||||
code = siteCode[0]
|
||||
}
|
||||
siteID, err := r.resolveSiteID(ctx, code)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 计算手续费和实际到账金额(分)
|
||||
feeCent := int64(float64(req.AmountCent) * WithdrawalFeeRate)
|
||||
actualAmountCent := req.AmountCent - feeCent
|
||||
@@ -41,18 +66,19 @@ func (r *Repository) Create(ctx context.Context, userID uint64, req CreateWithdr
|
||||
}
|
||||
|
||||
withdrawal := model.WithdrawalRequest{
|
||||
WithdrawNo: withdrawNo,
|
||||
UserID: userID,
|
||||
AmountCent: req.AmountCent,
|
||||
FeeCent: feeCent,
|
||||
ActualAmountCent: actualAmountCent,
|
||||
PaymentAccountID: &req.PaymentAccountID,
|
||||
AccountType: paymentAccount.AccountType,
|
||||
AccountName: paymentAccount.AccountName,
|
||||
AccountNo: maskAccountNo(decryptedNo, paymentAccount.AccountType),
|
||||
BankName: paymentAccount.BankName,
|
||||
BankBranch: paymentAccount.BankBranch,
|
||||
Status: "pending",
|
||||
WithdrawNo: withdrawNo,
|
||||
UserID: userID,
|
||||
DistributionSiteID: siteID,
|
||||
AmountCent: req.AmountCent,
|
||||
FeeCent: feeCent,
|
||||
ActualAmountCent: actualAmountCent,
|
||||
PaymentAccountID: &req.PaymentAccountID,
|
||||
AccountType: paymentAccount.AccountType,
|
||||
AccountName: paymentAccount.AccountName,
|
||||
AccountNo: maskAccountNo(decryptedNo, paymentAccount.AccountType),
|
||||
BankName: paymentAccount.BankName,
|
||||
BankBranch: paymentAccount.BankBranch,
|
||||
Status: "pending",
|
||||
}
|
||||
|
||||
// 事务处理
|
||||
@@ -64,21 +90,23 @@ func (r *Repository) Create(ctx context.Context, userID uint64, req CreateWithdr
|
||||
|
||||
// 冻结用户余额
|
||||
if err := wallet.AppendEntries(tx, wallet.Entry{
|
||||
UserID: userID,
|
||||
Direction: "out",
|
||||
AmountCent: req.AmountCent,
|
||||
BalanceType: "available",
|
||||
BizType: "withdraw_freeze",
|
||||
BizNo: withdrawNo,
|
||||
Remark: "提现冻结",
|
||||
UserID: userID,
|
||||
DistributionSiteID: siteID,
|
||||
Direction: "out",
|
||||
AmountCent: req.AmountCent,
|
||||
BalanceType: "available",
|
||||
BizType: "withdraw_freeze",
|
||||
BizNo: withdrawNo,
|
||||
Remark: "提现冻结",
|
||||
}, wallet.Entry{
|
||||
UserID: userID,
|
||||
Direction: "in",
|
||||
AmountCent: req.AmountCent,
|
||||
BalanceType: "frozen",
|
||||
BizType: "withdraw_freeze",
|
||||
BizNo: withdrawNo,
|
||||
Remark: "提现冻结",
|
||||
UserID: userID,
|
||||
DistributionSiteID: siteID,
|
||||
Direction: "in",
|
||||
AmountCent: req.AmountCent,
|
||||
BalanceType: "frozen",
|
||||
BizType: "withdraw_freeze",
|
||||
BizNo: withdrawNo,
|
||||
Remark: "提现冻结",
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -94,21 +122,35 @@ func (r *Repository) Create(ctx context.Context, userID uint64, req CreateWithdr
|
||||
}
|
||||
|
||||
// 用户查询提现列表
|
||||
|
||||
// 用户查询提现列表
|
||||
func (r *Repository) List(ctx context.Context, userID uint64, page, pageSize int) (*PaginatedResult, error) {
|
||||
func (r *Repository) List(ctx context.Context, userID uint64, page, pageSize int, siteCode ...string) (*PaginatedResult, error) {
|
||||
db := r.db.WithContext(ctx)
|
||||
var code string
|
||||
if len(siteCode) > 0 {
|
||||
code = siteCode[0]
|
||||
}
|
||||
siteID, err := r.resolveSiteID(ctx, code)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
countDB := db.Model(&model.WithdrawalRequest{}).Where("user_id = ?", userID)
|
||||
dataDB := db.Where("user_id = ?", userID)
|
||||
if siteID > 0 {
|
||||
countDB = countDB.Where("distribution_site_id = ?", siteID)
|
||||
dataDB = dataDB.Where("distribution_site_id = ?", siteID)
|
||||
} else {
|
||||
countDB = countDB.Where("distribution_site_id = 0 OR distribution_site_id IS NULL")
|
||||
dataDB = dataDB.Where("distribution_site_id = 0 OR distribution_site_id IS NULL")
|
||||
}
|
||||
|
||||
var total int64
|
||||
if err := db.Model(&model.WithdrawalRequest{}).
|
||||
Where("user_id = ?", userID).
|
||||
Count(&total).Error; err != nil {
|
||||
if err := countDB.Count(&total).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
var withdrawals []model.WithdrawalRequest
|
||||
if err := db.Where("user_id = ?", userID).
|
||||
Order("created_at DESC").
|
||||
if err := dataDB.Order("created_at DESC").
|
||||
Offset(offset).Limit(pageSize).
|
||||
Find(&withdrawals).Error; err != nil {
|
||||
return nil, err
|
||||
@@ -170,21 +212,23 @@ func (r *Repository) Cancel(ctx context.Context, userID, id uint64) error {
|
||||
|
||||
// 解冻余额
|
||||
if err := wallet.AppendEntries(tx, wallet.Entry{
|
||||
UserID: userID,
|
||||
Direction: "out",
|
||||
AmountCent: withdrawal.AmountCent,
|
||||
BalanceType: "frozen",
|
||||
BizType: "withdraw_cancel",
|
||||
BizNo: withdrawal.WithdrawNo,
|
||||
Remark: "用户取消提现",
|
||||
UserID: userID,
|
||||
DistributionSiteID: withdrawal.DistributionSiteID,
|
||||
Direction: "out",
|
||||
AmountCent: withdrawal.AmountCent,
|
||||
BalanceType: "frozen",
|
||||
BizType: "withdraw_cancel",
|
||||
BizNo: withdrawal.WithdrawNo,
|
||||
Remark: "用户取消提现",
|
||||
}, wallet.Entry{
|
||||
UserID: userID,
|
||||
Direction: "in",
|
||||
AmountCent: withdrawal.AmountCent,
|
||||
BalanceType: "available",
|
||||
BizType: "withdraw_cancel",
|
||||
BizNo: withdrawal.WithdrawNo,
|
||||
Remark: "用户取消提现",
|
||||
UserID: userID,
|
||||
DistributionSiteID: withdrawal.DistributionSiteID,
|
||||
Direction: "in",
|
||||
AmountCent: withdrawal.AmountCent,
|
||||
BalanceType: "available",
|
||||
BizType: "withdraw_cancel",
|
||||
BizNo: withdrawal.WithdrawNo,
|
||||
Remark: "用户取消提现",
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user