feat(bill,flow): add unified bill trace and flow get diagnostic engine (Phase 2)

This commit is contained in:
gitea committed 2026-10-14 22:00:55 +08:00
1 parent 7891959853
commit ffec604ed9
10 files changed
+970 -32

No files matched your search

+101 -2
View File
@@ -1,6 +1,6 @@
import { getBillByCodeOrId, getBillRisk, searchAndFormatBills } from "../../../domains/bill/tools.js";
import { getBillByCodeOrId, getBillRisk, searchAndFormatBills, traceBillFlowDetail } from "../../../domains/bill/tools.js";
import { booleanOption, numberOption, stringOption } from "../../parser.js";
import { printJson, printTable } from "../../printer.js";
import { printJson, printTable, printKeyValue } from "../../printer.js";
import type { CliResult } from "../../types.js";
import type { BillActionContext } from "./context.js";
@@ -198,3 +198,102 @@ export async function handleGet(ctx: BillActionContext): Promise<CliResult> {
exitCode: 0,
};
}
export async function handleTrace(ctx: BillActionContext): Promise<CliResult> {
const { parsed, runtime, corp, corpId } = ctx;
const target = parsed.args[0] || stringOption(parsed.options, ["code", "id", "target", "flowId"]);
if (!target) {
throw new Error("请提供要穿透诊断的单号或单据/流程 ID: hose-use bill trace <单号|ID> 或 hose-use flow get <单号|ID>");
}
const client = await ctx.getClient();
const report = await traceBillFlowDetail(client, corpId, target);
if (!report.found) {
if (runtime.jsonMode) {
return {
stdout: printJson(report, runtime.outputOptions),
stderr: "",
exitCode: 1,
};
}
return {
stdout: "",
stderr: `[NOT FOUND] 在企业 [${corp.name}] 下未找到匹配 [${target}] 的单据或审批流实例\n 诊断提示: ${report.diagnostic.advice}\n`,
exitCode: 1,
};
}
if (runtime.jsonMode) {
return {
stdout: printJson(
{
corpId,
corpName: corp.name,
...report,
},
runtime.outputOptions
),
stderr: "",
exitCode: 0,
};
}
const approverNames = report.pendingApprovers
.map((p) => (p.name ? `${p.name}${p.code ? ` (${p.code})` : ""}` : p.id))
.filter(Boolean)
.join("、");
const lines: string[] = [
`====== 单据全域穿透诊断 [${report.billCode || report.target}] ======`,
printKeyValue({
"单据编号": report.billCode || "-",
"单据 ID": report.billId || "-",
"流程实例 (flowId)": report.flowId || "-",
"单据类型": report.specificationName
? `${report.specificationName}${report.specificationId ? ` (${report.specificationId})` : ""}`
: "-",
"单据标题": report.title || "-",
"提报人员": report.submitter
? `${report.submitter.name || "-"}${report.submitter.id ? ` (${report.submitter.id})` : ""}`
: "-",
"归属部门": report.department || "-",
"提报时间": report.submitDate || "-",
"单据金额": report.amount || "-",
"全局流转状态": `${report.state || "-"} (${report.rawState || "-"})`,
}),
"",
"── 🩺 待办与审批诊断 ──",
` 阶段判定 : ${report.diagnostic.stageLabel}`,
` 当前驻留节点: ${report.currentNode?.name || "-"}${report.currentNode?.id ? ` (${report.currentNode.id})` : ""}`,
` 待办处理人: ${approverNames || "(当前无待办审批人)"}`,
` 是否可审批: ${report.diagnostic.canApprove ? "✅ 是 (支持执行同意/驳回)" : "❌ 否 (当前不可审批)"}`,
` 诊断结论 : ${report.diagnostic.summary}`,
` 排障建议 : ${report.diagnostic.advice}`,
];
if (report.timeline.length > 0) {
lines.push("");
lines.push("── ⏱️ 审批流转历史轨迹 (Timeline) ──");
const timelineRows = report.timeline.map((item) => [
`#${item.index}`,
item.time || "-",
item.nodeName || "-",
item.operator?.name || item.operator?.id || (item.automatic ? "自动执行" : "-"),
item.action || item.state || "-",
item.comment ? (item.comment.length > 30 ? item.comment.slice(0, 30) + "..." : item.comment) : "-",
]);
lines.push(
printTable(
["#", "时间", "节点名称", "操作人", "动作", "审批意见"],
timelineRows
)
);
}
return {
stdout: `${lines.join("\n")}\n`,
stderr: "",
exitCode: 0,
};
}