fix(withdrawal): 修复分站审核凭证与历史订单可见性

This commit is contained in:
yml
2026-11-27 21:37:34 +08:00
parent 17ca2f6672
commit 06dbf52f08
6 changed files with 283 additions and 22 deletions
@@ -274,6 +274,101 @@ func TestAdminReviewContextScopesIncomeByWithdrawalSite(t *testing.T) {
}
}
func TestAdminReviewContextFindsLegacyOrderByListingSite(t *testing.T) {
f := newWithdrawalFixture(t)
tenant := model.Tenant{ID: 101, Code: "dachui", Name: "大锤商行", Status: "active"}
if err := f.db.Create(&tenant).Error; err != nil {
t.Fatal(err)
}
site := model.DistributionSite{
SiteCode: "gz_dist",
SiteName: "天域商行",
Domain: "tysh.example.com",
Status: "active",
}
if err := f.db.Create(&site).Error; err != nil {
t.Fatal(err)
}
renter := model.User{Phone: "13800000004", Nickname: "legacy-renter", Status: "active"}
if err := f.db.Create(&renter).Error; err != nil {
t.Fatal(err)
}
account := model.GameAccount{OwnerID: f.userID, ServerRegion: "cn", LoginPlatform: "pc", Title: "历史天域订单"}
if err := f.db.Create(&account).Error; err != nil {
t.Fatal(err)
}
listing := model.RentalListing{
ListingNo: "TIANYU-LEGACY-1",
AccountID: account.ID,
OwnerID: f.userID,
Status: "active",
DistributionSiteID: &site.ID,
}
if err := f.db.Create(&listing).Error; err != nil {
t.Fatal(err)
}
order := model.RentalOrder{
OrderNo: "RO-TIANYU-LEGACY-1",
ListingID: listing.ID,
AccountID: account.ID,
OwnerID: f.userID,
RenterID: renter.ID,
Status: "completed",
SettlementStatus: "settled",
SettlementMode: "owner_wallet",
RentAmountCent: 100,
}
if err := f.db.Create(&order).Error; err != nil {
t.Fatal(err)
}
if err := f.db.Create(&model.WalletLedger{
LedgerNo: "WL-TIANYU-LEGACY-1",
TenantID: 0,
UserID: f.userID,
DistributionSiteID: site.ID,
OrderID: &order.ID,
Direction: "in",
AmountCent: 100,
BalanceType: "available",
BizType: "owner_income",
BizNo: order.OrderNo,
}).Error; err != nil {
t.Fatal(err)
}
current := model.WithdrawalRequest{
WithdrawNo: "WD-TIANYU-LEGACY-1",
TenantID: 0,
UserID: f.userID,
DistributionSiteID: site.ID,
AmountCent: 100,
ActualAmountCent: 100,
AccountType: "bank",
AccountName: "Test User",
AccountNo: "****0000",
Status: "pending",
}
if err := f.db.Create(&current).Error; err != nil {
t.Fatal(err)
}
ctx := tenancy.WithScope(context.Background(), tenancy.Scope{
TenantID: tenant.ID, TenantCode: tenant.Code,
})
contextDTO, err := f.repo.AdminReviewContext(ctx, current.ID, WithdrawalReviewContextQuery{PageSize: 10})
if err != nil {
t.Fatal(err)
}
if contextDTO.Summary.OwnerOrderCount != 1 || len(contextDTO.Orders) != 1 {
t.Fatalf("legacy order should fall back to listing site: %+v", contextDTO)
}
if contextDTO.Orders[0].OrderNo != order.OrderNo {
t.Fatalf("legacy order = %+v", contextDTO.Orders[0])
}
if contextDTO.Summary.WalletActualIncomeCent != 100 {
t.Fatalf("legacy wallet income = %d, want 100", contextDTO.Summary.WalletActualIncomeCent)
}
}
func (f withdrawalFixture) create(t *testing.T, amountCent int64) *WithdrawalDTO {
t.Helper()
dto, err := f.repo.Create(context.Background(), f.userID, CreateWithdrawalRequest{
@@ -82,6 +82,20 @@ func scopeWithdrawalSite(db *gorm.DB, column string, siteID uint64) *gorm.DB {
return db.Where("(" + column + " IS NULL OR " + column + " = 0)")
}
// scopeWithdrawalOrderSite 兼容历史订单未写 distribution_site_id 的数据:
// 订单快照为空时,用商品所属站点判断订单归属。
func scopeWithdrawalOrderSite(db *gorm.DB, siteID uint64) *gorm.DB {
if siteID > 0 {
return db.Where(`(
o.distribution_site_id = ? OR (
(o.distribution_site_id IS NULL OR o.distribution_site_id = 0)
AND l.distribution_site_id = ?
)
)`, siteID, siteID)
}
return db.Where("(o.distribution_site_id IS NULL OR o.distribution_site_id = 0)")
}
func scopeAdminTenant(ctx context.Context, db *gorm.DB, column string) *gorm.DB {
if tenancy.HasAdminAllTenantScope(ctx) {
return db
@@ -105,13 +119,15 @@ func (r *Repository) withdrawalReviewSummary(ctx context.Context, userID, siteID
FinalizedOrderCount int64
PlatformManagedIncomeCent int64
}
orderQuery := scopeWithdrawalSite(db.Table("rental_orders"), "distribution_site_id", siteID)
orderQuery := db.Table("rental_orders AS o").
Joins("JOIN rental_listings AS l ON l.id = o.listing_id")
orderQuery = scopeWithdrawalOrderSite(orderQuery, siteID)
if err := orderQuery.
Select(`COUNT(*) AS owner_order_count,
COALESCE(SUM(CASE WHEN settlement_status IN ('settled','arbitrated') THEN 1 ELSE 0 END), 0) AS finalized_order_count,
COALESCE(SUM(CASE WHEN settlement_mode = 'platform_managed' AND settlement_status IN ('settled','arbitrated')
THEN COALESCE(offline_settlement_amount_cent, 0) ELSE 0 END), 0) AS platform_managed_income_cent`).
Where("owner_id = ?", userID).
COALESCE(SUM(CASE WHEN o.settlement_status IN ('settled','arbitrated') THEN 1 ELSE 0 END), 0) AS finalized_order_count,
COALESCE(SUM(CASE WHEN o.settlement_mode = 'platform_managed' AND o.settlement_status IN ('settled','arbitrated')
THEN COALESCE(o.offline_settlement_amount_cent, 0) ELSE 0 END), 0) AS platform_managed_income_cent`).
Where("o.owner_id = ?", userID).
Scan(&orderSummary).Error; err != nil {
return result, err
}
@@ -119,9 +135,12 @@ func (r *Repository) withdrawalReviewSummary(ctx context.Context, userID, siteID
var walletIncome int64
walletQuery := scopeWithdrawalSite(db.Table("wallet_ledger AS wl"), "wl.distribution_site_id", siteID)
walletQuery = scopeAdminTenant(ctx, walletQuery, "wl.tenant_id")
walletQuery = walletQuery.
Joins("JOIN rental_orders AS o ON o.id = wl.order_id").
Joins("JOIN rental_listings AS l ON l.id = o.listing_id")
walletQuery = scopeWithdrawalOrderSite(walletQuery, siteID)
if err := walletQuery.
Select("COALESCE(SUM(wl.amount_cent), 0)").
Joins("JOIN rental_orders AS o ON o.id = wl.order_id").
Where("wl.user_id = ? AND o.owner_id = ? AND o.settlement_mode <> ?", userID, userID, "platform_managed").
Where("wl.direction = ? AND wl.biz_type IN ?", "in", ownerIncomeBizTypes).
Scan(&walletIncome).Error; err != nil {
@@ -217,13 +236,14 @@ func (r *Repository) withdrawalReviewOrders(ctx context.Context, userID, siteID
var rows []orderRow
offset := (query.OrderPage - 1) * query.PageSize
orderQuery := scopeWithdrawalSite(r.db.WithContext(ctx), "o.distribution_site_id", siteID)
if err := orderQuery.
orderQuery := r.db.WithContext(ctx).
Table("rental_orders AS o").
Joins("JOIN rental_listings AS l ON l.id = o.listing_id")
orderQuery = scopeWithdrawalOrderSite(orderQuery, siteID)
if err := orderQuery.
Select(`o.id, o.order_no, l.listing_no, a.title, o.status, o.settlement_status, o.settlement_mode,
o.rent_amount_cent, COALESCE(o.offline_settlement_amount_cent, 0) AS offline_settlement_amount_cent,
o.created_at, o.settled_at`).
Joins("JOIN rental_listings AS l ON l.id = o.listing_id").
Joins("JOIN game_accounts AS a ON a.id = o.account_id").
Where("o.owner_id = ?", userID).
Order("o.id DESC").