feat(tenant): 增加双站共享货架与跨站下单
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/rentergrowth"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
@@ -24,7 +25,7 @@ func (r *Repository) ListPublic(ctx context.Context, query PublicListQuery) (*Pu
|
||||
return r.listPublicPage(ctx, query, page, pageSize)
|
||||
}
|
||||
|
||||
db := r.baseQuery(ctx).
|
||||
db := applyTenantOfferVisibility(ctx, r.baseQuery(ctx)).
|
||||
Where("l.status = ? AND l.review_status = ? AND l.in_transaction = ?", "published", "approved", false)
|
||||
|
||||
if query.DistributionSiteCode != "" {
|
||||
@@ -40,6 +41,9 @@ func (r *Repository) ListPublic(ctx context.Context, query PublicListQuery) (*Pu
|
||||
return nil, err
|
||||
}
|
||||
items := publicListings(rowsToDTO(rows))
|
||||
if err := r.applyTenantOfferPrices(ctx, items); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := r.applyShelfMarkup(ctx, query.DistributionSiteCode, items); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -76,7 +80,8 @@ func (r *Repository) ListPublic(ctx context.Context, query PublicListQuery) (*Pu
|
||||
|
||||
func (r *Repository) listPublicPage(ctx context.Context, query PublicListQuery, page int, pageSize int) (*PublicListResult, error) {
|
||||
var total int64
|
||||
if err := applyPremiumVisibility(r.db.WithContext(ctx).Table("rental_listings AS l"), query).
|
||||
countDB := applyTenantOfferVisibility(ctx, r.db.WithContext(ctx).Table("rental_listings AS l"))
|
||||
if err := applyPremiumVisibility(countDB, query).
|
||||
Where("l.status = ? AND l.review_status = ? AND l.in_transaction = ?", "published", "approved", false).
|
||||
Count(&total).Error; err != nil {
|
||||
return nil, err
|
||||
@@ -84,7 +89,8 @@ func (r *Repository) listPublicPage(ctx context.Context, query PublicListQuery,
|
||||
|
||||
var rows []listingRow
|
||||
offset := (page - 1) * pageSize
|
||||
err := applyPublicSQLSort(applyPremiumVisibility(r.baseQuery(ctx), query), query.Sort).
|
||||
dataDB := applyTenantOfferVisibility(ctx, r.baseQuery(ctx))
|
||||
err := applyPublicSQLSort(ctx, applyPremiumVisibility(dataDB, query), query.Sort).
|
||||
Where("l.status = ? AND l.review_status = ? AND l.in_transaction = ?", "published", "approved", false).
|
||||
Limit(pageSize).
|
||||
Offset(offset).
|
||||
@@ -100,8 +106,12 @@ func (r *Repository) listPublicPage(ctx context.Context, query PublicListQuery,
|
||||
zoneCounts["all"] = nonPremiumTotal
|
||||
zoneCounts["premium"] = 0
|
||||
}
|
||||
items := publicListings(rowsToDTO(rows))
|
||||
if err := r.applyTenantOfferPrices(ctx, items); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &PublicListResult{
|
||||
Items: publicListings(rowsToDTO(rows)),
|
||||
Items: items,
|
||||
Total: total,
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
@@ -173,12 +183,16 @@ func canListPublicWithSQL(query PublicListQuery) bool {
|
||||
}
|
||||
}
|
||||
|
||||
func applyPublicSQLSort(db *gorm.DB, sortKey string) *gorm.DB {
|
||||
func applyPublicSQLSort(ctx context.Context, db *gorm.DB, sortKey string) *gorm.DB {
|
||||
priceExpression := "l.price_cent"
|
||||
if tenancy.IDFromContext(ctx) > 0 {
|
||||
priceExpression = "COALESCE(tlo.price_cent, l.price_cent)"
|
||||
}
|
||||
switch sortKey {
|
||||
case "priceAsc":
|
||||
return db.Order("l.price_cent ASC, l.published_at DESC, l.id DESC")
|
||||
return db.Order(priceExpression + " ASC, l.published_at DESC, l.id DESC")
|
||||
case "priceDesc":
|
||||
return db.Order("l.price_cent DESC, l.published_at DESC, l.id DESC")
|
||||
return db.Order(priceExpression + " DESC, l.published_at DESC, l.id DESC")
|
||||
case "coinDesc":
|
||||
return db.Order("a.haf_coin_amount DESC, l.published_at DESC, l.id DESC")
|
||||
case "coinAsc":
|
||||
@@ -196,6 +210,9 @@ func publicShuffleSeed() int64 {
|
||||
}
|
||||
|
||||
func (r *Repository) publicZoneCountsCached(ctx context.Context) (map[string]int64, int64, error) {
|
||||
if tenancy.IDFromContext(ctx) > 0 {
|
||||
return r.computePublicZoneCounts(ctx)
|
||||
}
|
||||
now := time.Now()
|
||||
r.publicZoneCountsMu.Lock()
|
||||
defer r.publicZoneCountsMu.Unlock()
|
||||
@@ -203,8 +220,22 @@ func (r *Repository) publicZoneCountsCached(ctx context.Context) (map[string]int
|
||||
return copyPublicZoneCounts(r.publicZoneCounts.Counts), r.publicZoneCounts.NonPremiumTotal, nil
|
||||
}
|
||||
|
||||
counts, nonPremiumTotal, err := r.computePublicZoneCounts(ctx)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
r.publicZoneCounts = publicZoneCountCache{
|
||||
Counts: copyPublicZoneCounts(counts),
|
||||
NonPremiumTotal: nonPremiumTotal,
|
||||
ExpiresAt: now.Add(publicZoneCountCacheTTL),
|
||||
}
|
||||
return counts, nonPremiumTotal, nil
|
||||
}
|
||||
|
||||
func (r *Repository) computePublicZoneCounts(ctx context.Context) (map[string]int64, int64, error) {
|
||||
var rows []publicZoneRow
|
||||
err := r.db.WithContext(ctx).Table("rental_listings AS l").
|
||||
query := applyTenantOfferVisibility(ctx, r.db.WithContext(ctx).Table("rental_listings AS l"))
|
||||
err := query.
|
||||
Select("a.login_platform, a.haf_coin_amount, a.asset_summary, l.is_premium").
|
||||
Joins("JOIN game_accounts AS a ON a.id = l.account_id").
|
||||
Where("l.status = ? AND l.review_status = ? AND l.in_transaction = ?", "published", "approved", false).
|
||||
@@ -245,12 +276,7 @@ func (r *Repository) publicZoneCountsCached(ctx context.Context) (map[string]int
|
||||
nonPremiumTotal++
|
||||
}
|
||||
}
|
||||
r.publicZoneCounts = publicZoneCountCache{
|
||||
Counts: counts,
|
||||
NonPremiumTotal: nonPremiumTotal,
|
||||
ExpiresAt: now.Add(publicZoneCountCacheTTL),
|
||||
}
|
||||
return copyPublicZoneCounts(counts), nonPremiumTotal, nil
|
||||
return counts, nonPremiumTotal, nil
|
||||
}
|
||||
|
||||
func copyPublicZoneCounts(counts map[string]int64) map[string]int64 {
|
||||
@@ -262,10 +288,13 @@ func copyPublicZoneCounts(counts map[string]int64) map[string]int64 {
|
||||
}
|
||||
|
||||
func (r *Repository) FindPublic(ctx context.Context, id uint64, siteCode string, viewerUserID uint64) (*ListingDTO, error) {
|
||||
dto, err := r.findDTO(ctx, "l.id = ? AND l.status = ? AND l.review_status = ? AND l.in_transaction = ?", id, "published", "approved", false)
|
||||
if err != nil {
|
||||
var row listingRow
|
||||
query := applyTenantOfferVisibility(ctx, r.baseQuery(ctx)).
|
||||
Where("l.id = ? AND l.status = ? AND l.review_status = ? AND l.in_transaction = ?", id, "published", "approved", false)
|
||||
if err := query.First(&row).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dto := row.toDTO()
|
||||
if dto.IsPremium {
|
||||
canView, err := r.canViewPremium(ctx, viewerUserID)
|
||||
if err != nil {
|
||||
@@ -275,13 +304,56 @@ func (r *Repository) FindPublic(ctx context.Context, id uint64, siteCode string,
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
}
|
||||
applyPublicListingURLs(dto)
|
||||
items := []ListingDTO{*dto}
|
||||
applyPublicListingURLs(&dto)
|
||||
items := []ListingDTO{dto}
|
||||
if err := r.applyTenantOfferPrices(ctx, items); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := r.applyShelfMarkup(ctx, siteCode, items); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
*dto = items[0]
|
||||
return dto, nil
|
||||
dto = items[0]
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
func applyTenantOfferVisibility(ctx context.Context, db *gorm.DB) *gorm.DB {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID == 0 {
|
||||
return db
|
||||
}
|
||||
return db.Joins(
|
||||
"INNER JOIN tenant_listing_offers AS tlo ON tlo.listing_id = l.id AND tlo.tenant_id = ? AND tlo.display_status = 'enabled'",
|
||||
tenantID,
|
||||
)
|
||||
}
|
||||
|
||||
func (r *Repository) applyTenantOfferPrices(ctx context.Context, items []ListingDTO) error {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID == 0 || len(items) == 0 {
|
||||
return nil
|
||||
}
|
||||
ids := make([]uint64, 0, len(items))
|
||||
for _, item := range items {
|
||||
ids = append(ids, item.ID)
|
||||
}
|
||||
var offers []model.TenantListingOffer
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND listing_id IN ?", tenantID, ids).
|
||||
Find(&offers).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
prices := make(map[uint64]int64, len(offers))
|
||||
for _, offer := range offers {
|
||||
if offer.PriceCent != nil && *offer.PriceCent > 0 {
|
||||
prices[offer.ListingID] = *offer.PriceCent
|
||||
}
|
||||
}
|
||||
for index := range items {
|
||||
if price, ok := prices[items[index].ID]; ok {
|
||||
items[index].PriceCent = price
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) canViewPremium(ctx context.Context, userID uint64) (bool, error) {
|
||||
@@ -363,10 +435,13 @@ func (r *Repository) FindPublicScreenshotKey(ctx context.Context, id uint64, ind
|
||||
if index < 0 {
|
||||
return "", gorm.ErrRecordNotFound
|
||||
}
|
||||
dto, err := r.findDTO(ctx, "l.id = ? AND l.status = ? AND l.review_status = ? AND l.in_transaction = ?", id, "published", "approved", false)
|
||||
if err != nil {
|
||||
var row listingRow
|
||||
query := applyTenantOfferVisibility(ctx, r.baseQuery(ctx)).
|
||||
Where("l.id = ? AND l.status = ? AND l.review_status = ? AND l.in_transaction = ?", id, "published", "approved", false)
|
||||
if err := query.First(&row).Error; err != nil {
|
||||
return "", err
|
||||
}
|
||||
dto := row.toDTO()
|
||||
if index >= len(dto.ScreenshotURLS) {
|
||||
return "", gorm.ErrRecordNotFound
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user