feat: 支持租户ID和关键词同时搜索

This commit is contained in:
wizardchen
2026-01-06 15:59:18 +08:00
parent a63a2a8daf
commit 953e0c1a55
2 changed files with 11 additions and 8 deletions
+8 -6
View File
@@ -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+"%")
}