import { listBillTemplates } from "../../../domains/bill/templates.js"; import { booleanOption, stringOption } from "../../parser.js"; import { printJson } from "../../printer.js"; import type { CliResult } from "../../types.js"; import type { BillActionContext } from "./context.js"; /** * `hose-use bill templates [--all] [--keyword=...]` * * 默认返回当前身份可见且启用中的模板(与页面「快速新建单据」同源、同分组); * `--all` 才会带出停用模板。 */ export async function handleTemplates(ctx: BillActionContext): Promise { const { parsed, runtime, corp, corpId } = ctx; const includeInactive = booleanOption(parsed.options, ["all"]); const keyword = (stringOption(parsed.options, ["keyword", "k", "q", "query"]) ?? "").trim(); const result = await listBillTemplates(await ctx.getClient(), corpId, { includeInactive }); let groups = result.groups; if (keyword) { groups = groups .map((group) => ({ ...group, specifications: group.specifications.filter((spec) => spec.name.includes(keyword)), })) .filter((group) => group.specifications.length > 0); } const total = groups.reduce((sum, group) => sum + group.specifications.length, 0); if (runtime.jsonMode) { return { stdout: printJson( { corpId, corpName: corp.name, includeInactive, total, inactive: result.inactive, groups, }, runtime.outputOptions ), stderr: "", exitCode: 0, }; } const lines: string[] = [ `====== 单据模板(${corp.name} | ${groups.length} 组 / ${total} 个${includeInactive ? ",含停用" : ""})======`, "", ]; for (const group of groups) { lines.push(`${group.name || "未分组"}`); for (const spec of group.specifications) { const inactiveMark = spec.active ? "" : "(已停用)"; lines.push(` - ${spec.name}${inactiveMark} [${spec.type}] ID: ${spec.id}`); } lines.push(""); } if (!includeInactive) { lines.push("提示: 追加 --all 可查看包含停用模板的全量列表"); } return { stdout: lines.join("\n") + "\n", stderr: "", exitCode: 0 }; }