'use strict'; /** * 订单创建后自动分配快递的消费队列模型。 * * 订单创建后的异步流程只入队(截单/未审核不入队),由定时任务 * app/schedule/consume_waybill_allocate_queue.js 按仓库+货主串行批量消费, * 避免高并发逐单取号时的 InnoDB AB-BA 死锁。 * * @param {import('egg').Application} app Egg 应用实例。 * @return {import('sequelize').ModelCtor} Sequelize 模型。 */ module.exports = app => { const { BIGINT, INTEGER, STRING, DATE } = app.Sequelize; const WaybillAllocateQueue = app.model.define('waybill_allocate_queue', { id: { type: BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键', }, order_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '订单ID(关联订单分表)', }, business_key: { type: INTEGER(3), allowNull: false, defaultValue: 1, comment: '业务键(与 wms_order_express.business_key 一致)', }, warehouse_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '仓库ID(消费分组与数据隔离维度)', }, store_id: { type: BIGINT(20).UNSIGNED, allowNull: false, defaultValue: 0, comment: '货主ID(消费分组维度)', }, status: { type: STRING(20), allowNull: false, defaultValue: 'pending', comment: '状态:pending/processing/done/failed/waiting_oms(waiting_oms=需要回 OMS 取号,等待外部接入,终态不重试)', }, retry_count: { type: INTEGER(3), allowNull: false, defaultValue: 0, comment: '重试次数', }, next_retry_at: { type: DATE, allowNull: true, comment: '下次可消费时间(失败退避)', }, error_message: { type: STRING(500), allowNull: true, comment: '最近一次失败原因', }, created_at: { type: DATE, allowNull: false, comment: '创建时间', }, updated_at: { type: DATE, allowNull: false, comment: '更新时间', }, }, { tableName: 'wms_waybill_allocate_queue', freezeTableName: true, underscored: true, timestamps: false, comment: '订单创建后自动分配快递消费队列', indexes: [ { name: 'uk_order_id_business_key', unique: true, fields: [ 'order_id', 'business_key' ], }, { name: 'idx_status_id', fields: [ 'status', 'id' ], }, { name: 'idx_status_updated_at', fields: [ 'status', 'updated_at' ], }, ], }); return WaybillAllocateQueue; };