WMS_MAIN/database/migrations/20260730210000-backfill-order-express-delivery-fields.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

61 lines
2.1 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 的 delivery_order_code 和 source_order_codes。
* 从对应的仓库分表(current → history → dispatch)中获取。
*
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<void>} 无返回值。
*/
async function backfill(queryInterface) {
const [warehouses] = await queryInterface.sequelize.query(
`SELECT DISTINCT warehouse_id FROM ${TABLE_NAME} WHERE delivery_order_code 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}`,
`wms_order_delivery_${warehouseId}_history`,
`wms_order_delivery_${warehouseId}_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.delivery_order_code = COALESCE(e.delivery_order_code, d.delivery_order_code),
e.source_order_codes = COALESCE(e.source_order_codes, d.source_order_codes)
WHERE (e.delivery_order_code IS NULL OR e.source_order_codes IS NULL)
AND (d.delivery_order_code IS NOT NULL OR d.source_order_codes 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 = {
async up(queryInterface, Sequelize) {
await backfill(queryInterface);
},
async down(queryInterface, Sequelize) {
await queryInterface.sequelize.query(
`UPDATE ${TABLE_NAME} SET delivery_order_code = NULL, source_order_codes = NULL`
);
},
};