import type { ExportFormat } from './documentExport'; import { getUiPreferenceApi, setUiPreferenceApi } from './workflowApi'; import { readOperatingBranchCode } from './operatingBranch'; import { UI_PREF_KEYS } from './uiPreferenceKeys'; export type SavedExportPreferences = { format?: ExportFormat; expandItems?: boolean; selectedColumnIds?: string[]; exportCustomerCode?: string; printTitle?: string; }; const STORAGE_KEY = 'canteen.export.preferences.v1'; type ExportPreferenceStore = Record>; function readStore(): ExportPreferenceStore { try { const raw = localStorage.getItem(STORAGE_KEY); if (!raw) return {}; const parsed = JSON.parse(raw) as ExportPreferenceStore; return parsed && typeof parsed === 'object' ? parsed : {}; } catch { return {}; } } function writeStore(store: ExportPreferenceStore): void { try { localStorage.setItem(STORAGE_KEY, JSON.stringify(store)); } catch { // ignore quota / private mode } } function asUserExportMap(raw: unknown): Record { if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return {}; return raw as Record; } export function loadExportPreferences( userId: string | null | undefined, exportKey: string ): SavedExportPreferences | null { if (!userId || !exportKey) return null; const prefs = readStore()[userId]?.[exportKey]; if (!prefs || typeof prefs !== 'object') return null; return prefs; } export function saveExportPreferences( userId: string | null | undefined, exportKey: string, prefs: SavedExportPreferences ): void { if (!userId || !exportKey) return; const store = readStore(); if (!store[userId]) store[userId] = {}; store[userId][exportKey] = prefs; writeStore(store); // 只提交本 key;服务端 merge 保留其他导出偏好 void setUiPreferenceApi({ key: UI_PREF_KEYS.EXPORT, value: { [exportKey]: prefs }, operatingBranchCode: readOperatingBranchCode(), }) .then((data) => { const remote = asUserExportMap(data.value); if (!Object.keys(remote).length) return; const next = readStore(); next[userId] = { ...(next[userId] || {}), ...remote }; writeStore(next); }) .catch(() => { /* keep local */ }); } /** Pull server user export map into local cache for the given user. */ export async function hydrateExportPreferencesFromServer( userId: string | null | undefined ): Promise { if (!userId) return; try { const data = await getUiPreferenceApi(UI_PREF_KEYS.EXPORT, readOperatingBranchCode()); const remote = asUserExportMap(data.value); const store = readStore(); const local = store[userId] || {}; if (!Object.keys(remote).length) { if (Object.keys(local).length) { await setUiPreferenceApi({ key: UI_PREF_KEYS.EXPORT, value: local, operatingBranchCode: readOperatingBranchCode(), }); } return; } // 服务端权威;本地仅补尚未上报的 key store[userId] = { ...local, ...remote }; writeStore(store); } catch { /* keep local */ } } export function resolveSavedColumnIds( savedIds: string[] | undefined, availableIds: string[], fallbackIds: string[] ): string[] { if (!savedIds?.length) return fallbackIds; const available = new Set(availableIds); const valid = savedIds.filter((id) => available.has(id)); return valid.length ? valid : fallbackIds; }