From 9283a6814380fd90105949df6be0e4a26adaecb2 Mon Sep 17 00:00:00 2001 From: yml Date: Tue, 24 Nov 2026 19:02:31 +0800 Subject: [PATCH] =?UTF-8?q?fix(order):=20=E4=BF=AE=E5=A4=8D=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E6=89=8B=E6=9C=BA=E5=8F=B7=E7=AD=9B=E9=80=89=E6=8A=A5?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/internal/modules/order/queries.go | 5 +- .../order/repository_integration_test.go | 70 +++++++++++++++++++ .../internal/modules/order/repository_test.go | 2 +- 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/backend/internal/modules/order/queries.go b/backend/internal/modules/order/queries.go index 12437b35..c5647c6b 100644 --- a/backend/internal/modules/order/queries.go +++ b/backend/internal/modules/order/queries.go @@ -337,10 +337,11 @@ func applyAdminOrderFilters(db *gorm.DB, query AdminOrderQuery) *gorm.DB { if keyword := strings.TrimSpace(query.Keyword); keyword != "" { upper := strings.ToUpper(keyword) if isPhoneKeyword(keyword) { - // 先由 users(phone) 唯一索引解析用户,再让订单表 owner_id/renter_id 索引驱动。 + // 同一手机号可在不同分销站点各有一个账号,必须按用户集合匹配; + // 标量子查询在命中多个用户时会直接报 SQL 错误。 // 不能直接写 owner.phone/renter.phone:跨表别名 OR 会让 MySQL 倒序扫描全部订单。 db = db.Where( - "(o.owner_id = (SELECT id FROM users WHERE phone = ?) OR o.renter_id = (SELECT id FROM users WHERE phone = ?))", + "(o.owner_id IN (SELECT id FROM users WHERE phone = ?) OR o.renter_id IN (SELECT id FROM users WHERE phone = ?))", keyword, keyword, ) diff --git a/backend/internal/modules/order/repository_integration_test.go b/backend/internal/modules/order/repository_integration_test.go index c699252a..d86cf2b5 100644 --- a/backend/internal/modules/order/repository_integration_test.go +++ b/backend/internal/modules/order/repository_integration_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "strconv" "strings" "testing" "time" @@ -2375,6 +2376,75 @@ func TestListAdminFiltersByCreatedAtRange(t *testing.T) { } } +func TestListAdminFiltersByPhoneWhenSamePhoneHasMultipleUsers(t *testing.T) { + db := setupOrderTestDB(t) + repo := NewRepository(db) + const phone = "16673815551" + + ownerMain := model.User{Phone: phone, DistributionSiteID: 0} + ownerDistribution := model.User{Phone: phone, DistributionSiteID: 1} + renter := model.User{Phone: "13900004003"} + if err := db.Create(&ownerMain).Error; err != nil { + t.Fatalf("create main-site owner failed: %v", err) + } + if err := db.Create(&ownerDistribution).Error; err != nil { + t.Fatalf("create distribution-site owner failed: %v", err) + } + if err := db.Create(&renter).Error; err != nil { + t.Fatalf("create renter failed: %v", err) + } + + for index, owner := range []model.User{ownerMain, ownerDistribution} { + suffix := strconv.Itoa(index + 1) + account := model.GameAccount{ + OwnerID: owner.ID, + Status: accountStatusRented, + ServerRegion: "国服", + LoginPlatform: "steam", + Title: "手机号跨站点订单测试账号 " + suffix, + } + if err := db.Create(&account).Error; err != nil { + t.Fatalf("create account %s failed: %v", suffix, err) + } + listing := model.RentalListing{ + ListingNo: "L-PHONE-SCOPE-" + suffix, + OwnerID: owner.ID, + AccountID: account.ID, + Status: listingStatusRented, + ReviewStatus: listingReviewStatusApproved, + InTransaction: true, + PriceCent: 10000, + } + if err := db.Create(&listing).Error; err != nil { + t.Fatalf("create listing %s failed: %v", suffix, err) + } + order := model.RentalOrder{ + OrderNo: "RO-PHONE-SCOPE-" + suffix, + ListingID: listing.ID, + AccountID: account.ID, + OwnerID: owner.ID, + RenterID: renter.ID, + RentAmountCent: 10000, + Status: orderStatusRenting, + } + if err := db.Create(&order).Error; err != nil { + t.Fatalf("create order %s failed: %v", suffix, err) + } + } + + result, err := repo.ListAdmin(t.Context(), AdminOrderQuery{ + Page: 1, + PageSize: 1, + Keyword: phone, + }) + if err != nil { + t.Fatalf("ListAdmin() by duplicate phone error = %v", err) + } + if result.Total != 2 || len(result.Items) != 1 { + t.Fatalf("ListAdmin() by duplicate phone total/items = %d/%d, want 2/1", result.Total, len(result.Items)) + } +} + func TestAdminPlatformCheckoutConfirmRequiresExplicitConfirmationForZeroOwnerIncome(t *testing.T) { db := setupOrderTestDB(t) repo := NewRepository(db, Dependencies{ diff --git a/backend/internal/modules/order/repository_test.go b/backend/internal/modules/order/repository_test.go index 79f1a9e1..1277bd3d 100644 --- a/backend/internal/modules/order/repository_test.go +++ b/backend/internal/modules/order/repository_test.go @@ -515,7 +515,7 @@ func TestApplyAdminOrderFiltersSmartRouting(t *testing.T) { { name: "11位手机号先解析用户再走订单角色索引", query: AdminOrderQuery{Keyword: "13800138000"}, - wantSQLFrag: "o.owner_id = (SELECT id FROM users WHERE phone = ?)", + wantSQLFrag: "o.owner_id IN (SELECT id FROM users WHERE phone = ?)", }, { name: "RO前缀订单号路由至rental_orders(order_no)",