fix(pickup): 支持天域分销货源提号结算
This commit is contained in:
@@ -56,6 +56,47 @@ func seedPickupTenantListing(t *testing.T, db *gorm.DB, tenantID uint64, suffix
|
||||
return listing
|
||||
}
|
||||
|
||||
func seedPickupDistributionListing(t *testing.T, db *gorm.DB, tenantID, siteID uint64, suffix string) model.RentalListing {
|
||||
t.Helper()
|
||||
owner := model.User{
|
||||
TenantID: tenantID,
|
||||
DistributionSiteID: siteID,
|
||||
Phone: "1890000" + suffix,
|
||||
Nickname: "分销号主" + suffix,
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(&owner).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
account := model.GameAccount{
|
||||
SourceTenantID: tenantID,
|
||||
OwnerID: owner.ID,
|
||||
ServerRegion: "测试区",
|
||||
LoginPlatform: "微信",
|
||||
Title: "天域账号" + suffix,
|
||||
Status: "published",
|
||||
}
|
||||
if err := db.Create(&account).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
listing := model.RentalListing{
|
||||
SourceTenantID: tenantID,
|
||||
ListingNo: "LST-PICKUP-DIST-" + suffix,
|
||||
AccountID: account.ID,
|
||||
OwnerID: owner.ID,
|
||||
PriceCent: 12000,
|
||||
Status: "published",
|
||||
ReviewStatus: "approved",
|
||||
HandoffMode: "owner",
|
||||
SettlementMode: "owner_wallet",
|
||||
DistributionSiteID: &siteID,
|
||||
}
|
||||
if err := db.Create(&listing).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return listing
|
||||
}
|
||||
|
||||
func TestPickupTenantScopeSupportsCrossTenantCreationAndSeparateLists(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
tenants := []model.Tenant{
|
||||
@@ -150,6 +191,110 @@ func TestPickupTenantScopeSupportsCrossTenantCreationAndSeparateLists(t *testing
|
||||
}
|
||||
}
|
||||
|
||||
func TestPickupSupportsSharedDistributionSourceAcrossTenants(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
tenants := []model.Tenant{
|
||||
{Code: "pickup_dist_source", Name: "货源站", Status: "active"},
|
||||
{Code: "pickup_dist_a", Name: "提号A站", Status: "active"},
|
||||
{Code: "pickup_dist_b", Name: "提号B站", Status: "active"},
|
||||
{Code: "pickup_dist_other", Name: "未铺货站", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&tenants).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
site := model.DistributionSite{
|
||||
SiteCode: "tianyu_pickup",
|
||||
SiteName: "天域商行",
|
||||
Domain: "tianyu-pickup.example.com",
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(&site).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sourceTenant, tenantA, tenantB, otherTenant := tenants[0], tenants[1], tenants[2], tenants[3]
|
||||
listing := seedPickupDistributionListing(t, db, sourceTenant.ID, site.ID, "01")
|
||||
listingNumber := "202611230328"
|
||||
if err := db.Model(&listing).Update("listing_no", listingNumber).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&[]model.TenantListingOffer{
|
||||
{TenantID: tenantA.ID, ListingID: listing.ID, DisplayStatus: "enabled"},
|
||||
{TenantID: tenantB.ID, ListingID: listing.ID, DisplayStatus: "enabled"},
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
repo := NewRepository(db)
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
ctx context.Context
|
||||
crossTenant bool
|
||||
}{
|
||||
{name: "A站", ctx: pickupTenantContext(tenantA.ID), crossTenant: true},
|
||||
{name: "B站", ctx: pickupTenantContext(tenantB.ID), crossTenant: true},
|
||||
} {
|
||||
available, err := repo.ListAvailableListings(tc.ctx, AvailableListingQuery{
|
||||
Keyword: listingNumber,
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("%s 查询可提号商品: %v", tc.name, err)
|
||||
}
|
||||
if len(available.Items) != 1 {
|
||||
t.Fatalf("%s 可提号商品 = %+v, want 天域商品", tc.name, available.Items)
|
||||
}
|
||||
item := available.Items[0]
|
||||
if item.ID != listing.ID ||
|
||||
item.SourceDistributionSiteID != site.ID ||
|
||||
item.SourceDistributionSiteName != site.SiteName ||
|
||||
item.IsCrossTenant != tc.crossTenant {
|
||||
t.Fatalf("%s 天域货源快照 = %+v", tc.name, item)
|
||||
}
|
||||
}
|
||||
|
||||
otherAvailable, err := repo.ListAvailableListings(pickupTenantContext(otherTenant.ID), AvailableListingQuery{Page: 1, PageSize: 20})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(otherAvailable.Items) != 0 {
|
||||
t.Fatalf("未铺货经营租户看到了天域商品: %+v", otherAvailable.Items)
|
||||
}
|
||||
|
||||
pickup, err := repo.Create(pickupTenantContext(tenantB.ID), CreateRequest{ListingID: listing.ID}, 9, auditlog.Meta{})
|
||||
if err != nil {
|
||||
t.Fatalf("从 B 站创建天域货源提号: %v", err)
|
||||
}
|
||||
if _, err := repo.Complete(pickupTenantContext(tenantB.ID), pickup.ID, CompleteRequest{
|
||||
SettleAmountCent: 8000,
|
||||
}, 9, auditlog.Meta{}); err != nil {
|
||||
t.Fatalf("完成天域货源提号: %v", err)
|
||||
}
|
||||
var ledger model.WalletLedger
|
||||
if err := db.Where("biz_type = ? AND biz_no = ?", "admin_pickup_settle", pickup.PickupNo).First(&ledger).Error; err != nil {
|
||||
t.Fatalf("查询天域货源结算流水: %v", err)
|
||||
}
|
||||
if ledger.TenantID != sourceTenant.ID || ledger.DistributionSiteID != site.ID {
|
||||
t.Fatalf("天域货源结算流水归属 = %+v, want tenant=%d site=%d", ledger, sourceTenant.ID, site.ID)
|
||||
}
|
||||
|
||||
adjustedSettleAmount := int64(9000)
|
||||
if _, err := repo.CreateFinancialAdjustment(pickupTenantContext(tenantB.ID), pickup.ID, FinancialAdjustmentRequest{
|
||||
SettleAmountCent: &adjustedSettleAmount,
|
||||
Reason: "补发天域货源结算差额",
|
||||
}, 9, auditlog.Meta{}); err != nil {
|
||||
t.Fatalf("创建天域货源结算补款: %v", err)
|
||||
}
|
||||
var adjustmentLedger model.WalletLedger
|
||||
if err := db.Where("biz_type = ? AND biz_no LIKE ?", "admin_pickup_adjustment", pickup.PickupNo+"-A%").
|
||||
First(&adjustmentLedger).Error; err != nil {
|
||||
t.Fatalf("查询天域货源补款流水: %v", err)
|
||||
}
|
||||
if adjustmentLedger.TenantID != sourceTenant.ID || adjustmentLedger.DistributionSiteID != site.ID {
|
||||
t.Fatalf("天域货源补款流水归属 = %+v, want tenant=%d site=%d", adjustmentLedger, sourceTenant.ID, site.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPickupShopsAreSeparatedByTenant(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
tenants := []model.Tenant{
|
||||
|
||||
Reference in New Issue
Block a user