import { supabase, type BatchOperation } from '../supabase/client'; export interface PriceLockItem { key: string; skuCode: string; unit?: string | null; price: number; } export interface PriceLock { quoteVersionCode: string; lockedPrice: number; priceSource: 'quote_version' | 'order_submit'; priceLockedAt: string; } interface QuoteVersionRow { quote_version_code: string; sku_code: string; unit: string | null; price: number | string; version_no: number | null; } function quoteVersionCode() { return `QV${Date.now()}${Math.random().toString(36).slice(2, 7).toUpperCase()}`; } function samePrice(left: unknown, right: unknown) { return Math.round(Number(left || 0) * 100) === Math.round(Number(right || 0) * 100); } export async function preparePriceLocks(input: { customerCode: string; items: PriceLockItem[]; createdBy?: string | null; }): Promise<{ locks: Map; operations: BatchOperation[] }> { const now = new Date().toISOString(); const skuCodes = [...new Set(input.items.map((item) => item.skuCode).filter(Boolean))]; const locks = new Map(); const operations: BatchOperation[] = []; if (!input.customerCode || skuCodes.length === 0) { return { locks, operations }; } const { data, error } = await supabase .from('customer_quote_versions') .select('quote_version_code, sku_code, unit, price, version_no, approval_status') .eq('customer_code', input.customerCode) .eq('status', 1) .in('sku_code', skuCodes) .order('effective_at', { ascending: false }); if (error) throw new Error(error.message || '读取客户报价版本失败'); const activeBySku = new Map(); ((data || []) as QuoteVersionRow[]).forEach((row) => { if (Number((row as any).approval_status) && Number((row as any).approval_status) !== 30) return; if (!activeBySku.has(row.sku_code)) activeBySku.set(row.sku_code, row); }); const preparedByFingerprint = new Map(); input.items.forEach((item) => { const active = activeBySku.get(item.skuCode); const unit = item.unit || null; const fingerprint = `${item.skuCode}::${unit || ''}::${Math.round(Number(item.price || 0) * 100)}`; const prepared = preparedByFingerprint.get(fingerprint); if (prepared) { locks.set(item.key, prepared); return; } if (active && samePrice(active.price, item.price) && (active.unit || null) === unit) { const lock = { quoteVersionCode: active.quote_version_code, lockedPrice: Number(active.price) || 0, priceSource: 'quote_version', priceLockedAt: now, } as PriceLock; preparedByFingerprint.set(fingerprint, lock); locks.set(item.key, lock); return; } const nextCode = quoteVersionCode(); if (active) { operations.push({ table: 'customer_quote_versions', action: 'update', updateData: { status: 0, expired_at: now, }, filters: [ { op: 'eq', column: 'customer_code', value: input.customerCode }, { op: 'eq', column: 'sku_code', value: item.skuCode }, { op: 'eq', column: 'status', value: 1 }, ], }); } operations.push({ table: 'customer_quote_versions', action: 'insert', insertData: { quote_version_code: nextCode, customer_code: input.customerCode, sku_code: item.skuCode, unit, price: item.price, version_no: Number(active?.version_no || 0) + 1, source_type: 'order_submit', effective_at: now, status: 1, created_by: input.createdBy || null, }, }); const lock = { quoteVersionCode: nextCode, lockedPrice: item.price, priceSource: 'order_submit', priceLockedAt: now, } as PriceLock; preparedByFingerprint.set(fingerprint, lock); locks.set(item.key, lock); }); return { locks, operations }; }