perf(query): 优化后台手机号精确查询
This commit is contained in:
@@ -1138,8 +1138,13 @@ func (r *Repository) ListOrders(ctx context.Context, query DistributionOrderList
|
||||
base = base.Where("f.settlement_status = ?", financialStatus)
|
||||
}
|
||||
if strings.TrimSpace(query.Keyword) != "" {
|
||||
kw := "%" + strings.TrimSpace(query.Keyword) + "%"
|
||||
base = base.Where("(ro.order_no LIKE ? OR rl.listing_no LIKE ? OR ga.title LIKE ? OR u.phone LIKE ?)", kw, kw, kw, kw)
|
||||
keyword := strings.TrimSpace(query.Keyword)
|
||||
if isPhoneKeyword(keyword) {
|
||||
base = base.Where("ro.renter_id = (SELECT id FROM users WHERE phone = ?)", keyword)
|
||||
} else {
|
||||
kw := "%" + keyword + "%"
|
||||
base = base.Where("(ro.order_no LIKE ? OR rl.listing_no LIKE ? OR ga.title LIKE ? OR u.phone LIKE ?)", kw, kw, kw, kw)
|
||||
}
|
||||
}
|
||||
switch strings.TrimSpace(query.Kind) {
|
||||
case "sold":
|
||||
@@ -3034,8 +3039,13 @@ func (r *Repository) ListReceivedListings(ctx context.Context, adminID, siteID u
|
||||
base = base.Where("ga.game_name = ?", q.GameName)
|
||||
}
|
||||
if q.Keyword != "" {
|
||||
kw := "%" + strings.TrimSpace(q.Keyword) + "%"
|
||||
base = base.Where("(rl.listing_no LIKE ? OR ga.title LIKE ? OR u.phone LIKE ? OR u.nickname LIKE ?)", kw, kw, kw, kw)
|
||||
keyword := strings.TrimSpace(q.Keyword)
|
||||
if isPhoneKeyword(keyword) {
|
||||
base = base.Where("rl.owner_id = (SELECT id FROM users WHERE phone = ?)", keyword)
|
||||
} else {
|
||||
kw := "%" + keyword + "%"
|
||||
base = base.Where("(rl.listing_no LIKE ? OR ga.title LIKE ? OR u.phone LIKE ? OR u.nickname LIKE ?)", kw, kw, kw, kw)
|
||||
}
|
||||
}
|
||||
|
||||
var total int64
|
||||
@@ -3609,7 +3619,11 @@ func (r *Repository) ListDistributionUsers(ctx context.Context, adminID, siteID
|
||||
}
|
||||
|
||||
if kw := strings.TrimSpace(query.Keyword); kw != "" {
|
||||
baseQuery = baseQuery.Where("u.phone LIKE ? OR u.nickname LIKE ?", "%"+kw+"%", "%"+kw+"%")
|
||||
if isPhoneKeyword(kw) {
|
||||
baseQuery = baseQuery.Where("u.phone = ?", kw)
|
||||
} else {
|
||||
baseQuery = baseQuery.Where("u.phone LIKE ? OR u.nickname LIKE ?", "%"+kw+"%", "%"+kw+"%")
|
||||
}
|
||||
}
|
||||
|
||||
var totalFiltered int64
|
||||
@@ -3675,3 +3689,15 @@ func (r *Repository) ListDistributionUsers(ctx context.Context, adminID, siteID
|
||||
OrderUsers: orderUsersCount,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func isPhoneKeyword(s string) bool {
|
||||
if len(s) != 11 || s[0] != '1' {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] < '0' || s[i] > '9' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -911,6 +911,17 @@ func TestDistributionReceivedListingsAndWallet(t *testing.T) {
|
||||
if recPage.Total != 1 || len(recPage.Items) != 1 {
|
||||
t.Fatalf("expected 1 received listing, got %d", recPage.Total)
|
||||
}
|
||||
phonePage, err := repo.ListReceivedListings(t.Context(), 0, site.ID, ReceivedListingQuery{
|
||||
Page: 1,
|
||||
PageSize: 10,
|
||||
Keyword: user.Phone,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("ListReceivedListings by phone failed: %v", err)
|
||||
}
|
||||
if phonePage.Total != 1 || phonePage.Items[0].ListingID != listing.ID {
|
||||
t.Fatalf("expected listing %d by phone, got total=%d items=%+v", listing.ID, phonePage.Total, phonePage.Items)
|
||||
}
|
||||
item := recPage.Items[0]
|
||||
if item.ListingNo != "RL-99001" || item.GameName != "delta_force" || item.OwnerNickname != "seller01" {
|
||||
t.Fatalf("unexpected received listing: %+v", item)
|
||||
@@ -1804,6 +1815,18 @@ func TestListOrdersPerformanceAndScoping(t *testing.T) {
|
||||
if len(allPage.Items) != 2 {
|
||||
t.Fatalf("expected 2 items, got %d", len(allPage.Items))
|
||||
}
|
||||
phonePage, err := repo.ListOrders(t.Context(), DistributionOrderListQuery{
|
||||
SiteID: 0,
|
||||
Page: 1,
|
||||
Size: 10,
|
||||
Keyword: u.Phone,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("ListOrders by phone failed: %v", err)
|
||||
}
|
||||
if phonePage.Total != 2 {
|
||||
t.Fatalf("expected 2 distribution orders by renter phone, got %d", phonePage.Total)
|
||||
}
|
||||
|
||||
// B. 分销商1查询其订单 (SiteID = site1.ID)
|
||||
// 预期:只查出 distOrder1
|
||||
|
||||
Reference in New Issue
Block a user