contract-audit.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * 合同「修改记录」卡片的口径层(详情右栏,2026-11-15 新增)。
  3. *
  4. * 数据源=`/contract/change_log@v1`,行集就是 `bos_audit_log` 的 `contract.*` 流水:
  5. * `detail.fields` 是服务端算好的字段级 diff(`{old, new}`),`detail.items_diff`/`ip_diff`
  6. * 是服务构成与 IP 条目的三向对比。页面只做**取名与取值格式化**,⛔ 前端自算差异——两套
  7. * diff 一旦分叉,日志与后端拦截就会各说各话。
  8. */
  9. import type { ContractChangeLogItem } from "./api/types";
  10. import { CONTRACT_TYPE_LABEL } from "../domain/contract-draft";
  11. import { attachmentKindOf, contractAttachmentLabel } from "./contract-attachments";
  12. /** 审计字段键 → 中文名(与 `draft_audit_view`/`row_audit_view` 的快照键一一对应)。 */
  13. const FIELD_LABELS: Record<string, string> = {
  14. amount_cents: "合同金额",
  15. contract_type: "合同类型",
  16. sign_date: "签署日期",
  17. service_months: "服务期(月)",
  18. start_date: "合同生效日",
  19. end_date: "合同结束日",
  20. external_no: "外部单号",
  21. agent_name: "经办人姓名",
  22. agent_mobile: "经办人联系电话",
  23. agent_org: "经办人机构",
  24. agent_handover_on: "交接日期",
  25. attachment_url: "合同附件",
  26. source: "合同来源",
  27. customer_id: "签约客户",
  28. contract_no: "合同编号",
  29. };
  30. /** 金额列按「分 → 元」显示;其余原样(日期/手机号都不该被本地化改写)。 */
  31. function formatValue(key: string, value: unknown): string {
  32. if (value === null || value === undefined || value === "") return "(空)";
  33. if (key === "amount_cents" && typeof value === "number") {
  34. return `¥${(value / 100).toLocaleString("zh-CN", { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
  35. }
  36. if (key === "contract_type" && typeof value === "string") return CONTRACT_TYPE_LABEL[value] ?? value;
  37. if (key === "service_months" && typeof value === "number") return `${value} 个月`;
  38. // 附件(单值):库里可能是平台对象键(末段是随机串)⇒ 显示可读名,⛔ 把随机串糊进审计卡;
  39. // 查看地址由读口现签,审计只说明「改了哪个附件」。
  40. if (key === "attachment_url" && typeof value === "string") {
  41. const kind = attachmentKindOf(value);
  42. if (kind === "object") return `${contractAttachmentLabel(value)}(平台对象存储)`;
  43. if (kind === "link") return contractAttachmentLabel(value);
  44. }
  45. return String(value);
  46. }
  47. export type ChangeLogField = { key: string; label: string; old: string; new: string };
  48. /** 字段级明细(可展开),按后端 diff 的键序;未知键原样显示,⛔ 静默丢行。 */
  49. export function changeLogFields(item: ContractChangeLogItem): ChangeLogField[] {
  50. const fields = item.detail?.fields;
  51. if (!fields || typeof fields !== "object") return [];
  52. return Object.entries(fields as Record<string, { old?: unknown; new?: unknown }>).map(([key, diff]) => ({
  53. key,
  54. label: FIELD_LABELS[key] ?? key,
  55. old: formatValue(key, diff?.old),
  56. new: formatValue(key, diff?.new),
  57. }));
  58. }
  59. const countOf = (value: unknown) => (Array.isArray(value) ? value.length : 0);
  60. /**
  61. * 摘要行(默认展开的那几行):字段变更取前 3 条,另加服务构成/IP 条数的增删改。
  62. * 返回空数组=这一条没有任何可读变化(例如只改了附件地址又改回),页面不渲染摘要块。
  63. */
  64. export function changeLogSummary(item: ContractChangeLogItem): string[] {
  65. const detail = (item.detail ?? {}) as Record<string, unknown>;
  66. const out: string[] = [];
  67. if (item.action === "contract.create") {
  68. const items = typeof detail.items_count === "number" ? detail.items_count : countOf(detail.service_items);
  69. out.push(`服务构成 ${items} 条`);
  70. // `ip_entries_count` 只存在于 2026-11-17 之前的存量审计行(固定 IP 条目已下线):
  71. // 读到就照实复述历史,⛔ 据此反推合同现在的 IP 核定。
  72. if (typeof detail.ip_entries_count === "number" && detail.ip_entries_count > 0) {
  73. out.push(`固定 IP ${detail.ip_entries_count} 条(历史审计)`);
  74. }
  75. return out;
  76. }
  77. if (item.action === "contract.delete") {
  78. out.push("合同已删除(不可恢复)");
  79. return out;
  80. }
  81. for (const field of changeLogFields(item).slice(0, 3)) {
  82. out.push(`${field.label}:${field.old} → ${field.new}`);
  83. }
  84. const hidden = changeLogFields(item).length - 3;
  85. if (hidden > 0) out.push(`另有 ${hidden} 个字段变更`);
  86. const items = detail.items_diff as { updated?: unknown[]; created?: unknown[]; deleted?: unknown[] } | undefined;
  87. if (items) {
  88. const parts = [
  89. countOf(items.updated) ? `改 ${countOf(items.updated)}` : "",
  90. countOf(items.created) ? `增 ${countOf(items.created)}` : "",
  91. countOf(items.deleted) ? `删 ${countOf(items.deleted)}` : "",
  92. ].filter(Boolean);
  93. if (parts.length) out.push(`服务构成:${parts.join(" · ")}`);
  94. }
  95. const ip = detail.ip_diff as { created?: unknown[]; deleted?: unknown[] } | undefined;
  96. if (ip) {
  97. const parts = [countOf(ip.created) ? `增 ${countOf(ip.created)}` : "", countOf(ip.deleted) ? `删 ${countOf(ip.deleted)}` : ""].filter(Boolean);
  98. if (parts.length) out.push(`固定 IP:${parts.join(" · ")}`);
  99. }
  100. if (detail.external_no_changed === true && typeof detail.order_no_writeback_note === "string") {
  101. out.push("外部单号已改(关联订阅 order_no 不回写)");
  102. }
  103. return out;
  104. }
  105. /** 时刻显示:`YYYY-MM-DD HH:mm`(只到分钟,秒级噪音对"谁改了什么"没用)。 */
  106. export function formatLogTime(raw: string): string {
  107. const text = raw.trim();
  108. const parsed = new Date(text.includes("T") ? text : text.replace(" ", "T"));
  109. if (Number.isNaN(parsed.getTime())) return text;
  110. const pad = (n: number) => String(n).padStart(2, "0");
  111. return `${parsed.getFullYear()}-${pad(parsed.getMonth() + 1)}-${pad(parsed.getDate())} ${pad(parsed.getHours())}:${pad(parsed.getMinutes())}`;
  112. }