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>
This commit is contained in:
parent
62df580393
commit
20147fb391
@ -88,6 +88,7 @@ const SHOP_PRODUCT_UPSERT_FIELDS = [
|
||||
'subsidiary_id',
|
||||
'title',
|
||||
'outer_code',
|
||||
'outer_sku_code',
|
||||
'spec',
|
||||
'image',
|
||||
'price',
|
||||
@ -763,6 +764,7 @@ class ExternalPrintAccountController extends Controller {
|
||||
shop_id: shop.id,
|
||||
outer_item_id: item.outer_item_id,
|
||||
outer_sku_id: item.outer_sku_id || '',
|
||||
outer_sku_code: item.outer_sku_code || null,
|
||||
title: item.title || null,
|
||||
outer_code: item.outer_code || null,
|
||||
spec: item.spec || null,
|
||||
|
||||
@ -60,10 +60,13 @@ function roundMoney(value) {
|
||||
*/
|
||||
function buildSnapshot(line, lineNo) {
|
||||
const data = line || {};
|
||||
// 店管家规格编码:平台规格ID为标题/缺失时(1688 单)靠它重新匹配店铺商品
|
||||
const outerSkuCode = text(data.outer_sku_code, 64);
|
||||
return {
|
||||
item_code: text(data.sku_code || data.outer_item_id, 100) || null,
|
||||
item_name: text(data.name, 255) || null,
|
||||
sku_code: text(data.sku_code, 100) || null,
|
||||
...(outerSkuCode ? { outer_sku_code: outerSkuCode } : {}),
|
||||
spec: text(data.spec, 255) || null,
|
||||
image: text(data.image, 255) || null,
|
||||
line_no: text(lineNo == null ? '' : lineNo, 20),
|
||||
@ -100,6 +103,7 @@ function foldPlatformLines(rows) {
|
||||
shop_product_id: row.shop_product_id == null ? null : Number(row.shop_product_id),
|
||||
outer_item_id: row.outer_item_id || null,
|
||||
outer_sku_id: row.outer_sku_id || null,
|
||||
outer_sku_code: (snapshot && snapshot.outer_sku_code) || null,
|
||||
platform_snapshot: snapshot,
|
||||
template: row,
|
||||
});
|
||||
@ -226,6 +230,24 @@ function expandPlatformLine(line, binding = {}) {
|
||||
}];
|
||||
}
|
||||
|
||||
/**
|
||||
* 取明细的店管家规格编码(匹配店铺商品的第二键)
|
||||
*
|
||||
* 优先用写入时的平台快照(拉单/重建时写入),历史数据没有快照字段时回落到平台原始报文:
|
||||
* 1688 的编码在 `PtProductInfo.SkuCode`,抖店/拼多多直接放在 `SkuCode`。
|
||||
* @param {Object} row 明细行(order_items / order_status_items,含 platform_snapshot 与 raw)
|
||||
* @return {string} 店管家规格编码,取不到时返回空字符串
|
||||
*/
|
||||
function resolveItemSkuCode(row) {
|
||||
const data = row || {};
|
||||
const snapshot = data.platform_snapshot && typeof data.platform_snapshot === 'object' ? data.platform_snapshot : null;
|
||||
if (snapshot && snapshot.outer_sku_code) return text(snapshot.outer_sku_code, 64);
|
||||
const raw = data.raw && typeof data.raw === 'object' ? data.raw : null;
|
||||
if (!raw) return '';
|
||||
const ptInfo = raw.PtProductInfo && typeof raw.PtProductInfo === 'object' ? raw.PtProductInfo : {};
|
||||
return text(ptInfo.SkuCode || raw.SkuCode, 64);
|
||||
}
|
||||
|
||||
/**
|
||||
* 子行来源编码(保留原来源 + 序号,便于溯源)
|
||||
* @param {string} [sourceCode] 原行来源编码
|
||||
@ -249,6 +271,7 @@ function normalizeSnapshot(line) {
|
||||
if (!snapshot.item_code) snapshot.item_code = line.item_code || null;
|
||||
if (!snapshot.item_name) snapshot.item_name = line.item_name || null;
|
||||
if (snapshot.sku_code === undefined) snapshot.sku_code = line.sku_code || null;
|
||||
if (!snapshot.outer_sku_code && line.outer_sku_code) snapshot.outer_sku_code = text(line.outer_sku_code, 64);
|
||||
if (snapshot.spec === undefined) snapshot.spec = line.spec || null;
|
||||
if (snapshot.image === undefined) snapshot.image = line.image || null;
|
||||
if (!snapshot.line_no) snapshot.line_no = text(line.line_no, 20);
|
||||
@ -295,4 +318,5 @@ module.exports = {
|
||||
foldPlatformLines,
|
||||
expandPlatformLine,
|
||||
collapseUniqueGiftRows,
|
||||
resolveItemSkuCode,
|
||||
};
|
||||
|
||||
@ -665,6 +665,22 @@ function resolveOrderSkuId(data) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单明细的店管家规格编码(SKU 的 `SkuCode`)
|
||||
*
|
||||
* 1688(Alibaba)订单里 `SkuId` 被平台塞成商品标题,规格的真实唯一键只剩这个编码:
|
||||
* 订单侧在 `PtProductInfo.SkuCode`(如 `10000446302877`),抖店/拼多多则直接放在
|
||||
* `SubOrders[].SkuCode`(与商品库 `Skus[].SkuCode` 同一个值),所以优先取 `PtProductInfo`。
|
||||
* @param {Object} data 明细原始结构
|
||||
* @return {string} 店管家规格编码,取不到时返回空字符串
|
||||
*/
|
||||
function resolveOrderSkuCode(data) {
|
||||
const ptInfo = (data && data.PtProductInfo) || {};
|
||||
const value = String(ptInfo.SkuCode == null ? '' : ptInfo.SkuCode).trim()
|
||||
|| String(data && data.SkuCode == null ? '' : data.SkuCode).trim();
|
||||
return truncate(value, 64);
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归收集打印数据里的运单号(旧系统口径:PrintDataList.task.documents[].documentID)
|
||||
* @param {*} value 已解析的打印数据对象/数组
|
||||
@ -1907,6 +1923,8 @@ class DgjService extends PrintPlatformService {
|
||||
* 平台商品ID取商品级 PlatformId;平台规格ID统一取 SKU 的 `SkuId`
|
||||
* (早期用过 `SpecId`,两者在部分平台/商品上不一致,统一后不再使用 SpecId;
|
||||
* 另注意 SKU.PlatformId 是商品级平台ID,不能当规格ID),缺失时退化为店管家内部ID。
|
||||
* 同时保留 SKU 的 `SkuCode`(店管家内部规格编码):1688 订单的 `SkuId` 是商品标题,
|
||||
* 只能靠 `SkuCode` 对上规格(见 lib/shopProductMatch)。
|
||||
* 原样保留商品与 SKU 结构供后续使用。
|
||||
* @param {Object} product 店管家商品原始结构
|
||||
* @return {Array<Object>} 店铺商品行(一个 SKU 一行)
|
||||
@ -1928,6 +1946,7 @@ class DgjService extends PrintPlatformService {
|
||||
return {
|
||||
outer_item_id: String(data.PlatformId || ptInfo.PrductId || data.Id || ''),
|
||||
outer_sku_id: String(item.SkuId || item.Id || ''),
|
||||
outer_sku_code: truncate(item.SkuCode, 64) || null,
|
||||
title: truncate(data.Subject, 255),
|
||||
outer_code: String(item.CargoNumber || item.SkuSelfCode || data.CargoNumber || data.ProductSelfCode || ''),
|
||||
spec: truncate(item.Name || attrs, 255),
|
||||
@ -2479,6 +2498,7 @@ class DgjService extends PrintPlatformService {
|
||||
external_item_id: data.OrderItemId == null ? '' : truncate(data.OrderItemId, 64),
|
||||
outer_item_id: String(data.ProductID || ptInfo.PrductId || ''),
|
||||
outer_sku_id: resolveOrderSkuId(data),
|
||||
outer_sku_code: resolveOrderSkuCode(data),
|
||||
sku_code: truncate(data.CargoNumber || data.productCargoNumber || ptInfo.CargoNumber, 100),
|
||||
name: truncate(data.ProductSubject || ptInfo.ProductSubject, 255),
|
||||
spec: truncate(spec, 255),
|
||||
|
||||
@ -7,19 +7,35 @@
|
||||
* 商品同步后的批量重匹配则一次查全量再内存比对,两条链路共用同一套口径,避免出现
|
||||
* 「拉单匹配上了、重匹配没匹配上」这类不一致。
|
||||
*
|
||||
* 匹配规则:
|
||||
* - 明细带平台规格ID 时必须精确命中同一规格,**不允许退化成按商品ID随便取一条**
|
||||
* (否则不同规格会绑到同一行店铺商品,出库发错货);
|
||||
* - 明细没有平台规格ID(无规格商品/部分平台不给 SkuId)时按商品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}>} rows 店铺商品行(建议按 id 升序)
|
||||
* @return {(line: {outer_item_id?:string, outer_sku_id?:string}) => number|null} 匹配函数,返回命中的店铺商品ID
|
||||
* @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 || []) {
|
||||
@ -28,6 +44,8 @@ function createShopProductMatcher(rows) {
|
||||
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);
|
||||
}
|
||||
|
||||
@ -35,33 +53,57 @@ function createShopProductMatcher(rows) {
|
||||
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;
|
||||
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}} line 订单明细
|
||||
* @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 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;
|
||||
const list = shopProductScopes(line, scope);
|
||||
return list.length ? list[0] : null;
|
||||
}
|
||||
|
||||
module.exports = { createShopProductMatcher, shopProductScope };
|
||||
module.exports = { createShopProductMatcher, shopProductScope, shopProductScopes };
|
||||
|
||||
|
||||
@ -17,6 +17,7 @@ module.exports = app => {
|
||||
shop_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属店铺ID' },
|
||||
outer_item_id: { type: STRING(100), allowNull: false, comment: '平台商品ID' },
|
||||
outer_sku_id: { type: STRING(100), comment: '平台规格ID(SKU),无规格时为空' },
|
||||
outer_sku_code: { type: STRING(64), comment: '店管家规格编码(平台 SKU 的 SkuCode),SkuId 不可用时的匹配键' },
|
||||
title: { type: STRING(255), comment: '商品标题' },
|
||||
outer_code: { type: STRING(100), comment: '商家编码,用于与系统商品匹配' },
|
||||
spec: { type: STRING(255), comment: '规格' },
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
const Service = require('egg').Service;
|
||||
const { createPrintPlatformService, hasPrintPlatformService } = require('../lib/printPlatform');
|
||||
const { shopProductScope } = require('../lib/shopProductMatch');
|
||||
const { shopProductScopes } = require('../lib/shopProductMatch');
|
||||
const { ORDER_STATUS } = require('../lib/orderStatus');
|
||||
const { buildSnapshot } = require('../lib/orderItemExpand');
|
||||
|
||||
@ -890,15 +890,19 @@ class ExternalPrintOrderService extends Service {
|
||||
|
||||
/**
|
||||
* 按平台商品ID/规格ID命中店铺商品(规格ID为空时按商品ID匹配任意规格)
|
||||
* 规格ID 命不中时按店管家规格编码再试(1688 订单的平台规格ID 是商品标题,只能靠编码对上)
|
||||
* @param {Object} shop 店铺实例
|
||||
* @param {Object} line 平台明细
|
||||
* @return {Promise<Object|null>} 店铺商品
|
||||
*/
|
||||
async matchShopProduct(shop, line) {
|
||||
const { ctx } = this;
|
||||
const where = shopProductScope(line, { shopId: shop.id, principalId: shop.principal_id });
|
||||
if (!where) return null;
|
||||
return ctx.model.ShopProduct.findOne({ where });
|
||||
const scopes = shopProductScopes(line, { shopId: shop.id, principalId: shop.principal_id });
|
||||
for (const where of scopes) {
|
||||
const row = await ctx.model.ShopProduct.findOne({ where });
|
||||
if (row) return row;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
const { Service } = require('egg');
|
||||
const { shopProductScope } = require('../lib/shopProductMatch');
|
||||
const { shopProductScopes } = require('../lib/shopProductMatch');
|
||||
const { resolveItemSkuCode } = require('../lib/orderItemExpand');
|
||||
|
||||
/**
|
||||
* 订单明细解析服务(商家编码 / 店铺商品 → 系统商品 / 组合商品)
|
||||
@ -17,15 +18,23 @@ class OrderItemParseService extends Service {
|
||||
* 按平台商品ID/规格ID命中本单店铺的店铺商品
|
||||
* 历史上这里只按 principal_id 查,同一个平台商品被多个店铺采集时会命中别家店铺的行,
|
||||
* 把别家的规格/绑定关系套到本单上;现在只认本单店铺(无店铺信息的手工明细才退到主体范围)。
|
||||
* 规格ID 命不中时按店管家规格编码(`outer_sku_code`)再试一次(1688 单只有这个键可用)。
|
||||
* @param {Object} item 明细
|
||||
* @param {Number} principalId 主体ID
|
||||
* @return {Promise<Object|null>} 店铺商品
|
||||
*/
|
||||
async findShopProduct(item, principalId) {
|
||||
const { ctx } = this;
|
||||
const where = shopProductScope(item, { shopId: item.shop_id, principalId });
|
||||
if (!where) return null;
|
||||
return ctx.model.ShopProduct.findOne({ where });
|
||||
const scopes = shopProductScopes({
|
||||
outer_item_id: item.outer_item_id,
|
||||
outer_sku_id: item.outer_sku_id,
|
||||
outer_sku_code: resolveItemSkuCode(item),
|
||||
}, { shopId: item.shop_id, principalId });
|
||||
for (const where of scopes) {
|
||||
const row = await ctx.model.ShopProduct.findOne({ where });
|
||||
if (row) return row;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const Service = require('egg').Service;
|
||||
const { foldPlatformLines, normalizeSnapshot, expandPlatformLine, collapseUniqueGiftRows } = require('../lib/orderItemExpand');
|
||||
const { foldPlatformLines, normalizeSnapshot, expandPlatformLine, collapseUniqueGiftRows, resolveItemSkuCode } = require('../lib/orderItemExpand');
|
||||
const { createShopProductMatcher } = require('../lib/shopProductMatch');
|
||||
const { ORDER_STATUS } = require('../lib/orderStatus');
|
||||
|
||||
@ -132,7 +132,7 @@ class OrderItemResolveService extends Service {
|
||||
// 以库内为准(不用平台快照的中间态),按 id 升序保证「无规格时取第一条」稳定
|
||||
const products = await ctx.model.ShopProduct.findAll({
|
||||
where: { shop_id: shopKey },
|
||||
attributes: [ 'id', 'outer_item_id', 'outer_sku_id' ],
|
||||
attributes: [ 'id', 'outer_item_id', 'outer_sku_id', 'outer_sku_code' ],
|
||||
order: [[ 'id', 'ASC' ]],
|
||||
raw: true,
|
||||
transaction: options.transaction,
|
||||
@ -142,7 +142,7 @@ class OrderItemResolveService extends Service {
|
||||
|
||||
const items = await ctx.model.OrderStatusItem.findAll({
|
||||
where: { shop_id: shopKey, shop_product_id: { [Op.is]: null } },
|
||||
attributes: [ 'id', 'order_id', 'outer_item_id', 'outer_sku_id' ],
|
||||
attributes: [ 'id', 'order_id', 'outer_item_id', 'outer_sku_id', 'platform_snapshot', 'raw' ],
|
||||
raw: true,
|
||||
transaction: options.transaction,
|
||||
});
|
||||
@ -165,7 +165,13 @@ class OrderItemResolveService extends Service {
|
||||
let matched = 0;
|
||||
for (const item of items) {
|
||||
if (!editableIds.has(Number(item.order_id))) continue;
|
||||
const shopProductId = pick(item);
|
||||
// 店管家规格编码在平台快照(历史数据回落到平台原始报文)里:
|
||||
// 1688 单的平台规格ID 是商品标题,只能靠它匹配到指定规格
|
||||
const shopProductId = pick({
|
||||
outer_item_id: item.outer_item_id,
|
||||
outer_sku_id: item.outer_sku_id,
|
||||
outer_sku_code: resolveItemSkuCode(item),
|
||||
});
|
||||
if (!shopProductId) continue;
|
||||
await ctx.model.OrderStatusItem.update(
|
||||
{ shop_product_id: shopProductId },
|
||||
|
||||
@ -0,0 +1,27 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 店铺商品增加「店管家规格编码」(平台 SKU 的 `SkuCode`)。
|
||||
*
|
||||
* 店管家对同一个规格给两个键:
|
||||
* - `SkuId`:平台规格ID。抖店/拼多多/淘宝是真规格ID,但 **1688(Alibaba)订单里被塞成了商品标题**;
|
||||
* - `SkuCode`:店管家内部规格编码。1688 商品在 `Skus[].SkuCode`(如 10000446302877),
|
||||
* 1688 订单在 `SubOrders[].PtProductInfo.SkuCode`,抖店/拼多多订单则直接放在 `SubOrders[].SkuCode`。
|
||||
*
|
||||
* 1688 单靠 `SkuId` 永远匹配不到店铺商品(一边是标题、一边是数字规格ID),
|
||||
* 因此把 `SkuCode` 落库,作为「平台规格ID 匹配不上」时的第二匹配键。
|
||||
*/
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
await queryInterface.addColumn('shop_products', 'outer_sku_code', {
|
||||
type: Sequelize.STRING(64),
|
||||
allowNull: true,
|
||||
comment: '店管家规格编码(平台 SKU 的 SkuCode),SkuId 不可用时的匹配键',
|
||||
after: 'outer_sku_id',
|
||||
});
|
||||
},
|
||||
|
||||
async down(queryInterface) {
|
||||
await queryInterface.removeColumn('shop_products', 'outer_sku_code');
|
||||
},
|
||||
};
|
||||
83
backend/test/app/lib/dgjOrderSkuCode.test.js
Normal file
83
backend/test/app/lib/dgjOrderSkuCode.test.js
Normal file
@ -0,0 +1,83 @@
|
||||
'use strict';
|
||||
|
||||
const { assert } = require('egg-mock/bootstrap');
|
||||
const DgjService = require('../../../app/lib/printPlatform/dgj');
|
||||
|
||||
/**
|
||||
* 店管家订单明细的规格键。
|
||||
*
|
||||
* 1688(Alibaba)订单的 `SkuId` 被平台塞成**商品标题**,规格的真实唯一键只剩
|
||||
* `PtProductInfo.SkuCode`(商品库 `Skus[].SkuCode` 是同一个值);抖店/拼多多则
|
||||
* `SubOrders[].SkuCode` 本身就是这个编码,规格ID 也是真 ID,两边不能混。
|
||||
*/
|
||||
describe('dgj.normalizeOrderItem 规格键口径', () => {
|
||||
const service = new DgjService({ platform: { code: 'dgj', name: '店管家' }, account: { token: 't', cookie: 'c' } });
|
||||
|
||||
it('1688 明细:SkuId 是标题时规格ID 留空,规格编码取 PtProductInfo.SkuCode', () => {
|
||||
const item = service.normalizeOrderItem({
|
||||
OrderItemId: '10000311832500',
|
||||
ProductID: '881526926136',
|
||||
ProductSubject: '定型喷雾蓬松持久空气刘海发廊同款高颅顶99ML+420ML大小容量组合',
|
||||
SkuId: '定型喷雾蓬松持久空气刘海发廊同款高颅顶99ML+420ML大小容量组合',
|
||||
SkuCode: '007+111',
|
||||
CargoNumber: '007+111',
|
||||
productCargoNumber: '007',
|
||||
Color: '420ml*1瓶+排骨梳',
|
||||
Count: 1,
|
||||
PtProductInfo: {
|
||||
PrductId: '881526926136',
|
||||
SkuCode: '10000446302877',
|
||||
SpuCode: '007',
|
||||
CargoNumber: '007+111',
|
||||
},
|
||||
});
|
||||
|
||||
assert.strictEqual(item.outer_item_id, '881526926136');
|
||||
assert.strictEqual(item.outer_sku_id, '', '标题不是平台规格ID,不能当成规格ID 落库');
|
||||
assert.strictEqual(item.outer_sku_code, '10000446302877');
|
||||
assert.strictEqual(item.sku_code, '007+111', '商家编码口径不变');
|
||||
});
|
||||
|
||||
it('1688 明细:SkuId 是商家编码(006-1)时规格编码仍取 PtProductInfo.SkuCode', () => {
|
||||
const item = service.normalizeOrderItem({
|
||||
ProductID: '881526926136',
|
||||
SkuId: '006-1',
|
||||
SkuCode: '006-1',
|
||||
CargoNumber: '006-1',
|
||||
PtProductInfo: { SkuCode: '10000446302878', SpuCode: '007' },
|
||||
});
|
||||
|
||||
assert.strictEqual(item.outer_sku_id, '006-1');
|
||||
assert.strictEqual(item.outer_sku_code, '10000446302878');
|
||||
});
|
||||
|
||||
it('抖店明细:规格ID 是真 ID,规格编码取 PtProductInfo.SkuCode(与商品库 Skus[].SkuCode 同值)', () => {
|
||||
const item = service.normalizeOrderItem({
|
||||
ProductID: '3773823319627923739',
|
||||
SkuId: '3548726733104386',
|
||||
SkuCode: '5cf84d471b5d992b',
|
||||
CargoNumber: 'wf-C05速干定型喷雾420*1',
|
||||
PtProductInfo: { SkuCode: '5cf84d471b5d992b' },
|
||||
});
|
||||
|
||||
assert.strictEqual(item.outer_sku_id, '3548726733104386');
|
||||
assert.strictEqual(item.outer_sku_code, '5cf84d471b5d992b');
|
||||
});
|
||||
|
||||
it('商品库规格里带上 SkuCode(店铺商品第二匹配键)', () => {
|
||||
const rows = service.normalizeProduct({
|
||||
PlatformId: '881526926136',
|
||||
ShopId: 7398588,
|
||||
Subject: '定型喷雾蓬松持久空气刘海发廊同款高颅顶99ML+420ML大小容量组合',
|
||||
Status: 'published',
|
||||
Skus: [
|
||||
{ SkuId: '5937045124144', SkuCode: '10000446302877', CargoNumber: '007+111', Name: '420ml*1瓶+排骨梳' },
|
||||
],
|
||||
});
|
||||
|
||||
assert.strictEqual(rows.length, 1);
|
||||
assert.strictEqual(rows[0].outer_sku_id, '5937045124144');
|
||||
assert.strictEqual(rows[0].outer_sku_code, '10000446302877');
|
||||
assert.strictEqual(rows[0].outer_code, '007+111');
|
||||
});
|
||||
});
|
||||
68
backend/test/app/lib/shopProductSkuCode.test.js
Normal file
68
backend/test/app/lib/shopProductSkuCode.test.js
Normal file
@ -0,0 +1,68 @@
|
||||
'use strict';
|
||||
|
||||
const { assert } = require('egg-mock/bootstrap');
|
||||
const { createShopProductMatcher, shopProductScopes } = require('../../../app/lib/shopProductMatch');
|
||||
|
||||
describe('app/lib/shopProductMatch 店管家规格编码兜底匹配', () => {
|
||||
// 真实数据(店铺 19 / 商品 881526926136):1688 商品的规格ID 是数字,规格编码是 10xxxxxxx
|
||||
const rows = [
|
||||
{ id: 31, outer_item_id: '881526926136', outer_sku_id: '5723026017470', outer_sku_code: '10000446302879' },
|
||||
{ id: 32, outer_item_id: '881526926136', outer_sku_id: '5764820261768', outer_sku_code: '10000446302881' },
|
||||
{ id: 33, outer_item_id: '881526926136', outer_sku_id: '5937045124144', outer_sku_code: '10000446302877' },
|
||||
{ id: 34, outer_item_id: '881526926136', outer_sku_id: '5764820261769', outer_sku_code: '10000446302878' },
|
||||
{ id: 41, outer_item_id: '881526926136', outer_sku_id: '9999999999', outer_sku_code: null },
|
||||
];
|
||||
|
||||
it('1688 订单规格ID 是商品标题时按规格编码命中对应规格', () => {
|
||||
const pick = createShopProductMatcher(rows);
|
||||
// 订单报文:SkuId=商品标题(resolveOrderSkuId 判定不是ID → 空串),PtProductInfo.SkuCode=10000446302877
|
||||
assert.strictEqual(pick({ outer_item_id: '881526926136', outer_sku_id: '', outer_sku_code: '10000446302877' }), 33);
|
||||
assert.strictEqual(pick({ outer_item_id: '881526926136', outer_sku_id: '', outer_sku_code: '10000446302878' }), 34);
|
||||
});
|
||||
|
||||
it('规格ID 是商家编码(006-1 之类)时规格ID 命不中仍能按编码命中', () => {
|
||||
const pick = createShopProductMatcher(rows);
|
||||
assert.strictEqual(pick({ outer_item_id: '881526926136', outer_sku_id: '006-1', outer_sku_code: '10000446302878' }), 34);
|
||||
});
|
||||
|
||||
it('规格ID 命中时优先用规格ID(编码不一致也不影响)', () => {
|
||||
const pick = createShopProductMatcher(rows);
|
||||
assert.strictEqual(pick({ outer_item_id: '881526926136', outer_sku_id: '5723026017470', outer_sku_code: 'zzz' }), 31);
|
||||
});
|
||||
|
||||
it('规格ID 与编码都命不中时不退化成按商品ID取一条', () => {
|
||||
const pick = createShopProductMatcher(rows);
|
||||
assert.strictEqual(pick({ outer_item_id: '881526926136', outer_sku_id: '888', outer_sku_code: '404' }), null);
|
||||
assert.strictEqual(pick({ outer_item_id: '881526926136', outer_sku_code: '404' }), null);
|
||||
});
|
||||
|
||||
it('没有任何规格信息时才按商品ID取 id 最小的', () => {
|
||||
const pick = createShopProductMatcher(rows);
|
||||
assert.strictEqual(pick({ outer_item_id: '881526926136' }), 31);
|
||||
assert.strictEqual(pick({ outer_item_id: '881526926136', outer_sku_id: ' ', outer_sku_code: '' }), 31);
|
||||
});
|
||||
|
||||
it('shopProductScopes 按「规格ID → 规格编码 → 商品ID」给出查询顺序', () => {
|
||||
assert.deepStrictEqual(
|
||||
shopProductScopes({ outer_item_id: '881526926136', outer_sku_id: '', outer_sku_code: '10000446302877' }, { shopId: 19 }),
|
||||
[{ outer_item_id: '881526926136', outer_sku_code: '10000446302877', shop_id: 19 }]
|
||||
);
|
||||
assert.deepStrictEqual(
|
||||
shopProductScopes({ outer_item_id: '881526926136', outer_sku_id: '006-1', outer_sku_code: '10000446302878' }, { shopId: 19 }),
|
||||
[
|
||||
{ outer_item_id: '881526926136', outer_sku_id: '006-1', shop_id: 19 },
|
||||
{ outer_item_id: '881526926136', outer_sku_code: '10000446302878', shop_id: 19 },
|
||||
]
|
||||
);
|
||||
// 带规格ID 但没给规格编码 → 只有一条,不再退化到按商品ID取一条
|
||||
assert.deepStrictEqual(
|
||||
shopProductScopes({ outer_item_id: '881526926136', outer_sku_id: '006-1' }, { shopId: 19 }),
|
||||
[{ outer_item_id: '881526926136', outer_sku_id: '006-1', shop_id: 19 }]
|
||||
);
|
||||
// 完全没有规格信息才按商品ID兜底
|
||||
assert.deepStrictEqual(
|
||||
shopProductScopes({ outer_item_id: '881526926136' }, { principalId: 5 }),
|
||||
[{ outer_item_id: '881526926136', principal_id: 5 }]
|
||||
);
|
||||
});
|
||||
});
|
||||
@ -507,6 +507,29 @@ WaybillCodeCreateDate='2026-11-04 08:20:37'
|
||||
|---|---|---|
|
||||
| `outer_item_id` | 明细 `ProductId`(商品级) | `shop_products.outer_item_id` = 商品 `PlatformId` |
|
||||
| `outer_sku_id` | 明细 `SkuId`(无合法值退 `SkuCode`) | `shop_products.outer_sku_id` = `Skus[].SkuId`(**早期用过 `SpecId`,两者在部分平台不一致**) |
|
||||
| `outer_sku_code` | 明细 `PtProductInfo.SkuCode`(退 `SkuCode`) | `shop_products.outer_sku_code` = `Skus[].SkuCode`(店管家内部规格编码) |
|
||||
|
||||
**1688(Alibaba)订单的 `SkuId` 是商品标题**(实测:`SubOrders[].SkuId = ProductSubject`,
|
||||
如「定型喷雾蓬松持久空气刘海同款…大小容量组合」),平台规格ID 这条路完全走不通。旧系统
|
||||
(`Dianguanjia.php` 的 `get_sku_bind`)**只按 `SubOrders[].SkuId` 与商品表 `SkuId` 严格等值匹配**,
|
||||
没有 `SkuCode` / `CargoNumber` / `PtProductInfo` 的兜底,所以 1688 分销单在旧系统里永远匹配不上
|
||||
(`hasnobind` → 订单 `ITEMS` 置空,同时给商品打 `hasOrder=1` 触发按 `SkuId` 反查商品列表补采,
|
||||
而用标题反查商品列表必然查不到)。
|
||||
|
||||
店管家对同一个规格给了两个键(同一份真实数据对照,商品 `881526926136`):
|
||||
|
||||
| 商品库 `Skus[]` (`/Product/LoadList`) | 1688 订单 `SubOrders[]` |
|
||||
|---|---|
|
||||
| `SkuId = 5937045124144` | `SkuId = 定型喷雾…大小容量组合`(标题) |
|
||||
| `SkuCode = 10000446302877` | `PtProductInfo.SkuCode = 10000446302877` ✔ |
|
||||
| `CargoNumber = 007+111` | `SkuCode / CargoNumber = 007+111`(商家编码,同商品内会重复,不能唯一定位规格) |
|
||||
|
||||
所以 OMS 的匹配改成两级(`lib/shopProductMatch`):
|
||||
|
||||
1. 平台规格ID(`outer_sku_id`)精确命中;
|
||||
2. 命不中时按**店管家规格编码**(`outer_sku_code`)命中;
|
||||
3. 只有「明细根本没有规格ID/编码」才按商品ID取 id 最小的一条(兜底),
|
||||
带了规格信息却都没命中时**不退化**,避免绑错规格。
|
||||
|
||||
- 明细带规格ID时**必须精确命中同规格**,不允许退化成「按商品ID随便取一条」(会绑错规格、发错货);
|
||||
规格ID对不上 → 保持未采集,等商品补齐后重匹配;
|
||||
@ -520,6 +543,13 @@ WaybillCodeCreateDate='2026-11-04 08:20:37'
|
||||
避免「先拉单、后同步商品」导致明细永远停在未采集(已审核之后的订单明细不参与,随审核锁定)。
|
||||
同步商品接口返回 `rematched`(补上的明细行数)。
|
||||
|
||||
> 实测(店铺 19「意洁」同步 1438 行店铺商品后):原先 21 条 1688 明细全部指向正确的规格,
|
||||
> 例如 `PtProductInfo.SkuCode=10000446302877` → `outer_sku_code=10000446302877`(420ml*1瓶+排骨梳)、
|
||||
> `10000446302878` → 99ML黑色、`10000446302881` → 420ML;`parse_msg` 也从
|
||||
> 「未采集到该店铺商品」变成「店铺商品未绑定系统商品」(差的是绑定关系,不再是匹配)。
|
||||
> 历史明细没有 `platform_snapshot.outer_sku_code` 时,`resolveItemSkuCode` 会回落到
|
||||
> 平台原始报文(`raw.PtProductInfo.SkuCode`),因此不用重拉订单也能修正。
|
||||
|
||||
> 早期还有一个「按平台商品ID单独补拉」的入口(`POST …/pull-products` + `listProductsByIds`),
|
||||
> 已移除:它绕开分区声明与 `SelectAgentId` 单查商品,容易把别的商家的同款商品写进本店铺,
|
||||
> 现在统一走「同步商品(全量快照)+ 自动重匹配」。
|
||||
@ -664,6 +694,8 @@ $documentID = $PrintDataList["task"]["documents"][0]["documentID"]; // = 运单
|
||||
| `service/shopAreaSync.refreshAccount(account)` | 拉一次账号商家列表,写回店铺 `external_data`(所在区),商家已解绑的店铺自动禁用 |
|
||||
| `service/shopOrderGuard.voidPendingOrders/restorePendingOrders(shop)` | 店铺禁用时作废待审核订单、启用时恢复(只处理因禁用被暂停的单) |
|
||||
| `service/externalPrintOrder.syncOrders(shop, account, { startTime, endTime })` | 拉取并写入店铺订单(手动/定时共用) |
|
||||
| `service/externalPrintOrder.matchShopProduct(shop, line)` / `service/orderItemParse.findShopProduct(item, principalId)` | 明细 → 店铺商品:平台规格ID → 店管家规格编码(`outer_sku_code`)→ 商品ID 兜底(口径见 §4.2.5) |
|
||||
| `lib/shopProductMatch.createShopProductMatcher/shopProductScopes` | 同一口径的内存索引与查询条件(拉单逐条查库、商品同步后批量重匹配共用) |
|
||||
|
||||
### 6.1.1 订单明细解析:组合拆分与「订单唯一赠品」
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user