线上出现「订单明细绑到了别家店铺的商品」:旧解析接口(`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>
198 lines
6.4 KiB
JavaScript
198 lines
6.4 KiB
JavaScript
'use strict';
|
||
|
||
const { Controller } = require('egg');
|
||
const { parsePagination, buildPageResult } = require('../lib/pagination');
|
||
|
||
/**
|
||
* @controller OrderItemController 订单明细管理
|
||
* 明细记录平台侧商品快照,商品ID(product_id)为空表示未解析出对应商品
|
||
*/
|
||
class OrderItemController extends Controller {
|
||
/**
|
||
* @summary 订单明细列表
|
||
* @description 按订单查询明细,带出解析到的系统商品/组合商品
|
||
* @router get /api/order-items
|
||
* @apikey
|
||
* @request query integer principal_id 所属主体ID(必填,需当前账号已绑定该主体)
|
||
* @request query integer order_id 订单ID(必填)
|
||
* @request query integer parse_status 解析状态:0未解析 1已解析 2解析失败,不传则不过滤
|
||
* @request query integer page 页码,默认 1
|
||
* @request query integer pageSize 每页条数,默认 50
|
||
* @response 200 orderItemLineListResponse success
|
||
*/
|
||
async index() {
|
||
const { ctx } = this;
|
||
const { page, pageSize, offset, limit } = parsePagination(ctx.query, { defaultPageSize: 50 });
|
||
const { order_id: orderId, parse_status: parseStatus } = ctx.query;
|
||
|
||
if (!orderId) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: 'order_id 为必填项' };
|
||
return;
|
||
}
|
||
|
||
const where = { order_id: orderId, principal_id: ctx.state.principalId };
|
||
if (parseStatus !== undefined && parseStatus !== '') where.parse_status = Number(parseStatus);
|
||
|
||
const { count, rows } = await ctx.model.OrderItem.findAndCountAll({
|
||
where,
|
||
offset,
|
||
limit,
|
||
order: [[ 'id', 'ASC' ]],
|
||
include: [
|
||
{ model: ctx.model.Product, as: 'product', attributes: [ 'id', 'item_code', 'item_name', 'sku_property' ] },
|
||
{ model: ctx.model.ComboProduct, as: 'comboProduct', attributes: [ 'id', 'code', 'name' ] },
|
||
],
|
||
});
|
||
|
||
ctx.body = { code: 0, message: 'success', data: buildPageResult(rows, count, { page, pageSize }) };
|
||
}
|
||
|
||
/**
|
||
* @summary 明细详情
|
||
* @description 按主键查询明细
|
||
* @router get /api/order-items/{id}
|
||
* @apikey
|
||
* @request path integer *id 明细主键ID
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async show() {
|
||
const { ctx } = this;
|
||
const item = await ctx.model.OrderItem.findByPk(ctx.params.id, {
|
||
include: [
|
||
{ model: ctx.model.Product, as: 'product', attributes: [ 'id', 'item_code', 'item_name' ] },
|
||
{ model: ctx.model.ComboProduct, as: 'comboProduct', attributes: [ 'id', 'code', 'name' ] },
|
||
],
|
||
});
|
||
|
||
if (!item) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '订单明细不存在' };
|
||
return;
|
||
}
|
||
|
||
ctx.body = { code: 0, message: 'success', data: item };
|
||
}
|
||
|
||
/**
|
||
* @summary 新增订单明细
|
||
* @description order_id 必填;传入 product_id 视为已解析(parse_status=1),否则为未解析(0)
|
||
* @router post /api/order-items
|
||
* @apikey
|
||
* @request body createOrderItemRequest *body
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async create() {
|
||
const { ctx } = this;
|
||
const { order_id: orderId, principal_id: principalId, product_id: productId, combo_product_id: comboProductId } = ctx.request.body;
|
||
|
||
if (!orderId) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: 'order_id 为必填项' };
|
||
return;
|
||
}
|
||
|
||
const order = await ctx.model.Order.findOne({ where: { id: orderId, principal_id: principalId } });
|
||
if (!order) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '订单不存在或不属于当前主体' };
|
||
return;
|
||
}
|
||
|
||
const data = {
|
||
...ctx.request.body,
|
||
// 明细继承订单的店铺,便于按店铺统计
|
||
shop_id: ctx.request.body.shop_id || order.shop_id || null,
|
||
parse_status: 0,
|
||
item_type: 0,
|
||
};
|
||
if (productId) {
|
||
data.parse_status = 1;
|
||
data.item_type = 1;
|
||
} else if (comboProductId) {
|
||
data.parse_status = 1;
|
||
data.item_type = 2;
|
||
}
|
||
|
||
const item = await ctx.model.OrderItem.create(data);
|
||
ctx.body = { code: 0, message: 'success', data: { id: item.id } };
|
||
}
|
||
|
||
/**
|
||
* @summary 更新订单明细
|
||
* @description 可修改数量、单价、快照信息与商品关联
|
||
* @router put /api/order-items/{id}
|
||
* @apikey
|
||
* @request path integer *id 明细主键ID
|
||
* @request body updateOrderItemRequest *body
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async update() {
|
||
const { ctx } = this;
|
||
const item = await ctx.model.OrderItem.findByPk(ctx.params.id);
|
||
|
||
if (!item) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '订单明细不存在' };
|
||
return;
|
||
}
|
||
|
||
delete ctx.request.body.principal_id;
|
||
await item.update(ctx.request.body);
|
||
ctx.body = { code: 0, message: 'success', data: { id: item.id } };
|
||
}
|
||
|
||
/**
|
||
* @summary 解析明细商品
|
||
* @description 按商家编码(sku_code)匹配系统商品/组合商品,再按平台商品ID + 规格ID匹配**本单店铺**已绑定的商品;
|
||
* 命中则回填 product_id/combo_product_id 并置为已解析,未命中置为解析失败(并清掉失效的店铺商品关联)
|
||
* @router post /api/order-items/{id}/parse
|
||
* @apikey
|
||
* @request path integer *id 明细主键ID
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async parse() {
|
||
const { ctx } = this;
|
||
const item = await ctx.model.OrderItem.findByPk(ctx.params.id);
|
||
|
||
if (!item) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '订单明细不存在' };
|
||
return;
|
||
}
|
||
|
||
const resolved = await ctx.service.orderItemParse.resolveItem(item, ctx.state.principalId || item.principal_id);
|
||
await item.update(resolved);
|
||
|
||
ctx.body = {
|
||
code: 0,
|
||
message: 'success',
|
||
data: { id: item.id, parse_status: resolved.parse_status, parse_msg: resolved.parse_msg || null },
|
||
};
|
||
}
|
||
|
||
/**
|
||
* @summary 删除订单明细
|
||
* @description 物理删除一条明细
|
||
* @router delete /api/order-items/{id}
|
||
* @apikey
|
||
* @request path integer *id 明细主键ID
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async destroy() {
|
||
const { ctx } = this;
|
||
const item = await ctx.model.OrderItem.findByPk(ctx.params.id);
|
||
|
||
if (!item) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '订单明细不存在' };
|
||
return;
|
||
}
|
||
|
||
await item.destroy();
|
||
ctx.body = { code: 0, message: 'success', data: { id: Number(ctx.params.id) } };
|
||
}
|
||
}
|
||
|
||
module.exports = OrderItemController;
|