WMS_MAIN/database/migrations/20261111033000-init-is-back-intercept-receive-to-n.js
liufumingren e3eff20aaa feat(waybill): 批量更新运单揽收状态(waybill.getreceive)与 Y/N 标记列治理
- 新增出站回调 waybill.getreceive(注册到 callback_functions,管理端可勾选):
  请求 { request: { waybill_codes: [...] } },响应裸数组/包装体均兼容,
  1/0 与 Y/N 归一后回写 wms_order_express.is_galaxy_receive(按 warehouse_id 隔离)
- 回调签名新增 signMode=simple(MD5(appSecret+timestamp+bodyJson)),
  waybill.getreceive 使用该口径,与仓库通知协议一致;其它事件默认口径不变
- 新增 waybill.warehouse.updateReceive / updateReceiveTaskProgress:
  复用导出的异步任务ID与进度模式(前缀 waybill_receive_),单批 10 单自动切批;
  支持 waybillCodes 或与 waybill.warehouse.list 一致的 where 条件圈选(上限 5000);
  仓库未配置外部接口时直接失败,不创建任务
- 抽出 buildListQueryWhere / resolveWaybillCodesByWhere,列表与批量更新共用筛选口径
- order_express:is_back / is_intercept / is_galaxy_receive 补默认值 N
- 迁移:存量 NULL 回填为 N(Y 不动);三列分别建立单列索引(INPLACE + LOCK=NONE)
- 文档:methods/system.json(0.3.324)、变更记录、设计文档与出站回调说明同步

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-11 04:22:30 +08:00

143 lines
5.5 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';
/**
* wms_order_express 三个 Y/N 标记列补默认值与存量回填:
*
* - `is_back`(是否退回)、`is_intercept`(是否拦截)、`is_galaxy_receive`(是否揽收)
* 建表时均为 `ENUM('Y','N') NULL` 且无默认值,业务创建运单时多数不显式赋值 →
* 存量约 100% 为 NULL,导致运单列表按「是否退回 / 是否拦截 / 是否揽收 = N」筛选时
* 直接返回空(NULL 不参与 ='N' 比较)。
*
* 本迁移做两件事:
* 1) 三列补 `DEFAULT 'N'`(保留 NULL 允许,避免历史裸 SQL 显式写 NULL 时报错);
* 2) 存量 NULL 回填为 'N',已为 'Y' 的一律不动。
*
* 回填按主键区间分批(每批 ≤ 20000 个 id),走 PK 范围扫描,
* 避免单条大 UPDATE 长时间持锁(生产约 159 万行 × 3 列)。
*
* 幂等:列默认值已是 'N' 时跳过 MODIFY;无 NULL 时跳过该批 UPDATE,可重复执行。
*/
const TABLE_NAME = 'wms_order_express';
const COLUMNS = [ 'is_back', 'is_intercept', 'is_galaxy_receive' ];
const FILL_VALUE = 'N';
/** 单批主键区间宽度(控制单条 UPDATE 的锁持有时间) */
const ID_WINDOW = 20000;
/**
* 读取列定义(类型 / 可空 / 默认值 / 注释),用于构造 MODIFY COLUMN。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {string} tableName 表名。
* @param {string} columnName 列名。
* @return {Promise<Object|null>} 列定义,列不存在时返回 null。
*/
async function describeColumn(queryInterface, tableName, columnName) {
const [ rows ] = await queryInterface.sequelize.query(
'SELECT COLUMN_TYPE AS columnType, IS_NULLABLE AS isNullable, '
+ 'COLUMN_DEFAULT AS columnDefault, COLUMN_COMMENT AS columnComment '
+ 'FROM INFORMATION_SCHEMA.COLUMNS '
+ 'WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ?',
{ replacements: [ tableName, columnName ] }
);
return (rows || [])[0] || null;
}
/**
* 将列默认值设置为 'N'。
*
* 使用 `ALTER COLUMN ... SET DEFAULT`(MySQL 5.7 元数据变更,不重建表、不锁写),
* 而不是 `MODIFY COLUMN`(5.7 下会触发表重建,生产 wms_order_express 约 159 万行)。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {string} columnName 列名。
* @return {Promise<void>} 无返回值。
*/
async function applyDefault(queryInterface, columnName) {
const column = await describeColumn(queryInterface, TABLE_NAME, columnName);
if (!column) {
console.log(`[${columnName}] skip: ${TABLE_NAME}.${columnName} 列不存在`);
return;
}
if (String(column.columnDefault || '').toUpperCase() === FILL_VALUE) {
console.log(`[${columnName}] skip: 默认值已是 '${FILL_VALUE}'`);
return;
}
await queryInterface.sequelize.query(
`ALTER TABLE \`${TABLE_NAME}\` ALTER COLUMN \`${columnName}\` SET DEFAULT '${FILL_VALUE}'`
);
console.log(`[${columnName}] default '${FILL_VALUE}' applied`);
}
/**
* 按主键区间分批把 NULL 回填为 'N'(已为 'Y'/'N' 的行不受影响)。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {string} columnName 列名。
* @return {Promise<number>} 实际回填行数。
*/
async function backfillColumn(queryInterface, columnName) {
const sequelize = queryInterface.sequelize;
const [ rangeRows ] = await sequelize.query(
`SELECT MIN(id) AS minId, MAX(id) AS maxId FROM \`${TABLE_NAME}\` `
+ `WHERE \`${columnName}\` IS NULL`,
{ type: sequelize.QueryTypes.SELECT }
);
const range = (rangeRows && rangeRows[0]) || rangeRows || {};
const minId = Number(range.minId);
const maxId = Number(range.maxId);
if (!Number.isFinite(minId) || !Number.isFinite(maxId)) {
console.log(`[${columnName}] skip: 无 NULL 数据`);
return 0;
}
let filled = 0;
for (let start = minId; start <= maxId; start += ID_WINDOW) {
const end = start + ID_WINDOW - 1;
const [ , affected ] = await sequelize.query(
`UPDATE \`${TABLE_NAME}\` SET \`${columnName}\` = ? `
+ 'WHERE `id` BETWEEN ? AND ? AND `' + columnName + '` IS NULL',
{ replacements: [ FILL_VALUE, start, end ] }
);
const count = Number(affected && affected.affectedRows ? affected.affectedRows : affected) || 0;
filled += count;
}
console.log(`[${columnName}] backfilled ${filled} rows`);
return filled;
}
module.exports = {
/**
* 执行迁移:补默认值 + 存量 NULL 回填为 'N'。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<void>} 无返回值。
*/
async up(queryInterface) {
for (const columnName of COLUMNS) {
await applyDefault(queryInterface, columnName);
await backfillColumn(queryInterface, columnName);
}
},
/**
* 回滚迁移:仅移除列默认值;数据回填不可逆('N' 与 NULL 在查询口径上等价,
* 反向置 NULL 会让筛选再次失效,故不还原数据)。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<void>} 无返回值。
*/
async down(queryInterface) {
for (const columnName of COLUMNS) {
const column = await describeColumn(queryInterface, TABLE_NAME, columnName);
if (!column) continue;
if (String(column.columnDefault || '').toUpperCase() !== FILL_VALUE) continue;
await queryInterface.sequelize.query(
`ALTER TABLE \`${TABLE_NAME}\` ALTER COLUMN \`${columnName}\` DROP DEFAULT`
);
console.log(`[${columnName}] default dropped`);
}
},
};