perf(query): 优化后台手机号精确查询

This commit is contained in:
yml2213
2026-11-02 18:28:12 +08:00
parent 9c379b7e0c
commit 599e22331d
17 changed files with 254 additions and 62 deletions
@@ -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
}