'use strict'; /** * 业务单号序列表模型。 * 按「仓库 + 业务类型 + 日期」维度原子分配序号, * 取号使用 INSERT ... ON DUPLICATE KEY UPDATE current_seq = LAST_INSERT_ID(current_seq + 1), * 避免「查当日最大序号+1」在并发下的撞号问题。 * @param {import('egg').Application} app Egg 应用实例。 * @return {import('sequelize').ModelCtor} Sequelize 模型。 */ module.exports = app => { const { BIGINT, STRING, INTEGER, CHAR } = app.Sequelize; const NumberSequence = app.model.define('wms_number_sequence', { id: { type: BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, 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: '当前已分配序号(原子递增)', }, }, { tableName: 'wms_number_sequence', comment: '业务单号序列表(原子取号)', timestamps: true, createdAt: 'created_at', updatedAt: 'updated_at', paranoid: false, indexes: [ { name: 'uk_warehouse_biz_date', unique: true, fields: [ 'warehouse_id', 'biz_type', 'date_part' ] }, ], }); return NumberSequence; };