import type { EvidenceRow, IndustryCode } from "../types"; /** 评审历史归档:浏览器优先 localStorage,非浏览器环境回退内存 */ export interface TraceEntry { time: string; agent: string; title: string; rows?: EvidenceRow[]; note?: string; } export interface ReviewRecordInput { fileName: string; customerCode: string; productType: string; quantity: number; unitPrice: number; industry: IndustryCode | null; deliveryDate: string; operator: { code: string; name: string }; startedAt: string; overall: "评审通过" | "评审不通过"; orderNo: string | null; creditStarted: boolean; ratingTotal: number; feasibilityCategory: string; reportFileName: string; reportHtml: string; trace: TraceEntry[]; } export interface ReviewRecord extends ReviewRecordInput { id: string; finishedAt: string; } const KEY = "bd-review-records-v1"; const memory = new Map(); function readAll(): ReviewRecord[] { try { const raw = typeof localStorage !== "undefined" ? localStorage.getItem(KEY) : (memory.get(KEY) ?? null); return raw ? (JSON.parse(raw) as ReviewRecord[]) : []; } catch { return []; } } function writeAll(list: ReviewRecord[]): void { const raw = JSON.stringify(list); try { if (typeof localStorage !== "undefined") localStorage.setItem(KEY, raw); else memory.set(KEY, raw); } catch { memory.set(KEY, raw); } } export function listReviewRecords(): ReviewRecord[] { return readAll(); } export function getReviewRecord(id: string): ReviewRecord | null { return readAll().find((r) => r.id === id) ?? null; } export function saveReviewRecord(input: ReviewRecordInput): ReviewRecord { const list = readAll(); const d = new Date(); const pad = (n: number) => `${n}`.padStart(2, "0"); const record: ReviewRecord = { ...input, id: `RV-${d.getFullYear()}${pad(d.getMonth() + 1)}${pad(d.getDate())}-${`${list.length + 1}`.padStart(3, "0")}`, finishedAt: d.toISOString(), }; writeAll([record, ...list].slice(0, 50)); return record; }