From 6468710730528b47617825a6f68679daeb11949c Mon Sep 17 00:00:00 2001 From: yunyingk Date: Sat, 5 Sep 2026 19:45:21 +0800 Subject: [PATCH] feat: add CLI bill risk review --- skills/hose-use/SKILL.md | 1 + skills/hose-use/references/bill.md | 3 ++ src/cli/commands/bill.ts | 34 +++++++++++++++++++- src/cli/printer.ts | 3 +- src/domains/bill/api.ts | 31 ++++++++++++++++++ src/domains/bill/tools.ts | 51 +++++++++++++++++++++++++++++- src/domains/bill/types.ts | 20 ++++++++++++ 7 files changed, 140 insertions(+), 3 deletions(-) diff --git a/skills/hose-use/SKILL.md b/skills/hose-use/SKILL.md index 4b1e8ed..db8ae23 100644 --- a/skills/hose-use/SKILL.md +++ b/skills/hose-use/SKILL.md @@ -34,6 +34,7 @@ metadata: | 企业租户切换 / 查看当前企业 | `hose corps list`, `hose corps switch ` | [references/auth.md](references/auth.md) | | 报销单据检索 / 费用明细下钻 / 中文字典 | `hose bill list`, `hose bill get <单号|ID>` | [references/bill.md](references/bill.md) | | 单据审批历史 / 当前节点 / 后续流程 | `hose bill flow <单号|ID>` | [references/bill.md](references/bill.md) | +| 单据风控预警 / 预算状态 / 发票风险 | `hose bill risk <单号|ID>` | [references/bill.md](references/bill.md) | | 单据 Web 详情直达链接生成 | `hose bill link <单号|ID>` | [references/bill.md](references/bill.md) | | 单据实体模型与字段元数据查询 | `hose bill schema` | [references/bill.md](references/bill.md) | | 企业通讯录检索 / 员工档案 / 防爆保护 | `hose contact search`, `hose contact list`, `hose contact get` | [references/contact.md](references/contact.md) | diff --git a/skills/hose-use/references/bill.md b/skills/hose-use/references/bill.md index 20f3299..67c070b 100644 --- a/skills/hose-use/references/bill.md +++ b/skills/hose-use/references/bill.md @@ -56,8 +56,11 @@ hose-use bill flow CL20260903001 ```bash hose-use bill flow CL20260903001 hose-use bill flow CL20260903001 --json +hose-use bill risk CL20260903001 ``` +`bill risk` 聚合审批页“审查”面板的风控预警、信用规则、预算状态与发票风险接口;默认只输出摘要,`--json` 保留结构化原始结果。 + ## 4. 单据实体元数据字典 (`bill schema`) 用于探查当前企业所有单据实体、字段类型与扩展属性定义: diff --git a/src/cli/commands/bill.ts b/src/cli/commands/bill.ts index e999843..4cd23f8 100644 --- a/src/cli/commands/bill.ts +++ b/src/cli/commands/bill.ts @@ -6,6 +6,7 @@ import { searchAndFormatBills, getBillByCodeOrId, getBillFlow, + getBillRisk, getFieldDictionary, } from "../../domains/bill/tools.js"; @@ -236,6 +237,37 @@ export async function handleBillCommand( return { stdout: output, stderr: "", exitCode: 0 }; } + case "risk": + case "review": + case "warnings": { + const target = parsed.args[0] || stringOption(parsed.options, ["code", "id"]); + if (!target) throw new Error("请提供要查询风控结果的单号或单据 ID: hose bill risk <单号|ID>"); + const report = await getBillRisk(client, 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 }; + } + case "link": case "url": { const target = parsed.args[0] || stringOption(parsed.options, ["code", "id", "c"]); @@ -298,6 +330,6 @@ export async function handleBillCommand( } default: - throw new Error(`未知 bill 子命令: ${action}。支持: list, get <单号>, flow <单号>, schema, link <单号>`); + throw new Error(`未知 bill 子命令: ${action}。支持: list, get <单号>, flow <单号>, risk <单号>, schema, link <单号>`); } } diff --git a/src/cli/printer.ts b/src/cli/printer.ts index c918e9d..76b3f2f 100644 --- a/src/cli/printer.ts +++ b/src/cli/printer.ts @@ -119,7 +119,7 @@ export function printHelp(): string { corps 多企业租户管理 (list, switch, current) config 全局运行模式与路径诊断 (show, paths, mode , set) workflow 工作流管理 (list, get, find, toggle) - bill 单据与费用明细检索 (list, get <单号|ID>, flow <单号|ID>, schema, link <单号|ID>) + bill 单据与费用明细检索 (list, get <单号|ID>, flow <单号|ID>, risk <单号|ID>, schema, link <单号|ID>) contact 企业通讯录与人员检索 (list, search <关键词>, get ) task 待办中心与审批任务 (todo, approved, records, submitted, cc) message 消息中心与通知管理 (list, count, read, read-all) @@ -161,6 +161,7 @@ export function printHelp(): string { hose-use workflow find --task=code-js2 # 搜索包含自定义脚本的工作流 hose-use bill list # 查看当前企业单据与报销列表 hose-use bill flow S26000001 # 查看单据审批历史与后续节点 + hose-use bill risk S26000001 # 查看风控预警、预算与发票风险 hose-use bill link CL20260901001 # 获取单据 Web 详情直达免密跳转链接 hose-use bill list --json -o /tmp/bills.json # 导出单据报文到本地磁盘并防爆 hose-use bill get n0GMIM0dkQmKbS # 根据单号精准查询单据结构化详情 diff --git a/src/domains/bill/api.ts b/src/domains/bill/api.ts index c0cfd98..498e50e 100644 --- a/src/domains/bill/api.ts +++ b/src/domains/bill/api.ts @@ -73,6 +73,37 @@ export async function getRawBillFlow( return json.value ?? json.items?.[0]; } +export async function getRawBillRisk( + client: KyInstance, + corpId: string, + flowId: string, + submitterId?: string +): Promise> { + const searchParams = (extra: Record = {}) => ({ corpId, ...extra }); + const requests: Array> = [ + client.get("api/credit/v2/model", { searchParams: searchParams() }).then((response) => response.json()), + client.get(`api/credit/v2/rules/byFlowId/withGroup/$${flowId}`, { searchParams: searchParams({ flowId }) }).then((response) => response.json()), + client.get(`api/credit/v2/notes/byFlowId/$${flowId}`, { searchParams: searchParams({ flowId }) }).then((response) => response.json()), + client.get(`api/flow/v2/riskwarning/coalition/info/$${flowId}`, { searchParams: searchParams({ flowId, privilegeId: "", newUI: "v2" }) }).then((response) => response.json()), + client.get(`api/flow/v3/riskwarning/invoice/riskWarning/[${flowId}]`, { searchParams: searchParams({ flowId }) }).then((response) => response.json()), + client.get(`api/v1/budget/report/flow/$${flowId}/status`, { searchParams: searchParams({ id: flowId }) }).then((response) => response.json()), + ]; + if (submitterId) { + requests.push(client.get(`api/credit/v2/stafflog/point/$${submitterId}`, { searchParams: searchParams({ submitterId }) }).then((response) => response.json())); + } + const results = await Promise.allSettled(requests); + const values = results.map((result) => result.status === "fulfilled" ? result.value : {}); + return { + creditModel: values[0] as Record, + rules: values[1] as Record, + notes: values[2] as Record, + coalition: values[3] as Record, + invoice: values[4] as Record, + budget: values[5] as Record, + staff: (submitterId ? values[6] : {}) as Record, + }; +} + // ── 原始单据检索 API ────────────────────────────────────────────────────────── export async function searchRawBills( diff --git a/src/domains/bill/tools.ts b/src/domains/bill/tools.ts index 708a7bf..8448bf6 100644 --- a/src/domains/bill/tools.ts +++ b/src/domains/bill/tools.ts @@ -1,6 +1,6 @@ import { z } from "zod"; import { KyInstance } from "ky"; -import { searchRawBills, getRawBillsByIds, listEntitySchemas, getRawBillFlow } from "./api.js"; +import { searchRawBills, getRawBillsByIds, listEntitySchemas, getRawBillFlow, getRawBillRisk } from "./api.js"; import { type RawBillItem, type FormattedBill, @@ -10,6 +10,8 @@ import { type BillFlowEntry, type BillFlowNode, type BillFlowTimeline, + type BillRiskReport, + type BillRiskWarning, } from "./types.js"; import { CorpContext } from "../../core/config/manager.js"; import { createEkbClient } from "../../core/auth/ekb.js"; @@ -231,6 +233,12 @@ function flowPerson(value: unknown): { id?: string; name?: string; code?: string }; } +function unwrapValue(value: unknown): Record { + if (!value || typeof value !== "object") return {}; + const object = value as Record; + return object.value && typeof object.value === "object" ? object.value as Record : object; +} + export async function getBillFlow( client: KyInstance, corpId: string, @@ -295,6 +303,47 @@ export async function getBillFlow( }; } +export async function getBillRisk( + client: KyInstance, + corpId: string, + target: string +): Promise { + const rawFlow = await getRawBillFlow(client, corpId, target); + if (!rawFlow || !rawFlow.flowId || typeof rawFlow.flowId !== "object") return undefined; + const flow = rawFlow.flowId as Record; + const flowId = typeof flow.id === "string" ? flow.id : undefined; + if (!flowId) return undefined; + const form = flow.form && typeof flow.form === "object" ? flow.form as Record : {}; + const submitter = form.submitterId && typeof form.submitterId === "object" ? form.submitterId as Record : {}; + const submitterId = typeof submitter.id === "string" ? submitter.id : undefined; + const rawRisk = await getRawBillRisk(client, corpId, flowId, submitterId); + const coalition = unwrapValue(rawRisk.coalition); + const rawWarnings = Array.isArray(coalition.riskWarning) ? coalition.riskWarning : []; + const warnings: BillRiskWarning[] = rawWarnings.map((value) => { + const item = value && typeof value === "object" ? value as Record : {}; + return { + riskLevel: typeof item.riskLevel === "string" ? item.riskLevel : undefined, + type: typeof item.type === "string" ? item.type : undefined, + title: typeof item.title === "string" ? item.title : undefined, + controlName: typeof item.controlName === "string" ? item.controlName : undefined, + messages: Array.isArray(item.messages) ? item.messages.filter((message): message is string => typeof message === "string") : [], + raw: item, + }; + }); + const invoice = unwrapValue(rawRisk.invoice); + const budget = unwrapValue(rawRisk.budget); + return { + flowId, + budgetExceeded: budget.value === true, + credit: unwrapValue(rawRisk.staff), + rules: unwrapValue(rawRisk.rules), + notes: unwrapValue(rawRisk.notes), + warnings, + invoiceWarnings: Array.isArray(invoice.items) ? invoice.items.filter((item): item is Record => Boolean(item && typeof item === "object")) : [], + raw: rawRisk, + }; +} + // ── 业务操作:多条件搜索与分页单据 ───────────────────────────────────────────── export async function searchAndFormatBills( diff --git a/src/domains/bill/types.ts b/src/domains/bill/types.ts index e15413b..c7a4c9c 100644 --- a/src/domains/bill/types.ts +++ b/src/domains/bill/types.ts @@ -136,3 +136,23 @@ export interface BillFlowTimeline { pending: BillFlowNode[]; raw: Record; } + +export interface BillRiskWarning { + riskLevel?: string; + type?: string; + title?: string; + controlName?: string; + messages: string[]; + raw: Record; +} + +export interface BillRiskReport { + flowId: string; + budgetExceeded?: boolean; + credit?: Record; + rules?: Record; + notes?: Record; + warnings: BillRiskWarning[]; + invoiceWarnings: Record[]; + raw: Record; +}