feat(order): 订单列表与导出支持波次范围 wave_scope 筛选

- extractWaveScopeVirtualFilter 解析 wave_scope(0=未分配波次/1=团购/2=散单)
- buildWaveScopeWhereCondition 经 wms_wave_batch 反查波次ID注入条件
- list 与 order_export 均接入该虚拟筛选
This commit is contained in:
liufumingren 2026-08-21 02:20:13 +08:00
parent 232985a272
commit 60b4babb34
2 changed files with 138 additions and 2 deletions

View File

@ -1127,6 +1127,52 @@ class OrderBaseService extends MainService {
return { where: normalizedWhere, waveIndexCondition: normalizedCondition };
}
/**
* 提取 wave_scope(波次范围)虚拟筛选条件。
* 将其从 where 中移除并解析为波次范围值集合,后续通过 wms_wave_batch 反查波次ID注入条件。
* 取值约定:0=未分配波次,1=团购,2=散单;value 支持单个值或数组(多选)。
* @param {Object} whereInput 标准化后的 where 条件。
* @return {{ where: Object, waveScope: number[]|null }} 清洗后的 where 与波次范围值集合。
*/
extractWaveScopeVirtualFilter(whereInput = {}) {
const normalizedWhere = this.isPlainObject(whereInput)
? { ...whereInput }
: {};
const waveScopeCondition = normalizedWhere.wave_scope ?? normalizedWhere.waveScope;
delete normalizedWhere.wave_scope;
delete normalizedWhere.waveScope;
// 条件为空时忽略
if (waveScopeCondition === undefined || waveScopeCondition === null || waveScopeCondition === '') {
return { where: normalizedWhere, waveScope: null };
}
const sourceValue = this.isPlainObject(waveScopeCondition)
&& Object.prototype.hasOwnProperty.call(waveScopeCondition, 'value')
? waveScopeCondition.value
: waveScopeCondition;
// 兼容简写:wave_scope 直接传数值或数组时按 eq/in 处理
const sourceItems = Array.isArray(sourceValue) ? sourceValue : [ sourceValue ];
const normalizedScopes = Array.from(new Set(
sourceItems
.filter(item => item !== undefined && item !== null && item !== '')
.map(item => Number(item))
));
for (const scope of normalizedScopes) {
if (!Number.isInteger(scope) || scope < 0 || scope > 2) {
throw new Error('wave_scope 参数不合法,仅支持 0(未分配波次)/1(团购)/2(散单)');
}
}
return {
where: normalizedWhere,
waveScope: normalizedScopes.length > 0 ? normalizedScopes : null,
};
}
/**
* 根据 wave_index 筛选条件查询 wms_order_wave_index 命中的订单ID。
* @param {Object} waveIndexCondition 序号筛选条件(含 operator/value)。
@ -1192,6 +1238,84 @@ class OrderBaseService extends MainService {
.filter(orderId => Number.isFinite(orderId) && orderId > 0)));
}
/**
* 根据波次范围(wave_scope)构建订单表 wave_id 过滤条件。
* 0(未分配波次)直接生成 wave_id = 0;1(团购)/2(散单)先查 wms_wave_batch
* 得到对应 wave_type 的波次ID集合,再生成 wave_id in (...)。
* @param {Array<number>} waveScopes 波次范围值集合(0/1/2)。
* @param {number|string} [warehouseId] 仓库ID。
* @return {Promise<Object|null>} Sequelize 条件,无有效范围时返回 null。
*/
async buildWaveScopeWhereCondition(waveScopes = [], warehouseId = this.getWarehouseId()) {
const contextWarehouseId = this.getWarehouseId();
if (contextWarehouseId === null || contextWarehouseId === undefined || contextWarehouseId === '') {
throw new Error('缺少当前用户仓库上下文 store_id');
}
if (warehouseId !== null && warehouseId !== undefined && warehouseId !== ''
&& String(warehouseId) !== String(contextWarehouseId)) {
throw new Error('订单工作台仓库ID必须来自当前用户上下文 store_id,禁止传入外部仓库ID。');
}
const normalizedScopes = Array.from(new Set(
(Array.isArray(waveScopes) ? waveScopes : [ waveScopes ])
.filter(scope => scope !== undefined && scope !== null && scope !== '')
.map(scope => Number(scope))
)).filter(scope => Number.isInteger(scope) && scope >= 0 && scope <= 2);
if (normalizedScopes.length === 0) {
return null;
}
const { Op } = this.app.Sequelize;
const waveTypeScopes = normalizedScopes.filter(scope => scope === 1 || scope === 2);
const conditions = [];
if (normalizedScopes.includes(0)) {
conditions.push({ wave_id: 0 });
}
if (waveTypeScopes.length > 0) {
const waveBatchModel = this.ctx.model && this.ctx.model.WaveBatch;
const waveIds = [];
if (waveBatchModel && typeof waveBatchModel.findAll === 'function') {
const waveRows = await waveBatchModel.findAll({
attributes: [ 'wave_id' ],
where: {
warehouse_id: contextWarehouseId,
wave_type: {
[Op.in]: waveTypeScopes,
},
},
raw: true,
});
for (const row of Array.isArray(waveRows) ? waveRows : []) {
const waveId = Number(row && row.wave_id || 0);
if (waveId > 0) {
waveIds.push(waveId);
}
}
}
// 空集合时 Op.in: [] 生成恒假条件,保证团购/散单无波次时结果为空
conditions.push({
wave_id: {
[Op.in]: Array.from(new Set(waveIds)),
},
});
}
if (conditions.length === 1) {
return conditions[0];
}
return {
[Op.or]: conditions,
};
}
/**
* 构建 buyer_message_has 虚拟字段的 Sequelize where 条件。
* 覆盖三个留言/备注字段:buyer_message、seller_message、remark。
@ -1594,7 +1718,8 @@ class OrderBaseService extends MainService {
const { where: whereAfterBuyerMessage, buyerMessageHas } = this.extractBuyerMessageHasVirtualFilter(sanitizedWhere);
const { where: whereAfterStockQuery, stockQuery } = this.extractStockQueryVirtualFilter(whereAfterBuyerMessage);
const { where: whereAfterWaveIndex, waveIndexCondition } = this.extractWaveIndexVirtualFilter(whereAfterStockQuery);
const mergedWhere = this.mergeWhereObjects(this.getDefaultWhere(moduleName), whereAfterWaveIndex);
const { where: whereAfterWaveScope, waveScope } = this.extractWaveScopeVirtualFilter(whereAfterWaveIndex);
const mergedWhere = this.mergeWhereObjects(this.getDefaultWhere(moduleName), whereAfterWaveScope);
let queryWhere = this.buildWhere(mergedWhere, contextWarehouseId, {
specialFieldMap: {
quickWhere: {
@ -1623,6 +1748,11 @@ class OrderBaseService extends MainService {
queryWhere = this.appendOrderIdsFilterToWhere(queryWhere, waveIndexOrderIds);
}
if (waveScope) {
const waveScopeCondition = await this.buildWaveScopeWhereCondition(waveScope, contextWarehouseId);
queryWhere = this.appendConditionToWhere(queryWhere, waveScopeCondition);
}
const model = await this.getOrderModel(moduleName, contextWarehouseId);
const order = this.buildListOrder(requestData.sort, moduleName);
const offset = (page - 1) * pageSize;

View File

@ -620,9 +620,10 @@ class OrderExportService extends OrderBaseService {
const { where: sanitizedWhere, expressNumbers } = this.extractOrderListExpressVirtualFilter(where);
const { where: whereAfterBuyerMessage, buyerMessageHas } = this.extractBuyerMessageHasVirtualFilter(sanitizedWhere);
const { where: whereAfterWaveIndex, waveIndexCondition } = this.extractWaveIndexVirtualFilter(whereAfterBuyerMessage);
const { where: whereAfterWaveScope, waveScope } = this.extractWaveScopeVirtualFilter(whereAfterWaveIndex);
const mergedWhere = this.mergeWhereObjects(
target.defaultWhereModuleName ? this.getDefaultWhere(target.defaultWhereModuleName) : {},
whereAfterWaveIndex
whereAfterWaveScope
);
let queryWhere = this.buildWhere(mergedWhere, warehouseId, {
specialFieldMap: {
@ -647,6 +648,11 @@ class OrderExportService extends OrderBaseService {
queryWhere = this.appendOrderIdsFilterToWhere(queryWhere, waveIndexOrderIds);
}
if (waveScope) {
const waveScopeCondition = await this.buildWaveScopeWhereCondition(waveScope, warehouseId);
queryWhere = this.appendConditionToWhere(queryWhere, waveScopeCondition);
}
const orderModel = await this.getOrderModelByTableType(target.tableType, warehouseId);
const pageSize = this.getExportPageSize();
const order = this.buildListOrder(requestData.sort, target.sortModuleName);