import { pool } from '../db'; import { sanitizeMenuGroupIconSvg } from './menuGroupIconService'; export interface MenuGroupIconLibraryEntry { label: string; iconSvg: string; updatedAt: string; } export interface MenuGroupIconLibrary { version: 1; entries: Record; } const CONFIG_KEY = 'menu_group_icon_library'; function emptyLibrary(): MenuGroupIconLibrary { return { version: 1, entries: {} }; } export function normalizeMenuGroupIconLabel(label: string): string { return label.trim().replace(/\s+/g, ''); } async function loadLibrary(): Promise { const result = await pool.query( `SELECT config_value FROM system_configs WHERE config_key = $1 AND branch_code IS NULL LIMIT 1`, [CONFIG_KEY] ); const raw = result.rows[0]?.config_value; if (!raw || typeof raw !== 'object') return emptyLibrary(); const entries = (raw as MenuGroupIconLibrary).entries; if (!entries || typeof entries !== 'object') return emptyLibrary(); return { version: 1, entries: entries as Record, }; } async function saveLibrary(library: MenuGroupIconLibrary): Promise { await pool.query( `INSERT INTO system_configs (config_key, config_value, config_group, description, branch_code) VALUES ($1, $2::jsonb, 'nav', '菜单组矢量图标库(按组名匹配复用)', NULL) ON CONFLICT ((COALESCE(branch_code, '__GLOBAL__')), config_key) DO UPDATE SET config_value = EXCLUDED.config_value, updated_at = NOW()`, [CONFIG_KEY, JSON.stringify(library)] ); } export async function lookupMenuGroupIcon(label: string): Promise { const key = normalizeMenuGroupIconLabel(label); if (!key) return null; const library = await loadLibrary(); const entry = library.entries[key]; if (!entry?.iconSvg || !entry.iconSvg.includes(' { const normalizedLabel = normalizeMenuGroupIconLabel(label); const sanitized = sanitizeMenuGroupIconSvg(iconSvg); if (!normalizedLabel || !sanitized) return; const library = await loadLibrary(); library.entries[normalizedLabel] = { label: label.trim(), iconSvg: sanitized, updatedAt: new Date().toISOString(), }; await saveLibrary(library); } export async function getMenuGroupIconLibrary(): Promise { return loadLibrary(); }