export interface NotificationNavigateInput { id?: string; title: string | null; content: string | null; notify_type: string | null; related_order_code: string | null; } export interface NotificationNavigateTarget { path: string; search?: Record; } function extractFirst(content: string | null, patterns: RegExp[]): string | null { if (!content) return null; for (const pattern of patterns) { const match = content.match(pattern); if (match?.[1]) return match[1]; } return null; } function isExceptionTicketCode(code: string | null | undefined): boolean { return Boolean(code && /^(EXP|EXC)/i.test(code)); } export function resolveNotificationTarget(item: NotificationNavigateInput): NotificationNavigateTarget { const title = item.title || ''; const content = item.content || ''; const notifyType = item.notify_type || ''; const orderCode = item.related_order_code || extractFirst(content, [ /订单\s+([A-Za-z0-9_-]+)/, /order\s+([A-Za-z0-9_-]+)/i, ]); const purchaseCode = extractFirst(content, [/采购单\s+([A-Za-z0-9_-]+)/]); const ticketCode = (isExceptionTicketCode(item.related_order_code) ? item.related_order_code : null) || extractFirst(content, [/工单\s+([A-Za-z0-9_-]+)/]); const quoteVersionCode = extractFirst(content, [/报价版本\s+([A-Za-z0-9_-]+)/]); const taskCode = extractFirst(content, [/配送任务\s+([A-Za-z0-9_-]+)/]); if (notifyType === 'inspection_rejected') { return { path: '/inspection/review', search: orderCode ? { q: orderCode } : undefined, }; } if (notifyType === 'inventory_shortage') { if (orderCode) { return { path: '/orders', search: { q: orderCode, open: '1' }, }; } return { path: '/inventory' }; } if ( notifyType === 'supplier_prep_amend_pending' || notifyType === 'supplier_prep_amend_rejected' || title.includes('追加修改') ) { const related = String(item.related_order_code || '').trim(); const isUuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(related); const search: Record = { amend: '1' }; if (isUuid) search.amend_id = related; else if (related) search.q = related; else if (orderCode && orderCode !== related) search.q = orderCode; return { path: '/purchase/preparations', search }; } if (notifyType === 'quote_approval') { return { path: '/customers/quote-versions', search: quoteVersionCode ? { q: quoteVersionCode } : undefined, }; } if (notifyType === 'exception_escalation' || title.includes('异常')) { const q = ticketCode || orderCode; return { path: '/exceptions', search: q ? { q, open: '1' } : undefined, }; } if (notifyType === 'allocation_backup') { return { path: '/orders', search: orderCode ? { q: orderCode } : undefined, }; } if (notifyType === 'zero_sale_price') { return { path: '/orders', search: orderCode ? { q: orderCode, open: '1' } : undefined, }; } if (title.includes('采购验收') || title.includes('采购入库')) { const q = purchaseCode || orderCode; return { path: '/inspection', search: q ? { q, open: '1' } : undefined, }; } if (notifyType === 'inspection_timeout' || title.includes('验收')) { return { path: '/inspection', search: orderCode ? { q: orderCode, open: '1' } : undefined, }; } if (title.includes('配送') || taskCode) { const q = taskCode || orderCode; return { path: '/delivery', search: q ? { q, open: '1' } : undefined, }; } if (title.includes('订单') || orderCode) { return { path: '/orders', search: orderCode ? { q: orderCode, open: '1' } : undefined, }; } return { path: '/system/notifications' }; } export function buildNotificationNavigateUrl(target: NotificationNavigateTarget): string { if (!target.search || Object.keys(target.search).length === 0) { return target.path; } const params = new URLSearchParams(target.search); return `${target.path}?${params.toString()}`; }