feat(order): 订单/明细 id 防回退——序列表唯一发号 + 分表免重建 + 水位自愈 + 迁移任务执行历史

This commit is contained in:
liufumingren 2026-08-15 20:12:23 +08:00
parent 0eb9e845dd
commit 37104079e5
10 changed files with 1495 additions and 330 deletions

12
app.js
View File

@ -38,6 +38,18 @@ module.exports = app => {
app.logger.error('❌ 工厂类初始化失败:', error);
}
// 启动初检:异步为已有分表的仓库确保订单 id 序列表存在且水位正确。
// 只做建表 + 水位抬升,不迁移数据(位置指针回填由 backfill-order-id-location 脚本负责);
// 异步执行不阻塞启动,失败仅告警,后续建单/迁移触发时还会再次 ensure。
setImmediate(() => {
const factory = app.model && app.model.OrderDeliveryFactory;
if (factory && typeof factory.ensureAllLocationSequenceTables === 'function') {
factory.ensureAllLocationSequenceTables().catch(error => {
app.logger.warn(`[启动初检] 序列表初检异常: ${error && error.message ? error.message : error}`);
});
}
});
// 验证缓存方法
if (typeof app.setCacheAsync === 'function') {
await app.setCacheAsync('system', 'wms', 10);

View File

@ -18,7 +18,9 @@ const {
buildColumnLine,
buildColumnLines,
buildIndexLines,
isColumnDefinitionDifferent,
parseManagedTableName,
AUTO_INCREMENT_SAFE_MARGIN,
} = require('./order_delivery_table_schema');
const LANES = Object.freeze({
@ -111,6 +113,7 @@ module.exports = app => {
constructor() {
this.modelCache = new Map();
this.inflightInitCache = new Map();
this.locationTableReady = new Map();
this.app = app;
}
@ -539,32 +542,7 @@ module.exports = app => {
}
for (const columnName of expectedAttributeNames) {
const expectedColumn = expectedAttributes[columnName];
const actualColumn = tables[columnName] || {};
const expectedType = expectedColumn.type && typeof expectedColumn.type.toSql === 'function'
? expectedColumn.type.toSql().toLowerCase()
: String(expectedColumn.type || '').toLowerCase();
const actualType = String(actualColumn.type || actualColumn.Type || '').toLowerCase();
const expectedAllowNull = expectedColumn.allowNull !== false;
const actualAllowNull = actualColumn.allowNull !== false;
const expectedDefault = Object.prototype.hasOwnProperty.call(expectedColumn, 'defaultValue')
? expectedColumn.defaultValue
: undefined;
const actualDefault = Object.prototype.hasOwnProperty.call(actualColumn, 'defaultValue')
? actualColumn.defaultValue
: undefined;
if (expectedType && actualType && expectedType !== actualType) {
return true;
}
if (expectedAllowNull !== actualAllowNull) {
return true;
}
const normalizedExpectedDefault = expectedDefault == null ? null : String(expectedDefault);
const normalizedActualDefault = actualDefault == null ? null : String(actualDefault);
if (normalizedExpectedDefault !== normalizedActualDefault) {
if (isColumnDefinitionDifferent(columnName, expectedAttributes[columnName], tables[columnName])) {
return true;
}
}
@ -678,6 +656,92 @@ module.exports = app => {
}
}
/**
* 获取同仓库共享 id 序列的全部分表的 MAX(id) 基线。
*
* 多表共用 id 契约:订单迁移到配送表时显式复制源 id,因此新分配的
* id 必须大于所有分表已占用的 id。
*
* @param {number|string} warehouseId 仓库ID。
* @return {Promise<number>} 最大 id,无任何分表时返回 0。
*/
async getSharedIdBaseline(warehouseId) {
const tables = [
buildManagedTableName(warehouseId, 'current'),
buildManagedTableName(warehouseId, 'dispatch'),
buildManagedTableName(warehouseId, 'history'),
buildManagedTableName(warehouseId, 'outsourced'),
];
let existingTableSet = null;
try {
existingTableSet = new Set(await this.app.model.getQueryInterface().showAllTables());
} catch (error) {
existingTableSet = null;
}
let baseline = 0;
for (const relatedTable of tables) {
const exists = existingTableSet
? existingTableSet.has(relatedTable)
: await this.checkTableExists(relatedTable);
if (!exists) {
continue;
}
const [ rows ] = await this.app.model.query(
`SELECT COALESCE(MAX(id), 0) AS maxId FROM ${quoteIdentifier(relatedTable)}`
);
const maxId = Number(rows && rows[0] && rows[0].maxId || 0);
if (Number.isFinite(maxId) && maxId > baseline) {
baseline = maxId;
}
}
return baseline;
}
/**
* 核对并修复表自增水位,确保 AUTO_INCREMENT 大于同仓库共享 id 序列
* 的全部分表(current/dispatch/history/outsourced)的 MAX(id)。
*
* 多表共用 id 契约:订单迁移到配送表时显式复制源 id。因此 current 表的
* 自增序列必须全局单调——新分配的 id 不能与任何分表已占用的 id 重叠。
* 若高 id 订单已迁走(current 表 MAX(id) 变小),表重建会把水位重置为
* current.MAX(id)+1,与配送表已占用的 id 重叠,后续迁移显式插入会撞主键。
* 所以水位基准必须取所有分表的最大 MAX(id)。
*
* @param {string} tableName 表名。
* @return {Promise<void>}
*/
async ensureAutoIncrementWatermark(tableName) {
const parsed = parseManagedTableName(tableName);
if (!parsed) {
return;
}
const baseline = await this.getSharedIdBaseline(parsed.warehouseId);
if (!Number.isFinite(baseline) || baseline <= 0) {
return;
}
const autoIncrement = await this.getTableAutoIncrement(tableName);
if (autoIncrement === null || autoIncrement > baseline) {
return;
}
// 修复目标加安全余量:防止读取 MAX(id) 与 SET 之间的并发插入抢先占号,
// 导致修复后水位再次低于已分配 id(MySQL 不会把计数器降到当前已分配值以下,
// 余量仅作为并发窗口的缓冲)
const safeTarget = baseline + AUTO_INCREMENT_SAFE_MARGIN;
await this.app.model.query(`ALTER TABLE ${quoteIdentifier(tableName)} AUTO_INCREMENT = ${safeTarget}`);
this.app.logger.warn(
`订单工厂表 ${tableName} 自增水位低于共享 id 序列最大 id(auto=${autoIncrement}, max=${baseline}),已修复为 ${safeTarget}`
);
}
/**
* 确保单个目标表与模型结构一致。
* @param {string} tableName 表名。
@ -695,29 +759,310 @@ module.exports = app => {
? await this.needsTableSchemaSync(tableName, modelType, warehouseId, tableExists)
: true;
if (!needsSync) {
return false;
}
if (!tableExists) {
await dynamicModel.sync({ force: false });
return true;
}
await this.assertManagedTableDataConstraints(tableName, modelType);
await this.alterManagedTableSchema(tableName, modelType, warehouseId, dynamicModel);
return true;
if (needsSync) {
await this.assertManagedTableDataConstraints(tableName, modelType);
await this.alterManagedTableSchema(tableName, modelType, warehouseId);
}
// 无论是否同步结构,都核对自增水位(表重建/迁移/手工修复都可能破坏水位)
await this.ensureAutoIncrementWatermark(tableName);
return needsSync;
}
/**
* 读取表的当前 AUTO_INCREMENT 水位。
* @param {string} tableName 表名。
* @return {Promise<number|null>} 自增水位,非自增表返回 null。
*/
async getTableAutoIncrement(tableName) {
const [ rows ] = await this.app.model.query(
'SELECT AUTO_INCREMENT AS autoIncrement FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?',
{ replacements: [ tableName ] }
);
const rawValue = rows && rows[0] && rows[0].autoIncrement;
const value = Number(rawValue);
return Number.isFinite(value) ? value : null;
}
/**
* 获取仓库订单 id 序列表名。
*
* 每仓库一张序列表:id 自增发号,location 记录订单数据当前所在表位。
*
* @param {number|string} warehouseId 仓库ID。
* @return {string} 序列表名。
*/
getLocationSequenceTableName(warehouseId) {
return `wms_order_id_location_${warehouseId}`;
}
/**
* 确保仓库订单 id 序列表存在,并将其自增水位抬升到同仓库所有
* 订单分表 MAX(id) 之上(避开存量订单 id)。
*
* 注意:本方法可能执行 DDL(隐式提交),必须在事务外调用。
*
* @param {number|string} warehouseId 仓库ID。
* @return {Promise<void>}
*/
async ensureLocationSequenceTable(warehouseId) {
const tableName = this.getLocationSequenceTableName(warehouseId);
// 进程内已初始化过则跳过:避免每次建单都重复 showAllTables + 分表 MAX 核对
if (this.locationTableReady.get(warehouseId)) {
return;
}
// 并发去重:同一仓库并发的 ensure 共享同一个初始化 Promise,
// 保证「建表 + 抬水位」只执行一次,其余调用等待完成后直接返回。
return await this.runModelInitializer(`init_seq_${warehouseId}`, async () => {
if (this.locationTableReady.get(warehouseId)) {
return;
}
// 先算基线再建表:建表语句直接携带正确水位。
// 消除「先建表(水位=1)再 ALTER」之间的发号窗口——否则窗口内
// 并发建单/多 worker 发号会拿到 1,与存量订单 id 冲突。
const baseline = await this.getSharedIdBaseline(warehouseId);
const safeTarget = baseline > 0 ? baseline + AUTO_INCREMENT_SAFE_MARGIN : 0;
const exists = await this.checkTableExists(tableName);
if (!exists) {
const autoIncrementClause = safeTarget > 0 ? ` AUTO_INCREMENT = ${safeTarget}` : '';
try {
await this.app.model.query(
`CREATE TABLE ${quoteIdentifier(tableName)} (
\`id\` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
\`location\` VARCHAR(20) NOT NULL DEFAULT 'current',
PRIMARY KEY (\`id\`)
) ENGINE=InnoDB${autoIncrementClause} DEFAULT CHARSET=utf8mb4 COMMENT='仓库订单全局id序列与位置索引(极简,不迁移)'`
);
this.app.logger.info(`创建订单 id 序列表成功: ${tableName}${safeTarget > 0 ? `, 初始水位=${safeTarget}` : ''}`);
} catch (createError) {
const errorCode = createError
&& ((createError.original && createError.original.code)
|| (createError.parent && createError.parent.code)
|| createError.code);
if (errorCode !== 'ER_TABLE_EXISTS') {
throw createError;
}
// 并发窗口:其他 worker 已建表,转入下方已存在核对分支
this.app.logger.warn(`订单 id 序列表 ${tableName} 已被并发创建,转入水位核对`);
}
}
// 表已存在(或刚被并发创建):核对并抬升水位
if (safeTarget > 0) {
const autoIncrement = await this.getTableAutoIncrement(tableName);
// ai 为 null(MariaDB 新建表/计数器未初始化)同样视为需要抬升
if (autoIncrement === null || autoIncrement <= baseline) {
await this.app.model.query(
`ALTER TABLE ${quoteIdentifier(tableName)} AUTO_INCREMENT = ${safeTarget}`
);
this.app.logger.warn(
`订单 id 序列表 ${tableName} 水位抬升至 ${safeTarget}(分表最大 id=${baseline},原水位=${autoIncrement ?? 'null'})`
);
}
}
this.locationTableReady.set(warehouseId, true);
});
}
/**
* 启动初检:为当前库中已存在的订单分表仓库确保序列表存在且水位正确。
*
* 仅做「建表 + 水位抬升」,不迁移任何数据——存量订单的位置指针回填由
* `script/backfill-order-id-location.cjs` 脚本负责。单个仓库失败仅告警,
* 不影响其他仓库与服务启动。
*
* @return {Promise<number>} 完成初检的仓库数。
*/
async ensureAllLocationSequenceTables() {
const managedTableNames = await this.listManagedTableNames();
const warehouseIds = Array.from(new Set(
managedTableNames
.map(tableName => {
const parsed = parseManagedTableName(tableName);
return parsed ? parsed.warehouseId : null;
})
.filter(Boolean)
));
let checkedCount = 0;
for (const warehouseId of warehouseIds) {
try {
await this.ensureLocationSequenceTable(warehouseId);
checkedCount += 1;
} catch (error) {
this.app.logger.warn(`[启动初检] 仓库 ${warehouseId} 序列表初始化失败: ${error.message}`);
}
}
if (warehouseIds.length > 0) {
this.app.logger.info(`[启动初检] 订单 id 序列表初检完成: ${checkedCount}/${warehouseIds.length} 个仓库`);
}
return checkedCount;
}
/**
* 从仓库序列表分配全局订单 id(在调用方事务内执行,随事务回滚)。
*
* 调用方必须在事务外先调用 ensureLocationSequenceTable 完成建表。
*
* @param {number|string} warehouseId 仓库ID。
* @param {Object} [transaction] Sequelize 事务。
* @param {string} [location='current'] 初始位置(current/dispatch/history/outsourced)。
* @return {Promise<number>} 新分配的订单 id。
*/
async allocateOrderId(warehouseId, transaction = null, location = 'current') {
const tableName = this.getLocationSequenceTableName(warehouseId);
if (!this.locationTableReady.get(warehouseId)) {
const exists = await this.checkTableExists(tableName);
if (!exists) {
// 不允许在事务内隐式 DDL,交给调用方在事务外初始化
throw new Error(`订单 id 序列表 ${tableName} 不存在,请先调用 ensureLocationSequenceTable`);
}
this.locationTableReady.set(warehouseId, true);
}
const options = transaction ? { transaction } : {};
const queryResult = await this.app.model.query(
`INSERT INTO ${quoteIdentifier(tableName)} (\`location\`) VALUES (?)`,
{ replacements: [ location ], ...options }
);
// sequelize.query 对 INSERT 的返回形态随驱动不同,兼容解析:
// - mysql2: [ insertId, affectedRows ](首个元素是数字)
// - 其他包装: [{ insertId, affectedRows }, ...]
const raw = Array.isArray(queryResult) ? queryResult[0] : queryResult;
let insertId = 0;
if (typeof raw === 'number') {
insertId = raw;
} else if (raw && typeof raw === 'object' && !Array.isArray(raw)) {
insertId = Number(raw.insertId || 0);
} else if (Array.isArray(raw) && raw.length > 0 && typeof raw[0] === 'number') {
insertId = Number(raw[0]);
}
if (!Number.isFinite(insertId) || insertId <= 0) {
throw new Error('订单 id 分配失败');
}
return insertId;
}
/**
* 更新订单在序列表中的位置指针(迁移时调用)。
*
* 存量订单(序列表无记录)以显式 id 补录;显式插入值大于当前水位时
* MySQL 会自动推进序列表自增,保证后续发号不与存量 id 冲突。
*
* @param {number|string} orderId 订单ID。
* @param {number|string} warehouseId 仓库ID。
* @param {string} location 新位置。
* @param {Object} [transaction] Sequelize 事务。
* @return {Promise<void>}
*/
async updateOrderLocation(orderId, warehouseId, location, transaction = null) {
const tableName = this.getLocationSequenceTableName(warehouseId);
const options = transaction ? { transaction } : {};
await this.app.model.query(
`INSERT INTO ${quoteIdentifier(tableName)} (\`id\`, \`location\`) VALUES (?, ?) ON DUPLICATE KEY UPDATE \`location\` = VALUES(\`location\`)`,
{ replacements: [ Number(orderId), location ], ...options }
);
}
/**
* 查询单个订单当前所在表位。
*
* @param {number|string} orderId 订单ID。
* @param {number|string} warehouseId 仓库ID。
* @param {Object} [transaction] Sequelize 事务。
* @return {Promise<string|null>} 位置(current/dispatch/history/outsourced),
* 无记录返回 null(存量订单未回填)。
*/
async getOrderLocation(orderId, warehouseId, transaction = null) {
const tableName = this.getLocationSequenceTableName(warehouseId);
if (!this.locationTableReady.get(warehouseId)) {
const exists = await this.checkTableExists(tableName);
if (!exists) {
return null;
}
this.locationTableReady.set(warehouseId, true);
}
const options = transaction ? { transaction } : {};
const [ rows ] = await this.app.model.query(
`SELECT \`location\` FROM ${quoteIdentifier(tableName)} WHERE \`id\` = ? LIMIT 1`,
{ replacements: [ Number(orderId) ], ...options }
);
return rows && rows[0] ? String(rows[0].location) : null;
}
/**
* 批量查询订单当前所在表位。
*
* @param {Array<number|string>} orderIds 订单ID列表。
* @param {number|string} warehouseId 仓库ID。
* @param {Object} [transaction] Sequelize 事务。
* @return {Promise<Map<number, string>>} orderId → location;
* 无记录的订单不在 Map 中(存量订单未回填)。
*/
async getOrderLocations(orderIds, warehouseId, transaction = null) {
const result = new Map();
const normalizedIds = Array.from(new Set(
(Array.isArray(orderIds) ? orderIds : [ orderIds ])
.map(id => Number(id))
.filter(id => Number.isFinite(id) && id > 0)
));
if (normalizedIds.length === 0) {
return result;
}
const tableName = this.getLocationSequenceTableName(warehouseId);
if (!this.locationTableReady.get(warehouseId)) {
const exists = await this.checkTableExists(tableName);
if (!exists) {
return result;
}
this.locationTableReady.set(warehouseId, true);
}
const options = transaction ? { transaction } : {};
const placeholders = normalizedIds.map(() => '?').join(',');
const [ rows ] = await this.app.model.query(
`SELECT \`id\`, \`location\` FROM ${quoteIdentifier(tableName)} WHERE \`id\` IN (${placeholders})`,
{ replacements: normalizedIds, ...options }
);
for (const row of rows || []) {
result.set(Number(row.id), String(row.location));
}
return result;
}
/**
* 将现有表的列结构与期望模型对齐,所有变更合并为单条 ALTER TABLE 语句。
*
* 自增保护:对自增主键 id 列不做 CHANGE——对 AUTO_INCREMENT 列重新声明
* 列定义可能触发表重建(ALGORITHM=COPY),重建后 AUTO_INCREMENT 被重置为
* MAX(id)+1。待处理表的高 id 订单已迁移删除,重置会使自增水位回退到历史
* 低值,与配送表已复制的 id 重叠,导致后续迁移主键冲突。
*
* @param {string} tableName 表名。
* @param {string} type 模型类型。
* @param {number|string} warehouseId 仓库ID。
* @param {Object} dynamicModel 已构建的动态模型(用于索引同步)。
* @return {Promise<void>}
*/
async alterManagedTableSchema(tableName, type, warehouseId, dynamicModel) {
async alterManagedTableSchema(tableName, type, warehouseId) {
const queryInterface = this.app.model.getQueryInterface();
const sequelize = this.app.model;
const actualColumns = await queryInterface.describeTable(tableName);
@ -727,17 +1072,27 @@ module.exports = app => {
const expectedColumnNames = Object.keys(expectedAttributes);
const clauses = [];
// ADD 新增列 / CHANGE 变更列
// ADD 新增列 / CHANGE 差异列;id 主键列永不参与 CHANGE——对 AUTO_INCREMENT
// 列重新声明列定义可能触发表重建(ALGORITHM=COPY),重建后 AUTO_INCREMENT 被重置为
// MAX(id)+1。待处理表的高 id 订单已迁移删除,重置会使自增水位回退到历史低值,
// 与配送表已复制的 id 重叠,导致后续迁移主键冲突。
for (const columnName of expectedColumnNames) {
const columnDef = buildColumnLine(columnName, expectedAttributes[columnName]);
const expectedAttribute = expectedAttributes[columnName];
if (columnName === 'id') {
continue;
}
const columnDef = buildColumnLine(columnName, expectedAttribute);
if (!actualColumnNames.has(columnName)) {
clauses.push(`ADD COLUMN ${columnDef}`);
} else {
} else if (isColumnDefinitionDifferent(columnName, expectedAttribute, actualColumns[columnName])) {
clauses.push(`CHANGE ${quoteIdentifier(columnName)} ${columnDef}`);
}
}
// DROP 多余列(现有表有但期望模型没有的列)
// DROP 多余列(现有表有但期望模型没有的列;id 必在期望模型中,不会被 DROP)
const expectedColumnSet = new Set(expectedColumnNames);
for (const columnName of actualColumnNames) {
if (!expectedColumnSet.has(columnName)) {
@ -745,12 +1100,39 @@ module.exports = app => {
}
}
const previousAutoIncrement = await this.getTableAutoIncrement(tableName);
if (clauses.length > 0) {
await sequelize.query(`ALTER TABLE ${quotedTableName} ${clauses.join(', ')}`);
}
// 索引同步仍委托 Sequelize sync({ alter: true }) 处理(仅触发索引差异)
await dynamicModel.sync({ alter: true });
// 若 ALTER 触发表重建并把自增水位重置到历史低值,恢复原水位
const currentAutoIncrement = await this.getTableAutoIncrement(tableName);
if (previousAutoIncrement !== null
&& currentAutoIncrement !== null
&& currentAutoIncrement < previousAutoIncrement) {
await sequelize.query(`ALTER TABLE ${quotedTableName} AUTO_INCREMENT = ${previousAutoIncrement}`);
this.app.logger.warn(
`订单工厂表 ${tableName} 自增水位被 ALTER 重置(${previousAutoIncrement} → ${currentAutoIncrement}),已恢复为 ${previousAutoIncrement}`
);
}
// 索引同步不再使用 sync({ alter: true }):历史事故表明它会重建表并重置自增水位,
// 且无法可靠检测 AUTO_INCREMENT 属性差异。改为仅补齐缺失索引,不重建、不删除已有索引。
const existingIndexNames = new Set((await queryInterface.showIndex(tableName))
.map(index => index.Key_name || index.name || '')
.filter(Boolean));
for (const indexDef of this.getIndexes(warehouseId, type)) {
if (existingIndexNames.has(indexDef.name)) {
continue;
}
await queryInterface.addIndex(tableName, indexDef.fields, {
name: indexDef.name,
unique: Boolean(indexDef.unique),
});
}
}
/**
@ -781,7 +1163,8 @@ module.exports = app => {
const tableName = this.generateTableName(warehouseId, modelType);
const dynamicModel = this.buildManagedModel(warehouseId, modelType, tableName);
await dynamicModel.sync({ alter: true });
// 统一走差异检测 + 自增水位保护的同步路径,不再使用 sync({ alter: true })
await this.ensureManagedTableSchema(tableName, modelType, warehouseId, dynamicModel);
this.app.logger.info(`创建/同步物理表成功: ${tableName}`);
}

View File

@ -7,6 +7,16 @@ const MANAGED_TYPES = Object.freeze({
OUTSOURCED: 'outsourced',
});
/**
* 自增水位修复时的安全余量。
*
* 修复水位时会读取各分表 MAX(id) 作为基准,但读取与 SET 之间存在并发窗口:
* 其他请求可能正在抢号分配更大的 id。若 SET 值刚好是 MAX(id)+1,并发插入
* 提交后表内 max 可能再次超过水位。预留余量(跳过若干 id)可避免该竞态,
* BIGINT 空间下跳过几十个 id 无实际影响。
*/
const AUTO_INCREMENT_SAFE_MARGIN = 50;
const TABLE_PATTERNS = Object.freeze([
{
type: MANAGED_TYPES.DISPATCH,
@ -210,7 +220,10 @@ function formatDefaultValue(defaultValue) {
}
if (defaultValue && typeof defaultValue.toSql === 'function') {
return defaultValue.toSql();
const rawSql = String(defaultValue.toSql());
// Sequelize.NOW 的 toSql() 返回 'NOW',MySQL/MariaDB 的 DATETIME 列
// 默认值必须写成 CURRENT_TIMESTAMP,否则生成 DEFAULT NOW 非法语法
return rawSql.trim().toUpperCase() === 'NOW' ? 'CURRENT_TIMESTAMP' : rawSql;
}
if (typeof defaultValue === 'string') {
@ -230,12 +243,11 @@ function formatDefaultValue(defaultValue) {
}
/**
* 将 Sequelize 属性定义转换为 DDL 列片段。
* @param {string} columnName 列名。
* 生成属性的类型 SQL 片段。
* @param {Object} attribute Sequelize 属性定义。
* @return {string} DDL 列片段。
* @return {string} 类型 SQL 片段。
*/
function buildColumnLine(columnName, attribute) {
function buildColumnTypeSql(attribute) {
let typeSql;
if (attribute.type && Array.isArray(attribute.type.values)) {
@ -249,6 +261,129 @@ function buildColumnLine(columnName, attribute) {
typeSql = String(attribute.type || '');
}
// 无 dialect 绑定时 DataTypes.DATE 序列化为 'DATE',
// 但 MySQL 实际列类型是 DATETIME;DDL 若输出 DATE 会把时间部分截断
if (typeSql && String(typeSql).trim().toUpperCase() === 'DATE') {
typeSql = 'DATETIME';
}
return typeSql;
}
/**
* 归一化列类型 SQL,用于模型定义与实际表结构的语义比对。
*
* 消除不会影响语义的差异,避免比对恒真而反复执行 ALTER:
* - INTEGER 与 INT 在 MySQL 中是同义词;
* - 整型 display width(如 BIGINT(20)、INT(11))不影响语义,
* 且 MySQL 8.0.19+ 的 information_schema 不再返回 display width;
* - 大小写与空格差异。
*
* @param {string} sql 类型 SQL。
* @return {string} 归一化后的类型 SQL。
*/
function normalizeColumnTypeSql(sql) {
let normalized = String(sql || '')
.trim()
.replace(/\s+/g, ' ')
.toUpperCase();
normalized = normalized.replace(/^INTEGER\b/, 'INT');
normalized = normalized.replace(/^(TINYINT|SMALLINT|MEDIUMINT|INT|BIGINT)\s*\(\s*\d+\s*\)/, '$1');
// Sequelize 无 dialect 绑定时 DATE 序列化为 'DATE',而 MySQL 实际列类型为 DATETIME
if (normalized === 'DATE') {
normalized = 'DATETIME';
}
// 删除逗号两侧空格(ENUM 值列表、DECIMAL(18, 2) 等)
normalized = normalized.replace(/\s*,\s*/g, ',');
return normalized;
}
/**
* 判断列定义是否与模型期望存在语义差异。
*
* 与 needsTableSchemaSync / alterManagedTableSchema 共用同一套比较规则,
* 保证“判定需要同步”与“生成 ALTER 子句”不会漂移。
*
* @param {string} columnName 列名。
* @param {Object} expectedAttribute 模型期望的属性定义。
* @param {Object} actualColumn describeTable 返回的实际列定义。
* @return {boolean} 是否存在语义差异。
*/
function isColumnDefinitionDifferent(columnName, expectedAttribute, actualColumn) {
const expectedType = normalizeColumnTypeSql(buildColumnTypeSql(expectedAttribute));
const actualType = normalizeColumnTypeSql(actualColumn && (actualColumn.type || actualColumn.Type) || '');
if (expectedType && actualType && expectedType !== actualType) {
return true;
}
// 主键/自增列在建表时由 Sequelize 自动补 NOT NULL,
// 即使模型定义未显式声明 allowNull: false,实际列也是 NOT NULL
const expectedAllowNull = expectedAttribute.primaryKey || expectedAttribute.autoIncrement
? false
: expectedAttribute.allowNull !== false;
const actualAllowNull = (actualColumn && actualColumn.allowNull) !== false;
if (expectedAllowNull !== actualAllowNull) {
return true;
}
const expectedDefault = Object.prototype.hasOwnProperty.call(expectedAttribute, 'defaultValue')
? expectedAttribute.defaultValue
: undefined;
const actualDefault = Object.prototype.hasOwnProperty.call(actualColumn || {}, 'defaultValue')
? actualColumn.defaultValue
: undefined;
// Sequelize.literal(如 CURRENT_TIMESTAMP)提取其原始表达式参与比较;
// Sequelize.NOW 的 val 为 'NOW',与表内实际 DEFAULT CURRENT_TIMESTAMP 归一
const extractDefaultValue = value => {
if (value && typeof value === 'object' && typeof value.val === 'string') {
return String(value.val).trim().toUpperCase() === 'NOW' ? 'CURRENT_TIMESTAMP' : value.val;
}
return value;
};
const normalizedExpectedDefault = extractDefaultValue(expectedDefault) == null
? null
: String(extractDefaultValue(expectedDefault));
const normalizedActualDefault = extractDefaultValue(actualDefault) == null
? null
: String(extractDefaultValue(actualDefault));
if (normalizedExpectedDefault === normalizedActualDefault) {
return false;
}
// 数字默认值允许不同文本表示(如 '0' 与 '0.00'),只要数值相等即视为一致
if (normalizedExpectedDefault !== null && normalizedActualDefault !== null) {
const expectedNumber = Number(normalizedExpectedDefault);
const actualNumber = Number(normalizedActualDefault);
if (normalizedExpectedDefault !== ''
&& normalizedActualDefault !== ''
&& Number.isFinite(expectedNumber)
&& Number.isFinite(actualNumber)
&& expectedNumber === actualNumber) {
return false;
}
}
return true;
}
/**
* 将 Sequelize 属性定义转换为 DDL 列片段。
* @param {string} columnName 列名。
* @param {Object} attribute Sequelize 属性定义。
* @return {string} DDL 列片段。
*/
function buildColumnLine(columnName, attribute) {
const typeSql = buildColumnTypeSql(attribute);
const parts = [ `\`${columnName}\``, typeSql ];
if (attribute.allowNull === false) {
@ -283,6 +418,12 @@ function buildTableAttributes(app, type) {
const modelType = normalizeManagedType(type);
const attributes = cloneAttributes(app.model.OrderDelivery.rawAttributes);
// 分表 id 统一由仓库序列表显式分配,分表自身不再携带自增序列。
// 保留两套发号序列(分表自增 + 序列表)会在降级等场景产生区间重叠的冲突 id,
// 这里从模型层移除 autoIncrement:新建分表不再带 AUTO_INCREMENT;
// 已有分表结构保持不变(不执行任何 id 列变更),显式 id 插入不受影响。
attributes.id = { ...attributes.id, autoIncrement: false };
if (isDispatchType(modelType)) {
const { ENUM } = app.Sequelize;
attributes.dispatch_lane = {
@ -513,6 +654,7 @@ function parseManagedTableName(tableName) {
module.exports = {
MANAGED_TYPES,
AUTO_INCREMENT_SAFE_MARGIN,
normalizeManagedType,
isDispatchType,
isHistoryType,
@ -525,6 +667,9 @@ module.exports = {
buildTableComment,
buildTableOptions,
buildIndexOptions,
buildColumnTypeSql,
normalizeColumnTypeSql,
isColumnDefinitionDifferent,
buildColumnLine,
buildColumnLines,
buildIndexLines,

View File

@ -5,6 +5,20 @@ const {
ensureShardWarehouseId,
clearShardWarehouseValidationCache,
} = require('../lib/warehouse_shard_guard');
const {
isColumnDefinitionDifferent,
buildColumnLine,
AUTO_INCREMENT_SAFE_MARGIN,
} = require('./order_delivery_table_schema');
/**
* 转义 SQL 标识符。
* @param {string} identifier 标识符。
* @return {string} 转义后的标识符。
*/
function quoteIdentifier(identifier) {
return `\`${String(identifier).replace(/`/g, '``')}\``;
}
/**
* 克隆模型属性定义。
@ -266,7 +280,11 @@ module.exports = app => {
getTableAttributes(type) {
const mainModel = this.app.model.OrderItems;
const attributes = cloneAttributes(mainModel && mainModel.rawAttributes);
// 模型 timestamps:true 会向 rawAttributes 注入 createdAt/updatedAt
// (与显式定义的 created_at/updated_at 指向同一列),结构对齐时会被
// 误判为缺失列而 ADD COLUMN,导致重复列与无默认值报错,剔除这两个注入属性。
delete attributes.createdAt;
delete attributes.updatedAt;
// 保留 id 字段的 autoIncrement,与 order_delivery_factory 行为一致,
// 确保分表 wms_order_items_{warehouseId} 的 bulkCreate 能正常自增生成 id
@ -389,33 +407,7 @@ module.exports = app => {
}
for (const columnName of expectedAttributeNames) {
const expectedColumn = expectedAttributes[columnName];
const actualColumn = tables[columnName] || {};
const expectedType = expectedColumn.type && typeof expectedColumn.type.toSql === 'function'
? expectedColumn.type.toSql().toLowerCase()
: String(expectedColumn.type || '').toLowerCase();
const actualType = String(actualColumn.type || actualColumn.Type || '').toLowerCase();
const expectedAllowNull = expectedColumn.allowNull !== false;
const actualAllowNull = actualColumn.allowNull !== false;
const expectedDefault = Object.prototype.hasOwnProperty.call(expectedColumn, 'defaultValue')
? expectedColumn.defaultValue
: undefined;
const actualDefault = Object.prototype.hasOwnProperty.call(actualColumn, 'defaultValue')
? actualColumn.defaultValue
: undefined;
if (expectedType && actualType && expectedType !== actualType) {
return true;
}
if (expectedAllowNull !== actualAllowNull) {
return true;
}
const normalizedExpectedDefault = expectedDefault == null ? null : String(expectedDefault);
const normalizedActualDefault = actualDefault == null ? null : String(actualDefault);
if (normalizedExpectedDefault !== normalizedActualDefault) {
if (isColumnDefinitionDifferent(columnName, expectedAttributes[columnName], tables[columnName])) {
return true;
}
}
@ -438,6 +430,170 @@ module.exports = app => {
return false;
}
/**
* 读取表的当前 AUTO_INCREMENT 水位。
* @param {string} tableName 表名。
* @return {Promise<number|null>} 自增水位,非自增表返回 null。
*/
async getTableAutoIncrement(tableName) {
const [ rows ] = await this.app.model.query(
'SELECT AUTO_INCREMENT AS autoIncrement FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?',
{ replacements: [ tableName ] }
);
const rawValue = rows && rows[0] && rows[0].autoIncrement;
const value = Number(rawValue);
return Number.isFinite(value) ? value : null;
}
/**
* 核对并修复表自增水位,确保 AUTO_INCREMENT 大于同仓库共享 id 序列
* 的全部分表(current/dispatch/history)的 MAX(id)。
*
* 明细迁移到配送明细表时同样显式复制源 id,因此 current 明细表的
* 自增序列必须全局单调,水位基准取所有分表的最大 MAX(id)。
*
* @param {string} tableName 表名。
* @return {Promise<void>}
*/
async ensureAutoIncrementWatermark(tableName) {
const sequelize = this.app.model;
const matched = /^wms_order_items_(\d+)(?:_(dispatch|history))?$/.exec(tableName);
const relatedTables = matched
? [
tableName,
`wms_order_items_${matched[1]}`,
`wms_order_items_${matched[1]}_dispatch`,
`wms_order_items_${matched[1]}_history`,
]
: [ tableName ];
let existingTableSet = null;
try {
existingTableSet = new Set(await sequelize.getQueryInterface().showAllTables());
} catch (error) {
existingTableSet = null;
}
let baseline = 0;
for (const relatedTable of new Set(relatedTables)) {
const exists = existingTableSet
? existingTableSet.has(relatedTable)
: await this.checkTableExists(relatedTable);
if (!exists) {
continue;
}
const [ rows ] = await sequelize.query(
`SELECT COALESCE(MAX(id), 0) AS maxId FROM \`${relatedTable}\``
);
const maxId = Number(rows && rows[0] && rows[0].maxId || 0);
if (Number.isFinite(maxId) && maxId > baseline) {
baseline = maxId;
}
}
if (!Number.isFinite(baseline) || baseline <= 0) {
return;
}
const autoIncrement = await this.getTableAutoIncrement(tableName);
if (autoIncrement === null || autoIncrement > baseline) {
return;
}
// 修复目标加安全余量:防止读取 MAX(id) 与 SET 之间的并发插入抢先占号
const safeTarget = baseline + AUTO_INCREMENT_SAFE_MARGIN;
await sequelize.query(`ALTER TABLE \`${tableName}\` AUTO_INCREMENT = ${safeTarget}`);
this.app.logger.warn(
`订单商品工厂表 ${tableName} 自增水位低于共享 id 序列最大 id(auto=${autoIncrement}, max=${baseline}),已修复为 ${safeTarget}`
);
}
/**
* 将现有表的列结构与期望模型对齐,所有变更合并为单条 ALTER TABLE 语句。
*
* 自增保护:对自增主键 id 列不做 CHANGE——对 AUTO_INCREMENT 列重新声明
* 列定义可能触发表重建(ALGORITHM=COPY),重建后 AUTO_INCREMENT 被重置为
* MAX(id)+1,与 dispatch/history 已复制的 id 重叠,导致后续迁移主键冲突。
* 索引同步仅补齐缺失索引,不重建、不删除已有索引。
*
* @param {string} tableName 表名。
* @param {string} type 模型类型。
* @param {number|string} warehouseId 仓库ID。
* @return {Promise<void>}
*/
async alterManagedTableSchema(tableName, type, warehouseId) {
const sequelize = this.app.model;
const queryInterface = sequelize.getQueryInterface();
const actualColumns = await queryInterface.describeTable(tableName);
const expectedAttributes = this.getTableAttributes(type);
const quotedTableName = quoteIdentifier(tableName);
const actualColumnNames = new Set(Object.keys(actualColumns || {}));
const expectedColumnNames = Object.keys(expectedAttributes);
const clauses = [];
// ADD 新增列 / CHANGE 差异列;id 主键列永不参与 CHANGE,避免重建表重置自增水位
for (const columnName of expectedColumnNames) {
const expectedAttribute = expectedAttributes[columnName];
if (columnName === 'id') {
continue;
}
const columnDef = buildColumnLine(columnName, expectedAttribute);
if (!actualColumnNames.has(columnName)) {
clauses.push(`ADD COLUMN ${columnDef}`);
} else if (isColumnDefinitionDifferent(columnName, expectedAttribute, actualColumns[columnName])) {
clauses.push(`CHANGE ${quoteIdentifier(columnName)} ${columnDef}`);
}
}
// DROP 多余列(现有表有但期望模型没有的列;id 必在期望模型中,不会被 DROP)
const expectedColumnSet = new Set(expectedColumnNames);
for (const columnName of actualColumnNames) {
if (!expectedColumnSet.has(columnName)) {
clauses.push(`DROP COLUMN ${quoteIdentifier(columnName)}`);
}
}
const previousAutoIncrement = await this.getTableAutoIncrement(tableName);
if (clauses.length > 0) {
await sequelize.query(`ALTER TABLE ${quotedTableName} ${clauses.join(', ')}`);
}
// 若 ALTER 触发表重建并把自增水位重置到历史低值,恢复原水位
const currentAutoIncrement = await this.getTableAutoIncrement(tableName);
if (previousAutoIncrement !== null
&& currentAutoIncrement !== null
&& currentAutoIncrement < previousAutoIncrement) {
await sequelize.query(`ALTER TABLE ${quotedTableName} AUTO_INCREMENT = ${previousAutoIncrement}`);
this.app.logger.warn(
`订单商品工厂表 ${tableName} 自增水位被 ALTER 重置(${previousAutoIncrement} → ${currentAutoIncrement}),已恢复为 ${previousAutoIncrement}`
);
}
// 仅补齐缺失索引,不重建、不删除已有索引
const existingIndexNames = new Set((await queryInterface.showIndex(tableName))
.map(index => index.Key_name || index.name || '')
.filter(Boolean));
for (const indexDef of this.generateIndexes(warehouseId)) {
if (existingIndexNames.has(indexDef.name)) {
continue;
}
await queryInterface.addIndex(tableName, indexDef.fields, {
name: indexDef.name,
unique: Boolean(indexDef.unique),
});
}
}
/**
* 确保目标表与模型结构一致。
* @param {string} tableName 表名。
@ -453,12 +609,21 @@ module.exports = app => {
? await this.needsTableSchemaSync(tableName, type, warehouseId, tableExists)
: true;
if (!needsSync) {
return false;
if (!tableExists) {
await dynamicModel.sync({ force: false });
return true;
}
await dynamicModel.sync({ alter: true });
return true;
if (needsSync) {
// 结构同步不再使用 sync({ alter: true }):历史事故表明它会重建表并重置
// 自增水位(与 dispatch/history 已复制 id 重叠,后续迁移主键冲突)。
// 改为手工差异 ALTER + 补齐索引,并跳过 id 列 CHANGE。
await this.alterManagedTableSchema(tableName, type, warehouseId);
}
// 无论是否同步结构,都核对自增水位(表重建/迁移/手工修复都可能破坏水位)
await this.ensureAutoIncrementWatermark(tableName);
return needsSync;
}
/**
@ -470,7 +635,8 @@ module.exports = app => {
const tableName = this.generateTableName(warehouseId, type);
const dynamicModel = this.buildDynamicModel(warehouseId, type, tableName);
await dynamicModel.sync({ alter: true });
// 统一走差异检测 + 自增水位保护的同步路径,不再直接 sync({ alter: true })
await this.ensureManagedTableSchema(tableName, type, warehouseId, dynamicModel);
this.app.logger.info(`创建/同步订单商品物理表成功: ${tableName}`);
}

View File

@ -4,6 +4,9 @@ const SHORT_TASK_QUERY_TIMEOUT_MS = 15000;
const SHORT_TASK_EXECUTION_TIMEOUT_MS = 30000;
const EXECUTION_LOG_CACHE_KEY = 'schedule:sync_outbound_orders_to_delivery:last_execution';
const EXECUTION_LOG_CACHE_TTL = 300; // 5 分钟
const EXECUTION_LOG_HISTORY_CACHE_KEY = 'schedule:sync_outbound_orders_to_delivery:history';
const EXECUTION_LOG_HISTORY_MAX = 20; // 保留最近 20 次执行记录
const EXECUTION_LOG_HISTORY_TTL = 86400; // 1 天
/**
* 将本次执行日志写入缓存。
@ -23,6 +26,36 @@ async function writeExecutionLogToCache(ctx, logEntry) {
}
}
/**
* 将本次执行日志追加到历史记录缓存(保留最近 N 次)。
*
* 用于不翻日志文件也能观察定时任务每轮执行情况(selected/created/status/error),
* 便于排查「订单满足迁移条件但迟迟未切表」类问题。
*
* @param {Object} ctx Egg 上下文。
* @param {Object} logEntry 日志条目。
* @return {Promise<void>} 无返回值。
*/
async function appendExecutionHistory(ctx, logEntry) {
try {
if (typeof ctx.app.getCacheAsync !== 'function' || typeof ctx.app.setCacheAsync !== 'function') {
return;
}
const rawHistory = await ctx.app.getCacheAsync(EXECUTION_LOG_HISTORY_CACHE_KEY);
const history = Array.isArray(rawHistory) ? rawHistory : [];
history.unshift(logEntry);
await ctx.app.setCacheAsync(
EXECUTION_LOG_HISTORY_CACHE_KEY,
history.slice(0, EXECUTION_LOG_HISTORY_MAX),
EXECUTION_LOG_HISTORY_TTL
);
} catch (err) {
// 历史日志写入失败不影响主流程
ctx.logger.warn(`[sync-outbound-orders-to-delivery] 写入执行历史缓存失败: ${err.message}`);
}
}
/**
* 提取错误详情,方便定位问题位置。
*
@ -139,6 +172,7 @@ module.exports = {
warehouse_ids: result ? result.warehouseIds : [],
};
await writeExecutionLogToCache(ctx, logEntry);
ctx.logger.info(`[sync-outbound-orders-to-delivery] 执行日志已写入缓存 key=${EXECUTION_LOG_CACHE_KEY}`);
await appendExecutionHistory(ctx, logEntry);
ctx.logger.info(`[sync-outbound-orders-to-delivery] 执行日志已写入缓存 key=${EXECUTION_LOG_CACHE_KEY} history=${EXECUTION_LOG_HISTORY_CACHE_KEY}`);
},
};

View File

@ -34,7 +34,13 @@ class deliveryorderServer extends MainService {
const info = this.getInfo();
const res = await this.ctx.service.systems.deliveryOrder.addOrder(data, info, 'QM');
if (res.success) {
return this.success(res.message, { createTime: this.app.formatDate(new Date()), deliveryOrderId: res.data.deliveryOrderId, warehouseCode: info.warehouseCode, logisticsCode: res.data.logisticsCode });
return this.success(res.message, {
createTime: this.app.formatDate(new Date()),
// addOrder 成功返回 data.orderId,兼容历史字段 deliveryOrderId
deliveryOrderId: (res.data && (res.data.orderId || res.data.deliveryOrderId)) || '',
warehouseCode: info.warehouseCode,
logisticsCode: (res.data && res.data.logisticsCode) || '',
});
}
return this.fail(res.message, { createTime: this.app.formatDate(new Date()) });
}

View File

@ -163,10 +163,34 @@ class DeliveryOrderService extends OrderBaseService {
{ label: 'current', getModel: () => factory.getCurrentModel(warehouseId) },
{ label: 'history', getModel: () => factory.getHistoryModel(warehouseId) },
];
const remainingIds = new Set(orderIds.map(String));
// 序列表批量定位:命中则只查对应分表,未命中(存量订单)回退三表遍历
const normalizedOrderIds = orderIds.map(Number).filter(n => Number.isFinite(n) && n > 0);
let locationMap = new Map();
try {
if (typeof factory.getOrderLocations === 'function') {
locationMap = await factory.getOrderLocations(normalizedOrderIds, warehouseId);
}
} catch (e) {
dbg(` locationMap ERROR: ${e.message}`);
}
dbg(` locationMap size: ${locationMap.size}`);
const idsByLabel = { dispatch: [], current: [], history: [] };
const unresolvedIds = [];
for (const id of normalizedOrderIds) {
const location = locationMap.get(id);
if (location && idsByLabel[location]) {
idsByLabel[location].push(id);
} else {
unresolvedIds.push(id);
}
}
for (const entry of tableDefs) {
if (remainingIds.size === 0) break;
dbg(` trying table: ${entry.label}, remainingIds=[${[ ...remainingIds ].join(',')}]`);
const targetIds = [ ...new Set([ ...(idsByLabel[entry.label] || []), ...unresolvedIds ]) ];
if (targetIds.length === 0) continue;
dbg(` trying table: ${entry.label}, targetIds=[${targetIds.join(',')}]`);
try {
const model = await entry.getModel();
dbg(` ${entry.label} model: ${!!model}, typeof findAll=${model && typeof model.findAll}`);
@ -174,7 +198,7 @@ class DeliveryOrderService extends OrderBaseService {
dbg(` ${entry.label} model invalid, skip`);
continue;
}
const whereClause = { id: [ ...remainingIds ].map(Number) };
const whereClause = { id: targetIds };
dbg(` ${entry.label} where: ${JSON.stringify(whereClause)}`);
const rows = await model.findAll({
attributes: [
@ -193,7 +217,7 @@ class DeliveryOrderService extends OrderBaseService {
for (const r of (rows || [])) {
dbg(` row: id=${r.id} delivery_order_code=${r.delivery_order_code} status=${r.status}`);
const rid = String(r.id);
if (!orderMap.has(rid)) { orderMap.set(rid, r); remainingIds.delete(rid); }
if (!orderMap.has(rid)) { orderMap.set(rid, r); }
}
} catch (e) {
dbg(` ${entry.label} ERROR: ${e.message} | stack: ${(e.stack || '').split('\n').slice(0, 3).join(' | ')}`);
@ -329,33 +353,58 @@ class DeliveryOrderService extends OrderBaseService {
return [];
}
const attributes = [
'id', 'delivery_order_code', 'source_order_codes', 'pay_no',
'store_id', 'status',
'name', 'mobile', 'province', 'city', 'area', 'detail_address',
'buyer_nick', 'seller_nick', 'shop_nick',
'total_amount', 'item_amount', 'freight',
'items_count', 'items_weight',
'wave_no', 'delivery_time', 'create_at',
];
const tableOrders = [
{ label: 'dispatch', getModel: () => orderDeliveryFactory.getDispatchModel(warehouseId) },
{ label: 'current', getModel: () => orderDeliveryFactory.getCurrentModel(warehouseId) },
{ label: 'history', getModel: () => orderDeliveryFactory.getHistoryModel(warehouseId) },
];
// 序列表批量定位:命中则只查对应分表,未命中(存量订单)回退三表遍历
const normalizedIds = [ ...new Set((orderIds || []).map(Number).filter(id => Number.isFinite(id) && id > 0)) ];
let locationMap = new Map();
try {
if (typeof orderDeliveryFactory.getOrderLocations === 'function') {
locationMap = await orderDeliveryFactory.getOrderLocations(normalizedIds, warehouseId);
}
} catch (_err) {
locationMap = new Map();
}
const idsByLabel = { dispatch: [], current: [], history: [] };
const unresolvedIds = [];
for (const id of normalizedIds) {
const location = locationMap.get(id);
if (location && idsByLabel[location]) {
idsByLabel[location].push(id);
} else {
unresolvedIds.push(id);
}
}
const resultMap = new Map();
const remainingIds = new Set(orderIds.map(id => String(id)));
for (const entry of tableOrders) {
if (remainingIds.size === 0) break;
const targetIds = [ ...new Set([ ...(idsByLabel[entry.label] || []), ...unresolvedIds ]) ];
if (targetIds.length === 0) continue;
try {
const model = await entry.getModel();
if (!model || typeof model.findAll !== 'function') continue;
const rows = await model.findAll({
attributes: [
'id', 'delivery_order_code', 'source_order_codes', 'pay_no',
'store_id', 'status',
'name', 'mobile', 'province', 'city', 'area', 'detail_address',
'buyer_nick', 'seller_nick', 'shop_nick',
'total_amount', 'item_amount', 'freight',
'items_count', 'items_weight',
'wave_no', 'delivery_time', 'create_at',
],
attributes,
where: {
id: { [Op.in]: [ ...remainingIds ].map(Number) },
id: { [Op.in]: targetIds },
},
raw: true,
});
@ -364,7 +413,6 @@ class DeliveryOrderService extends OrderBaseService {
const rid = String(r.id);
if (!resultMap.has(rid)) {
resultMap.set(rid, r);
remainingIds.delete(rid);
}
}
} catch (_err) {

View File

@ -628,7 +628,6 @@ class DeliveryOrderService extends Service {
*/
async addOrder(data, info, createChannel = '') {
const { ctx } = this;
const operationLogs = [];
let warehouseIdStr;
try {
warehouseIdStr = normalizeWarehouseShardId(info.warehouseId);
@ -655,255 +654,360 @@ class DeliveryOrderService extends Service {
|| 'MANUAL'
).trim().toUpperCase() || 'MANUAL';
// 开启事务
const transaction = await ctx.model.transaction();
// 订单 id 序列表初始化可能执行 DDL(隐式提交),必须在事务外先完成
try {
if (!info.storeId) {
throw new Error('非法参数');
}
if (!deliveryOrder || !deliveryOrder.deliveryOrderCode) {
throw new Error('订单信息不完整');
}
await ctx.model.OrderDeliveryFactory.ensureLocationSequenceTable(warehouseIdStr);
} catch (error) {
// 初始化失败不降级:后续发号会直接失败并回滚建单,绝不让分表自增介入发号
ctx.logger.warn('[DeliveryOrderService.addOrder] 序列表初始化失败,本次建单将失败:', error.message);
}
// 检查订单是否已存在(根据唯一索引)
const existingOrder = await DeliveryModel.findOne({
where: {
delivery_order_code: deliveryOrder.deliveryOrderCode,
store_id: info.storeId,
warehouse_id: info.warehouseId,
},
attributes: [ 'id' ],
transaction,
});
if (existingOrder) {
await transaction.rollback();
return {
success: true,
message: '订单已存在',
data: { deliveryOrderId: existingOrder.id },
};
}
// 单次建单执行体:重试时复用,事务随每次尝试重新开启
const createOnce = async operationLogs => {
// 开启事务
const transaction = await ctx.model.transaction();
// 准备订单数据
const step = await this.resolveOrderStep(info, transaction);
const orderData = await this.prepareOrderData(
deliveryOrder,
info,
normalizedCreateChannel,
step,
operationLogs,
requestExtendProps
);
try {
if (!info.storeId) {
throw new Error('非法参数');
}
if (!deliveryOrder || !deliveryOrder.deliveryOrderCode) {
throw new Error('订单信息不完整');
}
// 创建订单
const createdOrder = await DeliveryModel.create(orderData, { transaction });
const orderId = createdOrder.id;
this.appendOrderOperationLog(
operationLogs,
ORDER_OPERATION_CODES.CREATE,
`订单创建成功,orderId:${orderId}`,
{ prepend: true }
);
// 检查订单是否已存在(根据唯一索引)
const existingOrder = await DeliveryModel.findOne({
where: {
delivery_order_code: deliveryOrder.deliveryOrderCode,
store_id: info.storeId,
warehouse_id: info.warehouseId,
},
attributes: [ 'id' ],
transaction,
});
if (existingOrder) {
await transaction.rollback();
return {
success: true,
message: '订单已存在',
data: { deliveryOrderId: existingOrder.id },
};
}
// 将建单渠道注入 info,供 addOrderLines 区分 MANUAL 降级逻辑
info.createChannel = normalizedCreateChannel;
let itemsInfo = null;
// 添加订单明细
if (orderLines && (orderLines.orderLine)) {
itemsInfo = await this.addOrderLines(
orderLines.orderLine,
orderId,
// 准备订单数据
const step = await this.resolveOrderStep(info, transaction);
const orderData = await this.prepareOrderData(
deliveryOrder,
info,
transaction
normalizedCreateChannel,
step,
operationLogs,
requestExtendProps
);
} else {
throw new Error('[订单无明细]');
}
// 更新订单统计信息 - 直接使用itemsInfo中的计算结果
if (itemsInfo) {
const stats = {
items: itemsInfo.items_str || '', // 原始结构字符串
items_count: itemsInfo.stock_count || 0, // 商品种类数
items_total: itemsInfo.item_total_count || 0, // 商品总数(直接使用计算结果)
items_weight: itemsInfo.total_weight || 0, // 总重量
items_hash: this.generateItemsHash(itemsInfo), // 哈希值
items_crc: this.calculateCRC32(itemsInfo.items_str || ''), // CRC校验
source_order_codes: ((itemsInfo.stats && itemsInfo.stats.source_order_codes) || []).filter(code => code).join(',') || deliveryOrder.deliveryOrderCode,
updated_at: new Date(),
};
// 创建订单:订单 id 统一由仓库序列表分配,显式写入 current 分表。
// 不再回退分表自增:分表自增序列与序列表发号区间重叠会产生冲突 id,
// 发号失败时直接抛错,由事务回滚,杜绝脏 id 落库。
const orderId = await ctx.model.OrderDeliveryFactory.allocateOrderId(warehouseIdStr, transaction, 'current');
await DeliveryModel.create({ ...orderData, id: orderId }, { transaction });
this.appendOrderOperationLog(
operationLogs,
ORDER_OPERATION_CODES.CREATE,
`订单创建成功,orderId:${orderId}`,
{ prepend: true }
);
// ===== 快递绑定策略漏斗匹配(items 处理后 weight 可用) =====
try {
const receiverInfo = deliveryOrder.receiverInfo;
const shopNick = deliveryOrder.shopNick;
const hasEnoughData = shopNick || (receiverInfo && (receiverInfo.province || receiverInfo.city));
// 将建单渠道注入 info,供 addOrderLines 区分 MANUAL 降级逻辑
info.createChannel = normalizedCreateChannel;
if (hasEnoughData) {
const address = [
receiverInfo?.province || '',
receiverInfo?.city || '',
receiverInfo?.area || '',
receiverInfo?.town || '',
receiverInfo?.detail_address || '',
].join('').trim();
const matchResult = await ctx.service.systems.expressPolicy.matchPolicy(
info.warehouseId,
{
storeId: info.storeId,
customShopName: shopNick || '',
address: address || '',
weight: itemsInfo.total_weight || null,
prohibitedTypeIds: null, // TODO: 后续从商品 StockWarehouse.prohibited_id 聚合
}
);
if (matchResult && matchResult.expressCode) {
const expressList = await ctx.service.systems.express.getExpressList();
const matchedExpress = expressList.find(
item => item && String(item.code || item.value || '').trim() === String(matchResult.expressCode).trim()
);
let logisticsCode;
let logisticsName;
let sysLogisticsCode;
let sysLogisticsName;
if (matchedExpress) {
if (matchedExpress.is_system_setting) {
// 自定义映射:sys 写映射的系统快递,logistics 写自定义编码
sysLogisticsCode = String(matchedExpress.express_code || '').trim();
const sysItem = expressList.find(
item => item && !item.is_system_setting && String(item.code || item.value || '').trim() === sysLogisticsCode
);
sysLogisticsName = sysItem ? String(sysItem.name || sysItem.label || sysLogisticsCode).trim() : sysLogisticsCode;
logisticsCode = String(matchedExpress.code || matchedExpress.value || matchResult.expressCode).trim();
logisticsName = String(matchedExpress.alias || matchedExpress.name || logisticsCode).trim();
} else {
// 系统快递:4 字段一致
logisticsCode = String(matchedExpress.code || matchedExpress.value || matchResult.expressCode).trim();
logisticsName = String(matchedExpress.name || matchedExpress.label || logisticsCode).trim();
sysLogisticsCode = logisticsCode;
sysLogisticsName = logisticsName;
}
} else {
// 未在列表中,fallback
logisticsCode = String(matchResult.expressCode).trim();
logisticsName = logisticsCode;
sysLogisticsCode = logisticsCode;
sysLogisticsName = logisticsCode;
}
stats.logistics_code = logisticsCode;
stats.logistics_name = logisticsName;
stats.sys_logistics_code = sysLogisticsCode;
stats.sys_logistics_name = sysLogisticsName;
stats.weight_type = matchResult.weightType;
stats.updated_at = new Date();
// 写入操作日志:命中了哪条策略
this.appendOrderOperationLog(
operationLogs,
ORDER_OPERATION_CODES.EXPRESS_POLICY_MATCH,
`快递策略命中: ${matchResult.policyName || '未知'}(${matchResult.policyId || '?'}) → ${sysLogisticsName || sysLogisticsCode}`
);
ctx.logger.info('[ExpressPolicyMatch] 策略命中(延迟匹配)', {
orderId,
orderCode: deliveryOrder.deliveryOrderCode,
logisticsCode,
sysLogisticsCode,
weightType: matchResult.weightType,
policyName: matchResult.policyName,
policyId: matchResult.policyId,
totalWeight: itemsInfo.total_weight,
});
} else if (hasEnoughData) {
// 策略未命中:记快递降级日志(取已落库的初始快递值)
const fallbackCode = orderData.sys_logistics_code || orderData.logistics_code;
const fallbackName = orderData.sys_logistics_name || orderData.logistics_name;
this.appendOrderOperationLog(
operationLogs,
ORDER_OPERATION_CODES.EXPRESS_FALLBACK,
`未命中快递策略,使用默认快递: ${fallbackName || fallbackCode}`
);
}
}
} catch (policyErr) {
ctx.logger.warn('[ExpressPolicyMatch] 策略匹配异常,跳过', {
let itemsInfo = null;
// 添加订单明细
if (orderLines && (orderLines.orderLine)) {
itemsInfo = await this.addOrderLines(
orderLines.orderLine,
orderId,
error: policyErr.message,
info,
transaction
);
} else {
throw new Error('[订单无明细]');
}
// 更新订单统计信息 - 直接使用itemsInfo中的计算结果
if (itemsInfo) {
const stats = {
items: itemsInfo.items_str || '', // 原始结构字符串
items_count: itemsInfo.stock_count || 0, // 商品种类数
items_total: itemsInfo.item_total_count || 0, // 商品总数(直接使用计算结果)
items_weight: itemsInfo.total_weight || 0, // 总重量
items_hash: this.generateItemsHash(itemsInfo), // 哈希值
items_crc: this.calculateCRC32(itemsInfo.items_str || ''), // CRC校验
source_order_codes: ((itemsInfo.stats && itemsInfo.stats.source_order_codes) || []).filter(code => code).join(',') || deliveryOrder.deliveryOrderCode,
updated_at: new Date(),
};
// ===== 快递绑定策略漏斗匹配(items 处理后 weight 可用) =====
try {
const receiverInfo = deliveryOrder.receiverInfo;
const shopNick = deliveryOrder.shopNick;
const hasEnoughData = shopNick || (receiverInfo && (receiverInfo.province || receiverInfo.city));
if (hasEnoughData) {
const address = [
receiverInfo?.province || '',
receiverInfo?.city || '',
receiverInfo?.area || '',
receiverInfo?.town || '',
receiverInfo?.detail_address || '',
].join('').trim();
const matchResult = await ctx.service.systems.expressPolicy.matchPolicy(
info.warehouseId,
{
storeId: info.storeId,
customShopName: shopNick || '',
address: address || '',
weight: itemsInfo.total_weight || null,
prohibitedTypeIds: null, // TODO: 后续从商品 StockWarehouse.prohibited_id 聚合
}
);
if (matchResult && matchResult.expressCode) {
const expressList = await ctx.service.systems.express.getExpressList();
const matchedExpress = expressList.find(
item => item && String(item.code || item.value || '').trim() === String(matchResult.expressCode).trim()
);
let logisticsCode;
let logisticsName;
let sysLogisticsCode;
let sysLogisticsName;
if (matchedExpress) {
if (matchedExpress.is_system_setting) {
// 自定义映射:sys 写映射的系统快递,logistics 写自定义编码
sysLogisticsCode = String(matchedExpress.express_code || '').trim();
const sysItem = expressList.find(
item => item && !item.is_system_setting && String(item.code || item.value || '').trim() === sysLogisticsCode
);
sysLogisticsName = sysItem ? String(sysItem.name || sysItem.label || sysLogisticsCode).trim() : sysLogisticsCode;
logisticsCode = String(matchedExpress.code || matchedExpress.value || matchResult.expressCode).trim();
logisticsName = String(matchedExpress.alias || matchedExpress.name || logisticsCode).trim();
} else {
// 系统快递:4 字段一致
logisticsCode = String(matchedExpress.code || matchedExpress.value || matchResult.expressCode).trim();
logisticsName = String(matchedExpress.name || matchedExpress.label || logisticsCode).trim();
sysLogisticsCode = logisticsCode;
sysLogisticsName = logisticsName;
}
} else {
// 未在列表中,fallback
logisticsCode = String(matchResult.expressCode).trim();
logisticsName = logisticsCode;
sysLogisticsCode = logisticsCode;
sysLogisticsName = logisticsCode;
}
stats.logistics_code = logisticsCode;
stats.logistics_name = logisticsName;
stats.sys_logistics_code = sysLogisticsCode;
stats.sys_logistics_name = sysLogisticsName;
stats.weight_type = matchResult.weightType;
stats.updated_at = new Date();
// 写入操作日志:命中了哪条策略
this.appendOrderOperationLog(
operationLogs,
ORDER_OPERATION_CODES.EXPRESS_POLICY_MATCH,
`快递策略命中: ${matchResult.policyName || '未知'}(${matchResult.policyId || '?'}) → ${sysLogisticsName || sysLogisticsCode}`
);
ctx.logger.info('[ExpressPolicyMatch] 策略命中(延迟匹配)', {
orderId,
orderCode: deliveryOrder.deliveryOrderCode,
logisticsCode,
sysLogisticsCode,
weightType: matchResult.weightType,
policyName: matchResult.policyName,
policyId: matchResult.policyId,
totalWeight: itemsInfo.total_weight,
});
} else if (hasEnoughData) {
// 策略未命中:记快递降级日志(取已落库的初始快递值)
const fallbackCode = orderData.sys_logistics_code || orderData.logistics_code;
const fallbackName = orderData.sys_logistics_name || orderData.logistics_name;
this.appendOrderOperationLog(
operationLogs,
ORDER_OPERATION_CODES.EXPRESS_FALLBACK,
`未命中快递策略,使用默认快递: ${fallbackName || fallbackCode}`
);
}
}
} catch (policyErr) {
ctx.logger.warn('[ExpressPolicyMatch] 策略匹配异常,跳过', {
orderId,
error: policyErr.message,
});
}
await DeliveryModel.update(
stats,
{
where: { id: orderId },
transaction,
}
);
// 记录详细日志
ctx.logger.info('[订单统计信息已更新]', {
orderId,
orderCode: deliveryOrder.deliveryOrderCode,
stats: {
items_count: stats.items_count,
items_total: stats.items_total,
items_weight: stats.items_weight,
items_hash_preview: stats.items_hash?.substring(0, 8) + '...',
items_crc: stats.items_crc,
},
});
}
await DeliveryModel.update(
stats,
{
where: { id: orderId },
transaction,
}
);
await transaction.commit();
// 记录详细日志
ctx.logger.info('[订单统计信息已更新]', {
orderId,
orderCode: deliveryOrder.deliveryOrderCode,
stats: {
items_count: stats.items_count,
items_total: stats.items_total,
items_weight: stats.items_weight,
items_hash_preview: stats.items_hash?.substring(0, 8) + '...',
items_crc: stats.items_crc,
},
await this.flushOrderOperationLogs(operationLogs, orderId, {
warehouseId: info.warehouseId,
createChannel: normalizedCreateChannel,
});
// 异步处理订单后续流程
this.asyncProcessOrderCreated(orderId, info, normalizedCreateChannel).catch(error => {
ctx.logger.warn(`[DeliveryOrderService.addOrder] asyncProcessOrderCreated failed: ${error.message}`, {
orderId,
error: error.message,
});
});
return {
success: true,
message: '订单创建成功',
data: {
orderId,
step,
stats: itemsInfo ? {
items_count: itemsInfo.stock_count,
items_total: itemsInfo.item_total_count,
items_weight: itemsInfo.total_weight,
source_order_codes: (itemsInfo.stats && itemsInfo.stats.source_order_codes) || [],
} : null,
},
};
} catch (error) {
console.error('创建订单失败:', error);
// 回滚事务(安全处理,防止事务已结束导致二次回滚异常)
await transaction.rollback().catch(rollbackErr => {
console.error('回滚事务失败:', rollbackErr.message);
});
throw error;
}
};
// 主键冲突(分表自增水位与已有数据重叠)允许修复水位后重试一次
for (let attempt = 1; attempt <= 2; attempt++) {
const operationLogs = [];
try {
return await createOnce(operationLogs);
} catch (error) {
if (this.isPrimaryKeyDuplicate(error) && attempt === 1) {
try {
const factory = ctx.model.OrderDeliveryFactory;
const tableName = factory.generateTableName(warehouseIdStr, 'current');
await factory.ensureAutoIncrementWatermark(tableName);
ctx.logger.warn('[DeliveryOrderService.addOrder] 主键冲突,已修复自增水位,重试建单', {
deliveryOrderCode: deliveryOrder && deliveryOrder.deliveryOrderCode,
warehouseId: warehouseIdStr,
storeId: info.storeId,
error: error.message,
});
} catch (repairError) {
ctx.logger.warn('[DeliveryOrderService.addOrder] 自增水位修复失败,放弃重试', {
error: repairError.message,
});
}
continue;
}
return {
success: false,
message: `订单创建失败: ${this.formatOrderCreateError(error, deliveryOrder)}`,
data: null,
};
}
}
return {
success: false,
message: '订单创建失败: 主键冲突重试后仍未成功',
data: null,
};
}
/**
* 判断是否为分表主键自增冲突。
*
* 分表自增水位被破坏(显式 id 导入、表重建、迁移复制 id)后,自增插入会与
* 已有主键重叠,Sequelize 抛出的 UniqueConstraintError 对外 message 为
* “Validation error”,需按 errors[].path 识别。
*
* @param {Error} error Sequelize 错误对象。
* @return {boolean} 是否为主键唯一冲突。
*/
isPrimaryKeyDuplicate(error) {
if (!error || error.name !== 'SequelizeUniqueConstraintError' || !Array.isArray(error.errors)) {
return false;
}
return error.errors.some(item => item
&& item.type === 'unique violation'
&& (item.path === 'PRIMARY' || item.path === 'id'));
}
/**
* 将建单异常格式化为可读的失败原因。
*
* Sequelize 唯一约束错误的 message 固定为 “Validation error”,若不解析
* errors/parent 会掩盖真实原因,这里优先提取具体冲突字段与值。
*
* @param {Error} error 原始异常。
* @param {Object} [deliveryOrder] 请求中的发货单信息。
* @return {string} 可读的失败原因。
*/
formatOrderCreateError(error, deliveryOrder) {
if (error && error.name === 'SequelizeUniqueConstraintError' && Array.isArray(error.errors)) {
const primaryDup = error.errors.find(item => item
&& item.type === 'unique violation'
&& (item.path === 'PRIMARY' || item.path === 'id'));
if (primaryDup) {
return `订单ID主键冲突(id=${primaryDup.value} 已被占用):分表自增水位与已有数据重叠,已尝试修复并重试,请再次提交`;
}
await transaction.commit();
const codeDup = error.errors.find(item => item && item.path === 'delivery_order_code');
if (codeDup) {
const orderCode = (deliveryOrder && deliveryOrder.deliveryOrderCode) || codeDup.value;
return `订单号 ${orderCode} 已存在`;
}
await this.flushOrderOperationLogs(operationLogs, orderId, {
warehouseId: info.warehouseId,
createChannel: normalizedCreateChannel,
});
// 异步处理订单后续流程
this.asyncProcessOrderCreated(orderId, info, normalizedCreateChannel).catch(error => {
ctx.logger.warn(`[DeliveryOrderService.addOrder] asyncProcessOrderCreated failed: ${error.message}`, {
orderId,
error: error.message,
});
});
return {
success: true,
message: '订单创建成功',
data: {
orderId,
step,
stats: itemsInfo ? {
items_count: itemsInfo.stock_count,
items_total: itemsInfo.item_total_count,
items_weight: itemsInfo.total_weight,
source_order_codes: (itemsInfo.stats && itemsInfo.stats.source_order_codes) || [],
} : null,
},
};
} catch (error) {
console.error('创建订单失败:', error);
// 回滚事务(安全处理,防止事务已结束导致二次回滚异常)
await transaction.rollback().catch(rollbackErr => {
console.error('回滚事务失败:', rollbackErr.message);
});
return {
success: false,
message: `订单创建失败: ${error.message}`,
data: null,
};
const firstItem = error.errors[0] || {};
return String(firstItem.message || error.message || '唯一约束冲突').trim();
}
const parentSqlMessage = error && error.parent && error.parent.sqlMessage;
if (parentSqlMessage) {
return `${error.message} (${parentSqlMessage})`;
}
return String((error && error.message) || '').trim() || '未知错误';
}
/**

View File

@ -405,6 +405,12 @@ class OrderDeliveryMigrationService extends MainService {
const deadlineAt = Number.isFinite(executionTimeoutMs) && executionTimeoutMs > 0
? Date.now() + executionTimeoutMs
: 0;
// 序列表初始化可能执行 DDL(隐式提交),必须在事务外完成。
// 初始化失败直接终止迁移:位置指针与配送表写入必须在同一事务内原子完成,
// 没有序列表则无法保证索引一致,宁可迁移失败重试也不产生错位指针。
await this.ctx.model.OrderDeliveryFactory.ensureLocationSequenceTable(normalizedWarehouseId);
const transaction = await this.ctx.model.transaction();
const { Op } = this.ctx.app.Sequelize;
let sourceRows = [];
@ -538,6 +544,22 @@ class OrderDeliveryMigrationService extends MainService {
await targetOrderModel.bulkCreate(rowsToCreate, {
...this.buildMigrationQueryOptions(transaction, queryTimeoutMs),
});
// 更新序列表位置指针:current → dispatch。与上方 bulkCreate 处于同一事务,
// 任一步失败整体回滚,保证配送表数据与位置索引原子一致(存量订单自动补录,
// 显式 id 大于序列表水位时 MySQL 会自动推进,后续发号不会与存量 id 冲突)。
const locationFactory = this.ctx.model && this.ctx.model.OrderDeliveryFactory;
if (!locationFactory || typeof locationFactory.updateOrderLocation !== 'function') {
throw new Error('订单工厂缺少 updateOrderLocation,无法同步序列表位置指针');
}
for (const row of rowsToCreate) {
await locationFactory.updateOrderLocation(
Number(row.id),
normalizedWarehouseId,
'dispatch',
transaction
);
}
}
assertMigrationDeadline('查询配送表订单');

View File

@ -0,0 +1,245 @@
const fs = require('fs');
const path = require('path');
const mysql = require('mysql2/promise');
const ROOT = process.cwd();
const DEFAULT_CONFIG_PATH = path.join(ROOT, 'config', 'config.local.js');
const SLOT_LOCATIONS = [
{ suffix: '_dispatch', location: 'dispatch' },
{ suffix: '_history', location: 'history' },
{ suffix: '_outsourced', location: 'outsourced' },
];
function parseArgs(argv) {
const args = {
dryRun: false,
dbClient: 'wms',
configPath: DEFAULT_CONFIG_PATH,
warehouseId: null,
batchSize: 1000,
};
for (const item of argv) {
if (item === '--dry-run') {
args.dryRun = true;
continue;
}
if (item.startsWith('--warehouse-id=')) {
const raw = String(item.split('=')[1] || '').trim();
args.warehouseId = Number(raw);
continue;
}
if (item.startsWith('--batch-size=')) {
const raw = Number(item.split('=')[1] || 0);
if (Number.isFinite(raw) && raw > 0) {
args.batchSize = Math.min(50000, Math.max(100, Math.floor(raw)));
}
continue;
}
if (item.startsWith('--db-client=')) {
args.dbClient = String(item.split('=')[1] || '').trim() || 'wms';
continue;
}
if (item.startsWith('--config-path=')) {
args.configPath = path.resolve(ROOT, String(item.split('=')[1] || '').trim());
continue;
}
}
return args;
}
function loadDbClient(configPath, dbClientName) {
if (!fs.existsSync(configPath)) {
throw new Error(`Config file not found: ${configPath}`);
}
const configFactory = require(configPath);
const cfg = typeof configFactory === 'function'
? configFactory({ name: 'wms-order-id-location-backfill' })
: configFactory;
const dbClient = cfg && cfg.mysql && cfg.mysql.clients && cfg.mysql.clients[dbClientName];
if (!dbClient) {
throw new Error(`MySQL client not found in config.mysql.clients: ${dbClientName}`);
}
return dbClient;
}
function parseManagedTableName(tableName) {
const suffixMatch = /^wms_order_delivery_(\d+)(?:_(dispatch|history|outsourced))?$/.exec(tableName);
if (!suffixMatch) {
return null;
}
return {
warehouseId: Number(suffixMatch[1]),
location: suffixMatch[2] || 'current',
};
}
async function ensureSequenceTable(conn, warehouseId, dryRun) {
const tableName = `wms_order_id_location_${warehouseId}`;
const [ rows ] = await conn.query(
`SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?`,
[ tableName ]
);
if (rows.length > 0) {
return tableName;
}
if (dryRun) {
console.log(`[dry-run] would create sequence table ${tableName}`);
return tableName;
}
await conn.query(
`CREATE TABLE \`${tableName}\` (
\`id\` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
\`location\` VARCHAR(20) NOT NULL DEFAULT 'current',
PRIMARY KEY (\`id\`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='仓库订单全局id序列与位置索引(极简,不迁移)'`
);
console.log(`created sequence table ${tableName}`);
return tableName;
}
async function countMissing(conn, sourceTable, seqTable) {
const [ rows ] = await conn.query(
`SELECT COUNT(*) AS cnt FROM \`${sourceTable}\` d `
+ `LEFT JOIN \`${seqTable}\` s ON s.id = d.id WHERE s.id IS NULL`
);
return Number(rows[0].cnt || 0);
}
async function fetchMissingBatch(conn, sourceTable, seqTable, batchSize) {
const [ rows ] = await conn.query(
`SELECT d.id FROM \`${sourceTable}\` d `
+ `LEFT JOIN \`${seqTable}\` s ON s.id = d.id WHERE s.id IS NULL `
+ `ORDER BY d.id ASC LIMIT ${batchSize}`
);
return (rows || []).map(row => Number(row.id)).filter(id => Number.isFinite(id) && id > 0);
}
async function upsertIds(conn, seqTable, location, ids, dryRun) {
if (ids.length === 0) {
return 0;
}
if (dryRun) {
console.log(`[dry-run] would insert ${ids.length} rows into ${seqTable} (location=${location}, first=${ids[0]}, last=${ids[ids.length - 1]})`);
return ids.length;
}
const values = ids.map(() => '(?, ?)').join(',');
const replacements = [];
for (const id of ids) {
replacements.push(id, location);
}
const [ result ] = await conn.query(
`INSERT INTO \`${seqTable}\` (\`id\`, \`location\`) VALUES ${values} `
+ 'ON DUPLICATE KEY UPDATE `location` = VALUES(`location`)',
replacements
);
return Number((result && result.affectedRows) || 0);
}
async function main() {
const args = parseArgs(process.argv.slice(2));
const dbClient = loadDbClient(args.configPath, args.dbClient);
const conn = await mysql.createConnection({
host: dbClient.host,
port: Number(dbClient.port),
user: dbClient.user,
password: dbClient.password,
database: dbClient.database,
charset: dbClient.charset || 'utf8mb4',
});
try {
console.log(`connected: ${dbClient.host}:${dbClient.port}/${dbClient.database}, dryRun=${args.dryRun}`);
const [ tableRows ] = await conn.query(
"SELECT TABLE_NAME FROM information_schema.TABLES "
+ "WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME LIKE 'wms_order_delivery\\\\_%' "
+ "ORDER BY TABLE_NAME"
);
const tables = (tableRows || [])
.map(row => ({ tableName: row.TABLE_NAME, parsed: parseManagedTableName(row.TABLE_NAME) }))
.filter(item => item.parsed !== null)
.filter(item => !args.warehouseId || item.parsed.warehouseId === args.warehouseId);
const warehouseIds = [ ...new Set(tables.map(item => item.parsed.warehouseId)) ].sort((a, b) => a - b);
console.log(`found warehouses: [${warehouseIds.join(', ')}], tables: ${tables.length}`);
let totalInserted = 0;
for (const warehouseId of warehouseIds) {
const seqTable = await ensureSequenceTable(conn, warehouseId, args.dryRun);
const warehouseTables = tables.filter(item => item.parsed.warehouseId === warehouseId);
for (const { tableName, parsed } of warehouseTables) {
const missingCount = await countMissing(conn, tableName, seqTable);
if (missingCount === 0) {
console.log(`[${tableName}] no missing ids`);
continue;
}
console.log(`[${tableName}] missing ids: ${missingCount} (location=${parsed.location})`);
let inserted = 0;
for (;;) {
const batch = await fetchMissingBatch(conn, tableName, seqTable, args.batchSize);
if (batch.length === 0) {
break;
}
const affected = await upsertIds(conn, seqTable, parsed.location, batch, args.dryRun);
inserted += affected;
if (batch.length < args.batchSize) {
break;
}
}
console.log(`[${tableName}] done: inserted=${inserted}`);
totalInserted += inserted;
}
// 回填(显式 id)可能不会推进序列表自增计数器,结束时统一把水位
// 抬升到 max(id)+安全余量,保证后续发号不与存量 id 冲突
const [ seqMaxRows ] = await conn.query(
`SELECT COALESCE(MAX(id), 0) AS maxId FROM \`${seqTable}\``
);
const seqMaxId = Number(seqMaxRows[0].maxId || 0);
if (seqMaxId > 0) {
const safeTarget = seqMaxId + 50;
if (args.dryRun) {
console.log(`[dry-run] would raise ${seqTable} auto_increment to ${safeTarget}`);
} else {
await conn.query(`ALTER TABLE \`${seqTable}\` AUTO_INCREMENT = ${safeTarget}`);
console.log(`[${seqTable}] auto_increment raised to ${safeTarget}`);
}
}
}
console.log(`TOTAL inserted (or would insert): ${totalInserted}`);
} finally {
await conn.end();
}
}
main().catch(error => {
console.error('FAILED:', error && error.message);
process.exit(1);
});