'use strict'; /** * 获取当前数据库已存在的表名集合。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise>} 表名集合。 */ const getExistingTableSet = async queryInterface => { const tables = await queryInterface.showAllTables(); return new Set( tables.map(table => (typeof table === 'string' ? table : table.tableName)) ); }; /** * 创建库位维度出入库流水表。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {import('sequelize')} Sequelize Sequelize 实例。 * @return {Promise} 无返回值。 */ const createStorageTransactionTable = async (queryInterface, Sequelize) => { await queryInterface.createTable('wms_storage_transaction', { id: { type: Sequelize.BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键ID', }, transaction_type: { type: Sequelize.ENUM('INBOUND', 'OUTBOUND', 'TRANSFER_OUT', 'TRANSFER_IN'), allowNull: false, comment: '出入库类型', }, warehouse_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '仓库ID', }, store_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '货主ID', }, stock_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '商品库存ID,关联 wms_stock_warehouse.id', }, storage_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, comment: '库位库存ID,关联 wms_storage.id', }, space_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, comment: '库位ID', }, quantity: { type: Sequelize.INTEGER(12), allowNull: false, comment: '变动数量(正数)', }, before_quantity: { type: Sequelize.INTEGER(12), allowNull: true, comment: '变更前总库存数量', }, after_quantity: { type: Sequelize.INTEGER(12), allowNull: true, comment: '变更后总库存数量', }, before_locked_quantity: { type: Sequelize.INTEGER(12), allowNull: true, comment: '变更前锁定数量', }, after_locked_quantity: { type: Sequelize.INTEGER(12), allowNull: true, comment: '变更后锁定数量', }, batch_no: { type: Sequelize.STRING(100), allowNull: true, comment: '批次号', }, inventory_type: { type: Sequelize.ENUM('ZP', 'CC', 'JS', 'XS', 'ZT', 'DJ', 'FP'), allowNull: true, defaultValue: 'ZP', comment: '库存类型', }, order_type: { type: Sequelize.STRING(50), allowNull: true, comment: '关联单据类型', }, order_no: { type: Sequelize.TEXT, allowNull: true, comment: '关联单据号', }, order_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, comment: '关联单据ID', }, source_order_codes: { type: Sequelize.TEXT, allowNull: true, comment: '来源订单号集合', }, operator_id: { type: Sequelize.STRING(100), allowNull: true, comment: '操作人ID', }, operator_name: { type: Sequelize.STRING(100), allowNull: true, comment: '操作人名称', }, remark: { type: Sequelize.STRING(500), allowNull: true, comment: '备注', }, created_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, }, { charset: 'utf8mb4', collate: 'utf8mb4_general_ci', comment: '库位维度出入库流水记录表', }); await queryInterface.addIndex('wms_storage_transaction', [ 'storage_id' ], { name: 'idx_storage_id', }); await queryInterface.addIndex('wms_storage_transaction', [ 'stock_id' ], { name: 'idx_stock_id', }); await queryInterface.addIndex('wms_storage_transaction', [ 'warehouse_id', 'created_at' ], { name: 'idx_warehouse_time', }); await queryInterface.addIndex('wms_storage_transaction', [ 'order_type', 'order_id' ], { name: 'idx_order', }); await queryInterface.addIndex('wms_storage_transaction', [ 'created_at' ], { name: 'idx_created_at', }); }; /** * 新增库位维度出入库流水表。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {import('sequelize')} Sequelize Sequelize 实例。 * @return {Promise} 无返回值。 */ module.exports.up = async (queryInterface, Sequelize) => { const tableSet = await getExistingTableSet(queryInterface); if (tableSet.has('wms_storage_transaction')) { return; } await createStorageTransactionTable(queryInterface, Sequelize); }; /** * 回滚删除库位维度出入库流水表。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise} 无返回值。 */ module.exports.down = async queryInterface => { const tableSet = await getExistingTableSet(queryInterface); if (!tableSet.has('wms_storage_transaction')) { return; } await queryInterface.dropTable('wms_storage_transaction'); };