oms/backend/database/migrations/20261102000900-add-external-fields-to-orders.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

100 lines
4.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';
/**
* 订单与订单明细增加外部打单平台(如店管家)来源字段。
*
* orders:
* - external_print_account_id / external_order_id:来源打单账号与平台订单号(PlatformOrderId),用于去重与回查
* - platform_type / platform_status:平台类型(TouTiao/Taobao…)与平台侧状态(ErpState)
* - external_shop_id / external_shop_name:平台侧店铺(一个商家可有多个平台店铺)
* - external_created_at / external_updated_at / synced_at:平台上创建、更新时间与最近同步时间
* - external_data:平台返回的订单原始结构
*
* order_items:
* - external_item_id:平台明细ID(OrderItemId),与订单ID一起唯一标识一条明细
*/
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn('orders', 'external_print_account_id', {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: true,
comment: '来源外部打单账号ID',
});
await queryInterface.addColumn('orders', 'external_order_id', {
type: Sequelize.STRING(64),
allowNull: true,
comment: '平台订单号(店管家 PlatformOrderId)',
});
await queryInterface.addColumn('orders', 'platform_type', {
type: Sequelize.STRING(32),
allowNull: true,
comment: '平台类型:TouTiao/Taobao/Pinduoduo 等',
});
await queryInterface.addColumn('orders', 'platform_status', {
type: Sequelize.STRING(32),
allowNull: true,
comment: '平台侧订单状态(如 waitsellersend)',
});
await queryInterface.addColumn('orders', 'external_shop_id', {
type: Sequelize.STRING(64),
allowNull: true,
comment: '平台侧店铺ID(店管家 ShopId)',
});
await queryInterface.addColumn('orders', 'external_shop_name', {
type: Sequelize.STRING(128),
allowNull: true,
comment: '平台侧店铺名称',
});
await queryInterface.addColumn('orders', 'external_created_at', {
type: Sequelize.DATE,
allowNull: true,
comment: '平台侧订单创建时间',
});
await queryInterface.addColumn('orders', 'external_updated_at', {
type: Sequelize.DATE,
allowNull: true,
comment: '平台侧订单更新时间',
});
await queryInterface.addColumn('orders', 'synced_at', {
type: Sequelize.DATE,
allowNull: true,
comment: '最近一次从平台同步的时间',
});
await queryInterface.addColumn('orders', 'external_data', {
type: Sequelize.JSON,
allowNull: true,
comment: '平台返回的订单原始结构',
});
await queryInterface.addIndex('orders', { fields: [ 'principal_id', 'external_order_id' ], name: 'idx_orders_principal_external' });
await queryInterface.addIndex('orders', { fields: [ 'shop_id', 'external_order_id' ], name: 'idx_orders_shop_external' });
await queryInterface.addColumn('order_items', 'external_item_id', {
type: Sequelize.STRING(64),
allowNull: true,
comment: '平台明细ID(店管家 OrderItemId)',
});
await queryInterface.addIndex('order_items', {
fields: [ 'order_id', 'external_item_id' ],
unique: true,
name: 'uk_order_items_order_external',
});
},
async down(queryInterface) {
await queryInterface.removeIndex('order_items', 'uk_order_items_order_external');
await queryInterface.removeColumn('order_items', 'external_item_id');
await queryInterface.removeIndex('orders', 'idx_orders_shop_external');
await queryInterface.removeIndex('orders', 'idx_orders_principal_external');
await queryInterface.removeColumn('orders', 'external_data');
await queryInterface.removeColumn('orders', 'synced_at');
await queryInterface.removeColumn('orders', 'external_updated_at');
await queryInterface.removeColumn('orders', 'external_created_at');
await queryInterface.removeColumn('orders', 'external_shop_name');
await queryInterface.removeColumn('orders', 'external_shop_id');
await queryInterface.removeColumn('orders', 'platform_status');
await queryInterface.removeColumn('orders', 'platform_type');
await queryInterface.removeColumn('orders', 'external_order_id');
await queryInterface.removeColumn('orders', 'external_print_account_id');
},
};