import { notifyRoleUsers, notifyUser } from './notificationService'; import { listSupplierUsersForCodes } from './bidAwardService'; /** * 主线 7 事件推送:新备货 / 新自采 / 新验收 / 新分拣 / 新配送 / 结算待确认 / 送达。 * 站内通知(notifyRoleUsers,含 SSE 推送)。 * 所有函数均为 best-effort:调用方 .catch 即可,失败不阻断主流程。 */ const ADMIN_ROLES = ['operator', 'admin', 'super_admin']; async function notifyMainline(input: { roles: string[]; notifyType: string; title: string; content: string; relatedOrderCode?: string | null; branchCode?: string | null; }) { await notifyRoleUsers({ roles: input.roles, notifyType: input.notifyType, title: input.title, content: input.content, relatedOrderCode: input.relatedOrderCode || null, branchCode: input.branchCode || null, skipIfRecent: true, dedupWithinHours: 1, }); } /** 按供应商编码查绑定账号(tenant supplier_users),用于备货单精准推送 */ async function listSupplierUserIds( supplierCodes: string[], branchCode?: string | null ): Promise { const rows = await listSupplierUsersForCodes(supplierCodes, branchCode); return [...new Set(rows.map((r) => r.user_id).filter(Boolean))]; } /** ① 新备货单:通知运营 + 对应供应商账号 */ export async function notifyPreparationsCreated(input: { orderCode: string; branchCode?: string | null; count: number; supplierCodes?: string[]; }) { const supplierCodes = [...new Set((input.supplierCodes || []).filter(Boolean))]; const title = `新备货任务:订单 ${input.orderCode}`; const content = `订单 ${input.orderCode} 新增 ${input.count} 项供应商备货${supplierCodes.length ? `(涉及 ${supplierCodes.length} 家供应商)` : ''},请及时备货。`; await notifyMainline({ roles: ADMIN_ROLES, notifyType: 'preparation_created', title, content, relatedOrderCode: input.orderCode, branchCode: input.branchCode, }); const supplierUserIds = await listSupplierUserIds(supplierCodes, input.branchCode); for (const userId of supplierUserIds) { await notifyUser({ userId, notifyType: 'preparation_created', title, content, relatedOrderCode: input.orderCode, branchCode: input.branchCode || null, }).catch((error) => console.error('[mainline-notify] 供应商通知失败:', error)); } } /** ② 新自采任务:通知采购员 */ export async function notifyPurchaseTasksCreated(input: { orderCode: string; branchCode?: string | null; count: number; }) { await notifyMainline({ roles: ['purchaser', ...ADMIN_ROLES], notifyType: 'purchase_task_created', title: `新自采任务:订单 ${input.orderCode}`, content: `订单 ${input.orderCode} 新增 ${input.count} 项自采任务,请及时采购并回填实采价。`, relatedOrderCode: input.orderCode, branchCode: input.branchCode, }); } /** ③ 新验收单:通知仓管/验收 */ export async function notifyInspectionsCreated(input: { orderCode: string; branchCode?: string | null; count: number; }) { await notifyMainline({ roles: ['warehouse', ...ADMIN_ROLES], notifyType: 'inspection_created', title: `新验收任务:订单 ${input.orderCode}`, content: `订单 ${input.orderCode} 新增 ${input.count} 条待验收记录,请及时验收入库。`, relatedOrderCode: input.orderCode, branchCode: input.branchCode, }); } /** ④ 新分拣任务:通知分拣员 */ export async function notifyPickingTaskCreated(input: { pickCode: string; orderCode?: string | null; branchCode?: string | null; }) { await notifyMainline({ roles: ['picker', 'warehouse', ...ADMIN_ROLES], notifyType: 'picking_task_created', title: `新分拣任务:${input.pickCode}`, content: `分拣单 ${input.pickCode}${input.orderCode ? `(订单 ${input.orderCode})` : ''} 已生成,请及时拣货复核。`, relatedOrderCode: input.orderCode || null, branchCode: input.branchCode, }); } /** ⑤ 新配送任务:通知配送员 */ export async function notifyDeliveryTaskCreated(input: { taskCode: string; orderCode?: string | null; branchCode?: string | null; }) { await notifyMainline({ roles: ['delivery', ...ADMIN_ROLES], notifyType: 'delivery_task_created', title: `新配送任务:订单 ${input.orderCode || input.taskCode}`, content: `配送任务 ${input.taskCode}${input.orderCode ? `(订单 ${input.orderCode})` : ''} 已生成,请及时接单配送。`, relatedOrderCode: input.orderCode || null, branchCode: input.branchCode, }); } /** ⑥ 结算单生成待确认:通知财务 */ export async function notifyStatementPendingConfirm(input: { statementType: 'customer' | 'supplier'; cycleCode: string; count: number; branchCode?: string | null; }) { const typeLabel = input.statementType === 'customer' ? '客户' : '供应商'; await notifyMainline({ roles: ['finance', 'admin', 'super_admin'], notifyType: 'statement_pending_confirm', title: `${typeLabel}结算单待确认:${input.cycleCode}`, content: `账期 ${input.cycleCode} 新增 ${input.count} 张${typeLabel}结算单,请及时核对确认。`, branchCode: input.branchCode, }); } /** ⑦ 订单送达完成:通知运营 */ export async function notifyOrderDelivered(input: { orderCode: string; branchCode?: string | null; }) { await notifyMainline({ roles: ADMIN_ROLES, notifyType: 'order_delivered', title: `订单已送达:${input.orderCode}`, content: `订单 ${input.orderCode} 配送签收完成,已归档为已完成。`, relatedOrderCode: input.orderCode, branchCode: input.branchCode, }); }