问题:拉单早于商品入库(新店铺第一次拉单,或商品是事后才补拉的)时,明细会停在 shop_product_id=null(parse_msg「未采集到该店铺商品」),之后商品补齐也不会自动 变化 —— 订单页一直显示未绑定,还被「仅已绑定」过滤掉,看起来像「有商品却匹配不上」。 另外「重新解析」旧接口只回写 order_items,改不动订单页读的状态明细表。 - 新增 lib/shopProductMatch:把「店铺 + 平台商品ID + 平台规格ID」等值匹配抽成纯函数, 明细带规格ID时必须精确命中同规格,不退化按商品ID乱取(避免不同规格绑成一行) - orderItemResolve.rematchUnboundItems:商品写入后在同一事务里补匹配该店铺 shop_product_id 为空的明细,并重建命中订单(只动待审核/异常,已审核随审核锁定) - saveShopProducts 调用它并返回 rematched;同步/单品补拉两个接口与前端提示都带上 - 文档补 §4.2.5:明细 → 店铺商品 → 系统商品的匹配口径、两种 parse_status=2 的区别 - 单测 shopProductMatch.test.js(精确命中 / 不退化 / 无规格取首行 / 空输入) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
44 lines
1.8 KiB
JavaScript
44 lines
1.8 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 店铺商品匹配索引
|
||
*
|
||
* 把「店铺 + 平台商品ID + 平台规格ID」的等值匹配抽成纯函数:拉单时逐条查库,
|
||
* 商品同步后的批量重匹配则一次查全量再内存比对,两条链路共用同一套口径,避免出现
|
||
* 「拉单匹配上了、重匹配没匹配上」这类不一致。
|
||
*
|
||
* 匹配规则:
|
||
* - 明细带平台规格ID 时必须精确命中同一规格,**不允许退化成按商品ID随便取一条**
|
||
* (否则不同规格会绑到同一行店铺商品,出库发错货);
|
||
* - 明细没有平台规格ID(无规格商品/部分平台不给 SkuId)时按商品ID取 id 最小的那条。
|
||
*/
|
||
|
||
/**
|
||
* 生成匹配函数
|
||
* @param {Array<{id:number, outer_item_id:string, outer_sku_id?:string}>} rows 店铺商品行(建议按 id 升序)
|
||
* @return {(line: {outer_item_id?:string, outer_sku_id?:string}) => number|null} 匹配函数,返回命中的店铺商品ID
|
||
*/
|
||
function createShopProductMatcher(rows) {
|
||
const skuIndex = new Map();
|
||
const itemIndex = new Map();
|
||
|
||
for (const row of rows || []) {
|
||
const itemId = String((row && row.outer_item_id) || '').trim();
|
||
const id = Number(row && row.id);
|
||
if (!itemId || !id) continue;
|
||
const skuId = String((row && row.outer_sku_id) || '').trim();
|
||
if (skuId && !skuIndex.has(`${itemId}||${skuId}`)) skuIndex.set(`${itemId}||${skuId}`, id);
|
||
if (!itemIndex.has(itemId)) itemIndex.set(itemId, id);
|
||
}
|
||
|
||
return line => {
|
||
const itemId = String((line && line.outer_item_id) || '').trim();
|
||
if (!itemId) return null;
|
||
const skuId = String((line && line.outer_sku_id) || '').trim();
|
||
if (skuId) return skuIndex.get(`${itemId}||${skuId}`) || null;
|
||
return itemIndex.get(itemId) || null;
|
||
};
|
||
}
|
||
|
||
module.exports = { createShopProductMatcher };
|