feat(tenant): 按租户隔离用户身份与后台权限

This commit is contained in:
yml
2026-11-15 15:37:23 +08:00
parent ca57f604bf
commit abf0fce89b
43 changed files with 1572 additions and 196 deletions
@@ -6,6 +6,7 @@ import (
"time"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/tenancy"
"gorm.io/gorm"
)
@@ -34,7 +35,11 @@ func (r *Repository) AdminReviewContext(ctx context.Context, id uint64, query Wi
query = normalizeReviewContextQuery(query)
var current model.WithdrawalRequest
if err := r.db.WithContext(ctx).First(&current, id).Error; err != nil {
currentQuery := r.db.WithContext(ctx).Where("id = ?", id)
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
currentQuery = currentQuery.Where("tenant_id = ?", tenantID)
}
if err := currentQuery.First(&current).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrWithdrawalNotFound
}
@@ -79,6 +84,13 @@ 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 {
if tenantID > 0 {
return db.Where(column+" = ?", tenantID)
}
return db
}
func (r *Repository) withdrawalReviewSummary(ctx context.Context, userID, siteID uint64) (WithdrawalReviewSummaryDTO, error) {
db := r.db.WithContext(ctx)
var result WithdrawalReviewSummaryDTO
@@ -101,6 +113,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))
if err := walletQuery.
Select("COALESCE(SUM(wl.amount_cent), 0)").
Joins("JOIN rental_orders AS o ON o.id = wl.order_id").
@@ -159,6 +172,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))
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,
@@ -224,12 +238,12 @@ func (r *Repository) withdrawalReviewOrders(ctx context.Context, userID, siteID
OrderID uint64
AmountCent int64
}
if err := r.db.WithContext(ctx).
walletLedgerQuery := r.db.WithContext(ctx).
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).
Group("order_id").
Scan(&walletRows).Error; err != nil {
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))
if err := walletLedgerQuery.Group("order_id").Scan(&walletRows).Error; err != nil {
return nil, err
}
walletIncome := make(map[uint64]int64, len(walletRows))
@@ -343,6 +357,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))
if err := historyQuery.
Where("user_id = ?", userID).
Order("created_at DESC, id DESC").