fix(shop-product): 平台规格ID 统一取 SkuId,重新拉取时就地覆盖

店铺商品落库时 outer_sku_id 早期取 SpecId、后来取 SkuId,两种口径混在一张表里;
而订单明细侧的规格ID 一直是 SkuId(resolveOrderSkuId 优先 SkuId),
两边不一致会导致订单明细匹配不到店铺商品。

- dgj.normalizeProduct:平台规格ID 统一取 SKU.SkuId,不再使用 SpecId
  (SkuId 缺失时仍退化为店管家内部 ID,保持历史行为)
- saveShopProducts:新增 planShopProductRekey —— 老行按「平台商品ID + 规格名」唯一命中时
  就地改写 outer_sku_id,而不是插入新行;否则老行会因为旧键不在快照里被判「平台已下架」
  而删除/下架,绑定关系和订单溯源都会丢。同名规格不唯一、目标键已占用时不动,交给常规 upsert
- 先改键再 upsert,created/updated 统计与下架判定都按改键后的键计算
- 同步/补拉响应新增 rekeyed(校正行数),前端同步提示展示「校正规格ID N」
- 测试:shopProductRekey.test.js(4 例:SkuId 优先 / 就地改键 / 不猜同名 / 键未变);
  真实库验证:把某行改成 `SPEC_xxx` 后重新同步 → id 不变、值被覆盖、行数不变(41 → 41)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-11-07 17:56:42 +08:00
parent 9e1c476a55
commit c1d3e17188
6 changed files with 160 additions and 8 deletions

View File

