'use strict'; /** * 创建业务单号序列表 wms_number_sequence。 * * 用途:按「仓库 + 业务类型 + 日期」维度原子分配单号序号, * 取代「查当日最大序号+1」的并发撞号方案。 * 取号 SQL:INSERT ... ON DUPLICATE KEY UPDATE current_seq = LAST_INSERT_ID(current_seq + 1) * (同连接内再 SELECT LAST_INSERT_ID() 取回本次分配序号)。 * * 索引:uk_warehouse_biz_date(仓库+业务类型+日期 唯一,原子取号的幂等键)。 */ const TABLE_NAME = 'wms_number_sequence'; module.exports = { // eslint-disable-next-line no-unused-vars async up(queryInterface, Sequelize) { const { BIGINT, STRING, INTEGER, CHAR, DATE } = Sequelize; await queryInterface.createTable(TABLE_NAME, { id: { type: BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键', }, warehouse_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '仓库ID(序号隔离维度)', }, biz_type: { type: STRING(30), allowNull: false, comment: '业务类型,如 return_unpack(退回拆包单号)', }, date_part: { type: CHAR(8), allowNull: false, comment: '日期片段 YYYYMMDD', }, current_seq: { type: INTEGER(11), allowNull: false, defaultValue: 0, comment: '当前已分配序号(原子递增)', }, created_at: { type: DATE, allowNull: false }, updated_at: { type: DATE, allowNull: false }, }); await queryInterface.addIndex(TABLE_NAME, [ 'warehouse_id', 'biz_type', 'date_part' ], { unique: true, name: 'uk_warehouse_biz_date', }); }, // eslint-disable-next-line no-unused-vars async down(queryInterface, Sequelize) { await queryInterface.dropTable(TABLE_NAME); }, };