fix(tenant): 收紧双站后台边界并补齐货架管理
This commit is contained in:
@@ -5,6 +5,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -48,7 +50,7 @@ func (r *Repository) ListAdmin(ctx context.Context, query AdminListQuery) (*Pagi
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
var rows []disputeRow
|
||||
err := applyAdminListFilters(r.baseQuery(ctx), query).
|
||||
err := applyAdminListFilters(r.adminScopedBaseQuery(ctx), query).
|
||||
Order("d.id DESC").
|
||||
Offset(offset).
|
||||
Limit(pageSize).
|
||||
@@ -72,7 +74,7 @@ func (r *Repository) ListAdmin(ctx context.Context, query AdminListQuery) (*Pagi
|
||||
|
||||
func (r *Repository) FindAdminByID(ctx context.Context, id uint64) (*DisputeDTO, error) {
|
||||
var row disputeRow
|
||||
if err := r.baseQuery(ctx).Where("d.id = ?", id).First(&row).Error; err != nil {
|
||||
if err := r.adminScopedBaseQuery(ctx).Where("d.id = ?", id).First(&row).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dto := row.toDTO()
|
||||
@@ -105,6 +107,30 @@ func (r *Repository) baseQuery(ctx context.Context) *gorm.DB {
|
||||
CASE WHEN c.deposit_deduct_amount_cent > 0 THEN c.deposit_deduct_amount_cent ELSE c.other_amount_cent END AS checkout_deposit_deduct_amount_cent`)
|
||||
}
|
||||
|
||||
func (r *Repository) adminScopedBaseQuery(ctx context.Context) *gorm.DB {
|
||||
return applyAdminDisputeTenantScope(ctx, r.baseQuery(ctx))
|
||||
}
|
||||
|
||||
func applyAdminDisputeTenantScope(ctx context.Context, db *gorm.DB) *gorm.DB {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID == 0 {
|
||||
return db
|
||||
}
|
||||
return db.Where("o.buyer_tenant_id = ?", tenantID).
|
||||
Where("(o.distribution_site_id IS NULL OR o.distribution_site_id = 0)")
|
||||
}
|
||||
|
||||
func currentTenantCanAdminDisputeOrder(ctx context.Context, orderBuyerTenantID uint64, distributionSiteID *uint64) bool {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID == 0 {
|
||||
return true
|
||||
}
|
||||
if orderBuyerTenantID != tenantID {
|
||||
return false
|
||||
}
|
||||
return distributionSiteID == nil || *distributionSiteID == 0
|
||||
}
|
||||
|
||||
func (r *Repository) adminFilterQuery(ctx context.Context) *gorm.DB {
|
||||
return r.db.WithContext(ctx).Table("disputes AS d").
|
||||
Joins("JOIN rental_orders AS o ON o.id = d.order_id").
|
||||
@@ -116,29 +142,7 @@ func (r *Repository) adminFilterQuery(ctx context.Context) *gorm.DB {
|
||||
}
|
||||
|
||||
func (r *Repository) adminDisputeCountQuery(ctx context.Context, query AdminListQuery) *gorm.DB {
|
||||
kw := strings.TrimSpace(query.Keyword)
|
||||
if kw == "" {
|
||||
return r.db.WithContext(ctx).Table("disputes AS d")
|
||||
}
|
||||
upper := strings.ToUpper(kw)
|
||||
if id, err := strconv.ParseUint(kw, 10, 64); err == nil && id > 0 && kw[0] != '0' && id < 100000000 {
|
||||
return r.db.WithContext(ctx).Table("disputes AS d").
|
||||
Joins("JOIN rental_orders AS o ON o.id = d.order_id")
|
||||
}
|
||||
if strings.HasPrefix(upper, "RO") || strings.HasPrefix(upper, "ORD") {
|
||||
return r.db.WithContext(ctx).Table("disputes AS d").
|
||||
Joins("JOIN rental_orders AS o ON o.id = d.order_id")
|
||||
}
|
||||
if strings.HasPrefix(upper, "L") || (len(kw) >= 12 && isAllDigits(kw)) {
|
||||
return r.db.WithContext(ctx).Table("disputes AS d").
|
||||
Joins("JOIN rental_orders AS o ON o.id = d.order_id").
|
||||
Joins("JOIN rental_listings AS l ON l.id = o.listing_id")
|
||||
}
|
||||
if isPhoneKeyword(kw) {
|
||||
return r.db.WithContext(ctx).Table("disputes AS d").
|
||||
Joins("JOIN rental_orders AS o ON o.id = d.order_id")
|
||||
}
|
||||
return r.adminFilterQuery(ctx)
|
||||
return applyAdminDisputeTenantScope(ctx, r.adminFilterQuery(ctx))
|
||||
}
|
||||
|
||||
func applyAdminListFilters(db *gorm.DB, query AdminListQuery) *gorm.DB {
|
||||
|
||||
Reference in New Issue
Block a user