feat(tenant): 增加双站订单视图与租户统计
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package order
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"hfb_sys/backend/internal/logging"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
tenantOrderCapabilityHandoff = "handoff"
|
||||
tenantOrderCapabilityCheckout = "checkout"
|
||||
tenantOrderCapabilityRefund = "refund"
|
||||
)
|
||||
|
||||
func (r *Repository) EnsureAdminOrderCapability(ctx context.Context, orderID uint64, capability string) error {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID == 0 {
|
||||
return nil
|
||||
}
|
||||
var order struct {
|
||||
BuyerTenantID uint64
|
||||
SourceTenantID uint64
|
||||
}
|
||||
if err := r.db.WithContext(ctx).
|
||||
Table("rental_orders").
|
||||
Select("buyer_tenant_id, source_tenant_id").
|
||||
Where("id = ?", orderID).
|
||||
Scan(&order).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if order.BuyerTenantID == 0 && order.SourceTenantID == 0 {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
isBuyer := order.BuyerTenantID == tenantID
|
||||
isSource := order.SourceTenantID == tenantID
|
||||
if !isBuyer && !isSource {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
if r.adminIsPlatformSuper(ctx) {
|
||||
return nil
|
||||
}
|
||||
allowed := false
|
||||
switch capability {
|
||||
case tenantOrderCapabilityHandoff:
|
||||
allowed = isSource
|
||||
case tenantOrderCapabilityCheckout, tenantOrderCapabilityRefund:
|
||||
allowed = isBuyer
|
||||
default:
|
||||
allowed = true
|
||||
}
|
||||
if !allowed {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) adminIsPlatformSuper(ctx context.Context) bool {
|
||||
adminID := logging.AdminIDFromContext(ctx)
|
||||
if adminID == 0 {
|
||||
return false
|
||||
}
|
||||
var count int64
|
||||
err := r.db.WithContext(ctx).Table("admin_user_roles AS aur").
|
||||
Joins("JOIN roles AS r ON r.id = aur.role_id").
|
||||
Where("aur.admin_user_id = ? AND aur.tenant_id = 0 AND r.code = ?", adminID, "super_admin").
|
||||
Count(&count).Error
|
||||
return err == nil && count > 0
|
||||
}
|
||||
Reference in New Issue
Block a user