'use strict'; /** * 出入库单绑定货主(下属公司):inbound_orders / outbound_orders 增加 subsidiary_id。 * * 取值跟随单据仓库的货主(warehouses.subsidiary_id),历史数据按仓库回填; * 货主权限仅可见自己所属公司的出入库单,且不可越界操作。 */ const BILL_TABLES = [ 'inbound_orders', 'outbound_orders' ]; module.exports = { async up(queryInterface, Sequelize) { for (const table of BILL_TABLES) { await queryInterface.addColumn(table, 'subsidiary_id', { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: '所属货主(下属公司ID),跟随仓库', references: { model: 'principal_subsidiaries', key: 'id' }, after: 'principal_id', }); await queryInterface.addIndex(table, [ 'principal_id', 'subsidiary_id' ], { name: `idx_${table}_principal_subsidiary`, }); // 按单据仓库的货主回填历史数据 await queryInterface.sequelize.query( `UPDATE ${table} t` + ' JOIN warehouses w ON w.id = t.warehouse_id' + ' SET t.subsidiary_id = w.subsidiary_id' + ' WHERE t.subsidiary_id IS NULL AND w.subsidiary_id IS NOT NULL' ); } }, async down(queryInterface) { for (const table of BILL_TABLES) { await queryInterface.removeIndex(table, `idx_${table}_principal_subsidiary`); await queryInterface.removeColumn(table, 'subsidiary_id'); } }, };