feat(bill): 支持服务端跨字段检索与 --search-fields 参数
This commit is contained in:
1 parent
7e16a6a312
commit
bcb77feabb
5 files changed
+159
-8
No files matched your search
@@ -164,6 +164,7 @@ export async function handleBillCommand(
|
||||
|
||||
const code = stringOption(parsed.options, ["code", "c"]);
|
||||
const keyword = stringOption(parsed.options, ["keyword", "k", "q", "query"]);
|
||||
const searchFields = stringOption(parsed.options, ["search-fields", "searchFields", "search-field", "sf"]);
|
||||
const stage = stringOption(parsed.options, ["stage"]);
|
||||
const state = stringOption(parsed.options, ["state", "s"]);
|
||||
const count = numberOption(parsed.options, ["count", "limit", "n"], 20);
|
||||
@@ -179,6 +180,7 @@ export async function handleBillCommand(
|
||||
const result = await searchAndFormatBills(await getClient(), corpId, {
|
||||
code,
|
||||
keyword,
|
||||
searchFields,
|
||||
stage,
|
||||
state,
|
||||
count,
|
||||
@@ -223,7 +225,9 @@ export async function handleBillCommand(
|
||||
if (since) filterSummary.push(`近 ${since}`);
|
||||
if (startDate || endDate) filterSummary.push(`${startDate || "起"} ~ ${endDate || "止"}`);
|
||||
if (code) filterSummary.push(`单号: ${code}`);
|
||||
if (keyword) filterSummary.push(`关键字: ${keyword}`);
|
||||
if (keyword) {
|
||||
filterSummary.push(searchFields ? `关键字: ${keyword} (字段: ${searchFields})` : `关键字: ${keyword}`);
|
||||
}
|
||||
if (stage) filterSummary.push(`阶段: ${stage}`);
|
||||
if (state) filterSummary.push(`状态: ${state}`);
|
||||
const filterTag = filterSummary.length > 0 ? ` | 筛选: ${filterSummary.join(", ")}` : "";
|
||||
|
||||
@@ -20,7 +20,7 @@ src/domains/bill/
|
||||
- 原生支持合思服务端 `filterBy` 索引检索(杜绝全表拉取与大租户超时):
|
||||
- 时间范围(`since`、`startDate`、`endDate`、`from`、`to`);
|
||||
- 单号服务端索引匹配(`code`,使用 `.in(...)` 语法避免 WAF 拦截);
|
||||
- 名称与标题关键字匹配(`keyword`,走服务端 `INSTR`);
|
||||
- 名称与标题关键字跨字段复合匹配(`keyword` 与 `searchFields`,默认跨 `name,code,form` 服务端 `INSTR(...) > 0` 复合 OR 检索,支持单字段或自定义字段集);
|
||||
- 状态与软删除过滤(`active.in(true)`);
|
||||
- 服务端 `orderBy: [{ value: "createTime", order: "DESC" }]` 排序与精简 `select` 字段;
|
||||
- 客户端辅助精确二次过滤:阶段(`stage`)、状态(`state`)。
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
resolveBillStaffIds,
|
||||
parseDateFilter,
|
||||
parseSinceDuration,
|
||||
normalizeSearchFields,
|
||||
buildServerBillFilterBy,
|
||||
searchAndFormatBills,
|
||||
} from "./tools.js";
|
||||
@@ -588,6 +589,8 @@ describe("Bill Domain", () => {
|
||||
|
||||
const keywordFilter = buildServerBillFilterBy({ keyword: "打车" });
|
||||
expect(keywordFilter).toContain('INSTR(name, "打车") > 0');
|
||||
expect(keywordFilter).toContain('INSTR(code, "打车") > 0');
|
||||
expect(keywordFilter).toContain('INSTR(form, "打车") > 0');
|
||||
|
||||
const timeFilter = buildServerBillFilterBy({ since: "7d" });
|
||||
expect(timeFilter).toContain("createTime >");
|
||||
@@ -595,5 +598,119 @@ describe("Bill Domain", () => {
|
||||
const inactiveFilter = buildServerBillFilterBy({ active: false });
|
||||
expect(inactiveFilter).toBe("(active.in(false))");
|
||||
});
|
||||
|
||||
it("normalizes searchFields parameter correctly", () => {
|
||||
expect(normalizeSearchFields()).toEqual(["name", "code", "form"]);
|
||||
expect(normalizeSearchFields(undefined)).toEqual(["name", "code", "form"]);
|
||||
expect(normalizeSearchFields("")).toEqual(["name", "code", "form"]);
|
||||
expect(normalizeSearchFields("name,code")).toEqual(["name", "code"]);
|
||||
expect(normalizeSearchFields(" name , code , form ")).toEqual(["name", "code", "form"]);
|
||||
expect(normalizeSearchFields(["name", "form"])).toEqual(["name", "form"]);
|
||||
expect(normalizeSearchFields([" ", "code"])).toEqual(["code"]);
|
||||
expect(normalizeSearchFields([])).toEqual(["name", "code", "form"]);
|
||||
});
|
||||
|
||||
it("builds multi-field composite OR and single-field RQL based on searchFields", () => {
|
||||
// Default: name, code, form composite OR
|
||||
const defaultFilter = buildServerBillFilterBy({ keyword: "差旅" });
|
||||
expect(defaultFilter).toContain(
|
||||
'((INSTR(name, "差旅") > 0) || (INSTR(code, "差旅") > 0) || (INSTR(form, "差旅") > 0))'
|
||||
);
|
||||
|
||||
// Single field: direct INSTR without outer ||
|
||||
const singleFieldFilter = buildServerBillFilterBy({
|
||||
keyword: "差旅",
|
||||
searchFields: "name",
|
||||
});
|
||||
expect(singleFieldFilter).toContain('(INSTR(name, "差旅") > 0)');
|
||||
expect(singleFieldFilter).not.toContain("||");
|
||||
|
||||
// Multiple explicit fields array: composite OR
|
||||
const multiArrayFilter = buildServerBillFilterBy({
|
||||
keyword: "差旅",
|
||||
searchFields: ["code", "form"],
|
||||
});
|
||||
expect(multiArrayFilter).toContain(
|
||||
'((INSTR(code, "差旅") > 0) || (INSTR(form, "差旅") > 0))'
|
||||
);
|
||||
expect(multiArrayFilter).not.toContain("INSTR(name,");
|
||||
|
||||
// Multiple comma-separated string fields: composite OR
|
||||
const multiStringFilter = buildServerBillFilterBy({
|
||||
keyword: "差旅",
|
||||
searchFields: "name,code",
|
||||
});
|
||||
expect(multiStringFilter).toContain(
|
||||
'((INSTR(name, "差旅") > 0) || (INSTR(code, "差旅") > 0))'
|
||||
);
|
||||
expect(multiStringFilter).not.toContain("INSTR(form,");
|
||||
});
|
||||
|
||||
it("filters bills in memory accurately based on searchFields", async () => {
|
||||
const mockRawBills = [
|
||||
{
|
||||
id: "bill-1",
|
||||
code: "CODE-TAXI",
|
||||
name: "办公用品采购",
|
||||
form: {},
|
||||
createTime: 1000,
|
||||
updateTime: 1000,
|
||||
},
|
||||
{
|
||||
id: "bill-2",
|
||||
code: "CODE-OFFICE",
|
||||
name: "打车报销",
|
||||
form: {},
|
||||
createTime: 2000,
|
||||
updateTime: 2000,
|
||||
},
|
||||
{
|
||||
id: "bill-3",
|
||||
code: "CODE-MEAL",
|
||||
name: "餐饮费用",
|
||||
form: { description: "打车去餐厅" },
|
||||
createTime: 3000,
|
||||
updateTime: 3000,
|
||||
},
|
||||
];
|
||||
|
||||
const client = {
|
||||
get: async (path: string) => {
|
||||
if (path.includes("entities")) {
|
||||
return new Response(JSON.stringify({ items: [] }));
|
||||
}
|
||||
return new Response(JSON.stringify({}));
|
||||
},
|
||||
post: async () => new Response(JSON.stringify({ count: 3, items: mockRawBills })),
|
||||
} as any;
|
||||
|
||||
// 1. Search only 'name': only bill-2 matches
|
||||
const resName = await searchAndFormatBills(client, "test-corp", {
|
||||
keyword: "打车",
|
||||
searchFields: "name",
|
||||
});
|
||||
expect(resName.items.map((b) => b.billId)).toEqual(["bill-2"]);
|
||||
|
||||
// 2. Search only 'code': only bill-1 matches (CODE-TAXI)
|
||||
const resCode = await searchAndFormatBills(client, "test-corp", {
|
||||
keyword: "TAXI",
|
||||
searchFields: "code",
|
||||
});
|
||||
expect(resCode.items.map((b) => b.billId)).toEqual(["bill-1"]);
|
||||
|
||||
// 3. Search 'form': only bill-3 matches
|
||||
const resForm = await searchAndFormatBills(client, "test-corp", {
|
||||
keyword: "餐厅",
|
||||
searchFields: "form",
|
||||
});
|
||||
expect(resForm.items.map((b) => b.billId)).toEqual(["bill-3"]);
|
||||
|
||||
// 4. Search default (name, code, form): bill-2 and bill-3 match '打车'
|
||||
const resDefault = await searchAndFormatBills(client, "test-corp", {
|
||||
keyword: "打车",
|
||||
});
|
||||
expect(resDefault.items.map((b) => b.billId)).toEqual(["bill-2", "bill-3"]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1176,6 +1176,16 @@ export function parseSinceDuration(since: string | undefined): number | undefine
|
||||
}
|
||||
}
|
||||
|
||||
export function normalizeSearchFields(fields?: string[] | string): string[] {
|
||||
if (!fields) return ["name", "code", "form"];
|
||||
if (Array.isArray(fields)) {
|
||||
const list = fields.map((f) => f.trim()).filter(Boolean);
|
||||
return list.length > 0 ? list : ["name", "code", "form"];
|
||||
}
|
||||
const list = fields.split(",").map((f) => f.trim()).filter(Boolean);
|
||||
return list.length > 0 ? list : ["name", "code", "form"];
|
||||
}
|
||||
|
||||
export function buildServerBillFilterBy(params: BillQueryParams): string | undefined {
|
||||
if (params.filterBy && params.filterBy.trim()) {
|
||||
return params.filterBy.trim();
|
||||
@@ -1212,9 +1222,18 @@ export function buildServerBillFilterBy(params: BillQueryParams): string | undef
|
||||
parts.push(`(code.in(${JSON.stringify(params.code.trim())}))`);
|
||||
}
|
||||
|
||||
// 4. 标题/名称关键字过滤
|
||||
// 4. 关键字跨字段检索 (支持通过 searchFields 指定单字段或多字段联合 RQL)
|
||||
if (params.keyword && params.keyword.trim()) {
|
||||
parts.push(`(INSTR(name, ${JSON.stringify(params.keyword.trim())}) > 0)`);
|
||||
const kw = params.keyword.trim();
|
||||
const fields = normalizeSearchFields(params.searchFields);
|
||||
if (fields.length === 1) {
|
||||
parts.push(`(INSTR(${fields[0]}, ${JSON.stringify(kw)}) > 0)`);
|
||||
} else {
|
||||
const fieldParts = fields.map(
|
||||
(f) => `(INSTR(${f}, ${JSON.stringify(kw)}) > 0)`
|
||||
);
|
||||
parts.push(`(${fieldParts.join(" || ")})`);
|
||||
}
|
||||
}
|
||||
|
||||
if (parts.length === 0) return undefined;
|
||||
@@ -1302,11 +1321,20 @@ export async function searchAndFormatBills(
|
||||
|
||||
if (params.keyword) {
|
||||
const kw = params.keyword.trim().toLowerCase();
|
||||
const fields = normalizeSearchFields(params.searchFields);
|
||||
filtered = filtered.filter((it) => {
|
||||
if (it.name?.toLowerCase().includes(kw)) return true;
|
||||
if (it.code?.toLowerCase().includes(kw)) return true;
|
||||
for (const v of Object.values(it.form || {})) {
|
||||
if (typeof v === "string" && v.toLowerCase().includes(kw)) return true;
|
||||
if (fields.includes("name") && it.name?.toLowerCase().includes(kw)) return true;
|
||||
if (fields.includes("code") && it.code?.toLowerCase().includes(kw)) return true;
|
||||
if (fields.includes("form")) {
|
||||
for (const v of Object.values(it.form || {})) {
|
||||
if (typeof v === "string" && v.toLowerCase().includes(kw)) return true;
|
||||
}
|
||||
}
|
||||
for (const field of fields) {
|
||||
if (field !== "name" && field !== "code" && field !== "form") {
|
||||
const val = (it as any)[field] ?? (it.form as any)?.[field];
|
||||
if (typeof val === "string" && val.toLowerCase().includes(kw)) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
@@ -1330,6 +1358,7 @@ export const billTools = [
|
||||
parameters: z.object({
|
||||
code: z.string().optional().describe("按单据编号/单号过滤(如 jO1it3cIufDjNS)"),
|
||||
keyword: z.string().optional().describe("按关键字模糊搜索标题、名称或内容"),
|
||||
searchFields: z.union([z.array(z.string()), z.string()]).optional().describe("指定 keyword 检索匹配的字段列表,默认 ['name', 'code', 'form'],可传入 ['name'] 或 'name,code' 等"),
|
||||
since: z.string().optional().describe("相对时间范围,如 7d (7天), 30d (30天), 90d, 1y"),
|
||||
startDate: z.string().optional().describe("起始时间 (YYYY-MM-DD 或毫秒时间戳)"),
|
||||
endDate: z.string().optional().describe("截止时间 (YYYY-MM-DD 或毫秒时间戳)"),
|
||||
|
||||
@@ -98,6 +98,7 @@ export interface BillQueryParams {
|
||||
code?: string;
|
||||
id?: string;
|
||||
keyword?: string;
|
||||
searchFields?: string[] | string;
|
||||
stage?: string;
|
||||
state?: string;
|
||||
start?: number;
|
||||
|
||||
Reference in new issue
Block a user