GuDingZiChan/backend/src/utils/equipmentKeywordSearch.ts
2026-07-10 13:37:40 +08:00

75 lines
1.5 KiB
TypeScript

type EquipmentKeywordCondition = Record<string, unknown>
const buildOwnEquipmentKeywordConditions = (keyword: string): EquipmentKeywordCondition[] => [
{ equipment_name: { contains: keyword } },
{ equipment_code: { contains: keyword } },
{ equipment_model: { contains: keyword } },
{
department: {
is: {
name: { contains: keyword }
}
}
},
{
storageLocation: {
is: {
name: { contains: keyword }
}
}
},
{
borrow_records: {
some: {
status: '已借出',
OR: [
{ person_nickname: { contains: keyword } },
{ person_name: { contains: keyword } },
{
person: {
is: {
nickname: { contains: keyword }
}
}
},
{
person: {
is: {
real_name: { contains: keyword }
}
}
},
{
person: {
is: {
job_number: { contains: keyword }
}
}
}
]
}
}
}
]
export const buildEquipmentKeywordSearchConditions = (
keyword: string,
descendantDepth = 0
): EquipmentKeywordCondition[] => {
const ownConditions = buildOwnEquipmentKeywordConditions(keyword)
if (descendantDepth <= 0) {
return ownConditions
}
return [
...ownConditions,
{
children: {
some: {
OR: buildEquipmentKeywordSearchConditions(keyword, descendantDepth - 1)
}
}
}
]
}