搜索加入分类名称搜索
This commit is contained in:
@@ -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<ContractCategory[]>('contract_categories', {
|
||||
select: 'id',
|
||||
filter: { 'name': `ilike.*${cleanKeyword}*` }
|
||||
});
|
||||
|
||||
if (categoryResponse.data) {
|
||||
const categories = extractApiData<ContractCategory[]>(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
|
||||
|
||||
Reference in New Issue
Block a user