'use strict'; /** * 获取表字段定义。 * * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {string} tableName 表名。 * @return {Promise} 字段定义对象。 */ const getTableColumns = async (queryInterface, tableName) => { return queryInterface.describeTable(tableName); }; /** * 返回需要补齐的字段定义映射。 * * @param {typeof import('sequelize')} Sequelize Sequelize 类型集合。 * @return {Record>} 表字段映射。 */ const getRepairColumnDefinitions = Sequelize => ({ wms_wave_pick_task: { wave_count: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '波次数量', }, order_count: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '订单数量', }, box_count: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '框数量', }, batch_no: { type: Sequelize.STRING(64), allowNull: true, comment: '领单批次号', }, pick_user_name: { type: Sequelize.STRING(64), allowNull: true, comment: '拣货员名称', }, review_user_name: { type: Sequelize.STRING(64), allowNull: true, comment: '复核员名称', }, current_cart_id: { type: Sequelize.BIGINT, allowNull: true, comment: '当前绑定拣货车ID', }, current_cart_code_snapshot: { type: Sequelize.STRING(64), allowNull: true, comment: '当前绑定车码快照', }, review_status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 0, comment: '复核状态:0-未就绪 1-待复核 2-复核中 3-复核通过 4-复核异常', }, review_version: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '复核并发版本号', }, review_start_time: { type: Sequelize.DATE, allowNull: true, comment: '复核开始时间', }, review_finish_time: { type: Sequelize.DATE, allowNull: true, comment: '复核结束时间', }, review_result: { type: Sequelize.TINYINT, allowNull: true, comment: '复核结果:1-通过 2-异常', }, review_remark: { type: Sequelize.STRING(255), allowNull: true, comment: '复核备注', }, device_id: { type: Sequelize.STRING(64), allowNull: true, comment: '设备ID', }, expire_time: { type: Sequelize.DATE, allowNull: true, comment: '任务过期时间', }, transfer_from_task_id: { type: Sequelize.BIGINT, allowNull: true, comment: '转移来源任务ID', }, transfer_to_task_id: { type: Sequelize.BIGINT, allowNull: true, comment: '转移目标任务ID', }, }, wms_wave_pick_task_wave: { wave_no: { type: Sequelize.STRING(64), allowNull: true, comment: '波次号', }, box_no: { type: Sequelize.INTEGER, allowNull: true, comment: '框号', }, status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 0, comment: '波次状态:0-待拣 1-已拣 2-异常', }, bind_time: { type: Sequelize.DATE, allowNull: true, comment: '绑定时间', }, pick_time: { type: Sequelize.DATE, allowNull: true, comment: '拣货完成时间', }, }, wms_wave_pick_task_order: { order_no: { type: Sequelize.STRING(64), allowNull: true, comment: '订单号', }, box_no: { type: Sequelize.INTEGER, allowNull: true, comment: '框号', }, cart_id: { type: Sequelize.BIGINT, allowNull: true, comment: '拣货车ID', }, cart_code_snapshot: { type: Sequelize.STRING(64), allowNull: true, comment: '拣货车码快照', }, review_user_id: { type: Sequelize.BIGINT, allowNull: true, comment: '复核员ID', }, review_result: { type: Sequelize.TINYINT, allowNull: true, comment: '复核结果:1-通过 2-异常', }, review_remark: { type: Sequelize.STRING(255), allowNull: true, comment: '复核备注', }, exception_reason: { type: Sequelize.STRING(255), allowNull: true, comment: '异常原因', }, }, wms_wave_pick_task_box: { box_no: { type: Sequelize.INTEGER, allowNull: true, comment: '框号', }, box_type: { type: Sequelize.TINYINT, allowNull: true, comment: '框类型:1-SKU框 2-订单框 3-波次框', }, ref_id: { type: Sequelize.BIGINT, allowNull: true, comment: '关联ID(SKU或订单或波次)', }, ref_label: { type: Sequelize.STRING(128), allowNull: true, comment: '关联展示标签', }, required_qty: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '应放数量', }, picked_qty: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '已放数量', }, }, wms_pick_cart: { last_seen_time: { type: Sequelize.DATE, allowNull: true, comment: '最近扫码时间', }, }, wms_pick_task_cart_binding: { cart_code_snapshot: { type: Sequelize.STRING(64), allowNull: true, comment: '车码快照', }, bind_type: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '绑定类型:1-领单绑定 2-换车绑定 3-管理员改绑', }, }, wms_order_pick_trace: { cart_id: { type: Sequelize.BIGINT, allowNull: true, comment: '拣货车ID', }, cart_code_snapshot: { type: Sequelize.STRING(64), allowNull: true, comment: '拣货车码快照', }, pick_user_id: { type: Sequelize.BIGINT, allowNull: true, comment: '拣货员ID', }, pick_time: { type: Sequelize.DATE, allowNull: true, comment: '拣货时间', }, review_user_id: { type: Sequelize.BIGINT, allowNull: true, comment: '复核员ID', }, review_time: { type: Sequelize.DATE, allowNull: true, comment: '复核时间', }, review_result: { type: Sequelize.TINYINT, allowNull: true, comment: '复核结果:1-通过 2-异常', }, }, }); /** * 按字段映射执行幂等新增列。 * * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {Record>} repairColumnDefinitions 表字段映射。 * @return {Promise} 无返回值。 */ const addMissingColumns = async (queryInterface, repairColumnDefinitions) => { const tableNames = Object.keys(repairColumnDefinitions); for (const tableName of tableNames) { const columnsBefore = await getTableColumns(queryInterface, tableName); const columnEntries = Object.entries(repairColumnDefinitions[tableName]); for (const [ columnName, columnDefinition ] of columnEntries) { if (!columnsBefore[columnName]) { await queryInterface.addColumn(tableName, columnName, columnDefinition); } } } }; /** * 按字段映射执行幂等删除列。 * * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {Record>} repairColumnDefinitions 表字段映射。 * @return {Promise} 无返回值。 */ const removeExistingColumns = async (queryInterface, repairColumnDefinitions) => { const tableNames = Object.keys(repairColumnDefinitions); for (const tableName of tableNames) { const columnsBefore = await getTableColumns(queryInterface, tableName); const columnEntries = Object.keys(repairColumnDefinitions[tableName]); for (const columnName of columnEntries) { if (columnsBefore[columnName]) { await queryInterface.removeColumn(tableName, columnName); } } } }; module.exports = { /** * 正向迁移:修复拣货任务相关表的字段漂移问题。 * * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {typeof import('sequelize')} Sequelize Sequelize 类型集合。 * @return {Promise} 无返回值。 */ up: async (queryInterface, Sequelize) => { const repairColumnDefinitions = getRepairColumnDefinitions(Sequelize); await addMissingColumns(queryInterface, repairColumnDefinitions); }, /** * 回滚迁移:回退本迁移新增的字段(仅在字段存在时删除)。 * * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {typeof import('sequelize')} Sequelize Sequelize 类型集合。 * @return {Promise} 无返回值。 */ down: async (queryInterface, Sequelize) => { const repairColumnDefinitions = getRepairColumnDefinitions(Sequelize); await removeExistingColumns(queryInterface, repairColumnDefinitions); }, };