'use strict'; const TABLE_NAME = 'wms_stock_static_check_task'; module.exports = { up: async (queryInterface, Sequelize) => { const tables = await queryInterface.showAllTables(); const exists = tables.some(t => (typeof t === 'string' ? t : t.tableName) === TABLE_NAME); if (exists) return; await queryInterface.createTable(TABLE_NAME, { id: { type: Sequelize.BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键ID', }, stock_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '商品ID,关联 wms_stock_warehouse.id', }, task_time: { type: Sequelize.DATE, allowNull: false, comment: '创建盘点任务时间(有记录即表示已为该静态商品创建过盘点任务)', }, created_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, updated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), comment: '更新时间', }, deleted_at: { type: Sequelize.DATE, allowNull: true, comment: '软删除时间', }, }, { comment: '静态商品盘点任务关联表(记录是否已为静态商品创建盘点任务,避免重复创建)', }); // 核心查询:查某商品最近一次创建盘点任务时间 await queryInterface.addIndex(TABLE_NAME, [ 'stock_id', 'task_time' ], { name: 'idx_stock_task_time', }); // 时间范围扫描:按任务创建时间清理/统计 await queryInterface.addIndex(TABLE_NAME, [ 'task_time' ], { name: 'idx_task_time', }); }, down: async queryInterface => { await queryInterface.dropTable(TABLE_NAME); }, };