feat(bill,flow): add unified bill trace and flow get diagnostic engine (Phase 2)
This commit is contained in:
1 parent
7891959853
commit
ffec604ed9
10 files changed
+970
-32
No files matched your search
@@ -41,5 +41,11 @@ src/domains/bill/
|
||||
- `transferBill` 与 `addNodeBill` 共用 backlog 动作接口,分别使用 `SHIFT_NODE`、`PRE_ADD_NODE`/`AFT_ADD_NODE`;
|
||||
- `commentBill` 调用 `POST /api/flow/v1/flows/comment/$<flowId>`,使用 `freeflow.comment`,保留参与人和私密评论参数;
|
||||
- 动作执行前从审批详情解析 backlog ID、流程 ID、当前节点 ID,并可依据 `flow.actions` 校验当前身份是否具备该动作权限。
|
||||
8. **全域单据穿透与待办诊断 (`traceBillFlowDetail`)**:
|
||||
- 跨 `backlogs/search`、`flows/my`、`flows/approved`、`carbonCopy/search` 与 `backlogs/id` 多通道穿透检索单据流程实例;
|
||||
- 智能提取单据金额、提报人、提报时间、归属部门、当前驻留节点与待办审批人列表;
|
||||
- 内置待办根因诊断引擎,智能判定流转阶段(`DRAFT`、`APPROVING`、`PAYING`、`PAID`、`REJECTED`):
|
||||
- 识别“待出纳支付/打款”节点(如 `PAYING` 或节点名包含“打款/出纳/支付”),精准提示不可审批及自动化工作流调用报错 400(无待办审批事项)的根本原因与排障建议;
|
||||
- 结构化还原流转时间线轨迹(Timeline),自动标记 Ebot 智能审批与自动跳过节点。
|
||||
|
||||
CLI 入口为 `hose-use bill approve|reject|transfer|add-node|comment <单号|ID>`。所有会改变状态的动作都要求 `--yes`(交互终端未提供时会拒绝执行);转交和加签的 `--approver-id` 支持完整员工 ID、工号或唯一姓名匹配,驳回强制要求 `--comment`。
|
||||
CLI 入口为 `hose-use bill approve|reject|transfer|add-node|comment <单号|ID>`,以及 `hose-use flow [get|trace] <单号|ID>` / `hose-use bill trace <单号|ID>`。所有会改变状态的动作都要求 `--yes`(交互终端未提供时会拒绝执行);转交和加签的 `--approver-id` 支持完整员工 ID、工号或唯一姓名匹配,驳回强制要求 `--comment`。
|
||||
+73
-27
@@ -63,12 +63,16 @@ export async function getRawBillFlow(
|
||||
corpId: string,
|
||||
target: string
|
||||
): Promise<Record<string, unknown> | undefined> {
|
||||
const cleanTarget = target.trim();
|
||||
let match: Record<string, unknown> | undefined;
|
||||
const filters = [
|
||||
`(id==${quoted(target)})||((flowId.id==${quoted(target)})||(flowId.form.code==${quoted(target)}))`,
|
||||
|
||||
// 通道 1: api/flow/v2/backlogs/search 精准单号与 ID 匹配
|
||||
const searchFilters = [
|
||||
`(id==${quoted(cleanTarget)})||((flowId.id==${quoted(cleanTarget)})||(flowId.form.code==${quoted(cleanTarget)}))`,
|
||||
`(flowId.form.code==${quoted(cleanTarget)})`,
|
||||
`(state.in("APPROVING","PROCESSED","REJECTED","DRAFT"))`,
|
||||
];
|
||||
for (const filterBy of filters) {
|
||||
for (const filterBy of searchFilters) {
|
||||
try {
|
||||
const lookup = await client.post("api/flow/v2/backlogs/search", {
|
||||
searchParams: { corpId },
|
||||
@@ -82,36 +86,78 @@ export async function getRawBillFlow(
|
||||
match = (lookupJson.items ?? []).find((item) => {
|
||||
const flow = item.flowId && typeof item.flowId === "object" ? item.flowId as Record<string, unknown> : {};
|
||||
const form = flow.form && typeof flow.form === "object" ? flow.form as Record<string, unknown> : {};
|
||||
return item.id === target || flow.id === target || form.code === target;
|
||||
return item.id === cleanTarget || flow.id === cleanTarget || form.code === cleanTarget;
|
||||
});
|
||||
if (match) break;
|
||||
} catch {
|
||||
// Fall through to the broad status lookup or direct detail request.
|
||||
// 继续尝试其他检索
|
||||
}
|
||||
}
|
||||
const backlogId = typeof match?.id === "string" ? match.id : target;
|
||||
|
||||
const detail = await client.post("api/flow/v2/backlogs/id", {
|
||||
searchParams: (() => {
|
||||
const params = new URLSearchParams({ corpId });
|
||||
for (const join of [
|
||||
"flowId.form.submitterId,submitterId,/v1/organization/staffs?join=defaultDepartment,defaultDepartment,/v1/organization/departments",
|
||||
"flowId.form.ownerId,ownerId,/v1/organization/staffs?select=id,name,code",
|
||||
"flowId.form.expenseDepartment,expenseDepartment,/v1/organization/departments",
|
||||
"flowId.logs.operatorId,operatorId,/v1/organization/staffs?select=id,name,code",
|
||||
"flowId.logs.nextOperatorId,nextOperatorId,/v1/organization/staffs?select=id,name,code",
|
||||
"flowId.plan.nodes.approverId,approverId,/v1/organization/staffs?select=id,name,code",
|
||||
"flowId.plan.nodes.counterSigners.signerId,signerId,/v1/organization/staffs?select=id,name,code",
|
||||
]) params.append("join", join);
|
||||
return params;
|
||||
})(),
|
||||
json: {
|
||||
filterBy: `(id==${quoted(backlogId)})`,
|
||||
select: flowDetailSelect(),
|
||||
},
|
||||
});
|
||||
const json = (await detail.json()) as { value?: Record<string, unknown>; items?: Record<string, unknown>[] };
|
||||
return json.value ?? json.items?.[0];
|
||||
// 通道 2: 尝试从我发起的流程与已审批单据中检索 (flows/my & flows/approved)
|
||||
if (!match) {
|
||||
const endpoints = ["api/flow/v1/flows/my", "api/flow/v1/flows/approved", "api/flow/v2/carbonCopy/search"];
|
||||
for (const ep of endpoints) {
|
||||
try {
|
||||
const res = await client.post(ep, {
|
||||
searchParams: { corpId },
|
||||
json: {
|
||||
limit: { start: 0, count: 50 },
|
||||
filterBy: `(form.code==${quoted(cleanTarget)})||(flowId.form.code==${quoted(cleanTarget)})`,
|
||||
},
|
||||
});
|
||||
const json = (await res.json()) as { items?: Record<string, unknown>[] };
|
||||
const found = (json.items ?? []).find((item) => {
|
||||
const flow = item.flowId && typeof item.flowId === "object" ? item.flowId as Record<string, unknown> : {};
|
||||
const form = (flow.form ?? item.form) as Record<string, unknown> | undefined;
|
||||
return item.id === cleanTarget || flow.id === cleanTarget || form?.code === cleanTarget;
|
||||
});
|
||||
if (found) {
|
||||
match = found;
|
||||
break;
|
||||
}
|
||||
} catch {
|
||||
// 忽略通道异常
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const backlogId = typeof match?.id === "string" ? match.id : cleanTarget;
|
||||
const flowIdFromMatch = match?.flowId && typeof match.flowId === "object" && typeof (match.flowId as any).id === "string"
|
||||
? (match.flowId as any).id
|
||||
: undefined;
|
||||
|
||||
// 通道 3: 调用 api/flow/v2/backlogs/id 获取完整结构化详情
|
||||
try {
|
||||
const filterByCond = flowIdFromMatch
|
||||
? `(id==${quoted(backlogId)})||(flowId.id==${quoted(flowIdFromMatch)})`
|
||||
: `(id==${quoted(backlogId)})||((flowId.id==${quoted(cleanTarget)})||(flowId.form.code==${quoted(cleanTarget)}))`;
|
||||
|
||||
const detail = await client.post("api/flow/v2/backlogs/id", {
|
||||
searchParams: (() => {
|
||||
const params = new URLSearchParams({ corpId });
|
||||
for (const join of [
|
||||
"flowId.form.submitterId,submitterId,/v1/organization/staffs?join=defaultDepartment,defaultDepartment,/v1/organization/departments",
|
||||
"flowId.form.ownerId,ownerId,/v1/organization/staffs?select=id,name,code",
|
||||
"flowId.form.expenseDepartment,expenseDepartment,/v1/organization/departments",
|
||||
"flowId.logs.operatorId,operatorId,/v1/organization/staffs?select=id,name,code",
|
||||
"flowId.logs.nextOperatorId,nextOperatorId,/v1/organization/staffs?select=id,name,code",
|
||||
"flowId.plan.nodes.approverId,approverId,/v1/organization/staffs?select=id,name,code",
|
||||
"flowId.plan.nodes.counterSigners.signerId,signerId,/v1/organization/staffs?select=id,name,code",
|
||||
]) params.append("join", join);
|
||||
return params;
|
||||
})(),
|
||||
json: {
|
||||
filterBy: filterByCond,
|
||||
select: flowDetailSelect(),
|
||||
},
|
||||
});
|
||||
const json = (await detail.json()) as { value?: Record<string, unknown>; items?: Record<string, unknown>[] };
|
||||
return json.value ?? json.items?.[0] ?? match;
|
||||
} catch (err) {
|
||||
if (match) return match;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export interface BillActionTarget {
|
||||
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
normalizeSearchFields,
|
||||
buildServerBillFilterBy,
|
||||
searchAndFormatBills,
|
||||
traceBillFlowDetail,
|
||||
} from "./tools.js";
|
||||
import { RawBillItemSchema, EntitySchema } from "./types.js";
|
||||
|
||||
@@ -712,5 +713,130 @@ describe("Bill Domain", () => {
|
||||
expect(resDefault.items.map((b) => b.billId)).toEqual(["bill-2", "bill-3"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("traceBillFlowDetail 全域穿透诊断与待办根因分析", () => {
|
||||
it("待审批状态:精准解析提报人、金额、当前节点与待办审批人,判定可审批", async () => {
|
||||
const mockRawFlow = {
|
||||
id: "backlog-101",
|
||||
state: "APPROVING",
|
||||
flowId: {
|
||||
id: "flow-101",
|
||||
state: "APPROVING",
|
||||
form: {
|
||||
code: "BX-20261014-001",
|
||||
title: "差旅费用报销单",
|
||||
expenseMoney: { amount: "1280.50" },
|
||||
submitDate: 1781682826000,
|
||||
submitterId: { id: "staff-1", name: "张三", code: "ZS01" },
|
||||
expenseDepartment: { name: "研发部" },
|
||||
specificationId: { id: "spec-bx", name: "日常报销单" },
|
||||
},
|
||||
plan: {
|
||||
nextNodeId: "node-2",
|
||||
nodes: [
|
||||
{ id: "node-1", name: "提单", state: "PROCESSED" },
|
||||
{ id: "node-2", name: "直属主管审批", state: "APPROVING", approverId: { id: "staff-2", name: "李主管", code: "LZ01" } },
|
||||
],
|
||||
},
|
||||
logs: [
|
||||
{
|
||||
action: "SUBMIT",
|
||||
time: 1781682826000,
|
||||
operatorId: { id: "staff-1", name: "张三" },
|
||||
attributes: { nodeName: "提单", comment: "请领导审批" },
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
const client = {
|
||||
post: async (path: string) => {
|
||||
if (path.includes("backlogs/search")) {
|
||||
return new Response(JSON.stringify({ items: [mockRawFlow] }));
|
||||
}
|
||||
if (path.includes("backlogs/id")) {
|
||||
return new Response(JSON.stringify({ value: mockRawFlow }));
|
||||
}
|
||||
return new Response(JSON.stringify({ items: [] }));
|
||||
},
|
||||
} as any;
|
||||
|
||||
const report = await traceBillFlowDetail(client, "test-corp", "BX-20261014-001");
|
||||
expect(report.found).toBe(true);
|
||||
expect(report.billCode).toBe("BX-20261014-001");
|
||||
expect(report.amount).toBe("¥ 1280.50");
|
||||
expect(report.submitter?.name).toBe("张三");
|
||||
expect(report.department).toBe("研发部");
|
||||
expect(report.state).toBe("审批中");
|
||||
expect(report.currentNode?.name).toBe("直属主管审批");
|
||||
expect(report.pendingApprovers).toHaveLength(1);
|
||||
expect(report.pendingApprovers[0].name).toBe("李主管");
|
||||
expect(report.diagnostic.stage).toBe("APPROVING");
|
||||
expect(report.diagnostic.canApprove).toBe(true);
|
||||
expect(report.diagnostic.summary).toContain("待办审批人: 李主管");
|
||||
expect(report.timeline).toHaveLength(1);
|
||||
expect(report.timeline[0].action).toBe("SUBMIT");
|
||||
});
|
||||
|
||||
it("待出纳打款节点 (PAYING):精准识别并诊断 400 根因与排障建议", async () => {
|
||||
const mockRawFlow = {
|
||||
id: "backlog-paying-01",
|
||||
state: "APPROVING",
|
||||
nodeName: "出纳打款",
|
||||
flowId: {
|
||||
id: "flow-paying-01",
|
||||
state: "PAYING",
|
||||
form: {
|
||||
code: "ZJ-20261014-999",
|
||||
title: "对公转账调拨",
|
||||
payMoney: "50000.00",
|
||||
specificationId: { id: "spec-zj", name: "资金调拨单" },
|
||||
},
|
||||
plan: {
|
||||
nodes: [
|
||||
{ id: "n1", name: "财务复核", state: "PROCESSED" },
|
||||
{ id: "n2", name: "出纳打款", state: "APPROVING" },
|
||||
],
|
||||
},
|
||||
logs: [
|
||||
{
|
||||
action: "AGREE",
|
||||
time: 1781683000000,
|
||||
operatorId: { id: "ebot_system", name: "ebot" },
|
||||
attributes: { isEbotNode: true, nodeName: "智能审核", comment: "风控核验通过" },
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
const client = {
|
||||
post: async () => new Response(JSON.stringify({ value: mockRawFlow })),
|
||||
} as any;
|
||||
|
||||
const report = await traceBillFlowDetail(client, "test-corp", "ZJ-20261014-999");
|
||||
expect(report.found).toBe(true);
|
||||
expect(report.diagnostic.stage).toBe("PAYING");
|
||||
expect(report.diagnostic.canApprove).toBe(false);
|
||||
expect(report.diagnostic.stageLabel).toBe("待支付 / 出纳打款");
|
||||
expect(report.diagnostic.summary).toContain("出纳打款");
|
||||
expect(report.diagnostic.advice).toContain("400");
|
||||
expect(report.timeline[0].operator?.name).toBe("Ebot (智能审批)");
|
||||
expect(report.timeline[0].automatic).toBe(true);
|
||||
});
|
||||
|
||||
it("单据不存在时返回友好未找到诊断报告", async () => {
|
||||
const client = {
|
||||
post: async () => new Response(JSON.stringify({ items: [] })),
|
||||
} as any;
|
||||
|
||||
const report = await traceBillFlowDetail(client, "test-corp", "NOT-EXIST-999");
|
||||
expect(report.found).toBe(false);
|
||||
expect(report.target).toBe("NOT-EXIST-999");
|
||||
expect(report.diagnostic.canApprove).toBe(false);
|
||||
expect(report.diagnostic.stage).toBe("UNKNOWN");
|
||||
expect(report.diagnostic.advice).toContain("请核对单号是否准确");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -55,6 +55,8 @@ import {
|
||||
type BillRelationsReport,
|
||||
type BillAttachment,
|
||||
type BillAttachmentReport,
|
||||
type BillFlowTimelineItem,
|
||||
type BillFlowTraceDiagnostic,
|
||||
type FormSpecification,
|
||||
type FormSpecificationComponent,
|
||||
type FormPriorityItem,
|
||||
@@ -960,6 +962,258 @@ export async function getBillFlow(
|
||||
};
|
||||
}
|
||||
|
||||
function resolveFlowStage(
|
||||
rawState: string,
|
||||
nodeName?: string
|
||||
): "DRAFT" | "APPROVING" | "PAYING" | "PAID" | "REJECTED" | "UNKNOWN" {
|
||||
const s = rawState.toUpperCase();
|
||||
const n = (nodeName || "").toLowerCase();
|
||||
|
||||
if (s.includes("REJECT") || s === "TERMINATED") return "REJECTED";
|
||||
if (s === "PAID" || s === "COMPLETED" || s === "PROCESSED") return "PAID";
|
||||
if (s.includes("PAY") || n.includes("打款") || n.includes("出纳") || n.includes("支付")) return "PAYING";
|
||||
if (s.includes("APPROV") || s === "PENDING" || s === "SENDING") return "APPROVING";
|
||||
if (s.includes("DRAFT")) return "DRAFT";
|
||||
return "UNKNOWN";
|
||||
}
|
||||
|
||||
function buildDiagnosticReport(
|
||||
stage: "DRAFT" | "APPROVING" | "PAYING" | "PAID" | "REJECTED" | "UNKNOWN",
|
||||
rawState: string,
|
||||
currentNodeName?: string,
|
||||
pendingApprovers: { id?: string; name?: string; code?: string }[] = []
|
||||
): {
|
||||
canApprove: boolean;
|
||||
stage: "DRAFT" | "APPROVING" | "PAYING" | "PAID" | "REJECTED" | "UNKNOWN";
|
||||
stageLabel: string;
|
||||
summary: string;
|
||||
advice: string;
|
||||
} {
|
||||
const approverNames = pendingApprovers.map((p) => p.name || p.id).filter(Boolean).join("、");
|
||||
|
||||
switch (stage) {
|
||||
case "PAYING":
|
||||
return {
|
||||
canApprove: false,
|
||||
stage,
|
||||
stageLabel: "待支付 / 出纳打款",
|
||||
summary: `单据已通过全部分级审批流转,当前停留在【${currentNodeName || "出纳打款"}】环节`,
|
||||
advice: "该单据当前无待办审批任务,若调用自动审批接口必报 400(无待办审批事项)。出纳打款后单据即闭环;若属于自动化打款流水,请检查工作流触发器是否误推了待支付单据,建议增加条件【审批状态 等于 APPROVING】进行拦截。",
|
||||
};
|
||||
case "APPROVING":
|
||||
return {
|
||||
canApprove: true,
|
||||
stage,
|
||||
stageLabel: "审批中 (待办审批)",
|
||||
summary: `单据当前停留在【${currentNodeName || "审批"}】节点${approverNames ? `,待办审批人: ${approverNames}` : ""}`,
|
||||
advice: "单据处于正常待办审批状态,当前待办人员或工作流自动化可正常执行同意 (agree) 或驳回 (reject) 操作。",
|
||||
};
|
||||
case "REJECTED":
|
||||
return {
|
||||
canApprove: false,
|
||||
stage,
|
||||
stageLabel: "已驳回 / 已终止",
|
||||
summary: "单据已流转至驳回或终止状态",
|
||||
advice: "单据已终止流转,无法执行审批同意。请联系提报人员根据驳回意见重新修改并提交新单据。",
|
||||
};
|
||||
case "PAID":
|
||||
return {
|
||||
canApprove: false,
|
||||
stage,
|
||||
stageLabel: "已闭环 / 已打款",
|
||||
summary: "单据全流程已审批并通过出纳打款闭环",
|
||||
advice: "单据已结束流转,无需任何人工或自动化操作。",
|
||||
};
|
||||
case "DRAFT":
|
||||
return {
|
||||
canApprove: false,
|
||||
stage,
|
||||
stageLabel: "草稿未提交",
|
||||
summary: "单据尚未正式提交进入审批流",
|
||||
advice: "请提报人完成单据填写后点击提交。",
|
||||
};
|
||||
default:
|
||||
return {
|
||||
canApprove: false,
|
||||
stage,
|
||||
stageLabel: rawState || "未知状态",
|
||||
summary: `单据当前处于状态: ${rawState || "未知"}`,
|
||||
advice: "请通过 Web 后台或联系管理员核对单据流转配置。",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 全域单据穿透诊断与待办全貌洞察
|
||||
*/
|
||||
export async function traceBillFlowDetail(
|
||||
client: KyInstance,
|
||||
corpId: string,
|
||||
targetQuery: string
|
||||
): Promise<BillFlowTraceDiagnostic> {
|
||||
const cleanTarget = targetQuery.trim();
|
||||
if (!cleanTarget) {
|
||||
throw new ValidationError("请提供要穿透查询的单号或单据 ID");
|
||||
}
|
||||
|
||||
// 1. 尝试直接获取流程全貌
|
||||
let rawFlow = await getRawBillFlow(client, corpId, cleanTarget);
|
||||
|
||||
// 2. 如果直接流程未命中,尝试通过传统单据索引反查
|
||||
if (!rawFlow) {
|
||||
try {
|
||||
const bills = await getBillByCodeOrId(client, corpId, cleanTarget);
|
||||
if (bills.length > 0) {
|
||||
const firstBill = bills[0];
|
||||
const candidateId = firstBill.billId || firstBill.billCode;
|
||||
if (candidateId) {
|
||||
rawFlow = await getRawBillFlow(client, corpId, candidateId);
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// 容错并继续
|
||||
}
|
||||
}
|
||||
|
||||
if (!rawFlow) {
|
||||
return {
|
||||
found: false,
|
||||
target: cleanTarget,
|
||||
pendingApprovers: [],
|
||||
diagnostic: {
|
||||
canApprove: false,
|
||||
stage: "UNKNOWN",
|
||||
stageLabel: "未找到",
|
||||
summary: `在当前企业下未找到匹配 "${cleanTarget}" 的单据或流程实例`,
|
||||
advice: "请核对单号是否准确,或确认当前账号是否具备该业务流程的查看权限。",
|
||||
},
|
||||
timeline: [],
|
||||
};
|
||||
}
|
||||
|
||||
const flow = rawFlow.flowId && typeof rawFlow.flowId === "object" ? (rawFlow.flowId as Record<string, unknown>) : {};
|
||||
const form = flow.form && typeof flow.form === "object" ? (flow.form as Record<string, unknown>) : ((rawFlow.form as Record<string, unknown>) || {});
|
||||
const spec = form.specificationId && typeof form.specificationId === "object" ? (form.specificationId as Record<string, unknown>) : {};
|
||||
const nodeState = rawFlow.nodeState && typeof rawFlow.nodeState === "object" ? (rawFlow.nodeState as Record<string, unknown>) : {};
|
||||
const plan = flow.plan && typeof flow.plan === "object" ? (flow.plan as Record<string, unknown>) : {};
|
||||
const nodes = Array.isArray(plan.nodes) ? (plan.nodes as Record<string, unknown>[]) : [];
|
||||
const logs = Array.isArray(flow.logs) ? (flow.logs as Record<string, unknown>[]) : [];
|
||||
|
||||
// 基础信息提取
|
||||
const billCode = typeof form.code === "string" ? form.code : typeof rawFlow.code === "string" ? rawFlow.code : undefined;
|
||||
const billId = typeof rawFlow.id === "string" ? rawFlow.id : typeof form.id === "string" ? form.id : undefined;
|
||||
const flowId = typeof flow.id === "string" ? flow.id : undefined;
|
||||
const backlogId = typeof rawFlow.id === "string" ? rawFlow.id : undefined;
|
||||
const specificationName = typeof spec.name === "string" ? spec.name : undefined;
|
||||
const specificationId = typeof spec.id === "string" ? spec.id : undefined;
|
||||
const title = typeof form.title === "string" ? form.title : typeof form.name === "string" ? form.name : undefined;
|
||||
|
||||
// 金额提取
|
||||
let amountStr: string | undefined;
|
||||
const rawMoney = form.expenseMoney || form.payMoney || form.loanMoney || form.requisitionMoney || form.amount;
|
||||
if (rawMoney && typeof rawMoney === "object") {
|
||||
const std = (rawMoney as any).standard ?? (rawMoney as any).amount;
|
||||
if (std !== undefined) amountStr = `¥ ${std}`;
|
||||
} else if (typeof rawMoney === "string" || typeof rawMoney === "number") {
|
||||
amountStr = `¥ ${rawMoney}`;
|
||||
}
|
||||
|
||||
// 提交人与部门
|
||||
const submitter = flowPerson(form.submitterId || flow.ownerId);
|
||||
const deptObj = form.expenseDepartment || form.defaultDepartment;
|
||||
const department = deptObj && typeof deptObj === "object" ? (deptObj as any).name : undefined;
|
||||
const submitDate = typeof form.submitDate === "number" ? new Date(form.submitDate).toLocaleString("zh-CN", { hour12: false }) : undefined;
|
||||
|
||||
// 状态提取
|
||||
const rawState = String(rawFlow.state || flow.state || form.state || "").toUpperCase();
|
||||
const stateLabel = translateBillFlowState(rawState);
|
||||
|
||||
// 当前驻留节点提取
|
||||
const nextNodeId = typeof plan.nextNodeId === "string" ? plan.nextNodeId : undefined;
|
||||
const currentNodeName = typeof rawFlow.nodeName === "string" ? rawFlow.nodeName : typeof nodeState.nodeName === "string" ? nodeState.nodeName : undefined;
|
||||
|
||||
let activeNode = nodes.find((n) => (nextNodeId && n.id === nextNodeId) || (currentNodeName && n.name === currentNodeName));
|
||||
if (!activeNode && nodes.length > 0) {
|
||||
activeNode = nodes.find((n) => n.state === "APPROVING" || n.state === "approving" || n.status === "ACTIVE");
|
||||
}
|
||||
|
||||
const currentNode = activeNode
|
||||
? {
|
||||
id: typeof activeNode.id === "string" ? activeNode.id : undefined,
|
||||
name: typeof activeNode.name === "string" ? activeNode.name : currentNodeName,
|
||||
type: typeof activeNode.type === "string" ? activeNode.type : undefined,
|
||||
}
|
||||
: currentNodeName
|
||||
? { name: currentNodeName }
|
||||
: undefined;
|
||||
|
||||
// 待办审批人提取
|
||||
const pendingApprovers: { id?: string; name?: string; code?: string }[] = [];
|
||||
if (activeNode) {
|
||||
if (activeNode.approverId) {
|
||||
const p = flowPerson(activeNode.approverId);
|
||||
if (p) pendingApprovers.push(p);
|
||||
}
|
||||
if (Array.isArray(activeNode.counterSigners)) {
|
||||
for (const signer of activeNode.counterSigners) {
|
||||
const p = flowPerson(signer);
|
||||
if (p) pendingApprovers.push(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 诊断规则引擎
|
||||
const stage = resolveFlowStage(rawState, currentNode?.name);
|
||||
const diagnostic = buildDiagnosticReport(stage, rawState, currentNode?.name, pendingApprovers);
|
||||
|
||||
// 流转时间线
|
||||
const timeline: BillFlowTimelineItem[] = logs.map((log, index) => {
|
||||
const attrs = (log.attributes as Record<string, unknown>) || {};
|
||||
const op = flowPerson(log.operatorId);
|
||||
const isEbot = attrs.isEbotNode === true || `${op?.id || ""} ${op?.name || ""}`.toLowerCase().includes("ebot");
|
||||
const labeledOp = isEbot ? { ...(op || { id: "ebot" }), name: "Ebot (智能审批)" } : op;
|
||||
const timeNum = typeof log.time === "number" ? log.time : undefined;
|
||||
|
||||
return {
|
||||
index: index + 1,
|
||||
time: timeNum ? new Date(timeNum).toLocaleString("zh-CN", { hour12: false }) : undefined,
|
||||
timestamp: timeNum,
|
||||
action: translateBillFlowAction(typeof log.action === "string" ? log.action : undefined),
|
||||
rawAction: typeof log.action === "string" ? log.action : undefined,
|
||||
state: translateBillFlowState(typeof log.state === "string" ? log.state : undefined),
|
||||
rawState: typeof log.state === "string" ? log.state : undefined,
|
||||
nodeName: typeof attrs.nodeName === "string" ? attrs.nodeName : typeof attrs.nextName === "string" ? attrs.nextName : undefined,
|
||||
operator: labeledOp,
|
||||
comment: typeof attrs.comment === "string" ? attrs.comment.replace(/<[^>]+>/g, "").trim() : undefined,
|
||||
automatic: attrs.isEbotNode === true || attrs.isAuto === true || !log.operatorId,
|
||||
skipped: attrs.isSkip === true || attrs.skipped === true || log.state === "SKIPPED",
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
found: true,
|
||||
target: cleanTarget,
|
||||
billCode,
|
||||
billId,
|
||||
flowId,
|
||||
backlogId,
|
||||
specificationName,
|
||||
specificationId,
|
||||
title,
|
||||
amount: amountStr,
|
||||
submitter,
|
||||
department,
|
||||
submitDate,
|
||||
state: stateLabel,
|
||||
rawState,
|
||||
currentNode,
|
||||
pendingApprovers,
|
||||
diagnostic,
|
||||
timeline,
|
||||
raw: rawFlow,
|
||||
};
|
||||
}
|
||||
|
||||
function actionTarget(raw: Record<string, unknown>, fallback: string): BillActionTarget {
|
||||
const flow = raw.flowId && typeof raw.flowId === "object" ? raw.flowId as Record<string, unknown> : {};
|
||||
const form = flow.form && typeof flow.form === "object" ? flow.form as Record<string, unknown> : {};
|
||||
|
||||
@@ -290,3 +290,52 @@ export interface BillAttachmentReport {
|
||||
attachments: BillAttachment[];
|
||||
raw: Record<string, unknown>;
|
||||
}
|
||||
|
||||
// ── 流程单据全貌穿透与待办诊断模型 ──────────────────────────────────────────
|
||||
|
||||
export interface BillFlowTimelineItem {
|
||||
index: number;
|
||||
time?: string;
|
||||
timestamp?: number;
|
||||
durationMs?: number;
|
||||
action?: string;
|
||||
rawAction?: string;
|
||||
state?: string;
|
||||
rawState?: string;
|
||||
nodeName?: string;
|
||||
operator?: { id?: string; name?: string; code?: string };
|
||||
nextOperators?: { id?: string; name?: string; code?: string }[];
|
||||
comment?: string;
|
||||
automatic?: boolean;
|
||||
skipped?: boolean;
|
||||
}
|
||||
|
||||
export interface BillFlowTraceDiagnostic {
|
||||
found: boolean;
|
||||
target: string;
|
||||
billCode?: string;
|
||||
billId?: string;
|
||||
flowId?: string;
|
||||
backlogId?: string;
|
||||
specificationName?: string;
|
||||
specificationId?: string;
|
||||
title?: string;
|
||||
amount?: string;
|
||||
submitter?: { id?: string; name?: string; code?: string };
|
||||
department?: string;
|
||||
submitDate?: string;
|
||||
state?: string;
|
||||
rawState?: string;
|
||||
currentNode?: { id?: string; name?: string; type?: string };
|
||||
pendingApprovers: { id?: string; name?: string; code?: string }[];
|
||||
diagnostic: {
|
||||
canApprove: boolean;
|
||||
stage: "DRAFT" | "APPROVING" | "PAYING" | "PAID" | "REJECTED" | "UNKNOWN";
|
||||
stageLabel: string;
|
||||
summary: string;
|
||||
advice: string;
|
||||
};
|
||||
timeline: BillFlowTimelineItem[];
|
||||
raw?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user