diff --git a/app/api/contract-template/templates.ts b/app/api/contract-template/templates.ts index 7fe38ce..fe8d0fa 100644 --- a/app/api/contract-template/templates.ts +++ b/app/api/contract-template/templates.ts @@ -201,8 +201,36 @@ export async function getContractTemplates(searchParams: TemplateSearchParams = // 处理关键词搜索 if (keyword && keyword.trim()) { const cleanKeyword = keyword.trim(); - // 使用PostgREST的or条件进行模糊搜索,正确的格式需要括号 - params.or = `(title.ilike.*${cleanKeyword}*,description.ilike.*${cleanKeyword}*,template_code.ilike.*${cleanKeyword}*)`; + + // 先查询匹配的分类ID + let matchingCategoryIds: number[] = []; + try { + const categoryResponse = await postgrestGet('contract_categories', { + select: 'id', + filter: { 'name': `ilike.*${cleanKeyword}*` } + }); + + if (categoryResponse.data) { + const categories = extractApiData(categoryResponse.data) || []; + matchingCategoryIds = categories.map(cat => cat.id); + console.log('匹配的分类ID:', matchingCategoryIds); + } + } catch (error) { + console.error('查询分类失败:', error); + } + + // 构建搜索条件 + const searchConditions = [ + `title.ilike.*${cleanKeyword}*`, + `description.ilike.*${cleanKeyword}*`, + `template_code.ilike.*${cleanKeyword}*`, + // 如果有匹配的分类,添加分类ID条件 + ...(matchingCategoryIds.length > 0 ? [`category_id.in.(${matchingCategoryIds.join(',')})`] : []) + ]; + + params.or = `(${searchConditions.join(',')})`; + console.log('搜索关键词:', cleanKeyword); + console.log('搜索条件:', params.or); } // 如果有分类名称,需要先获取分类ID