fix(shop-product): 识别规格没拉全的商品,避免误判下架

平台 `SkuCount` 与实际下发的 `Skus` 会不一致:线上商品 3773823319627923739
`SkuCount=6`,`/Product/LoadList` 只回 3 个规格(分销/多来源商品常见),
库里有 4 行(含一个平台已不再返回的规格)。此前整份快照 verified 后,
那些「平台没返回」的规格会被当成平台已下架,直接删除或标下架,绑定关系跟着丢。

- dgj.listProducts:逐个商品比较 `SkuCount` 与实际下发的规格数,
  不一致就记入 `incompleteProducts: [{outer_item_id, expected, got}]` 并 warn 留痕
- controller.saveShopProducts:`incompleteProducts` 里的商品不参与「平台已下架」判定
  (既不删除也不标下架),其余商品行为不变
- 同步商品接口返回 incomplete / incomplete_products;contract 补
  externalPrintShopProductIncompleteItem
- 单测 dgjProductSkuCoverage.test.js 2 例(识别不全 / 一致时不标记)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-11-07 23:14:48 +08:00
parent 5d3025cb2f
commit b63204d9fe
4 changed files with 110 additions and 3 deletions

View File

@ -1500,13 +1500,19 @@ module.exports = {
verified: { type: 'boolean', description: '该域名快照是否完整(各分区去重数 = Total)' },
areas: { type: 'array', itemType: 'externalPrintShopProductSyncArea', description: '各分区拉取明细' },
},
externalPrintShopProductIncompleteItem: {
outer_item_id: { type: 'string', description: '平台商品ID' },
expected: { type: 'integer', description: '平台声明的规格数(SkuCount)' },
got: { type: 'integer', description: '平台实际下发的规格数' },
},
externalPrintShopProductSyncResult: {
total: { type: 'integer', description: '各域名商品总数之和' },
unique: { type: 'integer', description: '写入的商品行数(商品 + 规格去重后)' },
total: { type: 'integer', description: '各域名商品总数之和' }, unique: { type: 'integer', description: '写入的商品行数(商品 + 规格去重后)' },
created: { type: 'integer', description: '新增商品行数' },
updated: { type: 'integer', description: '更新商品行数' },
rekeyed: { type: 'integer', description: '按 SkuId 校正平台规格ID 的行数(同商品同规格名就地改键,老行不会因此被下架)' },
rematched: { type: 'integer', description: '本次写入后补匹配上的订单明细行数(原本未采集到店铺商品的待审核明细)' },
incomplete: { type: 'integer', description: '规格没拉全的商品数(平台 SkuCount > 实际下发规格;这些商品本次不做下架处理)' },
incomplete_products: { type: 'array', itemType: 'externalPrintShopProductIncompleteItem', description: '规格没拉全的商品明细' },
removed: { type: 'integer', description: '已删除的商品行数(平台已不存在,且未绑定系统商品、未被订单明细引用)' },
offline: { type: 'integer', description: '保留并标为下架的商品行数(平台已不存在,但已绑定系统商品/组合商品或被订单明细引用)' },
verified: { type: 'boolean', description: '快照是否完整;不完整时不会移除任何商品' },

View File

@ -613,6 +613,9 @@ class ExternalPrintAccountController extends Controller {
updated: saved.updated,
rekeyed: saved.rekeyed || 0,
rematched: saved.rematched || 0,
// 平台 SkuCount > 实际下发规格的商品(规格没拉全,本次未做下架处理)
incomplete: (snapshot.incompleteProducts || []).length,
incomplete_products: snapshot.incompleteProducts || [],
removed: saved.removed,
offline: saved.offline,
verified: snapshot.verified,
@ -840,9 +843,17 @@ class ExternalPrintAccountController extends Controller {
}
if (snapshot.verified) {
// 规格没拉全的商品(平台 SkuCount > 实际下发)不参与「平台已下架」判定:
// 否则会把平台没返回的那些规格误删/误标下架,绑定关系也跟着丢
const incompleteItems = new Set(
(Array.isArray(snapshot.incompleteProducts) ? snapshot.incompleteProducts : [])
.map(item => String(item && item.outer_item_id))
.filter(Boolean)
);
const syncedKeys = new Set(rows.map(row => shopProductKey(row.outer_item_id, row.outer_sku_id)));
const staleIds = effectiveExisting
.filter(row => row.synced_at && !syncedKeys.has(shopProductKey(row.outer_item_id, row.outer_sku_id)))
.filter(row => !incompleteItems.has(String(row.outer_item_id)))
.map(row => row.id);
if (staleIds.length) {

View File

@ -1755,19 +1755,38 @@ class DgjService extends PrintPlatformService {
}
const merged = new Map();
// 平台 `SkuCount` 与实际下发的 `Skus` 可能不一致(分销/多来源商品的规格列表未必全给),
// 这种商品的规格快照不完整,绝不能据此把库里的老规格判成「平台已下架」
const incomplete = new Map();
for (const snapshot of snapshots) {
for (const item of snapshot.rows) {
for (const row of this.normalizeProduct(item)) {
const rows = this.normalizeProduct(item);
const expected = Number((item && (item.SkuCount != null ? item.SkuCount : item.SkuNum)) || 0);
const outerItemId = String((item && item.PlatformId) || '');
if (outerItemId && expected > rows.length) {
incomplete.set(outerItemId, { outer_item_id: outerItemId, expected, got: rows.length });
}
for (const row of rows) {
merged.set(`${row.platform_type}|${row.outer_item_id}|${row.outer_sku_id}`, row);
}
}
}
if (incomplete.size && this.logger && typeof this.logger.warn === 'function') {
this.logger.warn(
'[dgj] 有 %d 个商品的规格没拉全(平台 SkuCount > 实际下发),本次不会把这些商品的规格判为下架:%s',
incomplete.size,
Array.from(incomplete.values()).map(item => `${item.outer_item_id}(${item.got}/${item.expected})`).join('、')
);
}
return {
total: snapshots.reduce((sum, snapshot) => sum + snapshot.total, 0),
verified: snapshots.every(snapshot => snapshot.verified),
// 声明分区没有商品时补查的域名(非空说明「店铺所在区」声明可能过期)
supplementHosts,
// 规格未拉全的商品(外部商品ID):写库时跳过下架处理
incompleteProducts: Array.from(incomplete.values()),
hosts: snapshots.map(snapshot => ({
host: snapshot.host,
total: snapshot.total,

View File

@ -0,0 +1,71 @@
'use strict';
const { assert } = require('egg-mock/bootstrap');
const DgjService = require('../../../app/lib/printPlatform/dgj');
describe('dgj.listProducts 规格不全的识别', () => {
it('平台 SkuCount 大于实际下发的 Skus 时标记为 incomplete,且不影响其它商品', async () => {
const service = new DgjService({ platform: { code: 'dgj', name: '店管家' }, account: { token: 't', cookie: 'c' } });
service.logger = { info() {}, warn() {}, error() {} };
service.getUserInfo = async () => ({ data: { TraceId: 'tr' } });
// 声明域名 → 一个分区快照,含两个商品:一个规格不全(6 报 3),一个正常
service.resolveProductHosts = () => [ 'fxdd.dgjapp.com' ];
service.fetchProductSnapshot = async () => ({
host: 'fxdd.dgjapp.com',
total: 2,
uniqueCount: 2,
pages: 1,
verified: true,
areas: [],
rows: [
{
PlatformId: '3773823319627923739',
SkuCount: 6,
Subject: '规格不全的商品',
Skus: [
{ Id: 1, SkuId: '3683584205645314', Name: 'A' },
{ Id: 2, SkuId: '3683584205645570', Name: 'B' },
{ Id: 3, SkuId: '3548726733104898', Name: 'C' },
],
},
{
PlatformId: '999',
SkuCount: 2,
Subject: '正常商品',
Skus: [
{ Id: 4, SkuId: '1001', Name: 'D' },
{ Id: 5, SkuId: '1002', Name: 'E' },
],
},
],
});
const snapshot = await service.listProducts({ external_data: { FxUserId: '953516', DbAreas: [] } });
console.log('list:', snapshot.list.length, 'incomplete:', JSON.stringify(snapshot.incompleteProducts));
assert.strictEqual(snapshot.list.length, 5, '两件商品共 5 个规格都写进 list');
assert.strictEqual(snapshot.incompleteProducts.length, 1);
assert.deepStrictEqual(snapshot.incompleteProducts[0], { outer_item_id: '3773823319627923739', expected: 6, got: 3 });
});
it('SkuCount 与实际一致时不标记', async () => {
const service = new DgjService({ platform: { code: 'dgj', name: '店管家' }, account: { token: 't', cookie: 'c' } });
service.logger = { info() {}, warn() {}, error() {} };
service.getUserInfo = async () => ({ data: {} });
service.resolveProductHosts = () => [ 'fxdd.dgjapp.com' ];
service.fetchProductSnapshot = async () => ({
host: 'fxdd.dgjapp.com',
total: 1,
uniqueCount: 1,
pages: 1,
verified: true,
areas: [],
rows: [{
PlatformId: '1',
SkuCount: 2,
Skus: [{ Id: 1, SkuId: 'a' }, { Id: 2, SkuId: 'b' }],
}],
});
const snapshot = await service.listProducts({ external_data: { FxUserId: '953516', DbAreas: [] } });
assert.deepStrictEqual(snapshot.incompleteProducts, []);
});
});