WMS_MAIN/app/model/waybill_allocate_queue.js
liufumingren 80c73bb34b feat(order): 订单创建自动分配快递改为队列+定时批量消费,消除取号死锁
- 新增 wms_waybill_allocate_queue 队列表(migration + model),订单创建异步流程仅入队(截单/未审核不入队)

- 新增 consume_waybill_allocate_queue 定时任务:15s/worker/重入保护,按仓库+货主串行批量消费,同关系下同一时刻仅一个取号事务

- express_platform 三处结算事务统一先 FOR UPDATE wms_store_warehouse 再更新 wms_order_express(lockAndDeductWaybillSettlement),消除 AB-BA 死锁
2026-08-27 20:11:52 +08:00

103 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 订单创建后自动分配快递的消费队列模型。
*
* 订单创建后的异步流程只入队(截单/未审核不入队),由定时任务
* app/schedule/consume_waybill_allocate_queue.js 按仓库+货主串行批量消费,
* 避免高并发逐单取号时的 InnoDB AB-BA 死锁。
*
* @param {import('egg').Application} app Egg 应用实例。
* @return {import('sequelize').ModelCtor<import('sequelize').Model>} 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',
},
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;
};