perf(query): 优化后台手机号精确查询
This commit is contained in:
@@ -307,10 +307,22 @@ func (r *Repository) adminConversationBase(ctx context.Context, principal Princi
|
||||
Joins("LEFT JOIN rental_listings AS l ON l.id = COALESCE(explicit_lo.listing_id, c.listing_id)")
|
||||
if keyword != "" {
|
||||
upper := strings.ToUpper(keyword)
|
||||
if len(keyword) == 11 && keyword[0] == '1' && isAllDigits(keyword) {
|
||||
db = db.Joins("LEFT JOIN users AS renter ON renter.id = explicit_lo.renter_id").
|
||||
Joins("LEFT JOIN users AS owner ON owner.id = COALESCE(explicit_lo.owner_id, l.owner_id)").
|
||||
Where("(renter.phone = ? OR owner.phone = ?)", keyword, keyword)
|
||||
if isPhoneKeyword(keyword) {
|
||||
// 先由 users(phone) 唯一索引解析用户,再分别从订单群和发布群定位会话。
|
||||
// 保持原有语义:订单群匹配订单号主/租客,发布群仅匹配商品号主。
|
||||
db = db.Joins(`JOIN (
|
||||
SELECT c2.id AS conversation_id
|
||||
FROM chat_conversations AS c2
|
||||
JOIN rental_orders AS o2 ON o2.id = c2.order_id
|
||||
WHERE o2.owner_id = (SELECT id FROM users WHERE phone = ?)
|
||||
OR o2.renter_id = (SELECT id FROM users WHERE phone = ?)
|
||||
UNION ALL
|
||||
SELECT c2.id AS conversation_id
|
||||
FROM chat_conversations AS c2
|
||||
JOIN rental_listings AS l2 ON l2.id = c2.listing_id
|
||||
WHERE c2.order_id IS NULL
|
||||
AND l2.owner_id = (SELECT id FROM users WHERE phone = ?)
|
||||
) AS matched_phone ON matched_phone.conversation_id = c.id`, keyword, keyword, keyword)
|
||||
} else if strings.HasPrefix(upper, "RO") || strings.HasPrefix(upper, "ORD") {
|
||||
db = db.Where("(explicit_lo.order_no = ? OR c.latest_order_no = ?)", keyword, keyword)
|
||||
} else if strings.HasPrefix(upper, "L") || (len(keyword) >= 12 && isAllDigits(keyword)) {
|
||||
@@ -492,3 +504,7 @@ func isAllDigits(s string) bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func isPhoneKeyword(s string) bool {
|
||||
return len(s) == 11 && s[0] == '1' && isAllDigits(s)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user