WMS_MAIN/app/schedule/wave_strategy_quick_scan_fallback.js
liufumingren b2ae5683e1 feat(order): 波次/拣货/复核透传货主信息 + 取消订单过滤
- wave: 透传 store_id/owner_code/store_remark

- recheckOrder: 波次视图新增 store_id/store_remark,批量查询货主备注

- pick: 分组聚合透传 store_id/owner_code/store_remark/stock

- order_storage_allocation: 储位分配注入 stock_id + 回传排除取消订单

- order_async: 标记异常排除取消状态订单
2026-07-27 03:48:45 +08:00

139 lines
4.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';
const FALLBACK_TASK_NAME = 'wave_strategy_quick_scan_fallback';
module.exports = {
schedule: {
interval: '10m',
type: 'worker',
immediate: false,
disable: false,
},
/**
* 执行波次策略快速扫描兜底任务。
*
* @param {Object} ctx Egg 上下文。
* @return {Promise<void>} 无返回值。
*/
async task(ctx) {
if (ctx.app._waveStrategyQuickScanFallbackRunning) {
ctx.logger.info(`[${FALLBACK_TASK_NAME}] 任务仍在运行中,跳过本轮调度`);
return;
}
ctx.app._waveStrategyQuickScanFallbackRunning = true;
let totalWarehouseCount = 0;
let submitSuccessCount = 0;
let reusedRunningTaskCount = 0;
let submitFailedCount = 0;
try {
const storeRows = await ctx.model.Store.findAll({
attributes: [ 'id' ],
where: {
status: 1,
project: 'warehouse',
},
raw: true,
});
const warehouseIds = Array.isArray(storeRows)
? storeRows.map(item => Number(item && item.id || 0)).filter(item => Number.isInteger(item) && item > 0)
: [];
totalWarehouseCount = warehouseIds.length;
for (const warehouseId of warehouseIds) {
try {
const submitPayload = await ctx.service.systemMethods.order.wave.submitWaveStrategyQuickScanForWarehouse(warehouseId, { wait: true, forceRescan: true });
const submitMessage = String(submitPayload && submitPayload.message || '').trim();
const submitState = String(submitPayload && submitPayload.state || '').trim();
const isReusedRunningTask = submitMessage.includes('已有快速扫描任务在运行中')
|| [ 'running', 'submitted' ].includes(submitState) && submitMessage.includes('运行中');
if (isReusedRunningTask) {
reusedRunningTaskCount += 1;
} else {
submitSuccessCount += 1;
}
} catch (error) {
submitFailedCount += 1;
const errorMessage = error && error.message ? error.message : `${error}`;
ctx.logger.warn(`[${FALLBACK_TASK_NAME}] 仓库触发失败`, {
warehouseId,
error: errorMessage,
});
const noticeService = ctx.service
&& ctx.service.systems
&& ctx.service.systems.notice
&& typeof ctx.service.systems.notice.send === 'function'
? ctx.service.systems.notice
: (ctx.service
&& ctx.service.systems
&& ctx.service.systems.notice
&& ctx.service.systems.notice.index
&& typeof ctx.service.systems.notice.index.send === 'function'
? ctx.service.systems.notice.index
: null);
if (!noticeService) {
ctx.logger.warn(`[${FALLBACK_TASK_NAME}] 通知服务不可用,跳过失败通知`, {
warehouseId,
error: errorMessage,
});
continue;
}
try {
await noticeService.send({
event: 'wave_strategy_quick_scan_failed',
businessName: 'order',
warehouseId,
subjectId: warehouseId,
subject: {
id: warehouseId,
type: 'warehouse',
},
title: `波次策略兜底扫描触发失败(仓库 ${warehouseId})`,
content: `兜底扫描任务触发失败,仓库 ${warehouseId},错误:${errorMessage}`,
data: {
warehouse_id: warehouseId,
error: errorMessage,
task: FALLBACK_TASK_NAME,
},
metadata: {
warehouse_id: warehouseId,
error: errorMessage,
task: FALLBACK_TASK_NAME,
},
sender: {
id: 0,
name: 'system',
},
});
} catch (noticeError) {
ctx.logger.warn(`[${FALLBACK_TASK_NAME}] 发送失败通知异常`, {
warehouseId,
error: noticeError && noticeError.message ? noticeError.message : `${noticeError}`,
});
}
}
}
ctx.logger.info(`[${FALLBACK_TASK_NAME}] 任务完成`, {
totalWarehouseCount,
submitSuccessCount,
reusedRunningTaskCount,
submitFailedCount,
});
} catch (error) {
ctx.logger.error(`[${FALLBACK_TASK_NAME}] 任务执行失败: ${error && error.message ? error.message : error}`);
} finally {
ctx.app._waveStrategyQuickScanFallbackRunning = false;
}
},
};