'use strict'; module.exports = { async up(queryInterface, Sequelize) { const { BIGINT, STRING, INTEGER, ENUM, DATE } = Sequelize; const [ tables ] = await queryInterface.sequelize.query( "SHOW TABLES LIKE 'wms_stock_transaction'" ); if (tables.length > 0) { console.log('Table wms_stock_transaction already exists, skipping creation'); return; } await queryInterface.createTable('wms_stock_transaction', { id: { type: BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键ID', }, transaction_type: { type: ENUM('INBOUND', 'OUTBOUND'), allowNull: false, comment: '出入库类型', }, warehouse_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '仓库ID', }, store_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '货主ID', }, stock_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '商品库存ID,关联 wms_stock_warehouse.id', }, storage_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '库位库存ID,关联 wms_storage.id', }, space_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '库位ID', }, quantity: { type: INTEGER(12), allowNull: false, comment: '变动数量(正数)', }, before_quantity: { type: INTEGER(12), allowNull: true, comment: '变更前总库存数量', }, after_quantity: { type: INTEGER(12), allowNull: true, comment: '变更后总库存数量', }, before_locked_quantity: { type: INTEGER(12), allowNull: true, comment: '变更前锁定数量', }, after_locked_quantity: { type: INTEGER(12), allowNull: true, comment: '变更后锁定数量', }, batch_no: { type: STRING(100), allowNull: true, comment: '批次号', }, inventory_type: { type: ENUM('ZP', 'CC', 'JS', 'XS', 'ZT', 'DJ', 'FP'), defaultValue: 'ZP', comment: '库存类型', }, order_type: { type: STRING(50), allowNull: true, comment: '关联单据类型', }, order_no: { type: STRING(100), allowNull: true, comment: '关联单据号', }, order_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '关联单据ID', }, receiving_id: { type: INTEGER, allowNull: true, comment: '关联收货单ID', }, entry_order_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '关联入库单ID', }, operator_id: { type: STRING(100), allowNull: true, comment: '操作人ID', }, operator_name: { type: STRING(100), allowNull: true, comment: '操作人名称', }, remark: { type: STRING(500), allowNull: true, comment: '备注', }, created_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, }, { engine: 'InnoDB', charset: 'utf8mb4', collate: 'utf8mb4_general_ci', comment: '出入库流水记录表', }); const safeCreateIndex = async (tableName, fields, options) => { const [ existingIndexes ] = await queryInterface.sequelize.query( `SHOW INDEX FROM ${tableName} WHERE Key_name = '${options.name}'` ); if (existingIndexes.length === 0) { await queryInterface.addIndex(tableName, fields, options); } }; await safeCreateIndex('wms_stock_transaction', [ 'stock_id' ], { name: 'idx_stock_id', }); await safeCreateIndex('wms_stock_transaction', [ 'warehouse_id', 'created_at' ], { name: 'idx_warehouse_time', }); await safeCreateIndex('wms_stock_transaction', [ 'order_type', 'order_id' ], { name: 'idx_order', }); await safeCreateIndex('wms_stock_transaction', [ 'receiving_id' ], { name: 'idx_receiving_id', }); await safeCreateIndex('wms_stock_transaction', [ 'entry_order_id' ], { name: 'idx_entry_order_id', }); await safeCreateIndex('wms_stock_transaction', [ 'created_at' ], { name: 'idx_created_at', }); await safeCreateIndex('wms_stock_transaction', [ 'store_id', 'transaction_type' ], { name: 'idx_store_transaction_type', }); }, async down(queryInterface) { const [ tables ] = await queryInterface.sequelize.query( "SHOW TABLES LIKE 'wms_stock_transaction'" ); if (tables.length > 0) { await queryInterface.dropTable('wms_stock_transaction'); } }, };