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>
136 lines
5.3 KiB
JavaScript
136 lines
5.3 KiB
JavaScript
'use strict';
|
||
|
||
const { Service } = require('egg');
|
||
const { shopProductScopes } = require('../lib/shopProductMatch');
|
||
const { resolveItemSkuCode } = require('../lib/orderItemExpand');
|
||
|
||
/**
|
||
* 订单明细解析服务(商家编码 / 店铺商品 → 系统商品 / 组合商品)
|
||
*
|
||
* 与拉单时的匹配口径保持一致:平台商品ID + 平台规格ID 只认**本单店铺**的店铺商品,
|
||
* 同一平台商品被多个店铺采集时不会串到别家的规格/绑定关系上。
|
||
*
|
||
* 放在 service 里而不是 controller:订单(`POST /api/orders/{id}/parse-items`)与
|
||
* 明细(`POST /api/order-items/{id}/parse`)两条入口共用同一套逻辑。
|
||
*/
|
||
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 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;
|
||
}
|
||
|
||
/**
|
||
* 明细当前关联的店铺商品是否已失效:属于别的店铺(历史跨店铺匹配留下的脏数据)或已被删除
|
||
* @param {Object} item 明细
|
||
* @return {Promise<boolean>} 是否失效
|
||
*/
|
||
async isStaleShopProduct(item) {
|
||
const { ctx } = this;
|
||
if (item.shop_product_id == null) return false;
|
||
const row = await ctx.model.ShopProduct.findByPk(item.shop_product_id, {
|
||
attributes: [ 'id', 'shop_id' ],
|
||
raw: true,
|
||
});
|
||
if (!row) return true;
|
||
return item.shop_id != null && Number(row.shop_id) !== Number(item.shop_id);
|
||
}
|
||
|
||
/**
|
||
* 解析单条明细,返回需要更新的字段
|
||
* 匹配顺序:商家编码 → 系统商品 → 组合商品 → 平台商品ID + 规格ID → 本店铺店铺商品已绑定的商品
|
||
* @param {Object} item 明细
|
||
* @param {Number} principalId 主体ID
|
||
* @return {Promise<Object>} 待更新字段
|
||
*/
|
||
async resolveItem(item, principalId) {
|
||
const { ctx } = this;
|
||
const code = item.sku_code;
|
||
|
||
if (code) {
|
||
const product = await ctx.model.Product.findOne({ where: { principal_id: principalId, item_code: code } });
|
||
if (product) {
|
||
return { item_type: 1, product_id: product.id, combo_product_id: null, parse_status: 1, parse_msg: null };
|
||
}
|
||
|
||
const combo = await ctx.model.ComboProduct.findOne({ where: { principal_id: principalId, code } });
|
||
if (combo) {
|
||
return { item_type: 2, product_id: null, combo_product_id: combo.id, parse_status: 1, parse_msg: null };
|
||
}
|
||
}
|
||
|
||
const shopProduct = await this.findShopProduct(item, principalId);
|
||
if (shopProduct && Number(shopProduct.bind_type) === 1 && shopProduct.product_id) {
|
||
return {
|
||
item_type: 1,
|
||
product_id: shopProduct.product_id,
|
||
combo_product_id: null,
|
||
shop_product_id: shopProduct.id,
|
||
parse_status: 1,
|
||
parse_msg: null,
|
||
};
|
||
}
|
||
if (shopProduct && Number(shopProduct.bind_type) === 2 && shopProduct.combo_product_id) {
|
||
return {
|
||
item_type: 2,
|
||
product_id: null,
|
||
combo_product_id: shopProduct.combo_product_id,
|
||
shop_product_id: shopProduct.id,
|
||
parse_status: 1,
|
||
parse_msg: null,
|
||
};
|
||
}
|
||
|
||
// 没命中:清掉失效的店铺商品关联;若原本就是靠别的店铺的商品解析出来的,
|
||
// 连同商品关联一起清空,避免继续沿用错误的系统商品
|
||
const reset = { shop_product_id: null };
|
||
if (await this.isStaleShopProduct(item)) {
|
||
Object.assign(reset, { product_id: null, combo_product_id: null, item_type: 0 });
|
||
}
|
||
return { ...reset, parse_status: 2, parse_msg: '未匹配到系统商品或组合商品' };
|
||
}
|
||
|
||
/**
|
||
* 批量解析某订单下未解析 / 解析失败的明细
|
||
* 已解析的跳过,但关联到别家店铺或已删除店铺商品的脏数据会重新解析(历史跨店铺匹配遗留)
|
||
* @param {Object} order 订单实例
|
||
* @return {Promise<{total: number, resolved: number, failed: number}>} 解析统计
|
||
*/
|
||
async parseOrderItems(order) {
|
||
const { ctx } = this;
|
||
const items = await ctx.model.OrderItem.findAll({ where: { order_id: order.id } });
|
||
let resolved = 0;
|
||
let failed = 0;
|
||
|
||
for (const item of items) {
|
||
const stale = await this.isStaleShopProduct(item);
|
||
if (Number(item.parse_status) === 1 && !stale) continue;
|
||
const data = await this.resolveItem(item, order.principal_id);
|
||
await item.update(data);
|
||
if (data.parse_status === 1) resolved++;
|
||
else failed++;
|
||
}
|
||
|
||
return { total: items.length, resolved, failed };
|
||
}
|
||
}
|
||
|
||
module.exports = OrderItemParseService;
|