import { supabase } from '../supabase/client'; export interface PurchaserInfo { purchaser_code: string; purchaser_name: string; } let cachedPrimaryPurchaser: PurchaserInfo | null = null; const branchPurchaserCache = new Map(); export async function fetchPrimaryPurchaser(forceRefresh = false): Promise { if (cachedPrimaryPurchaser && !forceRefresh) { return cachedPrimaryPurchaser; } const { data: primary } = await supabase .from('purchaser_users') .select('purchaser_code, purchaser_name') .eq('is_primary', true) .eq('status', 1) .limit(1) .maybeSingle(); if (primary?.purchaser_code) { cachedPrimaryPurchaser = primary; return primary; } const { data: fallback } = await supabase .from('purchaser_users') .select('purchaser_code, purchaser_name') .eq('status', 1) .order('created_at', { ascending: true }) .limit(1) .maybeSingle(); if (fallback?.purchaser_code) { cachedPrimaryPurchaser = fallback; return fallback; } return null; } /** 优先匹配订单所属分公司的采购员,无则回退全局主采购员 */ export async function fetchPrimaryPurchaserForBranch( branchCode?: string | null, forceRefresh = false ): Promise { const normalized = branchCode?.trim(); if (!normalized) { return fetchPrimaryPurchaser(forceRefresh); } if (!forceRefresh && branchPurchaserCache.has(normalized)) { return branchPurchaserCache.get(normalized)!; } const { data: profileRows } = await supabase .from('profiles') .select('id') .eq('branch_code', normalized); const userIds = (profileRows || []).map((row: { id: string }) => row.id).filter(Boolean); if (userIds.length) { const { data: branchPurchasers } = await supabase .from('purchaser_users') .select('purchaser_code, purchaser_name, is_primary, created_at') .in('user_id', userIds) .eq('status', 1) .order('is_primary', { ascending: false }) .order('created_at', { ascending: true }) .limit(1); const branchPurchaser = branchPurchasers?.[0]; if (branchPurchaser?.purchaser_code) { const info = { purchaser_code: branchPurchaser.purchaser_code, purchaser_name: branchPurchaser.purchaser_name, }; branchPurchaserCache.set(normalized, info); return info; } } const global = await fetchPrimaryPurchaser(forceRefresh); if (global) { branchPurchaserCache.set(normalized, global); } return global; } export async function fetchOrderBranchCode(orderCode: string): Promise { const { data } = await supabase .from('orders') .select('branch_code') .eq('order_code', orderCode) .maybeSingle(); return data?.branch_code?.trim() || null; } export async function resolvePurchaserForOrder(params: { orderCode: string; branchCode?: string | null; purchaser?: PurchaserInfo | null; }): Promise { if (params.purchaser?.purchaser_code) { return params.purchaser; } const branchCode = params.branchCode?.trim() || (await fetchOrderBranchCode(params.orderCode)); const purchaser = await fetchPrimaryPurchaserForBranch(branchCode); if (!purchaser) { throw new Error('未配置采购员,请先在采购员管理中创建主采购员'); } return purchaser; } export function clearPrimaryPurchaserCache() { cachedPrimaryPurchaser = null; branchPurchaserCache.clear(); }