线上出现「订单明细绑到了别家店铺的商品」:旧解析接口(`POST /api/order-items/{id}/parse`
与 `POST /api/orders/{id}/parse-items`)匹配店铺商品时只按 `principal_id + 平台商品ID +
规格ID` 查,同一个平台商品被多个店铺采集时(同款分销、多店铺同源)会命中别家店铺的行,
把别家的规格与绑定关系套到本单上,出库会发错货。
- 新增 lib/shopProductMatch.shopProductScope:匹配条件的唯一口径 —— 有店铺信息时只认本店铺,
只有无店铺信息的手工明细才退回主体范围;拉单(externalPrintOrder)与重解析共用它
- 解析逻辑下沉到 service/orderItemParse(`findShopProduct` / `isStaleShopProduct` /
`resolveItem` / `parseOrderItems`)。原来 order.js 里用的 `ctx.controller.orderItem`
在 Egg 里并不存在(controller 只挂到 app.controller),该接口一直是 500
- 失效关联自愈:明细指向别家店铺或已被删除的店铺商品时,重解析会连带清空
product_id/combo_product_id/item_type,不再沿用错误的系统商品;批量解析不再跳过这类脏数据
- 单测补 shopProductScope 4 例(本店铺 / 手工退回主体 / 空规格 / 缺商品ID)
实测(真实库,插入即回滚):shop 18 有、shop 19 没有时解析结果 shop_product_id=null、
product_id 同时被清空;补上 shop 19 的商品后命中 shop_product_id=1077、parse_status=1。
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
127 lines
4.9 KiB
JavaScript
127 lines
4.9 KiB
JavaScript
'use strict';
|
||
|
||
const { Service } = require('egg');
|
||
const { shopProductScope } = require('../lib/shopProductMatch');
|
||
|
||
/**
|
||
* 订单明细解析服务(商家编码 / 店铺商品 → 系统商品 / 组合商品)
|
||
*
|
||
* 与拉单时的匹配口径保持一致:平台商品ID + 平台规格ID 只认**本单店铺**的店铺商品,
|
||
* 同一平台商品被多个店铺采集时不会串到别家的规格/绑定关系上。
|
||
*
|
||
* 放在 service 里而不是 controller:订单(`POST /api/orders/{id}/parse-items`)与
|
||
* 明细(`POST /api/order-items/{id}/parse`)两条入口共用同一套逻辑。
|
||
*/
|
||
class OrderItemParseService extends Service {
|
||
/**
|
||
* 按平台商品ID/规格ID命中本单店铺的店铺商品
|
||
* 历史上这里只按 principal_id 查,同一个平台商品被多个店铺采集时会命中别家店铺的行,
|
||
* 把别家的规格/绑定关系套到本单上;现在只认本单店铺(无店铺信息的手工明细才退到主体范围)。
|
||
* @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 });
|
||
}
|
||
|
||
/**
|
||
* 明细当前关联的店铺商品是否已失效:属于别的店铺(历史跨店铺匹配留下的脏数据)或已被删除
|
||
* @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;
|