feat(workbench): 波次订单实时计数短TTL缓存与拦截/打包标志
This commit is contained in:
parent
41bede5391
commit
2e29f302de
@ -4,6 +4,11 @@ const Service = require('egg').Service;
|
||||
|
||||
const { isInterceptedOrderStatus } = require('../../lib/intercept_guard');
|
||||
|
||||
/** @type {string} 工作台波次订单实时计数缓存 key 前缀(最终 key:{前缀}:{warehouseId}:{waveId}) */
|
||||
const WAVE_ORDER_COUNT_CACHE_KEY_PREFIX = 'cache:workbench:wave_order_count';
|
||||
/** @type {number} 工作台波次订单实时计数缓存 TTL(秒),TTL 内分表 COUNT 只执行一次 */
|
||||
const WAVE_ORDER_COUNT_CACHE_TTL_SECONDS = 15;
|
||||
|
||||
class WorkbenchService extends Service {
|
||||
/**
|
||||
* 查询工作台列表(仓库隔离 + 软删除过滤)
|
||||
@ -213,10 +218,15 @@ class WorkbenchService extends Service {
|
||||
}
|
||||
|
||||
/**
|
||||
* 实时统计波次下的订单数量(遍历 current + dispatch 分表)
|
||||
* @param {number} warehouseId
|
||||
* @param {number} waveId
|
||||
* @returns {Promise<number>}
|
||||
* 实时统计波次下的订单数量(并发遍历 current + dispatch 分表)
|
||||
*
|
||||
* current 与 dispatch 两张分表的 COUNT 相互独立,使用 Promise.all 并发执行,
|
||||
* 将缓存未命中时的实时统计耗时从两张表串行累加压缩为单表耗时级别;
|
||||
* 单张分表查询失败时按 0 处理,不影响另一张表的统计结果。
|
||||
*
|
||||
* @param {number} warehouseId 仓库ID
|
||||
* @param {number} waveId 波次ID
|
||||
* @return {Promise<number>} 波次下 current + dispatch 两张分表的订单总数
|
||||
*/
|
||||
async _countWaveOrders(warehouseId, waveId) {
|
||||
const { ctx } = this;
|
||||
@ -225,15 +235,56 @@ class WorkbenchService extends Service {
|
||||
{ label: 'current', getModel: () => orderDeliveryFactory.getCurrentModel(warehouseId) },
|
||||
{ label: 'dispatch', getModel: () => orderDeliveryFactory.getDispatchModel(warehouseId) },
|
||||
];
|
||||
let total = 0;
|
||||
for (const entry of tableOrders) {
|
||||
const counts = await Promise.all(tableOrders.map(async entry => {
|
||||
try {
|
||||
const model = await entry.getModel();
|
||||
if (!model) continue;
|
||||
const count = await model.count({ where: { wave_id: waveId, warehouse_id: warehouseId } });
|
||||
total += count;
|
||||
} catch (_err) { continue; }
|
||||
if (!model) return 0;
|
||||
return await model.count({ where: { wave_id: waveId, warehouse_id: warehouseId } });
|
||||
} catch (_err) { return 0; }
|
||||
}));
|
||||
return counts.reduce((total, count) => total + count, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 团购波次订单实时计数(带 Redis 短 TTL 缓存兜底)
|
||||
*
|
||||
* 分表 COUNT 无可用索引时扫描行数大、耗时长(生产实测 680~730ms/次)。
|
||||
* wave_batch.total_orders 为多处异步维护的快照值,存在漂移风险,不能直接替代实时计数,
|
||||
* 因此对实时计数结果按 (warehouseId, waveId) 维度做短 TTL 缓存:
|
||||
* TTL 内重复查询直接命中缓存,缓存不可用或读取失败时降级为实时统计。
|
||||
*
|
||||
* @param {number} warehouseId 仓库ID
|
||||
* @param {number} waveId 波次ID
|
||||
* @return {Promise<number>} 波次下 current + dispatch 两张分表的订单总数
|
||||
*/
|
||||
async _countWaveOrdersCached(warehouseId, waveId) {
|
||||
const { ctx, app } = this;
|
||||
const cacheKey = `${WAVE_ORDER_COUNT_CACHE_KEY_PREFIX}:${warehouseId}:${waveId}`;
|
||||
|
||||
if (app && typeof app.getCacheAsync === 'function') {
|
||||
try {
|
||||
const cached = await app.getCacheAsync(cacheKey);
|
||||
if (cached !== null && cached !== undefined) {
|
||||
const parsed = Number(cached);
|
||||
if (Number.isFinite(parsed) && parsed >= 0) {
|
||||
return parsed;
|
||||
}
|
||||
}
|
||||
} catch (cacheReadErr) {
|
||||
ctx.logger.warn('[Workbench._countWaveOrdersCached] 读取缓存失败,降级为实时计数: %s', cacheReadErr.message);
|
||||
}
|
||||
}
|
||||
|
||||
const total = await this._countWaveOrders(warehouseId, waveId);
|
||||
|
||||
if (app && typeof app.setCacheAsync === 'function') {
|
||||
try {
|
||||
await app.setCacheAsync(cacheKey, total, WAVE_ORDER_COUNT_CACHE_TTL_SECONDS);
|
||||
} catch (cacheWriteErr) {
|
||||
ctx.logger.warn('[Workbench._countWaveOrdersCached] 写入缓存失败,不影响本次返回: %s', cacheWriteErr.message);
|
||||
}
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
@ -1257,8 +1308,11 @@ class WorkbenchService extends Service {
|
||||
|
||||
/**
|
||||
* 条码查订单:条码 → wms_order_express → 待处理/配送订单详情
|
||||
* 散单(isSandan=1)时在响应 order 对象中额外返回 intercept_flag / pack_flag 两个 0/1 标志:
|
||||
* 拦截状态(intercept_status)不等于 none 时 intercept_flag=1,否则 0;
|
||||
* 打包状态(pack_status)不等于 none 时 pack_flag=1,否则 0。
|
||||
* @param {Object} params - { barcode, warehouseId }
|
||||
* @returns {Promise<Object>}
|
||||
* @return {Promise<Object>} 查询结果 { barcode, waybill_code, isSandan, store_remark, express, order, items, source_table, batch? }
|
||||
*/
|
||||
async _queryOrder(params) {
|
||||
const { ctx } = this;
|
||||
@ -1357,11 +1411,12 @@ class WorkbenchService extends Service {
|
||||
// wave_type=1 → 团购单(isSandan=0); 其他(2=散单等) → 散单(isSandan=1)
|
||||
if (Number(waveType) === 1) {
|
||||
isSandan = 0;
|
||||
// 仅团购单组装波次总量:实时统计带短 TTL 缓存;
|
||||
// 散单不再触发分表 COUNT,避免 680ms+ 的无用扫描
|
||||
totalBatchOrders = await this._countWaveOrdersCached(warehouseId, waveId);
|
||||
} else {
|
||||
isSandan = 1;
|
||||
}
|
||||
// 实时统计波次订单数
|
||||
totalBatchOrders = await this._countWaveOrders(warehouseId, waveId);
|
||||
}
|
||||
} catch (waveErr) {
|
||||
ctx.logger.warn('[Workbench._queryOrder] 查询波次信息失败 waveId=%d: %s', waveId, waveErr.message);
|
||||
@ -1452,6 +1507,12 @@ class WorkbenchService extends Service {
|
||||
}
|
||||
}
|
||||
|
||||
// 步骤6:散单标志位计算(拦截/打包状态空值归一化为 none,非 none 记 1,否则 0)
|
||||
const normalizedInterceptStatus = String(orderRow.intercept_status || '').trim() || 'none';
|
||||
const normalizedPackStatus = String(orderRow.pack_status || '').trim() || 'none';
|
||||
const interceptFlag = normalizedInterceptStatus === 'none' ? 0 : 1;
|
||||
const packFlag = normalizedPackStatus === 'none' ? 0 : 1;
|
||||
|
||||
const result = {
|
||||
barcode: normalizedBarcode,
|
||||
waybill_code: waybillCode,
|
||||
@ -1475,6 +1536,7 @@ class WorkbenchService extends Service {
|
||||
post_status: orderRow.post_status,
|
||||
pick_status: orderRow.pick_status,
|
||||
pack_status: orderRow.pack_status,
|
||||
intercept_status: orderRow.intercept_status,
|
||||
recheck_status: orderRow.recheck_status,
|
||||
outbound_status: orderRow.outbound_status,
|
||||
items_count: orderRow.items_count,
|
||||
@ -1489,6 +1551,12 @@ class WorkbenchService extends Service {
|
||||
source_table: sourceTable,
|
||||
};
|
||||
|
||||
// 散单附加拦截/打包 0/1 标志(原始状态字段仍保留在 order 中,不破坏既有调用方)
|
||||
if (isSandan === 1) {
|
||||
result.order.intercept_flag = interceptFlag;
|
||||
result.order.pack_flag = packFlag;
|
||||
}
|
||||
|
||||
// 团购单附加波次总量信息
|
||||
if (isSandan === 0) {
|
||||
result.batch = {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user