Files
ai-platform/src/modules/baode-web/engine/report.ts
T
2026-10-18 15:27:02 +08:00

155 lines
8.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { EvidenceRow } from "../types";
import type { Scenario } from "../mock/scenarios";
import { pct, wan, yuan } from "./format";
/** 流程收尾自动出具的《需求评估报告》:自包含 HTML 网页,新标签页打开 */
export type ReviewOutcome = "order" | "credit";
const pad = (n: number) => `${n}`.padStart(2, "0");
export function reportFileName(s: Scenario, when: Date = new Date()): string {
const p = s.demand.parsed;
const stamp = `${when.getFullYear()}${pad(when.getMonth() + 1)}${pad(when.getDate())}-${pad(when.getHours())}${pad(when.getMinutes())}`;
return `需求评估报告-${p.productType}-${p.quantity}个-${stamp}.html`;
}
function esc(t: string): string {
return t.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
}
function evidenceRows(rows: EvidenceRow[]): string {
return rows
.map(
(r) =>
`<tr><td class="k">${esc(r.label)}</td><td>${esc(r.value)}</td><td class="v ${r.verdict === "通过" ? "pass" : r.verdict === "不通过" ? "fail" : ""}">${r.verdict ? esc(r.verdict) : ""}</td></tr>`,
)
.join("");
}
const CSS = `
* { box-sizing: border-box; }
body { margin: 0; background: #f4f6fb; color: #1e293b; font: 14px/1.65 -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, sans-serif; }
.wrap { max-width: 980px; margin: 0 auto; padding: 32px 20px 64px; }
.hero { background: linear-gradient(135deg, #0b2a6b 0%, #2563eb 62%, #3b82f6 100%); color: #fff; border-radius: 16px; padding: 28px 30px; box-shadow: 0 12px 32px rgba(37, 99, 235, .25); }
.hero .eyebrow { letter-spacing: .18em; font-size: 11px; opacity: .82; }
.hero h1 { margin: 6px 0 10px; font-size: 26px; letter-spacing: .02em; }
.hero .meta { display: flex; flex-wrap: wrap; gap: 8px 18px; font-size: 12px; opacity: .92; }
.banner { margin: 18px 0 0; border-radius: 12px; padding: 14px 18px; font-size: 15px; font-weight: 700; }
.banner.ok { background: #f0fdf4; color: #15803d; border: 1px solid #bbf7d0; }
.banner.fail { background: #fef2f2; color: #b91c1c; border: 1px solid #fecaca; }
.card { background: #fff; border: 1px solid #e2e8f0; border-radius: 14px; padding: 18px 20px; margin-top: 16px; box-shadow: 0 1px 2px rgba(15, 23, 42, .05), 0 8px 24px rgba(15, 23, 42, .06); }
.card h2 { margin: 0 0 10px; font-size: 15px; color: #0b2a6b; border-left: 4px solid #2563eb; padding-left: 10px; }
.lead { margin: 4px 0 10px; font-size: 13px; color: #475569; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 10px 18px; margin: 0; font-size: 13px; }
.grid dt { color: #64748b; font-size: 12px; }
.grid dd { margin: 2px 0 0; font-weight: 600; font-variant-numeric: tabular-nums; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
td, th { padding: 8px 10px; border-bottom: 1px solid #eef2f7; text-align: left; vertical-align: top; }
tr:last-child td { border-bottom: 0; }
td.k { color: #64748b; width: 34%; }
td.v { width: 64px; text-align: right; font-weight: 700; white-space: nowrap; }
td.v.pass { color: #15803d; }
td.v.fail { color: #b91c1c; }
.note { margin: 10px 0 0; padding: 10px 12px; background: #eff6ff; border-radius: 10px; color: #1d4ed8; font-size: 12.5px; }
.score { display: flex; align-items: baseline; gap: 10px; margin: 4px 0 8px; }
.score b { font-size: 30px; color: #0b2a6b; font-variant-numeric: tabular-nums; }
.score span { color: #64748b; font-size: 12px; }
.bar { height: 10px; border-radius: 999px; background: #e2e8f0; overflow: hidden; position: relative; }
.bar i { display: block; height: 100%; border-radius: 999px; background: linear-gradient(90deg, #2563eb, #60a5fa); }
.bar u { position: absolute; left: 90%; top: -3px; width: 2px; height: 16px; background: #b91c1c; }
.tags { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.tag { font-size: 12px; padding: 4px 10px; border-radius: 999px; background: #f1f5f9; color: #475569; }
.tag.ok { background: #f0fdf4; color: #15803d; }
.tag.fail { background: #fef2f2; color: #b91c1c; }
ul.sug { margin: 8px 0 0; padding-left: 20px; font-size: 13px; color: #475569; }
footer { margin-top: 22px; color: #94a3b8; font-size: 12px; text-align: center; }
b.ok { color: #15803d; } b.fail { color: #b91c1c; }
@media print { body { background: #fff; } .card, .hero { box-shadow: none; } }
`;
export function buildReportHtml(s: Scenario, outcome: ReviewOutcome): string {
const d = s.demand;
const p = d.parsed;
const order = s.summary.order;
const now = new Date().toLocaleString("zh-CN", { hour12: false });
const rndSection =
s.feasibility.route === "rnd"
? `<section class="card"><h2>新品研发评审</h2>
<p class="lead">结论:<b class="${s.rnd.pass ? "ok" : "fail"}">${s.rnd.pass ? "通过" : "不通过"}</b>(4 项评审全过才通过)</p>
<table><tbody>${s.rnd.items
.map(
(i) =>
`<tr><td class="k">${esc(i.label)}</td><td>标准 ${esc(i.standard)}</td><td>实际 ${esc(i.actual ?? "—")}</td><td class="v ${i.pass ? "pass" : "fail"}">${i.pass ? "通过" : "不通过"}</td></tr>`,
)
.join("")}</tbody></table>
<p class="note">毛利复算:收入 ${yuan(s.margin.revenue)} − 钢卷 ${yuan(s.margin.coilCostTotal)} = ${yuan(s.margin.grossProfit)} 元(毛利率 ${pct(s.margin.grossMarginRate)})</p></section>`
: "";
const ratingTags = s.rating.items
.map((i) => `<span class="tag ${i.pass ? "ok" : "fail"}">${esc(i.label)} ${i.score}/${i.weight}</span>`)
.join("");
const html = `<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>需求评估报告 · ${esc(p.productType)} ${p.quantity} 个</title>
<style>${CSS}</style>
</head>
<body>
<div class="wrap">
<header class="hero">
<div class="eyebrow">BAODE · INTELLIGENT REVIEW</div>
<h1>需求评估报告</h1>
<div class="meta">
<span>来源文档:${esc(d.document.fileName)}</span>
<span>生成时间:${esc(now)}</span>
<span>出具:需求评审多智能体系统</span>
</div>
</header>
<div class="banner ${order ? "ok" : "fail"}">整体结论:${esc(s.summary.overall)}${order ? ` · 订单 ${esc(order.orderNo)}` : ""}</div>
<section class="card"><h2>需求概要</h2>
<dl class="grid">
<div><dt>客户</dt><dd>${esc(d.customerCode)}</dd></div>
<div><dt>行业</dt><dd>${esc(p.industry ?? "—")}</dd></div>
<div><dt>产品类型</dt><dd>${esc(p.productType)}</dd></div>
<div><dt>数量 / 单价</dt><dd>${p.quantity} 个 / ${yuan(p.unitPrice)} 元</dd></div>
<div><dt>当期需求金额</dt><dd>${wan(p.quantity * p.unitPrice)} 万</dd></div>
<div><dt>交期</dt><dd>${esc(p.deliveryDate)}</dd></div>
<div><dt>钢卷材质</dt><dd>${esc(p.coilMaterial)}</dd></div>
</dl>
</section>
<section class="card"><h2>可行性判定</h2>
<p class="lead">结论:<b>${esc(s.feasibility.category)}</b>(${esc(s.feasibility.routeLabel)})</p>
<table><tbody>${evidenceRows(s.feasibility.evidence)}</tbody></table>
</section>
${rndSection}
<section class="card"><h2>交期评审</h2>
<p class="lead">结论:<b class="${s.delivery.pass ? "ok" : "fail"}">${s.delivery.pass ? "通过" : "不通过"}</b> · 距交期 ${s.delivery.daysToDelivery} 天</p>
<table><tbody>${evidenceRows(s.delivery.evidence)}</tbody></table>
${s.delivery.suggestions.length ? `<ul class="sug">${s.delivery.suggestions.map((t) => `<li>${esc(t)}</li>`).join("")}</ul>` : ""}
</section>
<section class="card"><h2>客户评级</h2>
<div class="score"><b>${s.rating.total}</b><span>/ 100 分 · ≥90 合格 · 红线为合格线</span></div>
<div class="bar"><i style="width:${Math.min(100, s.rating.total)}%"></i><u></u></div>
<div class="tags">${ratingTags}</div>
<table><tbody>${s.rating.items
.map((i) => `<tr><td class="k">${esc(i.label)}(权重 ${i.weight})</td><td>${esc(i.basis)}</td><td class="v ${i.pass ? "pass" : "fail"}">${i.score} 分</td></tr>`)
.join("")}</tbody></table>
</section>
<section class="card"><h2>综合结论与处理</h2>
<p class="lead">整体结论:<b class="${order ? "ok" : "fail"}">${esc(s.summary.overall)}</b></p>
${order ? `<p class="note">订单:${esc(order.orderNo)}|金额 ${yuan(order.amount)} 元|交期 ${esc(order.deliveryDate)}</p>` : ""}
${s.summary.suggestions.length ? `<ul class="sug">${s.summary.suggestions.map((t) => `<li>${esc(t)}</li>`).join("")}</ul>` : ""}
<p class="lead">${outcome === "order" ? "处理结果:用户已确认评审结论并发起订单生成。" : "处理结果:用户已发起客户临时授信流程(授信审批 → 评级复跑 → 重新汇总)。"}</p>
</section>
<footer>需求评审多智能体系统 自动出具 · ${esc(now)}</footer>
</div>
</body>
</html>`;
return html;
}