feat: 订单列表新增 stock_query 商品模糊查询条件
所有订单列表接口支持通过 where.stock_query 进行商品维度的模糊筛选: - 模糊匹配 stock_warehouse 的 item_code(商品编码)、item_name(商品名称)、id(主键) - 模糊匹配 stock_barcodes 的 barcode(条码) - 通过 order_items 关联查找匹配的订单 ID 并过滤订单列表 涉及方法: - base.js: listByModule(覆盖 9 个订单列表入口) - wave.js: groupOrderFetchList / singleOrderFetchList / waveStrategyFallbackOrderList - recheckOrder.js: queryRecheckOrderSingleList / queryRecheckOrderGroupList
This commit is contained in:
parent
b00e320262
commit
7ab67b239a
@ -946,6 +946,34 @@ class OrderBaseService extends MainService {
|
|||||||
return { where: normalizedWhere, buyerMessageHas: null };
|
return { where: normalizedWhere, buyerMessageHas: null };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 提取 stock_query 虚拟筛选条件。
|
||||||
|
* 将其从 where 中移除并返回清洗后的 where 与 stock_query 关键字。
|
||||||
|
* @param {Object} whereInput 标准化后的 where 条件。
|
||||||
|
* @return {{ where: Object, stockQuery: string }} 清洗后的 where 与 stock_query 值。
|
||||||
|
*/
|
||||||
|
extractStockQueryVirtualFilter(whereInput = {}) {
|
||||||
|
const normalizedWhere = this.isPlainObject(whereInput)
|
||||||
|
? { ...whereInput }
|
||||||
|
: {};
|
||||||
|
const stockQueryCondition = normalizedWhere.stock_query;
|
||||||
|
|
||||||
|
delete normalizedWhere.stock_query;
|
||||||
|
|
||||||
|
if (!stockQueryCondition) {
|
||||||
|
return { where: normalizedWhere, stockQuery: '' };
|
||||||
|
}
|
||||||
|
|
||||||
|
const rawValue = this.isPlainObject(stockQueryCondition)
|
||||||
|
&& Object.prototype.hasOwnProperty.call(stockQueryCondition, 'value')
|
||||||
|
? stockQueryCondition.value
|
||||||
|
: stockQueryCondition;
|
||||||
|
|
||||||
|
const stockQuery = typeof rawValue === 'string' ? rawValue.trim() : '';
|
||||||
|
|
||||||
|
return { where: normalizedWhere, stockQuery };
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建 buyer_message_has 虚拟字段的 Sequelize where 条件。
|
* 构建 buyer_message_has 虚拟字段的 Sequelize where 条件。
|
||||||
* 覆盖三个留言/备注字段:buyer_message、seller_message、remark。
|
* 覆盖三个留言/备注字段:buyer_message、seller_message、remark。
|
||||||
@ -1080,6 +1108,105 @@ class OrderBaseService extends MainService {
|
|||||||
.filter(orderId => Number.isFinite(orderId) && orderId > 0)));
|
.filter(orderId => Number.isFinite(orderId) && orderId > 0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据 stock_query 模糊查询 stock_warehouse(商品编码、商品名称、主键ID)和 stock_barcodes(条码),
|
||||||
|
* 找到匹配的 stock_id,再通过 order_items 查找关联的订单 ID。
|
||||||
|
* 若未匹配到任何 stock 则返回空数组,下游会将订单列表结果置空。
|
||||||
|
* @param {string} stockQuery 模糊查询关键词。
|
||||||
|
* @param {string} moduleName 模块名称(用于确定 order_items 表类型)。
|
||||||
|
* @param {number|string} [warehouseId] 仓库ID。
|
||||||
|
* @return {Promise<number[]>} 命中的订单ID列表。
|
||||||
|
*/
|
||||||
|
async findOrderIdsByStockQuery(stockQuery = '', moduleName = 'order', warehouseId = null) {
|
||||||
|
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。');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!stockQuery || typeof stockQuery !== 'string') {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const trimmedQuery = stockQuery.trim();
|
||||||
|
if (!trimmedQuery) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const { Op } = this.app.Sequelize;
|
||||||
|
const likePattern = `%${trimmedQuery}%`;
|
||||||
|
|
||||||
|
// 第一步:从 stock_warehouse 模糊查询 item_code、item_name 和 id
|
||||||
|
const stockWarehouseModel = this.ctx.model && this.ctx.model.StockWarehouse;
|
||||||
|
const stockIdsFromWarehouse = [];
|
||||||
|
if (stockWarehouseModel) {
|
||||||
|
const stockRows = await stockWarehouseModel.findAll({
|
||||||
|
where: {
|
||||||
|
warehouse_id: contextWarehouseId,
|
||||||
|
is_valid: 'Y',
|
||||||
|
[Op.or]: [
|
||||||
|
{ item_code: { [Op.like]: likePattern } },
|
||||||
|
{ item_name: { [Op.like]: likePattern } },
|
||||||
|
{ id: { [Op.like]: likePattern } },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
attributes: [ 'id' ],
|
||||||
|
raw: true,
|
||||||
|
});
|
||||||
|
for (const row of stockRows) {
|
||||||
|
stockIdsFromWarehouse.push(Number(row.id));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 第二步:从 stock_barcodes 模糊查询 barcode
|
||||||
|
const stockBarcodeModel = this.ctx.model && this.ctx.model.StockBarcode;
|
||||||
|
const stockIdsFromBarcodes = [];
|
||||||
|
if (stockBarcodeModel) {
|
||||||
|
const barcodeRows = await stockBarcodeModel.findAll({
|
||||||
|
where: {
|
||||||
|
warehouse_id: contextWarehouseId,
|
||||||
|
barcode: { [Op.like]: likePattern },
|
||||||
|
},
|
||||||
|
attributes: [ 'stock_id' ],
|
||||||
|
raw: true,
|
||||||
|
});
|
||||||
|
for (const row of barcodeRows) {
|
||||||
|
stockIdsFromBarcodes.push(Number(row.stock_id));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 第三步:合并去重所有匹配的 stock_id
|
||||||
|
const allStockIds = Array.from(new Set([ ...stockIdsFromWarehouse, ...stockIdsFromBarcodes ]))
|
||||||
|
.filter(id => Number.isFinite(id) && id > 0);
|
||||||
|
|
||||||
|
if (allStockIds.length === 0) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 第四步:通过 order_items 查找关联的 order_id
|
||||||
|
const moduleConfig = this.getModuleConfig(moduleName);
|
||||||
|
const tableType = moduleConfig.modelType || 'current';
|
||||||
|
const orderItemsModel = await this.getOrderItemsModelByTableType(tableType, contextWarehouseId);
|
||||||
|
const orderItemRows = await orderItemsModel.findAll({
|
||||||
|
where: {
|
||||||
|
stock_id: { [Op.in]: allStockIds },
|
||||||
|
},
|
||||||
|
attributes: [ 'order_id' ],
|
||||||
|
raw: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
return Array.from(new Set(
|
||||||
|
(Array.isArray(orderItemRows) ? orderItemRows : [])
|
||||||
|
.map(row => Number(row && row.order_id))
|
||||||
|
.filter(orderId => Number.isFinite(orderId) && orderId > 0)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将命中的订单ID并入 Sequelize where 条件。
|
* 将命中的订单ID并入 Sequelize where 条件。
|
||||||
* @param {Object} queryWhere 原始 where 条件。
|
* @param {Object} queryWhere 原始 where 条件。
|
||||||
@ -1247,7 +1374,8 @@ class OrderBaseService extends MainService {
|
|||||||
});
|
});
|
||||||
const { where: sanitizedWhere, expressNumbers } = this.extractOrderListExpressVirtualFilter(where);
|
const { where: sanitizedWhere, expressNumbers } = this.extractOrderListExpressVirtualFilter(where);
|
||||||
const { where: whereAfterBuyerMessage, buyerMessageHas } = this.extractBuyerMessageHasVirtualFilter(sanitizedWhere);
|
const { where: whereAfterBuyerMessage, buyerMessageHas } = this.extractBuyerMessageHasVirtualFilter(sanitizedWhere);
|
||||||
const mergedWhere = this.mergeWhereObjects(this.getDefaultWhere(moduleName), whereAfterBuyerMessage);
|
const { where: whereAfterStockQuery, stockQuery } = this.extractStockQueryVirtualFilter(whereAfterBuyerMessage);
|
||||||
|
const mergedWhere = this.mergeWhereObjects(this.getDefaultWhere(moduleName), whereAfterStockQuery);
|
||||||
let queryWhere = this.buildWhere(mergedWhere, contextWarehouseId, {
|
let queryWhere = this.buildWhere(mergedWhere, contextWarehouseId, {
|
||||||
specialFieldMap: {
|
specialFieldMap: {
|
||||||
quickWhere: {
|
quickWhere: {
|
||||||
@ -1266,6 +1394,11 @@ class OrderBaseService extends MainService {
|
|||||||
queryWhere = this.appendConditionToWhere(queryWhere, buyerMessageCondition);
|
queryWhere = this.appendConditionToWhere(queryWhere, buyerMessageCondition);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (stockQuery) {
|
||||||
|
const stockOrderIds = await this.findOrderIdsByStockQuery(stockQuery, moduleName, contextWarehouseId);
|
||||||
|
queryWhere = this.appendOrderIdsFilterToWhere(queryWhere, stockOrderIds);
|
||||||
|
}
|
||||||
|
|
||||||
const model = await this.getOrderModel(moduleName, contextWarehouseId);
|
const model = await this.getOrderModel(moduleName, contextWarehouseId);
|
||||||
const order = this.buildListOrder(requestData.sort, moduleName);
|
const order = this.buildListOrder(requestData.sort, moduleName);
|
||||||
const offset = (page - 1) * pageSize;
|
const offset = (page - 1) * pageSize;
|
||||||
|
|||||||
@ -3237,6 +3237,29 @@ class RecheckOrderService extends OrderBaseService {
|
|||||||
replacements.pickStatus = pickStatus;
|
replacements.pickStatus = pickStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// stock_query 商品模糊筛选:通过 stock_warehouse / stock_barcodes → order_items → order_id
|
||||||
|
const stockQuery = String(where.stock_query || '').trim();
|
||||||
|
if (stockQuery) {
|
||||||
|
const stockOrderIds = await this.findOrderIdsByStockQuery(stockQuery, 'order', warehouseId);
|
||||||
|
if (stockOrderIds.length > 0) {
|
||||||
|
conditions.push('o.id IN (:stockOrderIds)');
|
||||||
|
replacements.stockOrderIds = stockOrderIds;
|
||||||
|
} else {
|
||||||
|
// 未匹配到任何商品,直接返回空列表
|
||||||
|
return this.success('ok', {
|
||||||
|
list: [],
|
||||||
|
pagination: {
|
||||||
|
page,
|
||||||
|
pageSize,
|
||||||
|
total: 0,
|
||||||
|
totalPages: 0,
|
||||||
|
hasPrevious: false,
|
||||||
|
hasNext: false,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const whereClause = conditions.length > 0
|
const whereClause = conditions.length > 0
|
||||||
? `WHERE ${conditions.join('\n AND ')}`
|
? `WHERE ${conditions.join('\n AND ')}`
|
||||||
: '';
|
: '';
|
||||||
@ -3365,6 +3388,29 @@ class RecheckOrderService extends OrderBaseService {
|
|||||||
replacements.waveStatus = waveStatus;
|
replacements.waveStatus = waveStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// stock_query 商品模糊筛选:通过 stock_warehouse / stock_barcodes → order_items → order_id 过滤波次
|
||||||
|
const stockQuery = String(where.stock_query || '').trim();
|
||||||
|
if (stockQuery) {
|
||||||
|
const stockOrderIds = await this.findOrderIdsByStockQuery(stockQuery, 'order', warehouseId);
|
||||||
|
if (stockOrderIds.length > 0) {
|
||||||
|
conditions.push('o.id IN (:stockOrderIds)');
|
||||||
|
replacements.stockOrderIds = stockOrderIds;
|
||||||
|
} else {
|
||||||
|
// 未匹配到任何商品,直接返回空列表
|
||||||
|
return this.success('ok', {
|
||||||
|
list: [],
|
||||||
|
pagination: {
|
||||||
|
page,
|
||||||
|
pageSize,
|
||||||
|
total: 0,
|
||||||
|
totalPages: 0,
|
||||||
|
hasPrevious: false,
|
||||||
|
hasNext: false,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const whereClause = conditions.length > 0
|
const whereClause = conditions.length > 0
|
||||||
? `WHERE ${conditions.join('\n AND ')}`
|
? `WHERE ${conditions.join('\n AND ')}`
|
||||||
: '';
|
: '';
|
||||||
|
|||||||
@ -2145,6 +2145,7 @@ class WaveService extends OrderBaseService {
|
|||||||
const shopName = this.normalizeShopName(requestData.shop_name);
|
const shopName = this.normalizeShopName(requestData.shop_name);
|
||||||
const includeSku = this.normalizeSkuText(requestData.include_sku);
|
const includeSku = this.normalizeSkuText(requestData.include_sku);
|
||||||
const returnOrderIds = this.normalizeBoolean(requestData.return_order_ids, false);
|
const returnOrderIds = this.normalizeBoolean(requestData.return_order_ids, false);
|
||||||
|
const stockQuery = String(requestData.stock_query || '').trim();
|
||||||
const { page, pageSize } = this.normalizePagination(requestData);
|
const { page, pageSize } = this.normalizePagination(requestData);
|
||||||
|
|
||||||
const where = {
|
const where = {
|
||||||
@ -2164,6 +2165,20 @@ class WaveService extends OrderBaseService {
|
|||||||
where.shop_nick = { [Op.like]: `%${shopName}%` };
|
where.shop_nick = { [Op.like]: `%${shopName}%` };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// stock_query 商品模糊筛选:通过 stock_warehouse / stock_barcodes → order_items → 限定候选订单范围
|
||||||
|
if (stockQuery) {
|
||||||
|
const stockOrderIds = await this.findOrderIdsByStockQuery(stockQuery, 'order', warehouseId);
|
||||||
|
if (stockOrderIds.length > 0) {
|
||||||
|
where.id = { [Op.in]: stockOrderIds };
|
||||||
|
} else {
|
||||||
|
// 未匹配到任何商品,直接返回空列表
|
||||||
|
return {
|
||||||
|
list: [],
|
||||||
|
pagination: { page, pageSize, total: 0, totalPages: 0, hasPrevious: false, hasNext: false },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Phase 1: 主表全量查询,用 items_hash 做分组基准
|
// Phase 1: 主表全量查询,用 items_hash 做分组基准
|
||||||
const rawOrders = await model.findAll({
|
const rawOrders = await model.findAll({
|
||||||
where,
|
where,
|
||||||
@ -2383,6 +2398,7 @@ class WaveService extends OrderBaseService {
|
|||||||
const shopName = this.normalizeShopName(requestData.shop_name);
|
const shopName = this.normalizeShopName(requestData.shop_name);
|
||||||
const includeSku = this.normalizeSkuText(requestData.include_sku);
|
const includeSku = this.normalizeSkuText(requestData.include_sku);
|
||||||
const returnOrderIds = this.normalizeBoolean(requestData.return_order_ids, false);
|
const returnOrderIds = this.normalizeBoolean(requestData.return_order_ids, false);
|
||||||
|
const stockQuery = String(requestData.stock_query || '').trim();
|
||||||
const { page, pageSize } = this.normalizePagination(requestData);
|
const { page, pageSize } = this.normalizePagination(requestData);
|
||||||
|
|
||||||
const where = {
|
const where = {
|
||||||
@ -2402,6 +2418,20 @@ class WaveService extends OrderBaseService {
|
|||||||
where.shop_nick = { [Op.like]: `%${shopName}%` };
|
where.shop_nick = { [Op.like]: `%${shopName}%` };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// stock_query 商品模糊筛选:通过 stock_warehouse / stock_barcodes → order_items → 限定候选订单范围
|
||||||
|
if (stockQuery) {
|
||||||
|
const stockOrderIds = await this.findOrderIdsByStockQuery(stockQuery, 'order', warehouseId);
|
||||||
|
if (stockOrderIds.length > 0) {
|
||||||
|
where.id = { [Op.in]: stockOrderIds };
|
||||||
|
} else {
|
||||||
|
// 未匹配到任何商品,直接返回空列表
|
||||||
|
return {
|
||||||
|
list: [],
|
||||||
|
pagination: { page, pageSize, total: 0, totalPages: 0, hasPrevious: false, hasNext: false },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Phase 1: 主表全量查询,用 items_hash 做分组基准
|
// Phase 1: 主表全量查询,用 items_hash 做分组基准
|
||||||
const rawOrders = await model.findAll({
|
const rawOrders = await model.findAll({
|
||||||
where,
|
where,
|
||||||
@ -4000,8 +4030,9 @@ class WaveService extends OrderBaseService {
|
|||||||
);
|
);
|
||||||
const { where: sanitizedWhere, expressNumbers } = this.extractOrderListExpressVirtualFilter(ctxWhere);
|
const { where: sanitizedWhere, expressNumbers } = this.extractOrderListExpressVirtualFilter(ctxWhere);
|
||||||
const { where: whereAfterBuyerMessage, buyerMessageHas } = this.extractBuyerMessageHasVirtualFilter(sanitizedWhere);
|
const { where: whereAfterBuyerMessage, buyerMessageHas } = this.extractBuyerMessageHasVirtualFilter(sanitizedWhere);
|
||||||
|
const { where: whereAfterStockQuery, stockQuery } = this.extractStockQueryVirtualFilter(whereAfterBuyerMessage);
|
||||||
const defaultWhere = this.getDefaultWhere('waveStrategyFallbackOrder');
|
const defaultWhere = this.getDefaultWhere('waveStrategyFallbackOrder');
|
||||||
const mergedWhere = this.mergeWhereObjects(defaultWhere, whereAfterBuyerMessage);
|
const mergedWhere = this.mergeWhereObjects(defaultWhere, whereAfterStockQuery);
|
||||||
let queryWhere = this.buildWhere(mergedWhere, contextWarehouseId, {
|
let queryWhere = this.buildWhere(mergedWhere, contextWarehouseId, {
|
||||||
specialFieldMap: { quickWhere: { bucket: 'quick' } },
|
specialFieldMap: { quickWhere: { bucket: 'quick' } },
|
||||||
});
|
});
|
||||||
@ -4017,6 +4048,12 @@ class WaveService extends OrderBaseService {
|
|||||||
queryWhere = this.appendConditionToWhere(queryWhere, buyerMessageCondition);
|
queryWhere = this.appendConditionToWhere(queryWhere, buyerMessageCondition);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理 stock_query 虚拟筛选
|
||||||
|
if (stockQuery) {
|
||||||
|
const stockOrderIds = await this.findOrderIdsByStockQuery(stockQuery, 'waveStrategyFallbackOrder', contextWarehouseId);
|
||||||
|
queryWhere = this.appendOrderIdsFilterToWhere(queryWhere, stockOrderIds);
|
||||||
|
}
|
||||||
|
|
||||||
// 追加 OR 条件:未匹配策略(wave_scan_result=2) 或 已完全打印被排除(print_status='fin')
|
// 追加 OR 条件:未匹配策略(wave_scan_result=2) 或 已完全打印被排除(print_status='fin')
|
||||||
const { Sequelize } = this.app;
|
const { Sequelize } = this.app;
|
||||||
queryWhere = {
|
queryWhere = {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user