来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
132 lines
3.9 KiB
TypeScript
132 lines
3.9 KiB
TypeScript
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<string, PriceLock>; operations: BatchOperation[] }> {
|
|
const now = new Date().toISOString();
|
|
const skuCodes = [...new Set(input.items.map((item) => item.skuCode).filter(Boolean))];
|
|
const locks = new Map<string, PriceLock>();
|
|
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<string, QuoteVersionRow>();
|
|
((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<string, PriceLock>();
|
|
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 };
|
|
}
|