206 lines
7.7 KiB
TypeScript
206 lines
7.7 KiB
TypeScript
import ExcelJS from "exceljs";
|
||
import type { ReportBlock, ReportData } from "../pipeline/collect.js";
|
||
|
||
function csvEscape(val: string | number | undefined): string {
|
||
if (val === undefined || val === null) return "";
|
||
const str = String(val).replace(/\r?\n/g, " ").trim();
|
||
if (/[",\n]/.test(str)) {
|
||
return `"${str.replace(/"/g, '""')}"`;
|
||
}
|
||
return str;
|
||
}
|
||
|
||
function formatAgreement(agreement: ReportBlock["agreement"]): string {
|
||
if (agreement === "agreed") return "一致";
|
||
if (agreement === "diverged") return "分歧";
|
||
return "未完结/流转中";
|
||
}
|
||
|
||
function formatVerdict(block: ReportBlock): string {
|
||
if (block.agreement === "unknown") return "待人工审批完结";
|
||
const v = block.verdict;
|
||
if (v === "both_correct" || (!v && block.agreement === "agreed")) return "双方一致正确 (D1/D2)";
|
||
if (v === "ai_correct") return "AI 依规正确 / 人工失准 (D4)";
|
||
if (v === "human_correct") return "人工合理 / AI 失准";
|
||
if (v === "both_wrong") return "双方均失准";
|
||
if (v === "undecided") return "存疑待定";
|
||
return "待复核";
|
||
}
|
||
|
||
/**
|
||
* 构建带 UTF-8 BOM 的月度跑批与人工复核明细台账 CSV
|
||
*/
|
||
export function buildReportLedgerCsv(data: ReportData): string {
|
||
const headers = [
|
||
"单据编号(SN)",
|
||
"单据模板",
|
||
"单据标题/事由",
|
||
"提交人",
|
||
"费用承担部门",
|
||
"跑批时间",
|
||
"AI审批决策",
|
||
"人工审批结论",
|
||
"人机一致性",
|
||
"复核定性结论",
|
||
"复核人",
|
||
"复核批注说明",
|
||
"审批耗时(秒)",
|
||
"AI审核意见摘要"
|
||
];
|
||
|
||
const rows = data.blocks.map((block) => {
|
||
const review = data.reviews.get(block.sn);
|
||
return [
|
||
block.sn,
|
||
block.template || "—",
|
||
block.title || "—",
|
||
block.submitter || "—",
|
||
block.department || "—",
|
||
block.createdAt || "—",
|
||
block.aiDecision || "—",
|
||
block.humanResult || "—",
|
||
formatAgreement(block.agreement),
|
||
formatVerdict(block),
|
||
review?.reviewedBy || "—",
|
||
review?.note || "—",
|
||
block.durationSeconds ?? "",
|
||
block.aiSummary || "—"
|
||
].map(csvEscape).join(",");
|
||
});
|
||
|
||
return "\uFEFF" + [headers.join(","), ...rows].join("\n") + "\n";
|
||
}
|
||
|
||
/**
|
||
* 构建双 Sheet 的专业月度跑批与复核台账 Excel (.xlsx)
|
||
*/
|
||
export async function buildReportLedgerXlsx(data: ReportData): Promise<Buffer> {
|
||
const workbook = new ExcelJS.Workbook();
|
||
workbook.creator = "seal-use";
|
||
workbook.created = new Date();
|
||
|
||
const detailSheet = workbook.addWorksheet("跑批与复核明细", {
|
||
views: [{ state: "frozen", ySplit: 1 }]
|
||
});
|
||
|
||
detailSheet.columns = [
|
||
{ header: "单据编号(SN)", key: "sn", width: 16 },
|
||
{ header: "单据模板", key: "template", width: 20 },
|
||
{ header: "单据标题/事由", key: "title", width: 32 },
|
||
{ header: "提交人", key: "submitter", width: 12 },
|
||
{ header: "费用承担部门", key: "department", width: 22 },
|
||
{ header: "跑批时间", key: "createdAt", width: 20 },
|
||
{ header: "AI审批决策", key: "aiDecision", width: 15 },
|
||
{ header: "人工审批结论", key: "humanResult", width: 16 },
|
||
{ header: "人机一致性", key: "agreement", width: 14 },
|
||
{ header: "复核定性结论", key: "verdict", width: 24 },
|
||
{ header: "复核人", key: "reviewedBy", width: 14 },
|
||
{ header: "复核批注说明", key: "note", width: 38 },
|
||
{ header: "审批耗时(秒)", key: "durationSeconds", width: 14 },
|
||
{ header: "AI审核意见摘要", key: "aiSummary", width: 55 }
|
||
];
|
||
|
||
const headerRow = detailSheet.getRow(1);
|
||
headerRow.font = { bold: true, color: { argb: "FFFFFFFF" }, size: 11 };
|
||
headerRow.fill = { type: "pattern", pattern: "solid", fgColor: { argb: "FF1E3A8A" } };
|
||
headerRow.alignment = { vertical: "middle", horizontal: "center" };
|
||
headerRow.height = 24;
|
||
|
||
for (const block of data.blocks) {
|
||
const review = data.reviews.get(block.sn);
|
||
const row = detailSheet.addRow({
|
||
sn: block.sn,
|
||
template: block.template || "—",
|
||
title: block.title || "—",
|
||
submitter: block.submitter || "—",
|
||
department: block.department || "—",
|
||
createdAt: block.createdAt || "—",
|
||
aiDecision: block.aiDecision || "—",
|
||
humanResult: block.humanResult || "—",
|
||
agreement: formatAgreement(block.agreement),
|
||
verdict: formatVerdict(block),
|
||
reviewedBy: review?.reviewedBy || "—",
|
||
note: review?.note || "—",
|
||
durationSeconds: block.durationSeconds ?? "",
|
||
aiSummary: block.aiSummary || "—"
|
||
});
|
||
row.alignment = { vertical: "top", wrapText: true };
|
||
}
|
||
|
||
detailSheet.autoFilter = {
|
||
from: { row: 1, column: 1 },
|
||
to: { row: 1, column: detailSheet.columns.length }
|
||
};
|
||
|
||
if (data.templateGroups.length > 0) {
|
||
const summarySheet = workbook.addWorksheet("分模板统计汇总", {
|
||
views: [{ state: "frozen", ySplit: 1 }]
|
||
});
|
||
summarySheet.columns = [
|
||
{ header: "单据模板", key: "template", width: 24 },
|
||
{ header: "跑批总单量", key: "total", width: 14 },
|
||
{ header: "已完结单量", key: "evaluated", width: 14 },
|
||
{ header: "AI 准确率", key: "aiAcc", width: 14 },
|
||
{ header: "人工准确率", key: "humanAcc", width: 14 },
|
||
{ header: "人机一致率", key: "bothAcc", width: 14 },
|
||
{ header: "人工同意:驳回", key: "ratio", width: 18 }
|
||
];
|
||
const sumHeader = summarySheet.getRow(1);
|
||
sumHeader.font = { bold: true, color: { argb: "FFFFFFFF" }, size: 11 };
|
||
sumHeader.fill = { type: "pattern", pattern: "solid", fgColor: { argb: "FF1E3A8A" } };
|
||
sumHeader.alignment = { vertical: "middle", horizontal: "center" };
|
||
sumHeader.height = 24;
|
||
|
||
for (const g of data.templateGroups) {
|
||
const ev = g.evaluated || g.total || 1;
|
||
const aiPct = g.aiCorrect !== undefined ? `${((g.aiCorrect / ev) * 100).toFixed(2)}%` : `${((g.agreed / ev) * 100).toFixed(2)}%`;
|
||
const humanPct = g.humanCorrect !== undefined ? `${((g.humanCorrect / ev) * 100).toFixed(2)}%` : "—";
|
||
const bothPct = g.bothCorrect !== undefined ? `${((g.bothCorrect / ev) * 100).toFixed(2)}%` : `${((g.agreed / ev) * 100).toFixed(2)}%`;
|
||
const ratio = g.humanPass !== undefined && g.humanReject !== undefined
|
||
? `${g.evaluated ?? g.total}(${g.humanPass}:${g.humanReject})`
|
||
: `${g.total} 笔`;
|
||
|
||
summarySheet.addRow({
|
||
template: g.template,
|
||
total: g.total,
|
||
evaluated: g.evaluated ?? g.total,
|
||
aiAcc: aiPct,
|
||
humanAcc: humanPct,
|
||
bothAcc: bothPct,
|
||
ratio
|
||
});
|
||
}
|
||
}
|
||
|
||
const arrayBuffer = await workbook.xlsx.writeBuffer();
|
||
return Buffer.from(arrayBuffer);
|
||
}
|
||
|
||
/**
|
||
* 当针对独立 Markdown 文件直接执行 report export 且无预生成台账时,从 Markdown 表格提取构建台账
|
||
*/
|
||
export async function buildMarkdownFallbackLedger(markdownContent: string): Promise<{ csv: string; xlsx: Buffer }> {
|
||
const lines = markdownContent.split(/\r?\n/);
|
||
const rows: string[][] = [];
|
||
for (const line of lines) {
|
||
const trimmed = line.trim();
|
||
if (!trimmed.startsWith("|") || !trimmed.endsWith("|")) continue;
|
||
const cells = trimmed.slice(1, -1).split("|").map((c) => c.trim().replace(/\*\*/g, ""));
|
||
if (cells.every((c) => /^[-:]+$/.test(c))) continue;
|
||
if (cells.length >= 5) {
|
||
rows.push(cells);
|
||
}
|
||
}
|
||
|
||
const finalRows = rows.length > 0 ? rows : [["单据编号", "单据模板", "AI审批决策", "人工审批结论", "复核结论"]];
|
||
const csv = "\uFEFF" + finalRows.map((r) => r.map(csvEscape).join(",")).join("\n") + "\n";
|
||
|
||
const workbook = new ExcelJS.Workbook();
|
||
const sheet = workbook.addWorksheet("报告明细台账");
|
||
for (const r of finalRows) {
|
||
sheet.addRow(r);
|
||
}
|
||
const arrayBuffer = await workbook.xlsx.writeBuffer();
|
||
return { csv, xlsx: Buffer.from(arrayBuffer) };
|
||
}
|