- 模型/迁移:wms_order_delivery 新增 need_oms_waybill TINYINT(4) NOT NULL DEFAULT 0,
迁移覆盖基表与全部分表(含 _history/_dispatch/_outsourced),幂等且 down 可回滚
- 写入链路:openapi 请求根节点 need_oms_waybill(兼容 camelCase needOmsWaybill)→
openapiMethods/deliveryorder.js 透传;delivery_order.js 仅 API 来源可能为 1,
QM/MANUAL/STOCKOUT 一律强制 0(见相邻提交)
- 取号分流:need_oms_waybill=1 的订单不参与本地自动分配快递
1. asyncProcessOrderCreated 不入本地取号队列,写 ORDER_NEED_OMS_WAYBILL_WAIT 操作日志
2. express.createWaybillCodePipeline 按订单级标记过滤(同订单全部 business_key 一并跳过),
返回值新增 skipped_need_oms_waybill_order_ids
3. schedule/consume_waybill_allocate_queue 将对应队列行置终态 waiting_oms
(不再走 pending 重试 → failed)
- waybill_allocate_queue.status 注释补充 waiting_oms 语义(STRING(20),无需迁移)
- test: delivery_order.asyncProcessOrderCreated.test.js 新增分流/保持本地取号用例,
express.createWaybillCodePipeline.skipCancelled.test.js 补齐返回字段断言
107 lines
3.8 KiB
JavaScript
107 lines
3.8 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 订单表增加 need_oms_waybill(是否需要回 OMS 取号)字段。
|
||
*
|
||
* 背景:openapi 创建的订单存在“回到上游 OMS 取运单号”的诉求,需要把该标记
|
||
* 落到订单表以便后续流程判断;非 openapi 创建来源一律默认 0。
|
||
*
|
||
* 取值约定:
|
||
* - 0 = 不需要回 OMS 取号(默认,非 openapi 来源);
|
||
* - 1 = 需要回 OMS 取号(仅 openapi 创建来源使用)。
|
||
*
|
||
* 覆盖范围:
|
||
* - 基表 wms_order_delivery;
|
||
* - 全部仓库分表 wms_order_delivery_{wh}(current)及
|
||
* _{wh}_history / _{wh}_dispatch / _{wh}_outsourced。
|
||
*
|
||
* 幂等:列已存在则跳过。分表模型由 app/model/order_delivery_factory.js
|
||
* 运行时 drift 同步兜底,本迁移提前补齐避免运行时 ALTER。
|
||
*/
|
||
|
||
const BASE_TABLE = 'wms_order_delivery';
|
||
|
||
const SHARD_PATTERN = /^wms_order_delivery_(\d+)(?:_(history|dispatch|outsourced))?$/;
|
||
|
||
const COLUMN_NAME = 'need_oms_waybill';
|
||
|
||
/**
|
||
* 列举库内全部订单分表。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @return {Promise<string[]>} 分表名列表。
|
||
*/
|
||
async function listOrderShards(queryInterface) {
|
||
const [ rows ] = await queryInterface.sequelize.query(
|
||
'SELECT TABLE_NAME AS tableName FROM INFORMATION_SCHEMA.TABLES '
|
||
+ 'WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME LIKE \'wms_order_delivery\\_%\' '
|
||
+ 'ORDER BY TABLE_NAME'
|
||
);
|
||
return (rows || [])
|
||
.map(row => String(row.tableName || row.TABLE_NAME || ''))
|
||
.filter(name => SHARD_PATTERN.test(name));
|
||
}
|
||
|
||
/**
|
||
* 判断列是否已存在。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {string} tableName 表名。
|
||
* @param {string} columnName 列名。
|
||
* @return {Promise<boolean>} 是否存在。
|
||
*/
|
||
async function columnExists(queryInterface, tableName, columnName) {
|
||
const described = await queryInterface.describeTable(tableName);
|
||
return Boolean(described && described[columnName]);
|
||
}
|
||
|
||
module.exports = {
|
||
/**
|
||
* 执行迁移:为基表与全部分表增加 need_oms_waybill 列。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {import('sequelize')} Sequelize Sequelize 构造器。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async up(queryInterface, Sequelize) {
|
||
const tables = [ BASE_TABLE, ...await listOrderShards(queryInterface) ];
|
||
const results = [];
|
||
|
||
for (const tableName of tables) {
|
||
if (await columnExists(queryInterface, tableName, COLUMN_NAME)) {
|
||
results.push({ tableName, action: 'skip', reason: 'exists' });
|
||
continue;
|
||
}
|
||
|
||
await queryInterface.addColumn(tableName, COLUMN_NAME, {
|
||
type: Sequelize.TINYINT(4),
|
||
allowNull: false,
|
||
defaultValue: 0,
|
||
comment: '是否需要回OMS取号:0不需要 1需要(仅openapi创建来源使用,其他默认0)',
|
||
});
|
||
results.push({ tableName, action: 'added' });
|
||
}
|
||
|
||
console.log('[20260912100000-add-need-oms-waybill-to-order-delivery] up results:', JSON.stringify(results));
|
||
},
|
||
|
||
/**
|
||
* 回滚迁移:移除基表与全部分表的 need_oms_waybill 列。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async down(queryInterface) {
|
||
const tables = [ BASE_TABLE, ...await listOrderShards(queryInterface) ];
|
||
const results = [];
|
||
|
||
for (const tableName of tables) {
|
||
if (!await columnExists(queryInterface, tableName, COLUMN_NAME)) {
|
||
results.push({ tableName, action: 'skip', reason: 'missing' });
|
||
continue;
|
||
}
|
||
|
||
await queryInterface.removeColumn(tableName, COLUMN_NAME);
|
||
results.push({ tableName, action: 'removed' });
|
||
}
|
||
|
||
console.log('[20260912100000-add-need-oms-waybill-to-order-delivery] down results:', JSON.stringify(results));
|
||
},
|
||
};
|