feat(backend): 订单明细模块(order_items)与订单店铺字段
- 订单明细控制器与模型(controller/model/orderItem.js) - 迁移:创建 order_items 表、orders 增加 shop_id - 说明:旧 orders/order_items 模块保留启用,发货单已改用 order_deliveries
This commit is contained in:
parent
a51cb4df15
commit
8ef79914f9
250
backend/app/controller/orderItem.js
Normal file
250
backend/app/controller/orderItem.js
Normal file
@ -0,0 +1,250 @@
|
||||
'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;
|
||||
48
backend/app/model/orderItem.js
Normal file
48
backend/app/model/orderItem.js
Normal file
@ -0,0 +1,48 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 订单明细模型,对应 order_items 表(订单的副表)
|
||||
* 记录平台侧商品快照,并单向关联系统商品/组合商品:
|
||||
* product_id 为空表示尚未解析出商品(parse_status:0未解析 1已解析 2解析失败)
|
||||
* @param {Egg.Application} app egg application
|
||||
* @return {object} orderItem model
|
||||
*/
|
||||
module.exports = app => {
|
||||
const { BIGINT, STRING, TINYINT, DECIMAL, JSON, DATE } = app.Sequelize;
|
||||
|
||||
const OrderItem = app.model.define('order_item', {
|
||||
id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' },
|
||||
principal_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' },
|
||||
order_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属订单ID' },
|
||||
shop_id: { type: BIGINT.UNSIGNED, comment: '所属店铺ID(冗余订单的店铺)' },
|
||||
product_id: { type: BIGINT.UNSIGNED, comment: '系统商品ID,为空表示未解析出商品' },
|
||||
combo_product_id: { type: BIGINT.UNSIGNED, comment: '组合商品ID,为空表示非组合商品' },
|
||||
shop_product_id: { type: BIGINT.UNSIGNED, comment: '命中的店铺商品ID' },
|
||||
item_type: { type: TINYINT, allowNull: false, defaultValue: 0, comment: '明细类型:0未识别 1普通商品 2组合商品' },
|
||||
parse_status: { type: TINYINT, allowNull: false, defaultValue: 0, comment: '解析状态:0未解析 1已解析 2解析失败' },
|
||||
parse_msg: { type: STRING(255), comment: '解析失败原因' },
|
||||
outer_item_id: { type: STRING(100), comment: '平台商品ID' },
|
||||
outer_sku_id: { type: STRING(100), comment: '平台规格ID' },
|
||||
sku_code: { type: STRING(100), comment: '商家编码' },
|
||||
name: { type: STRING(255), comment: '商品名称(快照)' },
|
||||
spec: { type: STRING(255), comment: '规格(快照)' },
|
||||
image: { type: STRING(255), comment: '图片(快照)' },
|
||||
quantity: { type: DECIMAL(12, 3), allowNull: false, defaultValue: 0, comment: '数量' },
|
||||
price: { type: DECIMAL(12, 2), comment: '单价' },
|
||||
total_amount: { type: DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '明细金额' },
|
||||
raw: { type: JSON, comment: '平台返回的明细原始数据' },
|
||||
created_at: { type: DATE, comment: '创建时间' },
|
||||
updated_at: { type: DATE, comment: '更新时间' },
|
||||
});
|
||||
|
||||
OrderItem.associate = () => {
|
||||
app.model.OrderItem.belongsTo(app.model.Principal, { foreignKey: 'principal_id', as: 'principal' });
|
||||
app.model.OrderItem.belongsTo(app.model.Order, { foreignKey: 'order_id', as: 'order' });
|
||||
app.model.OrderItem.belongsTo(app.model.Shop, { foreignKey: 'shop_id', as: 'shop' });
|
||||
app.model.OrderItem.belongsTo(app.model.Product, { foreignKey: 'product_id', as: 'product' });
|
||||
app.model.OrderItem.belongsTo(app.model.ComboProduct, { foreignKey: 'combo_product_id', as: 'comboProduct' });
|
||||
app.model.OrderItem.belongsTo(app.model.ShopProduct, { foreignKey: 'shop_product_id', as: 'shopProduct' });
|
||||
};
|
||||
|
||||
return OrderItem;
|
||||
};
|
||||
100
backend/database/migrations/20260909001902-create-order-items.js
Normal file
100
backend/database/migrations/20260909001902-create-order-items.js
Normal file
@ -0,0 +1,100 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 11. 订单明细表:订单的副表,一个订单对应多条明细;
|
||||
* 明细记录了平台侧的原始商品信息,并单向关联系统商品/组合商品:
|
||||
* product_id 为空表示尚未解析出对应商品(parse_status=0 未解析 或 2 解析失败)。
|
||||
*/
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
await queryInterface.createTable(
|
||||
'order_items',
|
||||
{
|
||||
id: {
|
||||
type: Sequelize.BIGINT.UNSIGNED,
|
||||
allowNull: false,
|
||||
autoIncrement: true,
|
||||
primaryKey: true,
|
||||
},
|
||||
principal_id: {
|
||||
type: Sequelize.BIGINT.UNSIGNED,
|
||||
allowNull: false,
|
||||
comment: '所属主体ID',
|
||||
references: { model: 'principals', key: 'id' },
|
||||
},
|
||||
order_id: {
|
||||
type: Sequelize.BIGINT.UNSIGNED,
|
||||
allowNull: false,
|
||||
comment: '所属订单ID',
|
||||
references: { model: 'orders', key: 'id' },
|
||||
onDelete: 'CASCADE',
|
||||
},
|
||||
shop_id: {
|
||||
type: Sequelize.BIGINT.UNSIGNED,
|
||||
allowNull: true,
|
||||
comment: '所属店铺ID(冗余订单的店铺,便于按店统计)',
|
||||
references: { model: 'shops', key: 'id' },
|
||||
onDelete: 'SET NULL',
|
||||
},
|
||||
// 与系统商品的关联:均允许为空,表示未解析出商品
|
||||
product_id: {
|
||||
type: Sequelize.BIGINT.UNSIGNED,
|
||||
allowNull: true,
|
||||
comment: '系统商品ID,为空表示未解析出商品',
|
||||
references: { model: 'products', key: 'id' },
|
||||
onDelete: 'SET NULL',
|
||||
},
|
||||
combo_product_id: {
|
||||
type: Sequelize.BIGINT.UNSIGNED,
|
||||
allowNull: true,
|
||||
comment: '组合商品ID,为空表示非组合商品',
|
||||
references: { model: 'combo_products', key: 'id' },
|
||||
onDelete: 'SET NULL',
|
||||
},
|
||||
shop_product_id: {
|
||||
type: Sequelize.BIGINT.UNSIGNED,
|
||||
allowNull: true,
|
||||
comment: '命中的店铺商品ID',
|
||||
references: { model: 'shop_products', key: 'id' },
|
||||
onDelete: 'SET NULL',
|
||||
},
|
||||
item_type: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 0, comment: '明细类型:0未识别 1普通商品 2组合商品' },
|
||||
parse_status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 0, comment: '解析状态:0未解析 1已解析 2解析失败' },
|
||||
parse_msg: { type: Sequelize.STRING(255), allowNull: true, comment: '解析失败原因' },
|
||||
// 平台侧原始商品信息(快照)
|
||||
outer_item_id: { type: Sequelize.STRING(100), allowNull: true, comment: '平台商品ID' },
|
||||
outer_sku_id: { type: Sequelize.STRING(100), allowNull: true, comment: '平台规格ID' },
|
||||
sku_code: { type: Sequelize.STRING(100), allowNull: true, comment: '商家编码' },
|
||||
name: { type: Sequelize.STRING(255), allowNull: true, comment: '商品名称(快照)' },
|
||||
spec: { type: Sequelize.STRING(255), allowNull: true, comment: '规格(快照)' },
|
||||
image: { type: Sequelize.STRING(255), allowNull: true, comment: '图片(快照)' },
|
||||
// 数量与金额
|
||||
quantity: { type: Sequelize.DECIMAL(12, 3), allowNull: false, defaultValue: 0, comment: '数量' },
|
||||
price: { type: Sequelize.DECIMAL(12, 2), allowNull: true, comment: '单价' },
|
||||
total_amount: { type: Sequelize.DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '明细金额' },
|
||||
raw: { type: Sequelize.JSON, allowNull: true, comment: '平台返回的明细原始数据' },
|
||||
created_at: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
|
||||
},
|
||||
updated_at: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
|
||||
},
|
||||
},
|
||||
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '订单明细表' }
|
||||
);
|
||||
|
||||
await queryInterface.addIndex('order_items', { fields: [ 'order_id' ], name: 'idx_order_items_order' });
|
||||
await queryInterface.addIndex('order_items', { fields: [ 'principal_id' ], name: 'idx_order_items_principal' });
|
||||
await queryInterface.addIndex('order_items', { fields: [ 'shop_id' ], name: 'idx_order_items_shop' });
|
||||
await queryInterface.addIndex('order_items', { fields: [ 'product_id' ], name: 'idx_order_items_product' });
|
||||
await queryInterface.addIndex('order_items', { fields: [ 'principal_id', 'parse_status' ], name: 'idx_order_items_parse' });
|
||||
},
|
||||
|
||||
async down(queryInterface) {
|
||||
await queryInterface.dropTable('order_items');
|
||||
},
|
||||
};
|
||||
@ -0,0 +1,23 @@
|
||||
'use strict';
|
||||
|
||||
/** 12. 订单表增加店铺ID:订单归属到具体店铺(可为空,兼容历史订单) */
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
await queryInterface.addColumn('orders', 'shop_id', {
|
||||
type: Sequelize.BIGINT.UNSIGNED,
|
||||
allowNull: true,
|
||||
comment: '所属店铺ID,为空表示未关联店铺',
|
||||
references: { model: 'shops', key: 'id' },
|
||||
onDelete: 'SET NULL',
|
||||
});
|
||||
|
||||
await queryInterface.addIndex('orders', { fields: [ 'shop_id' ], name: 'idx_orders_shop' });
|
||||
await queryInterface.addIndex('orders', { fields: [ 'principal_id', 'shop_id' ], name: 'idx_orders_principal_shop' });
|
||||
},
|
||||
|
||||
async down(queryInterface) {
|
||||
await queryInterface.removeIndex('orders', 'idx_orders_principal_shop');
|
||||
await queryInterface.removeIndex('orders', 'idx_orders_shop');
|
||||
await queryInterface.removeColumn('orders', 'shop_id');
|
||||
},
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user