import { formatBillFlowOperator, getBillFlow, getBillRisk } 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 handleFlow(ctx: BillActionContext): Promise { 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 flow <单号|ID>"); const flow = await getBillFlow(await ctx.getClient(), corpId, target); if (!flow) { 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, ...flow }, runtime.outputOptions), stderr: "", exitCode: 0 }; } const historyRows = flow.history.map((entry) => [ entry.time ? new Date(entry.time).toLocaleString("zh-CN") : "-", entry.nodeName || "-", entry.action || "-", formatBillFlowOperator(entry.operator, entry.automatic, entry.operator?.id?.toLowerCase().includes("ebot")), entry.durationMs !== undefined ? `${Math.round(entry.durationMs / 1000)} 秒` : "-", entry.skipped ? "自动跳过" : entry.automatic ? "自动" : "", entry.comment || "-", ]); const pendingRows = flow.pending.map((node) => [ node.state || "-", node.name || "-", node.type || "-", node.approver?.name || node.approvers?.map((person) => person.name).filter(Boolean).join(", ") || "-", ]); const output = [ `====== 审批流程 (单号: ${flow.billCode || target} | 状态: ${flow.state || "-"}) ======`, `流程 ID: ${flow.flowId || "-"} 当前节点: ${flow.currentNode || "-"}`, "", "--- 历史流转 ---", flow.history.length ? printTable(["时间", "节点", "动作", "处理人", "停留时长", "标记", "意见"], historyRows) : " (暂无历史记录)", "", "--- 后续节点 ---", flow.pending.length ? printTable(["状态", "节点", "类型", "审批人"], pendingRows) : " (暂无后续节点)", "", ].join("\n"); return { stdout: output, stderr: "", exitCode: 0 }; } export async function handleRisk(ctx: BillActionContext): Promise { 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 risk <单号|ID>"); const report = await getBillRisk(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 warningRows = report.warnings.map((warning) => [ warning.riskLevel || "-", warning.type || "-", warning.controlName || warning.title || "-", warning.messages.join(";") || "-", ]); const output = [ `====== 单据风控审查 (流程: ${report.flowId}) ======`, `预算超限: ${report.budgetExceeded ? "是" : "否"}`, "", "--- 风险预警 ---", report.warnings.length ? printTable(["等级", "类型", "规则", "提示"], warningRows) : " (暂无风险预警)", `发票风险: ${report.invoiceWarnings.length} 条`, "", ].join("\n"); return { stdout: output, stderr: "", exitCode: 0 }; } export async function handleBudget(ctx: BillActionContext): Promise { 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 budget <单号|ID>"); const report = await getBillRisk(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, flowId: report.flowId, budgetExceeded: report.budgetExceeded, budget: report.budget || {} }, runtime.outputOptions), stderr: "", exitCode: 0 }; } const budget = report.budget || {}; const output = [ `====== 单据预算状态 (流程: ${report.flowId}) ======`, `预算超限: ${report.budgetExceeded ? "是" : "否"}`, `状态: ${typeof budget.status === "string" ? budget.status : "-"}`, `可用余额: ${budget.availableAmount ?? budget.available ?? "-"}`, `占用金额: ${budget.occupiedAmount ?? budget.occupied ?? "-"}`, `预算明细: ${Object.keys(budget).length ? JSON.stringify(budget) : "-"}`, "", ].join("\n"); return { stdout: output, stderr: "", exitCode: 0 }; }