feat(distribution): 优化货架批量操作、结算大事务、大盘与前台多端解析缓存及货架分页
This commit is contained in:
@@ -1287,3 +1287,96 @@ func TestSyncSiteAccountsOptimizations(t *testing.T) {
|
||||
}
|
||||
repo.siteSyncLocks.Delete(site.ID)
|
||||
}
|
||||
|
||||
func TestBatchOperationsAndCachingOptimizations(t *testing.T) {
|
||||
db := distributionTestDB(t)
|
||||
repo := NewRepository(db)
|
||||
|
||||
site, err := repo.CreateSite(t.Context(), CreateSiteRequest{
|
||||
SiteCode: "opt_batch_site",
|
||||
SiteName: "批量优化分站",
|
||||
Domain: "opt-batch.hfb.local",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
acc1 := model.GameAccount{Title: "王者生化账号-1", GameName: "cfhd", ServerRegion: "东部", HafCoinAmount: 50_000_000, Status: "active"}
|
||||
acc2 := model.GameAccount{Title: "王者爆破账号-2", GameName: "cfhd", ServerRegion: "北部", HafCoinAmount: 60_000_000, Status: "active"}
|
||||
db.Create(&acc1)
|
||||
db.Create(&acc2)
|
||||
|
||||
l1 := model.RentalListing{ListingNo: "L-BATCH-1", AccountID: acc1.ID, OwnerID: 1, PriceCent: 3000, Status: "published", ReviewStatus: "approved"}
|
||||
l2 := model.RentalListing{ListingNo: "L-BATCH-2", AccountID: acc2.ID, OwnerID: 1, PriceCent: 4000, Status: "published", ReviewStatus: "approved"}
|
||||
db.Create(&l1)
|
||||
db.Create(&l2)
|
||||
|
||||
// 同步到货架
|
||||
_, err = repo.SyncSiteAccounts(t.Context(), 0, site.ID, SyncSiteAccountsRequest{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var dsaList []model.DistributionSiteAccount
|
||||
db.Where("site_id = ?", site.ID).Find(&dsaList)
|
||||
if len(dsaList) != 2 {
|
||||
t.Fatalf("expected 2 accounts on shelf, got %d", len(dsaList))
|
||||
}
|
||||
|
||||
// 1. 验证 BatchUpdateAccountDisplay 批量下架与恢复展示
|
||||
if err := repo.BatchUpdateAccountDisplay(t.Context(), 0, site.ID, BatchUpdateAccountDisplayRequest{
|
||||
DisplayIDs: []uint64{dsaList[0].ID, dsaList[1].ID},
|
||||
TargetStatus: "disabled",
|
||||
}); err != nil {
|
||||
t.Fatalf("BatchUpdateAccountDisplay disabled failed: %v", err)
|
||||
}
|
||||
var countDisabled int64
|
||||
db.Model(&model.DistributionSiteAccount{}).Where("site_id = ? AND display_status = 'disabled'", site.ID).Count(&countDisabled)
|
||||
if countDisabled != 2 {
|
||||
t.Fatalf("expected 2 disabled, got %d", countDisabled)
|
||||
}
|
||||
|
||||
// 批量恢复
|
||||
if err := repo.BatchUpdateAccountDisplay(t.Context(), 0, site.ID, BatchUpdateAccountDisplayRequest{
|
||||
ListingIDs: []uint64{l1.ID, l2.ID},
|
||||
TargetStatus: "enabled",
|
||||
}); err != nil {
|
||||
t.Fatalf("BatchUpdateAccountDisplay enabled failed: %v", err)
|
||||
}
|
||||
var countEnabled int64
|
||||
db.Model(&model.DistributionSiteAccount{}).Where("site_id = ? AND display_status = 'enabled'", site.ID).Count(&countEnabled)
|
||||
if countEnabled != 2 {
|
||||
t.Fatalf("expected 2 enabled, got %d", countEnabled)
|
||||
}
|
||||
|
||||
// 2. 验证 BatchUpdateAccountMarkup 支持原子提交 Items 差异化加价
|
||||
if err := repo.BatchUpdateAccountMarkup(t.Context(), 0, site.ID, BatchUpdateAccountMarkupRequest{
|
||||
Items: []AccountMarkupItem{
|
||||
{ListingID: l1.ID, ShelfRatioDelta: 12.0},
|
||||
{ListingID: l2.ID, ShelfRatioDelta: 18.5},
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatalf("BatchUpdateAccountMarkup with Items failed: %v", err)
|
||||
}
|
||||
|
||||
var dsa1 model.DistributionSiteAccount
|
||||
var dsa2 model.DistributionSiteAccount
|
||||
db.Where("site_id = ? AND listing_id = ?", site.ID, l1.ID).First(&dsa1)
|
||||
db.Where("site_id = ? AND listing_id = ?", site.ID, l2.ID).First(&dsa2)
|
||||
if dsa1.ShelfRatioDelta != 12.0 || dsa2.ShelfRatioDelta != 18.5 {
|
||||
t.Fatalf("items markup mismatch: dsa1=%v, dsa2=%v", dsa1.ShelfRatioDelta, dsa2.ShelfRatioDelta)
|
||||
}
|
||||
|
||||
// 3. 验证 ListAccountDisplaysPaged 分页与关键字筛选
|
||||
pagedResult, err := repo.ListAccountDisplaysPaged(t.Context(), site.ID, AccountDisplayQuery{
|
||||
Page: 1,
|
||||
PageSize: 10,
|
||||
Keyword: "生化",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("ListAccountDisplaysPaged failed: %v", err)
|
||||
}
|
||||
if pagedResult.Total != 1 || len(pagedResult.Items) != 1 || pagedResult.Items[0].ListingNo != "L-BATCH-1" {
|
||||
t.Fatalf("paged keyword mismatch: total=%d, items=%+v", pagedResult.Total, pagedResult.Items)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user