Merge pull request #21 from yunyingk/feat/issue-7-bill-risk

feat: add CLI bill risk review
This commit is contained in:
yunyingk authored and GitHub committed 2026-09-05 19:46:09 +08:00
commit dbf8dd942e
7 files changed
+140 -3

No files matched your search

+1
View File
@@ -34,6 +34,7 @@ metadata:
| 企业租户切换 / 查看当前企业 | `hose corps list`, `hose corps switch <id>` | [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) |
+3
View File
@@ -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`)
用于探查当前企业所有单据实体、字段类型与扩展属性定义:
+33 -1
View File
@@ -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 <单号>`);
}
}
+2 -1
View File
@@ -119,7 +119,7 @@ export function printHelp(): string {
corps 多企业租户管理 (list, switch, current)
config 全局运行模式与路径诊断 (show, paths, mode <stateful|stateless>, 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 <staffId>)
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 # 根据单号精准查询单据结构化详情
+31
View File
@@ -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<Record<string, unknown>> {
const searchParams = (extra: Record<string, string> = {}) => ({ corpId, ...extra });
const requests: Array<Promise<unknown>> = [
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<string, unknown>,
rules: values[1] as Record<string, unknown>,
notes: values[2] as Record<string, unknown>,
coalition: values[3] as Record<string, unknown>,
invoice: values[4] as Record<string, unknown>,
budget: values[5] as Record<string, unknown>,
staff: (submitterId ? values[6] : {}) as Record<string, unknown>,
};
}
// ── 原始单据检索 API ──────────────────────────────────────────────────────────
export async function searchRawBills(
+50 -1
View File
@@ -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<string, unknown> {
if (!value || typeof value !== "object") return {};
const object = value as Record<string, unknown>;
return object.value && typeof object.value === "object" ? object.value as Record<string, unknown> : 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<BillRiskReport | undefined> {
const rawFlow = await getRawBillFlow(client, corpId, target);
if (!rawFlow || !rawFlow.flowId || typeof rawFlow.flowId !== "object") return undefined;
const flow = rawFlow.flowId as Record<string, unknown>;
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<string, unknown> : {};
const submitter = form.submitterId && typeof form.submitterId === "object" ? form.submitterId as Record<string, unknown> : {};
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<string, unknown> : {};
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<string, unknown> => Boolean(item && typeof item === "object")) : [],
raw: rawRisk,
};
}
// ── 业务操作:多条件搜索与分页单据 ─────────────────────────────────────────────
export async function searchAndFormatBills(
+20
View File
@@ -136,3 +136,23 @@ export interface BillFlowTimeline {
pending: BillFlowNode[];
raw: Record<string, unknown>;
}
export interface BillRiskWarning {
riskLevel?: string;
type?: string;
title?: string;
controlName?: string;
messages: string[];
raw: Record<string, unknown>;
}
export interface BillRiskReport {
flowId: string;
budgetExceeded?: boolean;
credit?: Record<string, unknown>;
rules?: Record<string, unknown>;
notes?: Record<string, unknown>;
warnings: BillRiskWarning[];
invoiceWarnings: Record<string, unknown>[];
raw: Record<string, unknown>;
}