/** * 合同「修改记录」卡片的口径层(详情右栏,2026-11-15 新增)。 * * 数据源=`/contract/change_log@v1`,行集就是 `bos_audit_log` 的 `contract.*` 流水: * `detail.fields` 是服务端算好的字段级 diff(`{old, new}`),`detail.items_diff`/`ip_diff` * 是服务构成与 IP 条目的三向对比。页面只做**取名与取值格式化**,⛔ 前端自算差异——两套 * diff 一旦分叉,日志与后端拦截就会各说各话。 */ import type { ContractChangeLogItem } from "./api/types"; import { CONTRACT_TYPE_LABEL } from "../domain/contract-draft"; import { attachmentKindOf, contractAttachmentLabel } from "./contract-attachments"; /** 审计字段键 → 中文名(与 `draft_audit_view`/`row_audit_view` 的快照键一一对应)。 */ const FIELD_LABELS: Record = { amount_cents: "合同金额", contract_type: "合同类型", sign_date: "签署日期", service_months: "服务期(月)", start_date: "合同生效日", end_date: "合同结束日", external_no: "外部单号", agent_name: "经办人姓名", agent_mobile: "经办人联系电话", agent_org: "经办人机构", agent_handover_on: "交接日期", attachment_url: "合同附件", source: "合同来源", customer_id: "签约客户", contract_no: "合同编号", }; /** 金额列按「分 → 元」显示;其余原样(日期/手机号都不该被本地化改写)。 */ function formatValue(key: string, value: unknown): string { if (value === null || value === undefined || value === "") return "(空)"; if (key === "amount_cents" && typeof value === "number") { return `¥${(value / 100).toLocaleString("zh-CN", { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; } if (key === "contract_type" && typeof value === "string") return CONTRACT_TYPE_LABEL[value] ?? value; if (key === "service_months" && typeof value === "number") return `${value} 个月`; // 附件(单值):库里可能是平台对象键(末段是随机串)⇒ 显示可读名,⛔ 把随机串糊进审计卡; // 查看地址由读口现签,审计只说明「改了哪个附件」。 if (key === "attachment_url" && typeof value === "string") { const kind = attachmentKindOf(value); if (kind === "object") return `${contractAttachmentLabel(value)}(平台对象存储)`; if (kind === "link") return contractAttachmentLabel(value); } return String(value); } export type ChangeLogField = { key: string; label: string; old: string; new: string }; /** 字段级明细(可展开),按后端 diff 的键序;未知键原样显示,⛔ 静默丢行。 */ export function changeLogFields(item: ContractChangeLogItem): ChangeLogField[] { const fields = item.detail?.fields; if (!fields || typeof fields !== "object") return []; return Object.entries(fields as Record).map(([key, diff]) => ({ key, label: FIELD_LABELS[key] ?? key, old: formatValue(key, diff?.old), new: formatValue(key, diff?.new), })); } const countOf = (value: unknown) => (Array.isArray(value) ? value.length : 0); /** * 摘要行(默认展开的那几行):字段变更取前 3 条,另加服务构成/IP 条数的增删改。 * 返回空数组=这一条没有任何可读变化(例如只改了附件地址又改回),页面不渲染摘要块。 */ export function changeLogSummary(item: ContractChangeLogItem): string[] { const detail = (item.detail ?? {}) as Record; const out: string[] = []; if (item.action === "contract.create") { const items = typeof detail.items_count === "number" ? detail.items_count : countOf(detail.service_items); out.push(`服务构成 ${items} 条`); // `ip_entries_count` 只存在于 2026-11-17 之前的存量审计行(固定 IP 条目已下线): // 读到就照实复述历史,⛔ 据此反推合同现在的 IP 核定。 if (typeof detail.ip_entries_count === "number" && detail.ip_entries_count > 0) { out.push(`固定 IP ${detail.ip_entries_count} 条(历史审计)`); } return out; } if (item.action === "contract.delete") { out.push("合同已删除(不可恢复)"); return out; } for (const field of changeLogFields(item).slice(0, 3)) { out.push(`${field.label}:${field.old} → ${field.new}`); } const hidden = changeLogFields(item).length - 3; if (hidden > 0) out.push(`另有 ${hidden} 个字段变更`); const items = detail.items_diff as { updated?: unknown[]; created?: unknown[]; deleted?: unknown[] } | undefined; if (items) { const parts = [ countOf(items.updated) ? `改 ${countOf(items.updated)}` : "", countOf(items.created) ? `增 ${countOf(items.created)}` : "", countOf(items.deleted) ? `删 ${countOf(items.deleted)}` : "", ].filter(Boolean); if (parts.length) out.push(`服务构成:${parts.join(" · ")}`); } const ip = detail.ip_diff as { created?: unknown[]; deleted?: unknown[] } | undefined; if (ip) { const parts = [countOf(ip.created) ? `增 ${countOf(ip.created)}` : "", countOf(ip.deleted) ? `删 ${countOf(ip.deleted)}` : ""].filter(Boolean); if (parts.length) out.push(`固定 IP:${parts.join(" · ")}`); } if (detail.external_no_changed === true && typeof detail.order_no_writeback_note === "string") { out.push("外部单号已改(关联订阅 order_no 不回写)"); } return out; } /** 时刻显示:`YYYY-MM-DD HH:mm`(只到分钟,秒级噪音对"谁改了什么"没用)。 */ export function formatLogTime(raw: string): string { const text = raw.trim(); const parsed = new Date(text.includes("T") ? text : text.replace(" ", "T")); if (Number.isNaN(parsed.getTime())) return text; const pad = (n: number) => String(n).padStart(2, "0"); return `${parsed.getFullYear()}-${pad(parsed.getMonth() + 1)}-${pad(parsed.getDate())} ${pad(parsed.getHours())}:${pad(parsed.getMinutes())}`; }