fix(价格回写): 补名称解析与商品库兜底 upsert
录单行只有名称时解析 SKU;syncCustomerMappings 未建客户商品库行时 upsert 兜底;避免价格静默丢失;共享报价单仍跳过。
This commit is contained in:
@@ -101,7 +101,7 @@ body:`{ sku_code, sale_price: number, target_type?: 'quote_sheet'|'customer_sk
|
||||
1. 客户执行中报价单含该 SKU:
|
||||
- 报价单只绑定一个客户 → 写 `sale_price + price`,并置 `sale_price_manual=true`;
|
||||
- 绑定多个客户 → 跳过(避免把 A 客户成交价串给 B 客户)。
|
||||
2. 否则只更新**已存在**的 `customer_skus.sale_price`,不 INSERT;
|
||||
2. 否则更新 `customer_skus.sale_price`;正常路径由 `syncCustomerMappings` 建行,若该非阻塞步骤未建行/未解析出 SKU,回写做 upsert 兜底,避免静默丢失;
|
||||
3. 两者都没有、加菜行、`price<=0` → 跳过;
|
||||
4. 同值不写;提交写全部订单行,改单只写本次价格变化的行;
|
||||
5. 失败只记日志,不影响下单/保存;成功后合并一条 `order_sale_price_writeback` 审计。
|
||||
|
||||
@@ -46,19 +46,54 @@ export async function writeBackOrderSalePrices(input: {
|
||||
if (!customerCode || !orderCode) return result;
|
||||
|
||||
const entries = new Map<string, number>();
|
||||
const namesBySku = new Map<string, string>();
|
||||
for (const item of input.items || []) {
|
||||
const extra = item?.is_extra_dish;
|
||||
if (extra === true || extra === 't' || extra === 'true' || extra === 1) {
|
||||
result.skipped += 1;
|
||||
continue;
|
||||
}
|
||||
const skuCode = trim(item?.sku_code);
|
||||
let skuCode = trim(item?.sku_code);
|
||||
const price = Number(item?.price);
|
||||
if (!skuCode || !Number.isFinite(price) || price <= 0) {
|
||||
if (!Number.isFinite(price) || price <= 0) {
|
||||
result.skipped += 1;
|
||||
continue;
|
||||
}
|
||||
// 提交行可能只有名称(syncCustomerMappings 按名称建了库行);这里补解析,避免价没写。
|
||||
if (!skuCode && item?.item_name) {
|
||||
const name = trim(item.item_name);
|
||||
const params: unknown[] = [name];
|
||||
let branchFilter = '';
|
||||
if (branchCode) {
|
||||
params.push(branchCode);
|
||||
branchFilter = `AND (branch_code IS NULL OR branch_code = $${params.length})`;
|
||||
}
|
||||
const found = await pool.query(
|
||||
`SELECT sku_code FROM skus
|
||||
WHERE sku_name = $1 AND status = 1 ${branchFilter}
|
||||
ORDER BY created_at ASC NULLS LAST
|
||||
LIMIT 1`,
|
||||
params
|
||||
);
|
||||
skuCode = trim(found.rows[0]?.sku_code);
|
||||
if (!skuCode) {
|
||||
const fuzzy = await pool.query(
|
||||
`SELECT sku_code FROM skus
|
||||
WHERE sku_name ILIKE $1 AND status = 1 ${branchFilter}
|
||||
ORDER BY created_at ASC NULLS LAST
|
||||
LIMIT 1`,
|
||||
[params[0], ...params.slice(1)]
|
||||
);
|
||||
skuCode = trim(fuzzy.rows[0]?.sku_code);
|
||||
}
|
||||
}
|
||||
if (!skuCode) {
|
||||
result.skipped += 1;
|
||||
continue;
|
||||
}
|
||||
entries.set(skuCode, Math.round(price * 10000) / 10000);
|
||||
const itemName = trim(item?.item_name);
|
||||
if (itemName) namesBySku.set(skuCode, itemName);
|
||||
}
|
||||
if (!entries.size) return result;
|
||||
|
||||
@@ -84,6 +119,7 @@ export async function writeBackOrderSalePrices(input: {
|
||||
AND qs.status = 1 AND qs.approval_status = 30
|
||||
AND (qs.expires_at IS NULL OR qs.expires_at > NOW())
|
||||
${branchFilter}
|
||||
ORDER BY qs.version_no DESC NULLS LAST, qs.updated_at DESC NULLS LAST, qs.created_at DESC NULLS LAST
|
||||
LIMIT 1`,
|
||||
params
|
||||
);
|
||||
@@ -117,17 +153,21 @@ export async function writeBackOrderSalePrices(input: {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 商品库只更新已存在行,不 INSERT。
|
||||
// 无报价单:正常路径 syncCustomerMappings 已建行;若该非阻塞步骤没建/没解析出 SKU,
|
||||
// 这里 upsert 兜底,避免“订单价只存在订单行、下次仍无默认价”的静默失败。
|
||||
const updated = await pool.query(
|
||||
`UPDATE customer_skus
|
||||
SET sale_price = $1::numeric,
|
||||
price_updated_at = NOW(),
|
||||
price_updated_by = $3::uuid,
|
||||
updated_at = NOW()
|
||||
WHERE customer_code = $2
|
||||
AND sku_code = $4
|
||||
AND sale_price IS DISTINCT FROM $1::numeric`,
|
||||
[price, customerCode, trim(input.operatorId) || null, skuCode]
|
||||
`INSERT INTO customer_skus
|
||||
(customer_code, sku_code, branch_code, customer_sku_name, sale_price,
|
||||
price_updated_at, price_updated_by)
|
||||
VALUES ($1, $2, $3, $4, $5::numeric, NOW(), $6::uuid)
|
||||
ON CONFLICT (customer_code, sku_code) DO UPDATE
|
||||
SET sale_price = EXCLUDED.sale_price,
|
||||
price_updated_at = NOW(),
|
||||
price_updated_by = EXCLUDED.price_updated_by,
|
||||
updated_at = NOW()
|
||||
WHERE customer_skus.sale_price IS DISTINCT FROM EXCLUDED.sale_price
|
||||
RETURNING sku_code`,
|
||||
[customerCode, skuCode, branchCode, namesBySku.get(skuCode) || null, price, trim(input.operatorId) || null]
|
||||
);
|
||||
if ((updated.rowCount || 0) > 0) {
|
||||
customerSkuChanges.push({ sku_code: skuCode, sale_price: price });
|
||||
|
||||
@@ -15,11 +15,11 @@ describe('订单销售价回写价格主数据', () => {
|
||||
expect(svc).toContain('result.skipped += 1');
|
||||
});
|
||||
|
||||
it('报价单优先并置 manual;无报价单只更新已有 customer_skus,不 INSERT', () => {
|
||||
it('报价单优先并置 manual;无报价单 upsert customer_skus 兜底', () => {
|
||||
expect(svc).toContain('sale_price_manual = true');
|
||||
expect(svc).toContain('UPDATE customer_quote_sheet_items');
|
||||
expect(svc).toContain('UPDATE customer_skus');
|
||||
expect(svc).not.toContain('INSERT INTO customer_skus');
|
||||
expect(svc).toContain('INSERT INTO customer_skus');
|
||||
expect(svc).toContain('ON CONFLICT (customer_code, sku_code)');
|
||||
expect(svc).toContain('IS DISTINCT FROM');
|
||||
expect(svc).toContain("operationType: 'order_sale_price_writeback'");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user