'use strict'; const ORDER_ITEM_SN_TABLE = 'wms_order_item_sn'; const ORDER_ITEM_SN_LOG_TABLE = 'wms_order_item_sn_log'; /** * 获取当前数据库已存在的表名集合。 * @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 {string} tableName 表名。 * @return {Promise>} 索引名集合。 */ const getIndexNameSet = async (queryInterface, tableName) => { const indexes = await queryInterface.showIndex(tableName); return new Set(indexes.map(index => String(index && index.name ? index.name : '').trim()).filter(Boolean)); }; /** * 创建订单明细 SN 当前关系表。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {import('sequelize')} Sequelize Sequelize 实例。 * @return {Promise} 无返回值。 */ const createOrderItemSnTable = async (queryInterface, Sequelize) => { await queryInterface.createTable(ORDER_ITEM_SN_TABLE, { id: { type: Sequelize.BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键ID', }, warehouse_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '仓库ID', }, order_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '订单ID', }, order_item_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '订单明细ID', }, sn_code: { type: Sequelize.STRING(128), allowNull: false, comment: 'SN编码', }, sn_status: { type: Sequelize.STRING(32), allowNull: false, defaultValue: 'BOUND', comment: 'SN状态:BOUND/VERIFIED/RETURNED/REISSUED/INVALID', }, verify_status: { type: Sequelize.STRING(32), allowNull: false, defaultValue: 'UNVERIFIED', comment: '核验状态:UNVERIFIED/PASS/FAIL', }, bound_at: { type: Sequelize.DATE, allowNull: true, comment: '绑定时间', }, verified_at: { type: Sequelize.DATE, allowNull: true, comment: '核验时间', }, returned_at: { type: Sequelize.DATE, allowNull: true, comment: '退回时间', }, reissued_at: { type: Sequelize.DATE, allowNull: true, comment: '重发时间', }, version: { type: Sequelize.INTEGER.UNSIGNED, allowNull: false, defaultValue: 1, 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: '更新时间', }, }, { engine: 'InnoDB', charset: 'utf8mb4', collate: 'utf8mb4_general_ci', comment: '订单明细SN当前关系与状态表', }); }; /** * 创建订单明细 SN 状态变更流水表。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {import('sequelize')} Sequelize Sequelize 实例。 * @return {Promise} 无返回值。 */ const createOrderItemSnLogTable = async (queryInterface, Sequelize) => { await queryInterface.createTable(ORDER_ITEM_SN_LOG_TABLE, { id: { type: Sequelize.BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键ID', }, warehouse_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '仓库ID', }, sn_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, comment: '关联SN主表ID', }, order_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '订单ID', }, order_item_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '订单明细ID', }, sn_code: { type: Sequelize.STRING(128), allowNull: false, comment: 'SN编码', }, from_sn_status: { type: Sequelize.STRING(32), allowNull: true, comment: '变更前SN状态', }, to_sn_status: { type: Sequelize.STRING(32), allowNull: false, comment: '变更后SN状态', }, verify_status: { type: Sequelize.STRING(32), allowNull: true, comment: '本次状态变更后的核验状态', }, action_type: { type: Sequelize.STRING(32), allowNull: false, comment: '动作类型:BIND/VERIFY/RETURN/REISSUE/UNBIND', }, remark: { type: Sequelize.STRING(255), allowNull: true, comment: '备注', }, operator_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, comment: '操作人ID', }, operated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), 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: '更新时间', }, }, { engine: 'InnoDB', charset: 'utf8mb4', collate: 'utf8mb4_general_ci', comment: '订单明细SN状态变更流水表', }); }; /** * 确保订单明细 SN 当前关系表索引存在。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise} 无返回值。 */ const ensureOrderItemSnIndexes = async queryInterface => { const indexNameSet = await getIndexNameSet(queryInterface, ORDER_ITEM_SN_TABLE); if (!indexNameSet.has('uk_wms_order_item_sn_warehouse_sn_code')) { await queryInterface.addIndex(ORDER_ITEM_SN_TABLE, [ 'warehouse_id', 'sn_code' ], { name: 'uk_wms_order_item_sn_warehouse_sn_code', unique: true, }); } if (!indexNameSet.has('idx_wms_order_item_sn_warehouse_id')) { await queryInterface.addIndex(ORDER_ITEM_SN_TABLE, [ 'warehouse_id' ], { name: 'idx_wms_order_item_sn_warehouse_id', }); } if (!indexNameSet.has('idx_wms_order_item_sn_order_id')) { await queryInterface.addIndex(ORDER_ITEM_SN_TABLE, [ 'order_id' ], { name: 'idx_wms_order_item_sn_order_id', }); } if (!indexNameSet.has('idx_wms_order_item_sn_order_item_id')) { await queryInterface.addIndex(ORDER_ITEM_SN_TABLE, [ 'order_item_id' ], { name: 'idx_wms_order_item_sn_order_item_id', }); } }; /** * 确保订单明细 SN 流水表索引存在。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise} 无返回值。 */ const ensureOrderItemSnLogIndexes = async queryInterface => { const indexNameSet = await getIndexNameSet(queryInterface, ORDER_ITEM_SN_LOG_TABLE); if (!indexNameSet.has('idx_wms_order_item_sn_log_sn_id')) { await queryInterface.addIndex(ORDER_ITEM_SN_LOG_TABLE, [ 'sn_id' ], { name: 'idx_wms_order_item_sn_log_sn_id', }); } if (!indexNameSet.has('idx_wms_order_item_sn_log_sn_code')) { await queryInterface.addIndex(ORDER_ITEM_SN_LOG_TABLE, [ 'sn_code' ], { name: 'idx_wms_order_item_sn_log_sn_code', }); } if (!indexNameSet.has('idx_wms_order_item_sn_log_order_id')) { await queryInterface.addIndex(ORDER_ITEM_SN_LOG_TABLE, [ 'order_id' ], { name: 'idx_wms_order_item_sn_log_order_id', }); } if (!indexNameSet.has('idx_wms_order_item_sn_log_order_item_id')) { await queryInterface.addIndex(ORDER_ITEM_SN_LOG_TABLE, [ 'order_item_id' ], { name: 'idx_wms_order_item_sn_log_order_item_id', }); } }; module.exports = { /** * 创建订单明细 SN 主表与流水表及索引。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {import('sequelize')} Sequelize Sequelize 实例。 * @return {Promise} 无返回值。 */ async up(queryInterface, Sequelize) { const existingTables = await getExistingTableSet(queryInterface); if (!existingTables.has(ORDER_ITEM_SN_TABLE)) { await createOrderItemSnTable(queryInterface, Sequelize); } if (!existingTables.has(ORDER_ITEM_SN_LOG_TABLE)) { await createOrderItemSnLogTable(queryInterface, Sequelize); } await ensureOrderItemSnIndexes(queryInterface); await ensureOrderItemSnLogIndexes(queryInterface); }, /** * 回滚删除订单明细 SN 主表与流水表。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {import('sequelize')} Sequelize Sequelize 实例。 * @return {Promise} 无返回值。 */ async down(queryInterface, Sequelize) { void Sequelize; const existingTables = await getExistingTableSet(queryInterface); if (existingTables.has(ORDER_ITEM_SN_LOG_TABLE)) { await queryInterface.dropTable(ORDER_ITEM_SN_LOG_TABLE); } if (existingTables.has(ORDER_ITEM_SN_TABLE)) { await queryInterface.dropTable(ORDER_ITEM_SN_TABLE); } }, };