Files
SupplierSystem/src/utils/purchaserAssignment.ts
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

123 lines
3.4 KiB
TypeScript

import { supabase } from '../supabase/client';
export interface PurchaserInfo {
purchaser_code: string;
purchaser_name: string;
}
let cachedPrimaryPurchaser: PurchaserInfo | null = null;
const branchPurchaserCache = new Map<string, PurchaserInfo>();
export async function fetchPrimaryPurchaser(forceRefresh = false): Promise<PurchaserInfo | null> {
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<PurchaserInfo | null> {
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<string | null> {
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<PurchaserInfo> {
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();
}