Files
hose-use/src/cli/commands/bill/related.ts
T
gitea 8c6ee6ca89 refactor(cli): split commands/bill.ts into bill/{context,approve,search,flow,related,meta,index}.ts
The five closures that captured corp/client/confirmation state move
into a per-invocation createBillActionContext factory; case bodies
become ctx-taking handlers grouped by concern (approval write actions,
list/get, flow/risk/budget, refs/relations/attachments/download,
schema/link/spec). index.ts keeps the original switch semantics.
No behavior change: tsc clean, 316 tests pass.
2026-10-04 14:18:29 +08:00

117 lines
6.5 KiB
TypeScript

import { downloadBillAttachments, getBillAttachments, getBillReferences, getBillRelations } from "../../../domains/bill/tools.js";
import { stringOption } from "../../parser.js";
import { printJson, printTable } from "../../printer.js";
import type { CliResult } from "../../types.js";
import type { BillActionContext } from "./context.js";
export async function handleRefs(ctx: BillActionContext): Promise<CliResult> {
const { parsed, runtime, corp, corpId } = ctx;
const target = parsed.args[0] || stringOption(parsed.options, ["code", "id"]);
if (!target) throw new Error("请提供要解析引用的单号或单据 ID: hose-use bill refs <单号|ID>");
const report = await getBillReferences(await ctx.getClient(), corpId, target);
if (!report) {
if (runtime.jsonMode) return { stdout: printJson({ found: false, target }, runtime.outputOptions), stderr: "", exitCode: 1 };
return { stdout: "", stderr: `[NOT FOUND] 未找到单据引用: ${target}\n`, exitCode: 1 };
}
if (runtime.jsonMode) {
return { stdout: printJson({ corpId, corpName: corp.name, ...report }, runtime.outputOptions), stderr: "", exitCode: 0 };
}
const person = (value: typeof report.submitter) => value ? `${value.name || "未解析"}${value.code ? ` (${value.code})` : ""} [${value.id}]` : "-";
const output = [
`====== 单据引用解析 (单号: ${report.billCode || target}) ======`,
`标题: ${report.title || "-"}`,
`提交人: ${person(report.submitter)}`,
`归属人: ${person(report.owner)}`,
`费用承担部门: ${report.department?.path || report.department?.name || "-"}${report.department?.id ? ` [${report.department.id}]` : ""}`,
`法人实体: ${report.legalEntity ? `${report.legalEntity.name || "未解析"}${report.legalEntity.id ? ` [${report.legalEntity.id}]` : ""}` : "-"}`,
`费用类型: ${report.feeTypes.length ? report.feeTypes.map((item) => `${item.name || "未解析"}${item.code ? ` (${item.code})` : ""} [${item.id}]`).join("、") : "-"}`,
`核算维度: ${report.dimensions.length ? report.dimensions.map((item) => `${item.name || "未解析"}${item.code ? ` (${item.code})` : ""} [${item.id}]`).join("、") : "-"}`,
report.unresolved.length ? `未解析 ID: ${report.unresolved.join("、")}` : "引用均已解析",
"",
].join("\n");
return { stdout: output, stderr: "", exitCode: 0 };
}
export async function handleRelations(ctx: BillActionContext): Promise<CliResult> {
const { parsed, runtime, corp, corpId } = ctx;
const target = parsed.args[0] || stringOption(parsed.options, ["code", "id"]);
if (!target) throw new Error("请提供要查询关联关系的单号或单据 ID: hose-use bill relations <单号|ID>");
const report = await getBillRelations(await ctx.getClient(), corpId, target);
if (!report) {
if (runtime.jsonMode) return { stdout: printJson({ found: false, target }, runtime.outputOptions), stderr: "", exitCode: 1 };
return { stdout: "", stderr: `[NOT FOUND] 未找到单据关联关系: ${target}\n`, exitCode: 1 };
}
if (runtime.jsonMode) {
return { stdout: printJson({ corpId, corpName: corp.name, ...report }, runtime.outputOptions), stderr: "", exitCode: 0 };
}
const rows: string[][] = [];
const visit = (node: typeof report.relations[number], depth = 0) => {
rows.push([
`${" ".repeat(depth)}${node.relation}`,
node.code || node.id || "-",
node.title || "-",
node.amount || "-",
]);
for (const child of node.children) visit(child, depth + 1);
};
for (const node of report.relations) visit(node);
const output = [
`====== 单据关联关系 (单号: ${report.billCode || target}) ======`,
`标题: ${report.title || "-"}`,
rows.length ? printTable(["关系", "关联 ID/单号", "标题/名称", "金额"], rows) : "暂无可识别的前置单据、冲借款或合同关联",
"",
].join("\n");
return { stdout: output, stderr: "", exitCode: 0 };
}
export async function handleAttachments(ctx: BillActionContext): Promise<CliResult> {
const { parsed, runtime, corp, corpId } = ctx;
const target = parsed.args[0] || stringOption(parsed.options, ["code", "id"]);
if (!target) throw new Error("请提供要查询附件的单号或单据 ID: hose-use bill attachments <单号|ID>");
const report = await getBillAttachments(await ctx.getClient(), corpId, target);
if (!report) {
if (runtime.jsonMode) return { stdout: printJson({ found: false, target }, runtime.outputOptions), stderr: "", exitCode: 1 };
return { stdout: "", stderr: `[NOT FOUND] 未找到单据附件: ${target}\n`, exitCode: 1 };
}
if (runtime.jsonMode) {
return { stdout: printJson({ corpId, corpName: corp.name, ...report }, runtime.outputOptions), stderr: "", exitCode: 0 };
}
const rows = report.attachments.map((item) => [item.fileName || "-", item.fileId || "-", item.key || "-", item.url || "-"]);
const output = [
`====== 单据附件 (单号: ${report.billCode || target}) ======`,
report.attachments.length ? printTable(["文件名", "fileId", "key", "URL"], rows) : "暂无附件或发票原件",
"",
].join("\n");
return { stdout: output, stderr: "", exitCode: 0 };
}
export async function handleDownload(ctx: BillActionContext): Promise<CliResult> {
const { parsed, runtime, corp, corpId } = ctx;
const target = parsed.args[0] || stringOption(parsed.options, ["code", "id"]);
if (!target) throw new Error("请提供要下载附件的单号或单据 ID: hose-use bill download <单号|ID>");
const outputDir = stringOption(parsed.options, ["output-dir", "dir"]) || `${process.cwd()}/downloads`;
const fileId = stringOption(parsed.options, ["file-id", "file"]);
const result = await downloadBillAttachments(await ctx.getClient(), corpId, target, outputDir, fileId);
const summary = {
corpId,
corpName: corp.name,
target: result.target,
outputDir: result.outputDir,
requestedFileId: result.requestedFileId,
downloaded: result.downloaded,
skipped: result.skipped,
};
if (runtime.jsonMode) {
return { stdout: printJson(summary, runtime.outputOptions), stderr: "", exitCode: 0 };
}
const rows = result.downloaded.map((item) => [item.fileName || item.key || item.fileId || "-", item.bytes, item.path]);
const output = [
`====== 单据附件下载 (单号: ${target}) ======`,
`输出目录: ${result.outputDir}`,
rows.length ? printTable(["文件名", "字节数", "本地路径"], rows) : "没有可下载的附件",
result.skipped ? `跳过 ${result.skipped} 个缺少下载地址的附件` : "",
"",
].filter(Boolean).join("\n");
return { stdout: output, stderr: "", exitCode: 0 };
}