'use strict'; const TABLE_NAME = 'wms_after_sale_work_order_step'; 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', }, work_order_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '工单ID,关联 wms_after_sale_work_order.id', }, step_order: { type: Sequelize.TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '步骤序号(从1开始递增)', }, step_name: { type: Sequelize.STRING(100), allowNull: false, comment: '步骤名称', }, step_status: { type: Sequelize.TINYINT.UNSIGNED, allowNull: false, defaultValue: 0, comment: '步骤状态:0=待开始 1=进行中 2=已完成 3=已跳过', }, operator_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, comment: '操作员工ID,关联 account.id', }, operator_snapshot: { type: Sequelize.TEXT, allowNull: true, comment: '操作人信息快照(JSON格式)', }, action: { type: Sequelize.STRING(100), allowNull: true, comment: '操作动作描述', }, remark: { type: Sequelize.TEXT, allowNull: true, comment: '处理备注', }, started_at: { type: Sequelize.DATE, allowNull: true, comment: '步骤开始时间', }, completed_at: { type: Sequelize.DATE, allowNull: true, 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: '软删除时间', }, }); // 按工单查所有步骤 await queryInterface.addIndex(TABLE_NAME, ['work_order_id'], { name: 'idx_work_order_id', }); // 按操作人反查 await queryInterface.addIndex(TABLE_NAME, ['operator_id'], { name: 'idx_operator_id', }); // 软删除查询优化 await queryInterface.addIndex(TABLE_NAME, ['deleted_at'], { name: 'idx_deleted_at', }); // 同一工单下步骤序号唯一(含软删除) await queryInterface.addIndex(TABLE_NAME, ['work_order_id', 'step_order', 'deleted_at'], { unique: true, name: 'uk_work_order_step', }); }, down: async queryInterface => { await queryInterface.dropTable(TABLE_NAME); }, };