feat(distribution): 独立分销前台商城多租户体系、货源与订单数据严格隔离

This commit is contained in:
yml2213
2026-09-07 17:18:14 +08:00
parent 550b42eb22
commit ecaf15417b
34 changed files with 2397 additions and 119 deletions
+30 -8
View File
@@ -11,13 +11,25 @@ import (
"gorm.io/gorm"
)
func (r *Repository) ListForUser(ctx context.Context, userID uint64, page, pageSize int) (*UserPaginatedResult, error) {
func (r *Repository) ListForUser(ctx context.Context, userID uint64, page, pageSize int, siteCode ...string) (*UserPaginatedResult, error) {
page, pageSize = normalizePagination(page, pageSize)
base := r.baseQuery(ctx).Where("o.renter_id = ? OR o.owner_id = ?", userID, userID)
countBase := r.db.WithContext(ctx).Table("rental_orders AS o").Where("o.renter_id = ? OR o.owner_id = ?", userID, userID)
var code string
if len(siteCode) > 0 {
code = strings.TrimSpace(siteCode[0])
}
if code != "" {
base = base.Joins("INNER JOIN distribution_sites AS ds ON ds.id = o.distribution_site_id AND ds.site_code = ?", code)
countBase = countBase.Joins("INNER JOIN distribution_sites AS ds ON ds.id = o.distribution_site_id AND ds.site_code = ?", code)
} else {
base = base.Where("o.distribution_site_id IS NULL OR o.distribution_site_id = 0")
countBase = countBase.Where("o.distribution_site_id IS NULL OR o.distribution_site_id = 0")
}
var total int64
if err := r.db.WithContext(ctx).Table("rental_orders AS o").
Where("o.renter_id = ? OR o.owner_id = ?", userID, userID).
Count(&total).Error; err != nil {
if err := countBase.Count(&total).Error; err != nil {
return nil, err
}
var rows []orderRow
@@ -308,12 +320,22 @@ func (r *Repository) FindLatestAdminByListing(ctx context.Context, listingID uin
return &dto, nil
}
func (r *Repository) FindForUser(ctx context.Context, userID uint64, orderID uint64) (*OrderDTO, error) {
func (r *Repository) FindForUser(ctx context.Context, userID uint64, orderID uint64, siteCode ...string) (*OrderDTO, error) {
var row orderRow
db := r.db.WithContext(ctx)
if err := r.baseQuery(ctx).
Where("o.id = ? AND (o.renter_id = ? OR o.owner_id = ?)", orderID, userID, userID).
First(&row).Error; err != nil {
query := r.baseQuery(ctx).Where("o.id = ? AND (o.renter_id = ? OR o.owner_id = ?)", orderID, userID, userID)
var code string
if len(siteCode) > 0 {
code = strings.TrimSpace(siteCode[0])
}
if code != "" {
query = query.Joins("INNER JOIN distribution_sites AS ds ON ds.id = o.distribution_site_id AND ds.site_code = ?", code)
} else if len(siteCode) > 0 {
query = query.Where("o.distribution_site_id IS NULL OR o.distribution_site_id = 0")
}
if err := query.First(&row).Error; err != nil {
return nil, err
}
dto := row.toDTOForUser(userID)