From 953e0c1a55e4489067beac1a3f19c34f1722eba2 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Tue, 6 Jan 2026 15:59:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E7=A7=9F=E6=88=B7ID?= =?UTF-8?q?=E5=92=8C=E5=85=B3=E9=94=AE=E8=AF=8D=E5=90=8C=E6=97=B6=E6=90=9C?= =?UTF-8?q?=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/components/TenantSelector.vue | 5 +++-- internal/application/repository/tenant.go | 14 ++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/frontend/src/components/TenantSelector.vue b/frontend/src/components/TenantSelector.vue index a591c5ae..4be657c8 100644 --- a/frontend/src/components/TenantSelector.vue +++ b/frontend/src/components/TenantSelector.vue @@ -179,12 +179,13 @@ const loadTenants = async (append = false) => { loading.value = true try { - let keyword = searchQuery.value.trim() + const keyword = searchQuery.value.trim() let tenantID: number | undefined = undefined + // 如果是纯数字,同时作为 tenant_id 和 keyword 搜索 + // 这样既能精确匹配租户ID,也能模糊匹配名称中包含数字的租户 if (keyword && /^\d+$/.test(keyword)) { tenantID = Number(keyword) - keyword = '' } const response = await searchTenants({ diff --git a/internal/application/repository/tenant.go b/internal/application/repository/tenant.go index 9c646f54..3337442b 100644 --- a/internal/application/repository/tenant.go +++ b/internal/application/repository/tenant.go @@ -59,13 +59,15 @@ func (r *tenantRepository) SearchTenants(ctx context.Context, keyword string, te query := r.db.WithContext(ctx).Model(&types.Tenant{}) - // Filter by tenant ID if provided - if tenantID > 0 { + // Build search conditions + if tenantID > 0 && keyword != "" { + // When both tenantID and keyword are provided, use OR to match either + query = query.Where("id = ? OR name LIKE ? OR description LIKE ?", tenantID, "%"+keyword+"%", "%"+keyword+"%") + } else if tenantID > 0 { + // Filter by tenant ID only query = query.Where("id = ?", tenantID) - } - - // Filter by keyword if provided (search in name and description) - if keyword != "" { + } else if keyword != "" { + // Filter by keyword only (search in name and description) query = query.Where("name LIKE ? OR description LIKE ?", "%"+keyword+"%", "%"+keyword+"%") }