oms/backend/test/app/lib/shopProductRekey.test.js
liufumingren c1d3e17188 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>
2026-11-07 17:56:42 +08:00

73 lines
3.1 KiB
JavaScript
Raw 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';
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 支装' }]
), []);
});
});