fix(withdrawal): 允许平台超管审核历史分销提现

This commit is contained in:
yml
2026-11-26 22:01:59 +08:00
parent 18b54e9f1a
commit abbb977ca6
5 changed files with 82 additions and 33 deletions
+4 -13
View File
@@ -8,7 +8,6 @@ import (
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/modules/wallet"
"hfb_sys/backend/internal/tenancy"
"gorm.io/gorm"
"gorm.io/gorm/clause"
@@ -18,9 +17,7 @@ import (
func (r *Repository) AdminList(ctx context.Context, query AdminListQuery) (*AdminPaginatedResult, error) {
db := r.db.WithContext(ctx).Model(&model.WithdrawalRequest{})
// distribution_site_id 只表示获客渠道,不能把同租户内分销站点用户的提现挡在财务审核列表外。
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
db = db.Where("tenant_id = ?", tenantID)
}
db = scopeAdminTenant(ctx, db, "tenant_id")
if query.Status != "" {
db = db.Where("status = ?", query.Status)
@@ -61,9 +58,7 @@ func (r *Repository) AdminList(ctx context.Context, query AdminListQuery) (*Admi
func (r *Repository) AdminFindByID(ctx context.Context, id uint64) (*WithdrawalDetailDTO, error) {
var withdrawal model.WithdrawalRequest
query := r.db.WithContext(ctx).Where("id = ?", id)
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
query = query.Where("tenant_id = ?", tenantID)
}
query = scopeAdminTenant(ctx, query, "tenant_id")
if err := query.First(&withdrawal).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrWithdrawalNotFound
@@ -80,9 +75,7 @@ func (r *Repository) Review(ctx context.Context, adminID, id uint64, req ReviewW
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var withdrawal model.WithdrawalRequest
query := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("id = ?", id)
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
query = query.Where("tenant_id = ?", tenantID)
}
query = scopeAdminTenant(ctx, query, "tenant_id")
if err := query.First(&withdrawal).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return ErrWithdrawalNotFound
@@ -155,9 +148,7 @@ func (r *Repository) ConfirmPayment(ctx context.Context, adminID, id uint64, req
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var withdrawal model.WithdrawalRequest
query := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("id = ?", id)
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
query = query.Where("tenant_id = ?", tenantID)
}
query = scopeAdminTenant(ctx, query, "tenant_id")
if err := query.First(&withdrawal).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return ErrWithdrawalNotFound
@@ -7,7 +7,6 @@ import (
"strings"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/tenancy"
)
// 转换为用户DTO
@@ -60,9 +59,7 @@ func (r *Repository) toAdminListItems(ctx context.Context, withdrawals []model.W
usersByID := make(map[uint64]model.User, len(userIDs))
var users []model.User
userQuery := r.db.WithContext(ctx).Select("id, nickname, phone").Where("id IN ?", userIDs)
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
userQuery = userQuery.Where("tenant_id = ?", tenantID)
}
userQuery = scopeAdminTenant(ctx, userQuery, "tenant_id")
if err := userQuery.Find(&users).Error; err != nil {
return nil, err
}
@@ -111,13 +108,10 @@ func (r *Repository) toAdminListItems(ctx context.Context, withdrawals []model.W
// 转换为管理员详细DTO
func (r *Repository) toDetailDTO(ctx context.Context, w model.WithdrawalRequest) (*WithdrawalDetailDTO, error) {
db := r.db.WithContext(ctx)
tenantID := tenancy.IDFromContext(ctx)
// 查询用户信息
var user model.User
userQuery := db.Select("nickname, phone").Where("id = ?", w.UserID)
if tenantID > 0 {
userQuery = userQuery.Where("tenant_id = ?", tenantID)
}
userQuery = scopeAdminTenant(ctx, userQuery, "tenant_id")
userQuery.First(&user)
// 查询审核人信息
@@ -144,9 +138,7 @@ func (r *Repository) toDetailDTO(ctx context.Context, w model.WithdrawalRequest)
if w.PaymentAccountID != nil {
var paymentAccount model.UserPaymentAccount
accountQuery := db.Where("id = ?", *w.PaymentAccountID)
if tenantID > 0 {
accountQuery = accountQuery.Where("tenant_id = ?", tenantID)
}
accountQuery = scopeAdminTenant(ctx, accountQuery, "tenant_id")
if err := accountQuery.First(&paymentAccount).Error; err == nil {
// 解密账号
decrypted, err := r.encryptor.Decrypt(paymentAccount.AccountNo)
@@ -520,3 +520,67 @@ func TestWithdrawalRequestsAreScopedByTenant(t *testing.T) {
t.Fatalf("jieyou withdrawal list = %+v", list)
}
}
func TestWithdrawalAdminAllTenantScopeIncludesLegacyDistribution(t *testing.T) {
db := database.NewTestDB()
site := model.DistributionSite{
SiteCode: "legacy_tianyu", SiteName: "天域商行",
Domain: "legacy-tianyu.example.com", Status: "active",
}
if err := db.Create(&site).Error; err != nil {
t.Fatal(err)
}
users := []model.User{
{TenantID: 0, DistributionSiteID: site.ID, Phone: "13300000101", Status: "active"},
{TenantID: 101, Phone: "13300000102", Status: "active"},
{TenantID: 202, Phone: "13300000103", Status: "active"},
}
if err := db.Create(&users).Error; err != nil {
t.Fatal(err)
}
withdrawals := []model.WithdrawalRequest{
{WithdrawNo: "WD-LEGACY-SITE", TenantID: 0, UserID: users[0].ID, DistributionSiteID: site.ID, AmountCent: 1000, ActualAmountCent: 1000, AccountType: "bank", AccountName: "张三", AccountNo: "****0001", Status: "pending"},
{WithdrawNo: "WD-DACHUI", TenantID: 101, UserID: users[1].ID, AmountCent: 2000, ActualAmountCent: 2000, AccountType: "bank", AccountName: "张三", AccountNo: "****0002", Status: "pending"},
{WithdrawNo: "WD-JIEYOU", TenantID: 202, UserID: users[2].ID, AmountCent: 3000, ActualAmountCent: 3000, AccountType: "bank", AccountName: "张三", AccountNo: "****0003", Status: "pending"},
}
if err := db.Create(&withdrawals).Error; err != nil {
t.Fatal(err)
}
repo := NewRepository(db, wallet.NewRepository(db), &crypto.MockEncryptor{})
dachuiCtx := withdrawalTenantContext(101)
dachuiList, err := repo.AdminList(dachuiCtx, AdminListQuery{Page: 1, Size: 20})
if err != nil {
t.Fatal(err)
}
if dachuiList.Total != 1 || dachuiList.Items[0].ID != withdrawals[1].ID {
t.Fatalf("dachui withdrawal list = %+v, want only dachui tenant", dachuiList)
}
if _, err := repo.AdminFindByID(dachuiCtx, withdrawals[0].ID); !errors.Is(err, ErrWithdrawalNotFound) {
t.Fatalf("dachui admin legacy detail error = %v, want not found", err)
}
superCtx := tenancy.WithAdminAllTenantScope(dachuiCtx)
superList, err := repo.AdminList(superCtx, AdminListQuery{Page: 1, Size: 20})
if err != nil {
t.Fatal(err)
}
if superList.Total != 3 || len(superList.Items) != 3 {
t.Fatalf("platform super admin withdrawal list = %+v, want all 3", superList)
}
detail, err := repo.AdminFindByID(superCtx, withdrawals[0].ID)
if err != nil {
t.Fatalf("platform super admin legacy detail: %v", err)
}
if detail.DistributionSiteID != site.ID || detail.DistributionSiteName != site.SiteName {
t.Fatalf("legacy withdrawal detail = %+v", detail)
}
approved := true
reviewed, err := repo.Review(superCtx, 1, withdrawals[0].ID, ReviewWithdrawalRequest{Approved: &approved})
if err != nil {
t.Fatalf("platform super admin legacy review: %v", err)
}
if reviewed.Status != "processing" {
t.Fatalf("legacy withdrawal review status = %q, want processing", reviewed.Status)
}
}
@@ -36,9 +36,7 @@ func (r *Repository) AdminReviewContext(ctx context.Context, id uint64, query Wi
var current model.WithdrawalRequest
currentQuery := r.db.WithContext(ctx).Where("id = ?", id)
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
currentQuery = currentQuery.Where("tenant_id = ?", tenantID)
}
currentQuery = scopeAdminTenant(ctx, currentQuery, "tenant_id")
if err := currentQuery.First(&current).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrWithdrawalNotFound
@@ -84,7 +82,11 @@ func scopeWithdrawalSite(db *gorm.DB, column string, siteID uint64) *gorm.DB {
return db.Where("(" + column + " IS NULL OR " + column + " = 0)")
}
func scopeWithdrawalTenant(db *gorm.DB, column string, tenantID uint64) *gorm.DB {
func scopeAdminTenant(ctx context.Context, db *gorm.DB, column string) *gorm.DB {
if tenancy.HasAdminAllTenantScope(ctx) {
return db
}
tenantID := tenancy.IDFromContext(ctx)
if tenantID > 0 {
return db.Where(column+" = ?", tenantID)
}
@@ -113,7 +115,7 @@ func (r *Repository) withdrawalReviewSummary(ctx context.Context, userID, siteID
var walletIncome int64
walletQuery := scopeWithdrawalSite(db.Table("wallet_ledger AS wl"), "wl.distribution_site_id", siteID)
walletQuery = scopeWithdrawalTenant(walletQuery, "wl.tenant_id", tenancy.IDFromContext(ctx))
walletQuery = scopeAdminTenant(ctx, walletQuery, "wl.tenant_id")
if err := walletQuery.
Select("COALESCE(SUM(wl.amount_cent), 0)").
Joins("JOIN rental_orders AS o ON o.id = wl.order_id").
@@ -172,7 +174,7 @@ func (r *Repository) withdrawalReviewSummary(ctx context.Context, userID, siteID
RejectedWithdrawalCount int64
}
withdrawalQuery := scopeWithdrawalSite(db.Table("withdrawal_requests"), "distribution_site_id", siteID)
withdrawalQuery = scopeWithdrawalTenant(withdrawalQuery, "tenant_id", tenancy.IDFromContext(ctx))
withdrawalQuery = scopeAdminTenant(ctx, withdrawalQuery, "tenant_id")
if err := withdrawalQuery.
Select(`COUNT(*) AS withdrawal_count,
COALESCE(SUM(CASE WHEN status = 'completed' THEN actual_amount_cent ELSE 0 END), 0) AS withdrawn_amount_cent,
@@ -242,7 +244,7 @@ func (r *Repository) withdrawalReviewOrders(ctx context.Context, userID, siteID
Table("wallet_ledger").
Select("order_id, COALESCE(SUM(amount_cent), 0) AS amount_cent").
Where("order_id IN ? AND user_id = ? AND direction = ? AND biz_type IN ?", orderIDs, userID, "in", ownerIncomeBizTypes)
walletLedgerQuery = scopeWithdrawalTenant(walletLedgerQuery, "tenant_id", tenancy.IDFromContext(ctx))
walletLedgerQuery = scopeAdminTenant(ctx, walletLedgerQuery, "tenant_id")
if err := walletLedgerQuery.Group("order_id").Scan(&walletRows).Error; err != nil {
return nil, err
}
@@ -357,7 +359,7 @@ func (r *Repository) withdrawalReviewHistory(ctx context.Context, userID, siteID
var rows []model.WithdrawalRequest
offset := (query.WithdrawalPage - 1) * query.PageSize
historyQuery := scopeWithdrawalSite(r.db.WithContext(ctx), "distribution_site_id", siteID)
historyQuery = scopeWithdrawalTenant(historyQuery, "tenant_id", tenancy.IDFromContext(ctx))
historyQuery = scopeAdminTenant(ctx, historyQuery, "tenant_id")
if err := historyQuery.
Where("user_id = ?", userID).
Order("created_at DESC, id DESC").