From 170e279e71eff4549aa9191dd590e988a9df76d4 Mon Sep 17 00:00:00 2001 From: yml2213 Date: Fri, 25 Sep 2026 17:25:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(distribution):=20=E5=88=86=E7=AB=99?= =?UTF-8?q?=E6=8C=82=E7=89=8C=E7=9A=84=E5=8F=B7=E4=B8=BB=E6=94=B6=E5=85=A5?= =?UTF-8?q?=E8=AE=B0=E5=85=A5=E5=AF=B9=E5=BA=94=E5=88=86=E7=AB=99=E9=92=B1?= =?UTF-8?q?=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 结账和仲裁不再把分销站号主收入写入主站钱包,用户列表也只展示主站余额。 Co-authored-by: Cursor --- .../internal/modules/adminuser/repository.go | 4 ++-- backend/internal/modules/dispute/arbitration.go | 17 +++++++++++++++++ .../internal/modules/dispute/repository_test.go | 17 +++++++++++++++++ .../internal/modules/order/checkout_finalize.go | 16 ++++++++++++---- .../internal/modules/order/repository_test.go | 13 +++++++++++++ 5 files changed, 61 insertions(+), 6 deletions(-) diff --git a/backend/internal/modules/adminuser/repository.go b/backend/internal/modules/adminuser/repository.go index c4bd6dd..cf578a2 100644 --- a/backend/internal/modules/adminuser/repository.go +++ b/backend/internal/modules/adminuser/repository.go @@ -58,7 +58,7 @@ func (r *Repository) List(ctx context.Context, page, pageSize int, query ListQue Joins("LEFT JOIN (SELECT owner_id AS user_id, COUNT(*) AS listing_count FROM rental_listings GROUP BY owner_id) AS l ON l.user_id = u.id"). Joins("LEFT JOIN (SELECT user_id, COUNT(*) AS dispute_count FROM (SELECT initiator_id AS user_id FROM disputes UNION ALL SELECT target_user_id AS user_id FROM disputes) AS dispute_users GROUP BY user_id) AS d ON d.user_id = u.id"). Joins("LEFT JOIN (SELECT renter_id AS user_id, SUM(deposit_waived_amount_cent) AS deposit_free_used_cent FROM rental_orders WHERE status NOT IN ('completed', 'cancelled', 'closed') GROUP BY renter_id) AS df ON df.user_id = u.id"). - Joins("LEFT JOIN wallet_accounts AS w ON w.user_id = u.id") + Joins("LEFT JOIN wallet_accounts AS w ON w.user_id = u.id AND w.distribution_site_id = 0") listTx = applyUserFilter(listTx, query, growthConfig) err = listTx. Order("u.id DESC"). @@ -397,7 +397,7 @@ func (r *Repository) Find(ctx context.Context, userID uint64) (*UserDTO, error) Joins("LEFT JOIN (SELECT owner_id AS user_id, COUNT(*) AS listing_count FROM rental_listings GROUP BY owner_id) AS l ON l.user_id = u.id"). Joins("LEFT JOIN (SELECT user_id, COUNT(*) AS dispute_count FROM (SELECT initiator_id AS user_id FROM disputes UNION ALL SELECT target_user_id AS user_id FROM disputes) AS dispute_users GROUP BY user_id) AS d ON d.user_id = u.id"). Joins("LEFT JOIN (SELECT renter_id AS user_id, SUM(deposit_waived_amount_cent) AS deposit_free_used_cent FROM rental_orders WHERE status NOT IN ('completed', 'cancelled', 'closed') GROUP BY renter_id) AS df ON df.user_id = u.id"). - Joins("LEFT JOIN wallet_accounts AS w ON w.user_id = u.id"). + Joins("LEFT JOIN wallet_accounts AS w ON w.user_id = u.id AND w.distribution_site_id = 0"). Where("u.id = ?", userID). First(&row).Error if err != nil { diff --git a/backend/internal/modules/dispute/arbitration.go b/backend/internal/modules/dispute/arbitration.go index 7545229..2c954d8 100644 --- a/backend/internal/modules/dispute/arbitration.go +++ b/backend/internal/modules/dispute/arbitration.go @@ -99,6 +99,7 @@ func (r *Repository) Arbitrate(ctx context.Context, adminID uint64, id uint64, r ); err != nil { return err } + stampOwnerIncomeSite(&settlement, &listing) if err := wallet.AppendEntries(tx, settlement.Entries...); err != nil { return err } @@ -442,6 +443,22 @@ func newArbitrationSettlement(order model.RentalOrder, renterFrozenBalanceCent i return settlement } +func stampOwnerIncomeSite(settlement *arbitrationSettlement, listing *model.RentalListing) { + if settlement == nil { + return + } + siteID := uint64(0) + if listing != nil && listing.DistributionSiteID != nil { + siteID = *listing.DistributionSiteID + } + for i := range settlement.Entries { + entry := &settlement.Entries[i] + if entry.Direction == "in" && entry.BalanceType == "available" && entry.BizType == "arbitration_owner_income" { + entry.DistributionSiteID = siteID + } + } +} + func (settlement *arbitrationSettlement) addOwnerIncome(order model.RentalOrder, amountCent int64, remark string) { if settlement == nil || amountCent <= 0 { return diff --git a/backend/internal/modules/dispute/repository_test.go b/backend/internal/modules/dispute/repository_test.go index e2b4e3f..2eb4f2c 100644 --- a/backend/internal/modules/dispute/repository_test.go +++ b/backend/internal/modules/dispute/repository_test.go @@ -9,6 +9,7 @@ import ( "hfb_sys/backend/internal/database" "hfb_sys/backend/internal/model" + "hfb_sys/backend/internal/modules/wallet" "gorm.io/datatypes" "gorm.io/gorm" @@ -753,3 +754,19 @@ func TestApplyAdminListFiltersSmartRouting(t *testing.T) { }) } } + +func TestStampOwnerIncomeSiteUsesListingSite(t *testing.T) { + siteID := uint64(3) + orderID := uint64(9) + settlement := arbitrationSettlement{Entries: []wallet.Entry{ + {UserID: 1, OrderID: &orderID, Direction: "in", AmountCent: 100, BalanceType: "available", BizType: "arbitration_owner_income"}, + {UserID: 2, OrderID: &orderID, Direction: "out", AmountCent: 50, BalanceType: "frozen", BizType: "arbitration_release_frozen"}, + }} + stampOwnerIncomeSite(&settlement, &model.RentalListing{DistributionSiteID: &siteID}) + if settlement.Entries[0].DistributionSiteID != 3 { + t.Fatalf("号主入账站点 = %d, want 3", settlement.Entries[0].DistributionSiteID) + } + if settlement.Entries[1].DistributionSiteID != 0 { + t.Fatalf("租客解冻站点 = %d, want 0", settlement.Entries[1].DistributionSiteID) + } +} diff --git a/backend/internal/modules/order/checkout_finalize.go b/backend/internal/modules/order/checkout_finalize.go index 94a47b0..3ba5083 100644 --- a/backend/internal/modules/order/checkout_finalize.go +++ b/backend/internal/modules/order/checkout_finalize.go @@ -33,7 +33,7 @@ func (r *Repository) finalizeCheckout(tx *gorm.DB, order *model.RentalOrder, che if settlement.ShortfallCent > 0 { return nil, ErrCheckoutDepositShortfall } - if err := appendCheckoutOwnerIncome(tx, order, settlement); err != nil { + if err := appendCheckoutOwnerIncome(tx, order, listing, settlement); err != nil { return nil, err } refund, err := r.prepareCheckoutRefund(order, settlement) @@ -65,7 +65,14 @@ func (r *Repository) finalizeCheckout(tx *gorm.DB, order *model.RentalOrder, che return refund, nil } -func appendCheckoutOwnerIncome(tx *gorm.DB, order *model.RentalOrder, settlement checkoutSettlement) error { +func ownerWalletSiteID(listing *model.RentalListing) uint64 { + if listing != nil && listing.DistributionSiteID != nil { + return *listing.DistributionSiteID + } + return 0 +} + +func appendCheckoutOwnerIncome(tx *gorm.DB, order *model.RentalOrder, listing *model.RentalListing, settlement checkoutSettlement) error { if isPlatformSettlementOrder(*order) { amountCent := settlement.OwnerRentIncomeCent + settlement.DepositCompensationCent order.OfflineSettlementAmountCent = amountCent @@ -77,11 +84,12 @@ func appendCheckoutOwnerIncome(tx *gorm.DB, order *model.RentalOrder, settlement return nil } orderID := order.ID + siteID := ownerWalletSiteID(listing) var ownerEntries []wallet.Entry if settlement.OwnerRentIncomeCent > 0 { ownerEntries = append(ownerEntries, wallet.Entry{ UserID: order.OwnerID, - DistributionSiteID: 0, + DistributionSiteID: siteID, OrderID: &orderID, Direction: "in", AmountCent: settlement.OwnerRentIncomeCent, @@ -94,7 +102,7 @@ func appendCheckoutOwnerIncome(tx *gorm.DB, order *model.RentalOrder, settlement if settlement.DepositCompensationCent > 0 { ownerEntries = append(ownerEntries, wallet.Entry{ UserID: order.OwnerID, - DistributionSiteID: 0, + DistributionSiteID: siteID, OrderID: &orderID, Direction: "in", AmountCent: settlement.DepositCompensationCent, diff --git a/backend/internal/modules/order/repository_test.go b/backend/internal/modules/order/repository_test.go index 6b6c07c..980ee38 100644 --- a/backend/internal/modules/order/repository_test.go +++ b/backend/internal/modules/order/repository_test.go @@ -694,3 +694,16 @@ func TestApplyCheckoutPriceView_RoleIsolationAndPlatformFeeHidden(t *testing.T) t.Fatalf("租客视角必须展示 RenterRefundAmountCent, 实际为: %v", renterDTO.RenterRefundAmountCent) } } + +func TestOwnerWalletSiteIDFollowsListingSite(t *testing.T) { + if got := ownerWalletSiteID(nil); got != 0 { + t.Fatalf("空挂牌站点 = %d, want 0", got) + } + if got := ownerWalletSiteID(&model.RentalListing{}); got != 0 { + t.Fatalf("主站挂牌站点 = %d, want 0", got) + } + siteID := uint64(3) + if got := ownerWalletSiteID(&model.RentalListing{DistributionSiteID: &siteID}); got != 3 { + t.Fatalf("分站挂牌站点 = %d, want 3", got) + } +}