线上出现「订单明细绑到了别家店铺的商品」:旧解析接口(`POST /api/order-items/{id}/parse`
与 `POST /api/orders/{id}/parse-items`)匹配店铺商品时只按 `principal_id + 平台商品ID +
规格ID` 查,同一个平台商品被多个店铺采集时(同款分销、多店铺同源)会命中别家店铺的行,
把别家的规格与绑定关系套到本单上,出库会发错货。
- 新增 lib/shopProductMatch.shopProductScope:匹配条件的唯一口径 —— 有店铺信息时只认本店铺,
只有无店铺信息的手工明细才退回主体范围;拉单(externalPrintOrder)与重解析共用它
- 解析逻辑下沉到 service/orderItemParse(`findShopProduct` / `isStaleShopProduct` /
`resolveItem` / `parseOrderItems`)。原来 order.js 里用的 `ctx.controller.orderItem`
在 Egg 里并不存在(controller 只挂到 app.controller),该接口一直是 500
- 失效关联自愈:明细指向别家店铺或已被删除的店铺商品时,重解析会连带清空
product_id/combo_product_id/item_type,不再沿用错误的系统商品;批量解析不再跳过这类脏数据
- 单测补 shopProductScope 4 例(本店铺 / 手工退回主体 / 空规格 / 缺商品ID)
实测(真实库,插入即回滚):shop 18 有、shop 19 没有时解析结果 shop_product_id=null、
product_id 同时被清空;补上 shop 19 的商品后命中 shop_product_id=1077、parse_status=1。
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
68 lines
2.9 KiB
JavaScript
68 lines
2.9 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;
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 明细匹配店铺商品时的查询条件
|
||
*
|
||
* 有店铺信息时**只认本店铺**:同一个平台商品可能被多个店铺采集(同款分销、多店铺同源),
|
||
* 按主体范围查会命中别家店铺的规格行,把别家的规格/绑定关系套到本单上(发错货)。
|
||
* 只有没有店铺信息的明细(手工录入)才退回主体范围。
|
||
* @param {{outer_item_id?:string, outer_sku_id?:string}} line 订单明细
|
||
* @param {{shopId?:number|string, principalId?:number|string}} scope 归属范围
|
||
* @return {Object|null} Sequelize where 条件,缺少平台商品ID时返回 null
|
||
*/
|
||
function shopProductScope(line, scope = {}) {
|
||
const itemId = String((line && line.outer_item_id) || '').trim();
|
||
if (!itemId) return null;
|
||
|
||
const where = { outer_item_id: itemId };
|
||
const skuId = String((line && line.outer_sku_id) || '').trim();
|
||
if (skuId) where.outer_sku_id = skuId;
|
||
|
||
const shopId = scope.shopId;
|
||
if (shopId != null && shopId !== '') where.shop_id = Number(shopId);
|
||
else if (scope.principalId != null && scope.principalId !== '') where.principal_id = Number(scope.principalId);
|
||
return where;
|
||
}
|
||
|
||
module.exports = { createShopProductMatcher, shopProductScope };
|