oms/backend/app/model/order.js
liufumingren ab630e7c01 feat(external-print): 订单拉取与定时同步,组合商品拆分为最小单位
后端能力:
- 接入店管家订单接口(/NewOrder/List),明细归一化后按店铺商品匹配系统商品/组合商品
- 新增 service/externalPrintOrder:手动与定时拉单共用入库逻辑,同时写入待审核状态分表
- 新增 schedule/fetchExternalShopOrders:按店铺配置的间隔自动拉单(最小 3 分钟,起点重叠 10 分钟)
- 新增 lib/orderItemExpand + service/orderItemResolve:平台行与最小单位行互转;
  绑定组合商品时按「数量 = 平台数量 x 组合内数量、单价取子商品零售价」拆成子商品行,解绑可还原
- 明细新增 platform_snapshot,保留平台原样(编码/名称/规格/图片/原行号/原数量/原单价)
- 店铺商品列表支持 has_unbound_order 筛选,订单列表支持 exclude_unbound(均已同步导出定义)
- 店铺商品只读化:移除新增/编辑/删除动作,仅保留查询与绑定;
  同步商品时平台已不存在的商品自动清理(未绑定且未被订单引用直接删除,已绑定或被引用仅下架)
- 新增 scripts/rebuild-order-items 存量明细重建脚本,新增 orderItemExpand 单测(8 项)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-03 03:29:09 +08:00

52 lines
3.1 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';
/**
* 订单模型,对应 orders 表
* 含本系统主状态 status、外部 WMS 状态 wms_status、上交状态 submit_status
* @param {Egg.Application} app egg application
* @return {object} order model
*/
module.exports = app => {
const { BIGINT, STRING, TINYINT, DECIMAL, JSON, DATE } = app.Sequelize;
const Order = app.model.define('order', {
id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' },
order_no: { type: STRING(50), allowNull: false, comment: '业务订单号' },
principal_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' },
shop_id: { type: BIGINT.UNSIGNED, comment: '所属店铺ID,为空表示未关联店铺' },
external_print_account_id: { type: BIGINT.UNSIGNED, comment: '来源外部打单账号ID' },
external_order_id: { type: STRING(64), comment: '平台订单号(店管家 PlatformOrderId)' },
platform_type: { type: STRING(32), comment: '平台类型:TouTiao/Taobao/Pinduoduo 等' },
platform_status: { type: STRING(32), comment: '平台侧订单状态' },
external_shop_id: { type: STRING(64), comment: '平台侧店铺ID(店管家 ShopId)' },
external_shop_name: { type: STRING(128), comment: '平台侧店铺名称' },
external_created_at: { type: DATE, comment: '平台侧订单创建时间' },
external_updated_at: { type: DATE, comment: '平台侧订单更新时间' },
synced_at: { type: DATE, comment: '最近一次从平台同步的时间' },
external_data: { type: JSON, comment: '平台返回的订单原始结构' },
order_type: { type: TINYINT, allowNull: false, defaultValue: 1, comment: '订单类型' },
status: { type: TINYINT, allowNull: false, defaultValue: 1, comment: '主状态:1待处理 2处理中 3已完成 4已取消' },
wms_status: { type: STRING(50), comment: '外部WMS状态' },
wms_order_no: { type: STRING(50), comment: '外部WMS单号' },
wms_synced_at: { type: DATE, comment: '最近一次WMS同步时间' },
submit_status: { type: TINYINT, allowNull: false, defaultValue: 0, comment: '上交状态:0未上交 1上交中 2已上交 3上交失败' },
submit_order_no: { type: STRING(50), comment: '上交后返回的单号' },
submit_at: { type: DATE, comment: '上交时间' },
submit_msg: { type: STRING(255), comment: '上交结果/失败原因' },
total_amount: { type: DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '订单金额' },
pay_amount: { type: DECIMAL(12, 2), comment: '实付金额' },
receiver: { type: JSON, comment: '收货人信息 JSON:{ name, phone, address }' },
remark: { type: STRING(255), comment: '备注' },
created_at: { type: DATE, comment: '创建时间' },
updated_at: { type: DATE, comment: '更新时间' },
});
Order.associate = () => {
app.model.Order.belongsTo(app.model.Principal, { foreignKey: 'principal_id', as: 'principal' });
app.model.Order.belongsTo(app.model.Shop, { foreignKey: 'shop_id', as: 'shop' });
app.model.Order.hasMany(app.model.OrderItem, { foreignKey: 'order_id', as: 'items' });
};
return Order;
};