From 14a5f13ad5c5d98dc7ad4344bbe75453b45cff23 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Thu, 9 Jul 2026 03:36:03 +0800 Subject: [PATCH] =?UTF-8?q?feat(db):=20wms=5Fstore=5Fwarehouse=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=20initiator=5Fsubject=5Fid=EF=BC=8Cwms=5Fpicking=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=20stock=5Fid=20=E5=8F=8A=E8=81=94=E5=90=88?= =?UTF-8?q?=E7=B4=A2=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - wms_store_warehouse: 新增 initiator_subject_id 记录发起方主体ID - wms_picking: 新增 stock_id 字段 + idx_wms_picking_warehouse_stock 联合索引 + 历史数据回填 - Model 层同步更新关联定义 --- app/model/picking.js | 19 ++++++++++- app/model/store_warehouse.js | 5 +++ ...initiator-subject-id-to-store-warehouse.js | 24 +++++++++++++ ...60708000000-add-stock-id-to-wms-picking.js | 34 +++++++++++++++++++ 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 database/migrations/20260707-add-initiator-subject-id-to-store-warehouse.js create mode 100644 database/migrations/20260708000000-add-stock-id-to-wms-picking.js diff --git a/app/model/picking.js b/app/model/picking.js index 987d337..f731c97 100644 --- a/app/model/picking.js +++ b/app/model/picking.js @@ -31,6 +31,11 @@ module.exports = app => { allowNull: true, comment: '库存记录ID', }, + stock_id: { + type: BIGINT(20), + allowNull: true, + comment: '商品SKU ID', + }, quantity: { type: INTEGER(10).UNSIGNED, allowNull: false, @@ -102,11 +107,15 @@ module.exports = app => { name: 'idx_wms_picking_order_item_space', fields: [ 'order_item_id', 'space_id' ], }, + { + name: 'idx_wms_picking_warehouse_stock', + fields: [ 'warehouse_id', 'stock_id' ], + }, ], }); Picking.associate = function() { - const { OrderItems, Storage, LocationSpace } = app.model; + const { OrderItems, Storage, LocationSpace, StockWarehouse } = app.model; if (OrderItems) { Picking.belongsTo(OrderItems, { @@ -131,6 +140,14 @@ module.exports = app => { constraints: false, }); } + + if (StockWarehouse) { + Picking.belongsTo(StockWarehouse, { + foreignKey: 'stock_id', + as: 'stock', + constraints: false, + }); + } }; return Picking; diff --git a/app/model/store_warehouse.js b/app/model/store_warehouse.js index e204d38..52136f9 100644 --- a/app/model/store_warehouse.js +++ b/app/model/store_warehouse.js @@ -509,6 +509,11 @@ module.exports = app => { allowNull: true, comment: '解除人', }, + initiator_subject_id: { + type: BIGINT(20).UNSIGNED, + allowNull: true, + comment: '发起方主体ID(wms_store.id)', + }, created_at: { type: DATE, allowNull: false, diff --git a/database/migrations/20260707-add-initiator-subject-id-to-store-warehouse.js b/database/migrations/20260707-add-initiator-subject-id-to-store-warehouse.js new file mode 100644 index 0000000..9699f88 --- /dev/null +++ b/database/migrations/20260707-add-initiator-subject-id-to-store-warehouse.js @@ -0,0 +1,24 @@ +'use strict'; + +/** + * wms_store_warehouse 增加 initiator_subject_id 记录发起方主体 ID, + * 用于区分签-约发起方(与 applied_by 账号 ID 互补)。 + */ +module.exports = { + async up(queryInterface) { + const { sequelize } = queryInterface; + await sequelize.query(` + ALTER TABLE wms_store_warehouse + ADD COLUMN initiator_subject_id BIGINT(20) UNSIGNED NULL COMMENT '发起方主体ID(wms_store.id)' + AFTER ended_by + `); + }, + + async down(queryInterface) { + const { sequelize } = queryInterface; + await sequelize.query(` + ALTER TABLE wms_store_warehouse + DROP COLUMN initiator_subject_id + `); + }, +}; diff --git a/database/migrations/20260708000000-add-stock-id-to-wms-picking.js b/database/migrations/20260708000000-add-stock-id-to-wms-picking.js new file mode 100644 index 0000000..d9ac4f6 --- /dev/null +++ b/database/migrations/20260708000000-add-stock-id-to-wms-picking.js @@ -0,0 +1,34 @@ +'use strict'; + +/** + * 为 wms_picking 表新增 stock_id 字段及联合索引。 + * + * 用途:消除面单打印自定义区对 wms_order_items 明细表的强依赖, + * 使打印链路可通过 wms_picking.stock_id 直读商品信息与条码数据。 + */ +module.exports = { + up: async queryInterface => { + await queryInterface.sequelize.query(` + ALTER TABLE wms_picking + ADD COLUMN stock_id BIGINT(20) DEFAULT NULL COMMENT '商品SKU ID' AFTER storage_id, + ADD INDEX idx_wms_picking_warehouse_stock (warehouse_id, stock_id) + `); + + // 回填已有 picking 记录的 stock_id(通过 storage 链路补全) + await queryInterface.sequelize.query(` + UPDATE wms_picking p + INNER JOIN wms_storage s ON p.storage_id = s.id + SET p.stock_id = s.stock_id + WHERE p.stock_id IS NULL + AND p.storage_id IS NOT NULL + `); + }, + + down: async queryInterface => { + await queryInterface.sequelize.query(` + ALTER TABLE wms_picking + DROP INDEX idx_wms_picking_warehouse_stock, + DROP COLUMN stock_id + `); + }, +};