@ -1505,6 +1505,7 @@ module.exports = {
unique: { type: 'integer', description: '写入的商品行数(商品 + 规格去重后)' },
created: { type: 'integer', description: '新增商品行数' },
updated: { type: 'integer', description: '更新商品行数' },
rekeyed: { type: 'integer', description: '按 SkuId 校正平台规格ID 的行数(同商品同规格名就地改键,老行不会因此被下架)' },
removed: { type: 'integer', description: '已删除的商品行数(平台已不存在,且未绑定系统商品、未被订单明细引用)' },
offline: { type: 'integer', description: '保留并标为下架的商品行数(平台已不存在,但已绑定系统商品/组合商品或被订单明细引用)' },
verified: { type: 'boolean', description: '快照是否完整;不完整时不会移除任何商品' },
@ -1546,6 +1547,7 @@ module.exports = {
matched: { type: 'integer', description: '命中的商品行数(商品 + 规格)' },
created: { type: 'integer', description: '新增的店铺商品行数' },
updated: { type: 'integer', description: '更新的店铺商品行数' },
rekeyed: { type: 'integer', description: '按 SkuId 校正平台规格ID 的行数(同商品同规格名就地改键)' },
hosts: { type: 'array', itemType: 'string', description: '命中的域名与分区' },
missing: { type: 'array', itemType: 'externalPrintShopProductPullMissing', description: '未命中的商品及原因' },
products: { type: 'array', itemType: 'externalPrintShopProductPullItem', description: '拉取后写入的店铺商品' },

View File

@ -41,6 +41,49 @@ function shopProductKey(outerItemId, outerSkuId) {
return `${outerItemId || ''}|${outerSkuId || ''}`;
}
/**
* 规划「平台规格ID 改键」
*
* 平台规格ID 的口径变过(早期部分商品取了 `SpecId`,现在统一取 `SkuId`),
* 重新拉取时这些老行的唯一键会对不上,如果按新键插入、老键又不在快照里,
* 老行会被判成「平台已下架」而删除或下架(绑定关系、订单溯源都会丢)。
* 所以这里先按「商品ID + 规格名」认出是同一个 SKU,就地改掉老行的 `outer_sku_id`。
*
* 只在两边都唯一命中时才改键(同名规格不猜),改键目标已被占用时也不动(交给常规 upsert)。
* @param {Array<Object>} rows 本次拉取并归一化的商品行(含 outer_item_id / outer_sku_id / spec)
* @param {Array<Object>} existing 已有店铺商品行(含 id / outer_item_id / outer_sku_id / spec)
* @return {Array<{id: number, outer_sku_id: string}>} 需要改键的行
*/
function planShopProductRekey(rows, existing) {
const existingKeys = new Set(existing.map(row => shopProductKey(row.outer_item_id, row.outer_sku_id)));
const pending = rows.filter(row => !existingKeys.has(shopProductKey(row.outer_item_id, row.outer_sku_id)));
if (!pending.length) return [];
const groupBySpec = list => {
const map = new Map();
for (const item of list) {
const spec = String(item.spec == null ? '' : item.spec).trim();
if (!spec) continue;
const key = `${item.outer_item_id || ''}|${spec}`;
if (!map.has(key)) map.set(key, []);
map.get(key).push(item);
}
return map;
};
const existingBySpec = groupBySpec(existing);
const pendingBySpec = groupBySpec(pending);
const plan = [];
for (const [ key, incoming ] of pendingBySpec) {
const current = existingBySpec.get(key);
if (!current || current.length !== 1 || incoming.length !== 1) continue;
const target = current[0];
if (existingKeys.has(shopProductKey(target.outer_item_id, incoming[0].outer_sku_id))) continue;
plan.push({ id: Number(target.id), outer_sku_id: String(incoming[0].outer_sku_id || '') });
}
return plan;
}
/** 商品同步时允许更新的字段(新增行仍由 bulkCreate 写入) */
const SHOP_PRODUCT_UPSERT_FIELDS = [
'subsidiary_id',
@ -569,6 +612,7 @@ class ExternalPrintAccountController extends Controller {
unique: saved.unique,
created: saved.created,
updated: saved.updated,
rekeyed: saved.rekeyed || 0,
removed: saved.removed,
offline: saved.offline,
verified: snapshot.verified,
@ -676,6 +720,7 @@ class ExternalPrintAccountController extends Controller {
matched: result.list.length,
created: saved.created,
updated: saved.updated,
rekeyed: saved.rekeyed || 0,
hosts: result.hosts,
missing: result.missing,
products: savedRows,
@ -817,7 +862,7 @@ class ExternalPrintAccountController extends Controller {
* 快照不完整(平台分页前后数据不一致)时只更新,不做移除处理,避免误删。
* @param {Object} shop 店铺实例
* @param {Object} snapshot 平台商品快照
* @return {Promise<{unique: number, created: number, updated: number, removed: number, offline: number}>} 写入结果
* @return {Promise<{unique: number, created: number, updated: number, rekeyed: number, removed: number, offline: number}>} 写入结果
*/
async saveShopProducts(shop, snapshot) {
const { ctx } = this;
@ -825,10 +870,9 @@ class ExternalPrintAccountController extends Controller {
const now = new Date();
const existing = await ctx.model.ShopProduct.findAll({
where: { shop_id: shop.id },
attributes: [ 'id', 'outer_item_id', 'outer_sku_id', 'synced_at' ],
attributes: [ 'id', 'outer_item_id', 'outer_sku_id', 'spec', 'synced_at' ],
raw: true,
});
const existingKeys = new Set(existing.map(row => shopProductKey(row.outer_item_id, row.outer_sku_id)));
const rows = snapshot.list
.filter(item => item.outer_item_id)
@ -853,6 +897,14 @@ class ExternalPrintAccountController extends Controller {
updated_at: now,
}));
// 平台规格ID 口径变更(SpecId → SkuId):同一个 SKU 就地改键,保住绑定关系与订单溯源
const rekey = planShopProductRekey(rows, existing);
const rekeyMap = new Map(rekey.map(item => [ item.id, item.outer_sku_id ]));
const effectiveExisting = rekeyMap.size
? existing.map(row => (rekeyMap.has(Number(row.id)) ? { ...row, outer_sku_id: rekeyMap.get(Number(row.id)) } : row))
: existing;
const existingKeys = new Set(effectiveExisting.map(row => shopProductKey(row.outer_item_id, row.outer_sku_id)));
let created = 0;
let updated = 0;
for (const row of rows) {
@ -864,6 +916,17 @@ class ExternalPrintAccountController extends Controller {
let offline = 0;
const transaction = await ctx.model.transaction();
try {
// 先改键,再 upsert:否则老键那行会被当成"平台已消失"
for (const item of rekey) {
await ctx.model.ShopProduct.update(
{ outer_sku_id: item.outer_sku_id, synced_at: now, updated_at: now },
{ where: { id: item.id }, transaction }
);
}
if (rekey.length) {
ctx.logger.info('[shop-product] 店铺 %d 按 SkuId 校正平台规格ID %d 行', shop.id, rekey.length);
}
if (rows.length) {
await ctx.model.ShopProduct.bulkCreate(rows, {
transaction,
@ -900,7 +963,7 @@ class ExternalPrintAccountController extends Controller {
if (snapshot.verified) {
const syncedKeys = new Set(rows.map(row => shopProductKey(row.outer_item_id, row.outer_sku_id)));
const staleIds = existing
const staleIds = effectiveExisting
.filter(row => row.synced_at && !syncedKeys.has(shopProductKey(row.outer_item_id, row.outer_sku_id)))
.map(row => row.id);
@ -980,7 +1043,18 @@ class ExternalPrintAccountController extends Controller {
throw err;
}
return { unique: rows.length, created, updated, removed, offline };
return { unique: rows.length, created, updated, rekeyed: rekey.length, removed, offline };
}
/**
* 规划「平台规格ID 改键」:同一个 SKU 换了 outer_sku_id 口径(早期用 SpecId、统一后用 SkuId)时,
* 就地改掉老行的 `outer_sku_id`,而不是插入新行再让老行被判成「平台已下架」。
* @param {Array<Object>} rows 本次拉取并归一化的商品行
* @param {Array<Object>} existing 已有店铺商品行
* @return {Array<{id: number, outer_sku_id: string}>} 需要改键的行
*/
static planShopProductRekey(rows, existing) {
return planShopProductRekey(rows, existing);
}
/**

View File

@ -1765,8 +1765,9 @@ class DgjService extends PrintPlatformService {
/**
* 把店管家商品(含 SKU 列表)转换为店铺商品行的统一结构。
* 平台商品ID取商品级 PlatformId;平台规格ID取 SKU 的 SpecId/SkuId
* (注意 SKU.PlatformId 是商品级平台ID,不能当作规格ID),缺失时退化为店管家内部ID。
* 平台商品ID取商品级 PlatformId;平台规格ID统一取 SKU 的 `SkuId`
* (早期用过 `SpecId`,两者在部分平台/商品上不一致,统一后不再使用 SpecId;
* 另注意 SKU.PlatformId 是商品级平台ID,不能当规格ID),缺失时退化为店管家内部ID。
* 原样保留商品与 SKU 结构供后续使用。
* @param {Object} product 店管家商品原始结构
* @return {Array<Object>} 店铺商品行(一个 SKU 一行)
@ -1787,7 +1788,7 @@ class DgjService extends PrintPlatformService {
const attrs = [ item.AttributeValue1, item.AttributeValue2 ].filter(Boolean).join(' ');
return {
outer_item_id: String(data.PlatformId || ptInfo.PrductId || data.Id || ''),
outer_sku_id: String(item.SpecId || item.SkuId || item.Id || ''),
outer_sku_id: String(item.SkuId || item.Id || ''),
title: truncate(data.Subject, 255),
outer_code: String(item.CargoNumber || item.SkuSelfCode || data.CargoNumber || data.ProductSelfCode || ''),
spec: truncate(item.Name || attrs, 255),

View File

@ -0,0 +1,72 @@
'use strict';
const { assert } = require('egg-mock/bootstrap');
const Controller = require('../../../app/controller/externalPrintAccount');
const DgjService = require('../../../app/lib/printPlatform/dgj');
describe('店铺商品:平台规格ID 统一取 SkuId', () => {
it('商品归一化用 SKU.SkuId,不再用 SpecId', () => {
const service = new DgjService({ platform: { code: 'dgj', name: '店管家' } });
const rows = service.normalizeProduct({
PlatformId: '3835275595751752044',
ShopId: 11638641,
Subject: '高露洁牙膏',
PlatformType: 'DouYin',
Status: 'OnSale',
Skus: [
{ Id: '40000102661342', SkuId: '3703510240083970', SpecId: '9999999999', Name: '2 支装' },
{ Id: '40000102661343', SkuId: '', SpecId: '8888888888', Name: '1 支装' },
],
});
console.log('归一化结果:', rows.map(row => `${row.spec}->${row.outer_sku_id}`).join(' | '));
assert.strictEqual(rows[0].outer_sku_id, '3703510240083970', '优先取 SkuId,不能取 SpecId');
assert.strictEqual(rows[1].outer_sku_id, '40000102661343', '没有 SkuId 时退化为店管家内部 ID(历史行为)');
});
it('重新拉取时同一 SKU 就地改键(同商品 + 同规格名唯一命中)', () => {
const incoming = [
{ outer_item_id: 'P1', outer_sku_id: '222', spec: '2 支装' },
{ outer_item_id: 'P1', outer_sku_id: '333', spec: '1 支装' },
];
const existing = [
{ id: 11, outer_item_id: 'P1', outer_sku_id: '111', spec: '2 支装' },
{ id: 12, outer_item_id: 'P1', outer_sku_id: '333', spec: '1 支装' },
];
const plan = Controller.planShopProductRekey(incoming, existing);
console.log('改键计划:', JSON.stringify(plan));
assert.deepStrictEqual(plan, [{ id: 11, outer_sku_id: '222' }]);
});
it('同名规格不唯一 / 目标键已存在 / 无规格名时都不改键', () => {
// 同名规格:两个老行同名,不猜
assert.deepStrictEqual(Controller.planShopProductRekey(
[{ outer_item_id: 'P1', outer_sku_id: '222', spec: '同款' }],
[
{ id: 11, outer_item_id: 'P1', outer_sku_id: '111', spec: '同款' },
{ id: 12, outer_item_id: 'P1', outer_sku_id: '333', spec: '同款' },
]
), []);
// 目标键已被别的行占用:交给常规 upsert
assert.deepStrictEqual(Controller.planShopProductRekey(
[{ outer_item_id: 'P1', outer_sku_id: '222', spec: '2 支装' }],
[
{ id: 11, outer_item_id: 'P1', outer_sku_id: '111', spec: '2 支装' },
{ id: 12, outer_item_id: 'P1', outer_sku_id: '222', spec: '其它规格' },
]
), []);
// 没有规格名:无法确认是同一个 SKU
assert.deepStrictEqual(Controller.planShopProductRekey(
[{ outer_item_id: 'P1', outer_sku_id: '222', spec: null }],
[{ id: 11, outer_item_id: 'P1', outer_sku_id: '111', spec: null }]
), []);
});
it('键没变时不需要改键', () => {
assert.deepStrictEqual(Controller.planShopProductRekey(
[{ outer_item_id: 'P1', outer_sku_id: '222', spec: '2 支装' }],
[{ id: 11, outer_item_id: 'P1', outer_sku_id: '222', spec: '2 支装' }]
), []);
});
});

View File

@ -132,6 +132,8 @@ export interface ExternalPrintShopProductSyncResult {
unique: number
created: number
updated: number
/** 平台规格ID 统一取 SkuId 后就地改键的行数(同商品同规格名唯一命中才改) */
rekeyed?: number
/** 平台已不存在、且未绑定未被订单引用 → 已删除的行数 */
removed: number
/** 平台已不存在、但已绑定或被订单引用 → 保留并标为下架的行数 */

View File

@ -848,6 +848,7 @@ async function handleSyncProducts(row: ShopItem) {
try {
const res = await syncExternalPrintShopProducts(accountId, row.id)
const parts = [ `新增 ${res.created}`, `更新 ${res.updated}` ]
if (res.rekeyed) parts.push(`校正规格ID ${res.rekeyed}`)
if (res.removed) parts.push(`移除 ${res.removed}`)
if (res.offline) parts.push(`下架 ${res.offline}(已绑定或被订单引用,已保留)`)
const suffix = res.verified ? '' : '(平台数据在拉取过程中有变动,本次未处理下架,建议重试)'