fix(search): 修复跨租户重复手机号查询报错

This commit is contained in:
yml
2026-11-29 20:07:57 +08:00
parent 2e0018b943
commit a1e6089aa5
15 changed files with 382 additions and 28 deletions
@@ -0,0 +1,78 @@
package chat
import (
"fmt"
"testing"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/tenancy"
)
func TestAdminConversationPhoneFilterWithDuplicateAccounts(t *testing.T) {
db := setupAdminListTestDB(t)
const phone = "13812345678"
users := []model.User{
{TenantID: 101, Phone: phone}, {TenantID: 202, Phone: phone},
{TenantID: 303, Phone: phone}, {TenantID: 101, Phone: "13912345678"},
}
if err := db.Create(&users).Error; err != nil {
t.Fatal(err)
}
wantByTenant := map[uint64]map[uint64]bool{101: {}, 202: {}}
for i := 0; i < 5; i++ {
tenantID := uint64(101)
ownerID, renterID := users[0].ID, users[3].ID
if i == 1 {
ownerID, renterID = users[3].ID, users[2].ID
} else if i == 2 || i == 3 {
ownerID = users[1].ID
} else if i == 4 {
ownerID = users[3].ID
}
if i == 3 {
tenantID = 202
}
listing := model.RentalListing{ListingNo: fmt.Sprintf("L-PHONE-%d", i),
OwnerID: ownerID, AccountID: uint64(i + 1), Status: "published"}
if err := db.Create(&listing).Error; err != nil {
t.Fatal(err)
}
conversation := model.ChatConversation{TenantID: tenantID,
Type: ConversationTypeListingGroup, ListingID: &listing.ID, Status: "active"}
if i != 2 {
order := model.RentalOrder{OrderNo: fmt.Sprintf("RO-PHONE-%d", i),
ListingID: listing.ID, AccountID: listing.AccountID, OwnerID: ownerID, RenterID: renterID,
BuyerTenantID: tenantID, Status: "renting"}
if err := db.Create(&order).Error; err != nil {
t.Fatal(err)
}
conversation.Type, conversation.OrderID = ConversationTypeOrderGroup, &order.ID
}
if err := db.Create(&conversation).Error; err != nil {
t.Fatal(err)
}
if i < 4 {
wantByTenant[tenantID][conversation.ID] = true
}
}
repo := NewRepository(db, nil, nil)
for tenantID, want := range wantByTenant {
t.Run(fmt.Sprint(tenantID), func(t *testing.T) {
ctx := tenancy.WithScope(t.Context(), tenancy.Scope{TenantID: tenantID})
got, err := repo.ListConversationsWithFilter(ctx, Principal{Type: "admin"},
1, 20, adminChatFilterAll, adminChatStageAll, phone)
if err != nil {
t.Fatal(err)
}
items := got.Items.([]ConversationDTO)
if got.Total != int64(len(want)) || len(items) != len(want) {
t.Fatalf("unexpected conversations: total=%d items=%+v", got.Total, items)
}
for _, item := range items {
if !want[item.ID] {
t.Fatalf("unexpected conversation: %+v", item)
}
}
})
}
}
+4 -4
View File
@@ -339,20 +339,20 @@ func (r *Repository) adminConversationBase(ctx context.Context, principal Princi
if keyword != "" {
upper := strings.ToUpper(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 = ?)
WHERE o2.owner_id IN (SELECT id FROM users WHERE phone = ?)
OR o2.renter_id IN (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 = ?)
AND l2.owner_id IN (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)