WMS_MAIN/database/migrations/20260907100000-add-intercept-time-to-order-delivery.js
liufumingren 6939d9600a feat: 拦截链路完善与车绑定修复
- 订单表新增 intercept_time 拦截时间字段(迁移覆盖基表与全部分表),全拦截链路写入拦截时间、解除拦截清空

- 新增自动库内拦截确定定时任务(1h 间隔,扫描 requested+未打印订单,超时自动库内拦截确定)

- 复核链路自动移除波次内未打印拦截订单回池,拦截解除后可重新抓取发货

- 打印解绑车判定排除拦截订单,修复波次含拦截订单时车无法解锁

- 同步 methods 文档与变更记录;含 wave_scope 回填、workbench token 过期等前序迁移
2026-09-07 03:48:43 +08:00

107 lines
3.9 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';
/**
* 订单表增加 intercept_time(拦截时间)字段。
*
* 背景:订单表当前仅维护 intercept_status 状态机,无拦截发生时间点;
* 业务需要直接记录订单进入拦截流程/库内拦截确认的时间,避免依赖
* 操作日志/售后工单反查。新增本列用于拦截时间直查。
*
* 取值约定:DATETIME,可空;订单未进入拦截流程时为 NULL,
* 拦截状态发生关键变更(拦截发起/库内拦截确认)时由业务写入。
*
* 覆盖范围:
* - 基表 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 = 'intercept_time';
/**
* 列举库内全部订单分表。
* @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 = {
/**
* 执行迁移:为基表与全部分表增加 intercept_time 列。
* @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.DATE,
allowNull: true,
defaultValue: null,
comment: '拦截时间(进入拦截流程/库内拦截确认时写入)',
});
results.push({ tableName, action: 'added' });
}
console.log('[20260907100000-add-intercept-time-to-order-delivery] up results:', JSON.stringify(results));
},
/**
* 回滚迁移:移除基表与全部分表的 intercept_time 列。
* @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('[20260907100000-add-intercept-time-to-order-delivery] down results:', JSON.stringify(results));
},
};