oms/backend/app/controller/orderItem.js
liufumingren 8ef79914f9 feat(backend): 订单明细模块(order_items)与订单店铺字段
- 订单明细控制器与模型(controller/model/orderItem.js)
- 迁移:创建 order_items 表、orders 增加 shop_id
- 说明:旧 orders/order_items 模块保留启用,发货单已改用 order_deliveries
2026-09-13 02:24:01 +08:00

251 lines
8.2 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 { 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匹配店铺商品已绑定的商品;
* 命中则回填 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 this.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) } };
}
/**
* 解析单条明细,返回需要更新的字段
* 匹配顺序:商家编码 → 系统商品 → 组合商品 → 平台商品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 };
}
}
if (item.outer_item_id) {
const where = { principal_id: principalId, outer_item_id: item.outer_item_id };
if (item.outer_sku_id) where.outer_sku_id = item.outer_sku_id;
const shopProduct = await ctx.model.ShopProduct.findOne({ where });
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,
};
}
}
return { parse_status: 2, parse_msg: '未匹配到系统商品或组合商品' };
}
}
module.exports = OrderItemController;