feat(tenant): 增加双站订单视图与租户统计
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/distribution"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -208,6 +209,7 @@ func (r *Repository) ListAdmin(ctx context.Context, query AdminOrderQuery) (*Adm
|
||||
items := make([]AdminOrderListItemDTO, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
item := row.toAdminListItem()
|
||||
item.SiteRole = tenantOrderRole(ctx, row.RentalOrder)
|
||||
if amount, ok := ownerIncome[row.ID]; ok {
|
||||
value := amount
|
||||
item.OwnerIncomeAmountCent = &value
|
||||
@@ -250,7 +252,7 @@ func (r *Repository) adminOrderCountQuery(ctx context.Context, query AdminOrderQ
|
||||
}
|
||||
}
|
||||
if !needsJoin {
|
||||
return r.db.WithContext(ctx).Table("rental_orders AS o")
|
||||
return applyTenantOrderScope(r.db.WithContext(ctx).Table("rental_orders AS o"), ctx)
|
||||
}
|
||||
return r.adminOrderJoinQuery(ctx)
|
||||
}
|
||||
@@ -367,6 +369,8 @@ func (r *Repository) FindAdmin(ctx context.Context, orderID uint64) (*OrderDTO,
|
||||
return nil, err
|
||||
}
|
||||
dto := row.toAdminDTO()
|
||||
dto.SiteRole = tenantOrderRole(ctx, row.RentalOrder)
|
||||
filterAdminActionsForSiteRole(&dto)
|
||||
applyPaymentDeadline(&dto, row.RentalOrder, pendingPaymentTimeoutMinutes(db))
|
||||
dto.ActiveDispute = r.activeDisputeDTO(ctx, orderID)
|
||||
dto.Checkout = r.latestCheckoutAdminDTO(ctx, orderID)
|
||||
@@ -383,12 +387,28 @@ func (r *Repository) FindLatestAdminByListing(ctx context.Context, listingID uin
|
||||
return nil, err
|
||||
}
|
||||
dto := row.toAdminDTO()
|
||||
dto.SiteRole = tenantOrderRole(ctx, row.RentalOrder)
|
||||
filterAdminActionsForSiteRole(&dto)
|
||||
applyPaymentDeadline(&dto, row.RentalOrder, pendingPaymentTimeoutMinutes(db))
|
||||
dto.ActiveDispute = r.activeDisputeDTO(ctx, row.ID)
|
||||
dto.Checkout = r.latestCheckoutAdminDTO(ctx, row.ID)
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
func (r *Repository) EnsureAdminOrderAccess(ctx context.Context, orderID uint64) error {
|
||||
if tenancy.IDFromContext(ctx) == 0 {
|
||||
return nil
|
||||
}
|
||||
var count int64
|
||||
if err := r.adminOrderJoinQuery(ctx).Where("o.id = ?", orderID).Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if count == 0 {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) FindForUser(ctx context.Context, userID uint64, orderID uint64, siteCode ...string) (*OrderDTO, error) {
|
||||
var row orderRow
|
||||
db := r.db.WithContext(ctx)
|
||||
@@ -411,6 +431,7 @@ func (r *Repository) FindForUser(ctx context.Context, userID uint64, orderID uin
|
||||
return nil, err
|
||||
}
|
||||
dto := row.toDTOForUser(userID)
|
||||
dto.SiteRole = tenantOrderRole(ctx, row.RentalOrder)
|
||||
applyPaymentDeadline(&dto, row.RentalOrder, pendingPaymentTimeoutMinutes(db))
|
||||
dto.ActiveDispute = r.activeDisputeDTO(ctx, orderID)
|
||||
dto.Checkout = r.latestCheckoutDTOForUser(ctx, orderID, userID, row.RentalOrder)
|
||||
@@ -506,11 +527,65 @@ func (r *Repository) adminQuery(ctx context.Context) *gorm.DB {
|
||||
}
|
||||
|
||||
func (r *Repository) adminOrderJoinQuery(ctx context.Context) *gorm.DB {
|
||||
return r.db.WithContext(ctx).Table("rental_orders AS o").
|
||||
query := r.db.WithContext(ctx).Table("rental_orders AS o").
|
||||
Joins("JOIN rental_listings AS l ON l.id = o.listing_id").
|
||||
Joins("JOIN game_accounts AS a ON a.id = o.account_id").
|
||||
Joins("JOIN users AS owner ON owner.id = o.owner_id").
|
||||
Joins("JOIN users AS renter ON renter.id = o.renter_id")
|
||||
return applyTenantOrderScope(query, ctx)
|
||||
}
|
||||
|
||||
func applyTenantOrderScope(query *gorm.DB, ctx context.Context) *gorm.DB {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID == 0 {
|
||||
return query
|
||||
}
|
||||
return query.Where("(o.buyer_tenant_id = ? OR o.source_tenant_id = ?)", tenantID, tenantID)
|
||||
}
|
||||
|
||||
func applyBuyerTenantOrderScope(query *gorm.DB, ctx context.Context) *gorm.DB {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID == 0 {
|
||||
return query
|
||||
}
|
||||
return query.Where("o.buyer_tenant_id = ?", tenantID)
|
||||
}
|
||||
|
||||
func tenantOrderRole(ctx context.Context, order model.RentalOrder) string {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID == 0 {
|
||||
return ""
|
||||
}
|
||||
isBuyer := order.BuyerTenantID == tenantID
|
||||
isSource := order.SourceTenantID == tenantID
|
||||
switch {
|
||||
case isBuyer && isSource:
|
||||
return "both"
|
||||
case isBuyer:
|
||||
return "buyer"
|
||||
case isSource:
|
||||
return "source"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func filterAdminActionsForSiteRole(dto *OrderDTO) {
|
||||
if dto == nil || dto.AdminActions == nil || dto.SiteRole == "" || dto.SiteRole == "both" {
|
||||
return
|
||||
}
|
||||
actions := dto.AdminActions
|
||||
if dto.SiteRole == "buyer" {
|
||||
actions.ResetHandoff = nil
|
||||
actions.PlatformHandoff = nil
|
||||
actions.ForceHandoff = nil
|
||||
actions.PlatformOfflineSettlement = nil
|
||||
}
|
||||
if dto.SiteRole == "source" {
|
||||
actions.PlatformCheckoutConfirm = nil
|
||||
actions.PlatformCheckoutCounter = nil
|
||||
actions.PlatformCheckoutDispute = nil
|
||||
}
|
||||
}
|
||||
|
||||
type orderRow struct {
|
||||
|
||||
Reference in New Issue
Block a user