WMS_MAIN/app/lib/intercept_guard.js
liufumingren 0fe43c87b1 fix: 跨仓 order_express 校验与 recheckOrder 原生 SQL 表名修复
- 跨仓 bug 修复:order_express 读取强制校验订单所属仓库 id(order.detail 与全部订单列表等 17 处)

- recheckOrder 原生 SQL 表名补 wms_ 前缀,修复 queryRecheckOrderSingleList 恒 500

- 同步 methods 文档与变更记录
2026-09-10 03:46:41 +08:00

201 lines
7.8 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';
/**
* 订单拦截状态共享定义。
*
* wms_order_delivery.intercept_status 枚举(order_delivery.js / order_delivery_factory.js)实际取值为:
* none / requesting / requested / intercepting / intercepted_in_transit / intercepted_at_warehouse /
* returning / returned / delivered_by_mistake / timeout / cancelled / failed / exception。
*
* 业务约定:拦截状态只要不是 'none'(即任意进入拦截流程/拦截结果的状态),一律视为"已拦截",
* 禁止继续执行仓储作业(分配波次/分配储位/分配快递/打印/抓取波次/拣货/复核/打包/称重/绑定包材等),
* 被拦截订单一律跳过。
*/
const INTERCEPT_STATUS_NONE = 'none';
/**
* 判断订单拦截状态是否属于"已拦截"。
* 空值/NULL 归一化为 none 处理;除此之外任何非 none 状态(含 requested/intercepting/intercepted_*、
* returning/returned/cancelled/failed/exception 等)均视为已拦截。
* @param {*} status 拦截状态值。
* @return {boolean} 是否已拦截。
*/
function isInterceptedOrderStatus(status) {
const normalizedStatus = String(status || '').trim() || INTERCEPT_STATUS_NONE;
return normalizedStatus !== INTERCEPT_STATUS_NONE;
}
/** 团购波次类型。 */
const WAVE_TYPE_GROUP_BUY = 1;
/** 散单波次类型。 */
const WAVE_TYPE_SCATTER = 2;
/**
* 返回不允许写入拦截请求状态(requested / intercepting)的阻断原因文案;空字符串表示允许写入。
*
* 规则:
* - 团购波次(wave_type=1):inspection_status === 'fin'(已复核)→ 不允许写入拦截请求状态;
* - 散单波次(wave_type=2):weighing_weight > 0(已称重)→ 不允许写入拦截请求状态;
* - 其余情况(无波次 / 其他波次类型 / 未复核 / 未称重)不阻断。
*
* @param {Object} params 参数。
* @param {number|string} [params.waveType] 波次类型。
* @param {string} [params.inspectionStatus] 订单复核状态。
* @param {number|string} [params.weighingWeight] 散单称重重量。
* @return {string} 阻断原因;空字符串表示允许写入。
*/
function getInterceptRequestBlockReason({ waveType, inspectionStatus, weighingWeight } = {}) {
const normalizedWaveType = Number(waveType || 0);
if (normalizedWaveType === WAVE_TYPE_GROUP_BUY) {
return String(inspectionStatus || '').trim() === 'fin' ? '团购单已复核' : '';
}
if (normalizedWaveType === WAVE_TYPE_SCATTER) {
return Number(weighingWeight || 0) > 0 ? '散单已称重' : '';
}
return '';
}
/**
* 判断订单是否因「团购已复核 / 散单已称重」而不允许写入拦截请求状态。
*
* @param {Object} params 参数,同 getInterceptRequestBlockReason。
* @return {boolean} true 表示不允许写入拦截请求状态。
*/
function isInterceptRequestBlockedByRecheckOrWeigh(params = {}) {
return getInterceptRequestBlockReason(params) !== '';
}
/**
* 拉取单订单的拦截请求前置校验信息(波次类型 + 散单称重重量)。
*
* @param {Object} ctx Egg 上下文。
* @param {Object} params 参数。
* @param {number|string} params.orderId 订单 ID。
* @param {number|string} [params.waveId] 波次 ID。
* @param {number|string} params.warehouseId 仓库 ID。
* @param {Object} [params.transaction] 可选事务对象。
* @return {Promise<{ waveType: number, weighingWeight: number }>} 校验信息。
*/
async function resolveInterceptRequestGuardInfo(ctx, params = {}) {
const { orderId, waveId, warehouseId, transaction } = params;
let waveType = 0;
if (Number(waveId || 0) > 0 && ctx && ctx.model && ctx.model.WaveBatch) {
try {
const waveRow = await ctx.model.WaveBatch.findOne({
where: { wave_id: Number(waveId), warehouse_id: warehouseId },
attributes: [ 'wave_id', 'wave_type' ],
raw: true,
transaction,
});
waveType = Number(waveRow && waveRow.wave_type || 0);
} catch (error) { /* 查询失败不阻断主流程,按无波次处理 */ }
}
let weighingWeight = 0;
if (ctx && ctx.model && ctx.model.OrderExpress) {
try {
// 强制同仓校验:读取称重重量必须同时带订单ID与仓库ID,禁止跨仓串读
const expressRows = await ctx.model.OrderExpress.findAll({
where: {
order_id: Number(orderId || 0),
warehouse_id: warehouseId,
},
attributes: [ 'weighing_weight' ],
raw: true,
transaction,
});
for (const row of (Array.isArray(expressRows) ? expressRows : [])) {
weighingWeight = Math.max(weighingWeight, Number(row && row.weighing_weight || 0));
}
} catch (error) { /* 查询失败不阻断主流程,按未称重处理 */ }
}
return { waveType, weighingWeight };
}
/**
* 批量拉取订单的拦截请求前置校验信息(波次类型 + 散单称重重量)。
*
* @param {Object} ctx Egg 上下文。
* @param {Object} params 参数。
* @param {Array<Object>} params.orderRows 订单行数组(需含 id、wave_id 字段)。
* @param {number|string} params.warehouseId 仓库 ID。
* @param {Object} [params.transaction] 可选事务对象。
* @return {Promise<Map<number, { waveType: number, weighingWeight: number }>>} 按订单 ID 索引的校验信息。
*/
async function resolveInterceptRequestGuardInfoMap(ctx, params = {}) {
const orderRows = Array.isArray(params.orderRows) ? params.orderRows : [];
const { warehouseId, transaction } = params;
const infoMap = new Map();
if (orderRows.length === 0 || !ctx) {
return infoMap;
}
const orderIds = orderRows
.map(row => Number(row && row.id || 0))
.filter(orderId => Number.isInteger(orderId) && orderId > 0);
const waveIds = [ ...new Set(orderRows
.map(row => Number(row && row.wave_id || 0))
.filter(waveId => Number.isInteger(waveId) && waveId > 0)) ];
for (const orderId of orderIds) {
infoMap.set(orderId, { waveType: 0, weighingWeight: 0 });
}
if (waveIds.length > 0 && ctx.model && ctx.model.WaveBatch) {
try {
const { Op } = ctx.app && ctx.app.Sequelize || {};
const waves = await ctx.model.WaveBatch.findAll({
where: { wave_id: { [Op.in]: waveIds }, warehouse_id: warehouseId },
attributes: [ 'wave_id', 'wave_type' ],
raw: true,
transaction,
});
const waveTypeMap = new Map((Array.isArray(waves) ? waves : [])
.map(wave => [ Number(wave && wave.wave_id || 0), Number(wave && wave.wave_type || 0) ]));
for (const row of orderRows) {
const info = infoMap.get(Number(row.id));
if (info) {
info.waveType = waveTypeMap.get(Number(row.wave_id || 0)) || 0;
}
}
} catch (error) { /* 查询失败不阻断主流程,按无波次处理 */ }
}
if (orderIds.length > 0 && ctx.model && ctx.model.OrderExpress) {
try {
const { Op } = ctx.app && ctx.app.Sequelize || {};
const expressRows = await ctx.model.OrderExpress.findAll({
where: { order_id: { [Op.in]: orderIds }, warehouse_id: warehouseId },
attributes: [ 'order_id', 'weighing_weight' ],
raw: true,
transaction,
});
for (const expressRow of (Array.isArray(expressRows) ? expressRows : [])) {
const info = infoMap.get(Number(expressRow && expressRow.order_id || 0));
if (info) {
info.weighingWeight = Math.max(
info.weighingWeight,
Number(expressRow && expressRow.weighing_weight || 0)
);
}
}
} catch (error) { /* 查询失败不阻断主流程,按未称重处理 */ }
}
return infoMap;
}
module.exports = {
INTERCEPT_STATUS_NONE,
isInterceptedOrderStatus,
WAVE_TYPE_GROUP_BUY,
WAVE_TYPE_SCATTER,
getInterceptRequestBlockReason,
isInterceptRequestBlockedByRecheckOrWeigh,
resolveInterceptRequestGuardInfo,
resolveInterceptRequestGuardInfoMap,
};