oms/backend/app/lib/shopProductMatch.js
liufumingren 20147fb391 fix(shop-product): 1688 单靠店管家规格编码匹配店铺商品
1688(Alibaba)订单的 SubOrders[].SkuId 被店管家塞成了商品标题,
平台规格ID 这条路永远匹配不到店铺商品(旧系统 Dianguanjia.php 的 get_sku_bind
只按 SkuId 严格等值,同样匹配不上)。规格的真实唯一键是店管家规格编码:
商品库 Skus[].SkuCode(如 10000446302877)= 订单 PtProductInfo.SkuCode,
抖店/拼多多的 SubOrders[].SkuCode 也是这个值,跨平台通用。

- shop_products 增列 outer_sku_code(迁移 20261109100000),商品同步时写入
  Skus[].SkuCode,并加入 upsert 字段
- 订单明细归一化增补 outer_sku_code(PtProductInfo.SkuCode 优先,退 SkuCode),
  随 platform_snapshot 落库;历史明细由 resolveItemSkuCode 回落到 raw.PtProductInfo.SkuCode
- 匹配口径改为「平台规格ID → 店管家规格编码 → (仅当完全无规格信息)商品ID 兜底」,
  带了规格信息却都没命中时不退化成按商品ID取一条,避免绑错规格
- 拉单逐条匹配、明细解析、商品同步后批量重匹配三处共用 shopProductScopes/createShopProductMatcher
- 实测店铺 19 同步 1438 行后 rematched=44,1688 明细全部指向正确规格
  (10000446302877→420ml*1瓶+排骨梳、…878→99ML黑色、…881→420ML)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-08 18:01:24 +08:00

110 lines
5.0 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 店铺商品匹配索引
*
* 把「店铺 + 平台商品ID + 平台规格ID」的等值匹配抽成纯函数:拉单时逐条查库,
* 商品同步后的批量重匹配则一次查全量再内存比对,两条链路共用同一套口径,避免出现
* 「拉单匹配上了、重匹配没匹配上」这类不一致。
*
* 匹配规则(按优先级):
* 1. 明细带平台规格ID 时先精确命中同一规格;
* 2. 命不中(或明细没有规格ID)时按**店管家规格编码**(SKU 的 `SkuCode`)匹配:
* 1688(Alibaba)订单的平台规格ID 被店管家塞成了商品标题,规格唯一键只剩这个编码,
* 商品库 `Skus[].SkuCode` 与订单 `PtProductInfo.SkuCode` 是同一个值;
* 3. 明细完全没有规格ID(无规格商品/部分平台不给)时按商品ID取 id 最小的那条。
*
* 明细带规格ID但两种键都命不中时**不允许退化成按商品ID随便取一条**
* (否则不同规格会绑到同一行店铺商品,出库发错货)。
*/
/**
* 规格编码索引键(与平台规格ID 分开命名,避免两者的值撞在一起)
* @param {string} itemId 平台商品ID
* @param {string} code 店管家规格编码
* @return {string} 索引键
*/
function skuCodeKey(itemId, code) {
return `C${itemId}||${code}`;
}
/**
* 生成匹配函数
* @param {Array<{id:number, outer_item_id:string, outer_sku_id?:string, outer_sku_code?:string}>} rows 店铺商品行(建议按 id 升序)
* @return {(line: {outer_item_id?:string, outer_sku_id?:string, outer_sku_code?:string}) => number|null} 匹配函数,返回命中的店铺商品ID
*/
function createShopProductMatcher(rows) {
const skuIndex = new Map();
const codeIndex = 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);
const code = String((row && row.outer_sku_code) || '').trim();
if (code && !codeIndex.has(skuCodeKey(itemId, code))) codeIndex.set(skuCodeKey(itemId, code), 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();
const code = String((line && line.outer_sku_code) || '').trim();
if (skuId) {
const hit = skuIndex.get(`${itemId}||${skuId}`);
if (hit) return hit;
return code ? codeIndex.get(skuCodeKey(itemId, code)) || null : null;
}
if (code) return codeIndex.get(skuCodeKey(itemId, code)) || null;
return itemIndex.get(itemId) || null;
};
}
/**
* 明细匹配店铺商品时的查询条件(按优先级返回多个,逐个尝试)
*
* 有店铺信息时**只认本店铺**:同一个平台商品可能被多个店铺采集(同款分销、多店铺同源),
* 按主体范围查会命中别家店铺的规格行,把别家的规格/绑定关系套到本单上(发错货)。
* 只有没有店铺信息的明细(手工录入)才退回主体范围。
* @param {{outer_item_id?:string, outer_sku_id?:string, outer_sku_code?:string}} line 订单明细
* @param {{shopId?:number|string, principalId?:number|string}} scope 归属范围
* @return {Array<Object>} Sequelize where 条件列表,缺少平台商品ID时返回空数组
*/
function shopProductScopes(line, scope = {}) {
const itemId = String((line && line.outer_item_id) || '').trim();
if (!itemId) return [];
const owner = {};
const shopId = scope.shopId;
if (shopId != null && shopId !== '') owner.shop_id = Number(shopId);
else if (scope.principalId != null && scope.principalId !== '') owner.principal_id = Number(scope.principalId);
const skuId = String((line && line.outer_sku_id) || '').trim();
const code = String((line && line.outer_sku_code) || '').trim();
const where = [];
if (skuId) where.push({ outer_item_id: itemId, outer_sku_id: skuId, ...owner });
if (code) where.push({ outer_item_id: itemId, outer_sku_code: code, ...owner });
// 带了规格ID/规格编码却没命中时不再退化(会绑错规格),只有「本来就没有规格信息」才按商品ID兜底
if (!skuId && !code) where.push({ outer_item_id: itemId, ...owner });
return where;
}
/**
* 明细匹配店铺商品时的首选查询条件
* @param {{outer_item_id?:string, outer_sku_id?:string, outer_sku_code?:string}} line 订单明细
* @param {{shopId?:number|string, principalId?:number|string}} scope 归属范围
* @return {Object|null} Sequelize where 条件,缺少平台商品ID时返回 null
*/
function shopProductScope(line, scope = {}) {
const list = shopProductScopes(line, scope);
return list.length ? list[0] : null;
}
module.exports = { createShopProductMatcher, shopProductScope, shopProductScopes };