WMS_MAIN/database/migrations/20260730200000-backfill-wms-order-express-store-id.js
liufumingren edc164298d feat(waybill): 新增面单/运单管理模块
- 新增 systems/waybill.js 业务层:运单列表查询/详情/导出
- 新增 systemMethods/waybill/warehouse.js 入口层
- 新增 6 个迁移脚本:面单列表索引、store_id 列、物流编码/名称回填、发货单号列、货主ID回填、发货字段回填
- 更新 waybill-module-design.md 设计文档
2026-08-01 03:43:47 +08:00

73 lines
2.4 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';
const TABLE_NAME = 'wms_order_express';
/**
* 回填 wms_order_express.store_id。
* 从对应的仓库分表(current → history → dispatch)中获取 store_id。
*
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<void>} 无返回值。
*/
async function backfill(queryInterface) {
// 获取所有不同的 warehouse_id
const [warehouses] = await queryInterface.sequelize.query(
`SELECT DISTINCT warehouse_id FROM ${TABLE_NAME} WHERE store_id IS NULL AND warehouse_id IS NOT NULL`
);
if (!warehouses || warehouses.length === 0) {
return;
}
for (const row of warehouses) {
const warehouseId = row.warehouse_id;
const tableTypes = [
`wms_order_delivery_${warehouseId}`, // current(配送表 / 待处理表)
`wms_order_delivery_${warehouseId}_history`, // history
`wms_order_delivery_${warehouseId}_dispatch`, // dispatch
];
for (const deliveryTable of tableTypes) {
try {
const [result] = await queryInterface.sequelize.query(`
UPDATE ${TABLE_NAME} e
INNER JOIN ${deliveryTable} d ON e.order_id = d.id AND e.warehouse_id = d.warehouse_id
SET e.store_id = d.store_id
WHERE e.store_id IS NULL AND d.store_id IS NOT NULL
`);
console.log(` warehouse=${warehouseId} table=${deliveryTable} rows=${result.info || result}`);
} catch (err) {
// 表可能不存在,跳过
if (err && err.parent && err.parent.code === 'ER_NO_SUCH_TABLE') {
continue;
}
console.warn(` warehouse=${warehouseId} table=${deliveryTable} error=${err.message}`);
}
}
}
}
module.exports = {
/**
* 执行迁移:回填 store_id。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {import('sequelize')} Sequelize Sequelize 实例。
* @return {Promise<void>} 无返回值。
*/
async up(queryInterface, Sequelize) {
await backfill(queryInterface);
},
/**
* 回滚迁移:将 store_id 置为 NULL。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {import('sequelize')} Sequelize Sequelize 实例。
* @return {Promise<void>} 无返回值。
*/
async down(queryInterface, Sequelize) {
await queryInterface.sequelize.query(
`UPDATE ${TABLE_NAME} SET store_id = NULL`
);
},
};