feat(listing): 商品列表展示来源商行
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
type ListingDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
SourceTenantID uint64 `json:"source_tenant_id"`
|
||||
SourceMerchantName string `json:"source_merchant_name,omitempty"`
|
||||
ListingNo string `json:"listing_no"`
|
||||
AccountID uint64 `json:"account_id"`
|
||||
OwnerID uint64 `json:"owner_id"`
|
||||
|
||||
@@ -23,6 +23,7 @@ type listingRow struct {
|
||||
IsExternalUpload bool `gorm:"column:is_external_upload"`
|
||||
DistributionSiteCode string `gorm:"column:distribution_site_code"`
|
||||
DistributionSiteName string `gorm:"column:distribution_site_name"`
|
||||
SourceMerchantName string `gorm:"column:source_merchant_name"`
|
||||
Description string
|
||||
GameName string
|
||||
ServerRegion string
|
||||
@@ -72,6 +73,7 @@ func applyPublicListingURLs(item *ListingDTO) {
|
||||
return
|
||||
}
|
||||
item.SourceChannel = ""
|
||||
item.SourceMerchantName = ""
|
||||
item.IsExternalUpload = false
|
||||
rawScreenshotURLs := item.ScreenshotURLS
|
||||
item.ScreenshotURLS = publicScreenshotURLs(item.ID, item.ScreenshotURLS, item.Status, item.ReviewStatus)
|
||||
@@ -138,6 +140,7 @@ func applySellerListingPrice(item *ListingDTO) {
|
||||
return
|
||||
}
|
||||
item.SourceChannel = ""
|
||||
item.SourceMerchantName = ""
|
||||
item.IsExternalUpload = false
|
||||
if item.AssetSummary == nil {
|
||||
return
|
||||
@@ -198,6 +201,7 @@ func (row listingRow) toDTO() ListingDTO {
|
||||
DistributionSiteID: row.DistributionSiteID,
|
||||
DistributionSiteCode: row.DistributionSiteCode,
|
||||
DistributionSiteName: row.DistributionSiteName,
|
||||
SourceMerchantName: row.SourceMerchantName,
|
||||
IsPremium: row.IsPremium,
|
||||
CompensationPlan: normalizedCompensationPlan(row.CompensationPlan),
|
||||
InsuranceFeeBps: row.InsuranceFeeBps,
|
||||
|
||||
@@ -245,6 +245,7 @@ func (r *Repository) baseQuery(ctx context.Context) *gorm.DB {
|
||||
CASE WHEN lu.id IS NULL THEN 0 ELSE 1 END AS is_external_upload,
|
||||
COALESCE(src_site.site_code, '') AS distribution_site_code,
|
||||
COALESCE(src_site.site_name, '') AS distribution_site_name,
|
||||
COALESCE(NULLIF(src_site.site_name, ''), NULLIF(src_tenant.name, ''), '') AS source_merchant_name,
|
||||
CASE
|
||||
WHEN l.distribution_site_id IS NOT NULL AND src_site.id IS NOT NULL THEN CONCAT('分销站 - ', src_site.site_name, ' (', src_site.site_code, ')')
|
||||
WHEN lu.id IS NULL THEN ?
|
||||
@@ -254,6 +255,7 @@ func (r *Repository) baseQuery(ctx context.Context) *gorm.DB {
|
||||
Joins("JOIN game_accounts AS a ON a.id = l.account_id").
|
||||
Joins("LEFT JOIN users AS u ON u.id = l.owner_id").
|
||||
Joins("LEFT JOIN distribution_sites AS src_site ON src_site.id = l.distribution_site_id").
|
||||
Joins("LEFT JOIN tenants AS src_tenant ON src_tenant.id = l.source_tenant_id").
|
||||
// 逐商品按 listing_id 索引定位最后一次导入,避免每次列表查询都对全量导入记录分组。
|
||||
Joins(`LEFT JOIN listing_uploads AS lu ON lu.id = (
|
||||
SELECT MAX(lu_latest.id) FROM listing_uploads AS lu_latest WHERE lu_latest.listing_id = l.id
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
package listing
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
)
|
||||
|
||||
func TestAdminListIncludesSourceMerchantName(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
if err := database.MigrateListingLifecycleTestSchema(db); err != nil {
|
||||
t.Fatalf("failed to migrate test db: %v", err)
|
||||
}
|
||||
|
||||
tenants := []model.Tenant{
|
||||
{Code: "source_merchant_dachui", Name: "大锤商行", Status: "active"},
|
||||
{Code: "source_merchant_jieyou", Name: "解忧商行", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&tenants).Error; err != nil {
|
||||
t.Fatalf("failed to create tenants: %v", err)
|
||||
}
|
||||
site := model.DistributionSite{
|
||||
SiteCode: "source_merchant_tianyu",
|
||||
SiteName: "天域商行",
|
||||
Domain: "source-merchant-tianyu.example.com",
|
||||
Status: "active",
|
||||
DisplayAccountEnabled: true,
|
||||
}
|
||||
if err := db.Create(&site).Error; err != nil {
|
||||
t.Fatalf("failed to create distribution site: %v", err)
|
||||
}
|
||||
users := []model.User{
|
||||
{TenantID: tenants[0].ID, Phone: "18800000901", RealnameStatus: "verified", Status: "active"},
|
||||
{TenantID: tenants[1].ID, Phone: "18800000902", RealnameStatus: "verified", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&users).Error; err != nil {
|
||||
t.Fatalf("failed to create users: %v", err)
|
||||
}
|
||||
|
||||
createListing := func(sourceTenantID, ownerID uint64, siteID *uint64, listingNo string) model.RentalListing {
|
||||
account := model.GameAccount{
|
||||
SourceTenantID: sourceTenantID,
|
||||
OwnerID: ownerID,
|
||||
GameName: "delta_force",
|
||||
ServerRegion: "QQ",
|
||||
LoginPlatform: "扫码",
|
||||
Title: listingNo,
|
||||
Status: "draft",
|
||||
}
|
||||
if err := db.Create(&account).Error; err != nil {
|
||||
t.Fatalf("failed to create account %s: %v", listingNo, err)
|
||||
}
|
||||
listing := model.RentalListing{
|
||||
SourceTenantID: sourceTenantID,
|
||||
ListingNo: listingNo,
|
||||
AccountID: account.ID,
|
||||
OwnerID: ownerID,
|
||||
PriceCent: 10000,
|
||||
Status: "draft",
|
||||
ReviewStatus: "pending",
|
||||
DistributionSiteID: siteID,
|
||||
}
|
||||
if err := db.Create(&listing).Error; err != nil {
|
||||
t.Fatalf("failed to create listing %s: %v", listingNo, err)
|
||||
}
|
||||
return listing
|
||||
}
|
||||
|
||||
dachuiMain := createListing(tenants[0].ID, users[0].ID, nil, "SM-DACHUI")
|
||||
jieyouMain := createListing(tenants[1].ID, users[1].ID, nil, "SM-JIEYOU")
|
||||
tianyuOwned := createListing(tenants[0].ID, users[0].ID, &site.ID, "SM-TIANYU")
|
||||
tianyuLegacy := createListing(0, users[0].ID, &site.ID, "SM-TIANYU-OLD")
|
||||
|
||||
repo := NewRepository(db, nil)
|
||||
result, err := repo.ListAdmin(t.Context(), AdminListQuery{Page: 1, PageSize: 20})
|
||||
if err != nil {
|
||||
t.Fatalf("ListAdmin() error = %v", err)
|
||||
}
|
||||
if result.Total != 4 || len(result.Items) != 4 {
|
||||
t.Fatalf("ListAdmin() total=%d items=%d, want 4", result.Total, len(result.Items))
|
||||
}
|
||||
|
||||
got := make(map[uint64]string, len(result.Items))
|
||||
for _, item := range result.Items {
|
||||
got[item.ID] = item.SourceMerchantName
|
||||
}
|
||||
want := map[uint64]string{
|
||||
dachuiMain.ID: "大锤商行",
|
||||
jieyouMain.ID: "解忧商行",
|
||||
tianyuOwned.ID: "天域商行",
|
||||
tianyuLegacy.ID: "天域商行",
|
||||
}
|
||||
for listingID, merchantName := range want {
|
||||
if got[listingID] != merchantName {
|
||||
t.Fatalf("listing %d source merchant = %q, want %q", listingID, got[listingID], merchantName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublicAndSellerListingsHideSourceMerchantName(t *testing.T) {
|
||||
publicItem := ListingDTO{SourceMerchantName: "大锤商行"}
|
||||
applyPublicListingURLs(&publicItem)
|
||||
if publicItem.SourceMerchantName != "" {
|
||||
t.Fatalf("public listing leaked source merchant name: %q", publicItem.SourceMerchantName)
|
||||
}
|
||||
|
||||
sellerItem := ListingDTO{SourceMerchantName: "解忧商行"}
|
||||
applySellerListingPrice(&sellerItem)
|
||||
if sellerItem.SourceMerchantName != "" {
|
||||
t.Fatalf("seller listing leaked source merchant name: %q", sellerItem.SourceMerchantName)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user