// database/migrations/20241201000009-create-wms-entry-order.js 'use strict'; module.exports = { async up(queryInterface, Sequelize) { const { BIGINT, STRING, INTEGER, DATE, ENUM, TEXT, TINYINT } = Sequelize; // 先检查表是否存在,避免重复创建 const [ tables ] = await queryInterface.sequelize.query( "SHOW TABLES LIKE 'wms_entry_order'" ); if (tables.length > 0) { console.log('Table wms_entry_order already exists, skipping creation'); return; } await queryInterface.createTable('wms_entry_order', { id: { type: BIGINT(20), primaryKey: true, autoIncrement: true, allowNull: false, comment: '入库单ID', }, entry_order_code: { type: STRING(50), allowNull: false, comment: '入库单号', }, order_type: { type: ENUM('SCRK', 'LYRK', 'CCRK', 'CGRK', 'DBRK', 'QTRK', 'B2BRK', 'XNRK'), allowNull: false, comment: '业务类型', }, purchase_order_code: { type: STRING(50), allowNull: true, comment: '采购单号', }, total_order_lines: { type: INTEGER(11), allowNull: true, defaultValue: 0, comment: '单据总行数', }, order_create_time: { type: DATE, allowNull: true, comment: '订单创建时间', }, expect_start_time: { type: DATE, allowNull: true, comment: '预期到货时间', }, expect_end_time: { type: DATE, allowNull: true, comment: '最迟预期到货时间', }, logistics_code: { type: STRING(50), allowNull: true, comment: '物流公司编码', }, logistics_name: { type: STRING(200), allowNull: true, comment: '物流公司名称', }, express_code: { type: STRING(50), allowNull: true, comment: '运单号', }, supplier_code: { type: STRING(50), allowNull: true, comment: '供应商编码', }, supplier_name: { type: STRING(200), allowNull: true, comment: '供应商名称', }, source_type: { type: ENUM('WMS', 'API', 'QM'), allowNull: true, defaultValue: 'API', comment: '来源类型', }, order_status: { type: ENUM('PENDING', 'CONFIRMED', 'PARTIAL_RECEIVED', 'COMPLETED', 'CANCELLED'), allowNull: true, defaultValue: 'PENDING', comment: '订单状态', }, sync_status: { type: ENUM('PENDING', 'SUCCESS', 'FAILED'), allowNull: true, defaultValue: 'PENDING', comment: '同步状态', }, sync_message: { type: TEXT, allowNull: true, comment: '同步返回信息', }, is_enabled: { type: TINYINT(1), allowNull: true, defaultValue: 1, comment: '是否启用', }, created_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, updated_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), comment: '更新时间', }, warehouse_id: { type: BIGINT(20), allowNull: false, references: { model: 'wms_store', key: 'id', }, onUpdate: 'NO ACTION', onDelete: 'NO ACTION', comment: '仓库ID', }, store_id: { type: BIGINT(20), allowNull: false, references: { model: 'wms_store', key: 'id', }, onUpdate: 'NO ACTION', onDelete: 'NO ACTION', comment: '货主ID', }, remark: { type: STRING(500), allowNull: true, comment: 'ERP备注', }, store_name: { type: STRING(200), allowNull: true, comment: '货主名称快照', }, warehouse_name: { type: STRING(200), allowNull: true, comment: '仓库名称快照', }, }, { engine: 'InnoDB', charset: 'utf8mb4', collate: 'utf8mb4_unicode_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_entry_order', [ 'entry_order_code', 'warehouse_id', 'store_id' ], { name: 'uk_entry_order_code', unique: true, }); // 添加普通索引 await safeCreateIndex('wms_entry_order', [ 'order_type' ], { name: 'idx_order_type', }); await safeCreateIndex('wms_entry_order', [ 'order_status' ], { name: 'idx_order_status', }); await safeCreateIndex('wms_entry_order', [ 'sync_status' ], { name: 'idx_sync_status', }); await safeCreateIndex('wms_entry_order', [ 'source_type' ], { name: 'idx_source', }); await safeCreateIndex('wms_entry_order', [ 'order_create_time' ], { name: 'idx_create_time', }); await safeCreateIndex('wms_entry_order', [ 'warehouse_id' ], { name: 'wms_entry_order_ibfk_1', }); await safeCreateIndex('wms_entry_order', [ 'store_id' ], { name: 'wms_entry_order_ibfk_2', }); await safeCreateIndex('wms_entry_order', [ 'purchase_order_code' ], { name: 'idx_purchase_order', }); await safeCreateIndex('wms_entry_order', [ 'supplier_code' ], { name: 'idx_supplier_code', }); // 添加复合索引 await safeCreateIndex('wms_entry_order', [ 'warehouse_id', 'order_status' ], { name: 'idx_warehouse_status', }); await safeCreateIndex('wms_entry_order', [ 'store_id', 'order_status' ], { name: 'idx_store_status', }); await safeCreateIndex('wms_entry_order', [ 'order_create_time', 'order_status' ], { name: 'idx_create_time_status', }); }, async down(queryInterface) { // 安全删除表 const [ tables ] = await queryInterface.sequelize.query( "SHOW TABLES LIKE 'wms_entry_order'" ); if (tables.length > 0) { await queryInterface.dropTable('wms_entry_order'); } }, };