fix(order): 运单号检索优化(分段后缀剥离等值匹配、短词禁降级、数组精确匹配)
This commit is contained in:
parent
05177d00f7
commit
2189911073
35
app/lib/waybill_code.js
Normal file
35
app/lib/waybill_code.js
Normal file
@ -0,0 +1,35 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 运单号公共工具。
|
||||
* 统一处理一单多件分段单号(如 JDX057941864338-1-1-)的包裹序号后缀剥离与 like 通配符转义,
|
||||
* 保证「剥离后缀后主体运单号必须等值匹配」的业务规则在各扫描入口保持一致。
|
||||
*/
|
||||
|
||||
/**
|
||||
* 剥离运单号尾部的包裹序号段(一单多件分段格式:-包裹序号-总件数[-])。
|
||||
* 例如 'JDX057941864338-1-1-' → 'JDX057941864338';
|
||||
* 'JDX057941864338' 无分段后缀时原样返回。
|
||||
*
|
||||
* @param {string} waybillCode 运单号
|
||||
* @return {string} 剥离分段后缀后的主体运单号
|
||||
*/
|
||||
function stripWaybillSegmentSuffix(waybillCode) {
|
||||
return String(waybillCode || '').trim().replace(/(?:-\d+){2,}-?$/, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* 转义 like 匹配中的通配符(% _ \),避免用户输入中的特殊字符扩大匹配范围。
|
||||
* 用于「主体运单号 + -%」前缀匹配场景,确保主体部分被等值比较。
|
||||
*
|
||||
* @param {string} value 待转义的字符串
|
||||
* @return {string} 转义后的字符串
|
||||
*/
|
||||
function escapeLikeWildcards(value) {
|
||||
return String(value || '').replace(/[%_\\]/g, m => `\\${m}`);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
stripWaybillSegmentSuffix,
|
||||
escapeLikeWildcards,
|
||||
};
|
||||
@ -3,6 +3,7 @@
|
||||
const OrderBaseService = require('./base');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { stripWaybillSegmentSuffix, escapeLikeWildcards } = require('../../../lib/waybill_code');
|
||||
|
||||
const DEBUG_LOG = path.join(__dirname, '..', '..', '..', '..', 'temp', 'quickSearch_debug.log');
|
||||
|
||||
@ -62,7 +63,8 @@ class DeliveryOrderService extends OrderBaseService {
|
||||
|
||||
/**
|
||||
* 订单快速搜索。
|
||||
* 跨表联合搜索:运单号(wms_order_express,精确)、发货单号/外部订单号/平台交易单号(dispatch 表,模糊)。
|
||||
* 跨表联合搜索:运单号(wms_order_express,精确匹配,含剥离一单多件分段后缀后的主体等值匹配)、
|
||||
* 发货单号/外部订单号/平台交易单号(dispatch 表,模糊)。
|
||||
* 返回匹配的订单列表(最多 20 条),包含订单关键信息及关联运单号。
|
||||
*
|
||||
* @param {Object} data 请求体。
|
||||
@ -96,10 +98,19 @@ class DeliveryOrderService extends OrderBaseService {
|
||||
|
||||
// ====== 第一步:express 表 OR 搜索 ======
|
||||
dbg('--- Step1: express OR query ---');
|
||||
// 业务规则:运单号先剥离一单多件包裹序号后缀(如 -1-1-),主体必须等值匹配,
|
||||
// 不得落入模糊 like;发货单号/平台交易单号保留模糊匹配能力
|
||||
const waybillBase = stripWaybillSegmentSuffix(keyword);
|
||||
const escapedWaybillBase = escapeLikeWildcards(waybillBase);
|
||||
const waybillConditions = [
|
||||
{ waybill_code: keyword },
|
||||
...(waybillBase !== keyword ? [{ waybill_code: waybillBase }] : []),
|
||||
...(escapedWaybillBase ? [{ waybill_code: { [Op.like]: `${escapedWaybillBase}-%` } }] : []),
|
||||
];
|
||||
const expressWhere = {
|
||||
warehouse_id: warehouseId,
|
||||
[Op.or]: [
|
||||
{ waybill_code: keyword },
|
||||
...waybillConditions,
|
||||
{ delivery_order_code: { [Op.like]: `%${keyword}%` } },
|
||||
{ source_order_codes: { [Op.like]: `%${keyword}%` } },
|
||||
],
|
||||
|
||||
@ -1,6 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
const Service = require('egg').Service;
|
||||
const { stripWaybillSegmentSuffix, escapeLikeWildcards } = require('../../lib/waybill_code');
|
||||
|
||||
/**
|
||||
* 快捷搜索运单号模糊降级的最小关键词长度。
|
||||
* 运单号通常较长(如 JDX057941864338),短于该长度的输入(如单字符 "7")没有模糊搜索意义,
|
||||
* 且 %短词% 极易误命中海量历史单号(实测 "7" 可命中 24149 行),
|
||||
* 违背「不完整短串不得命中订单」的业务约束,因此禁止短词降级进入 %kw% 模糊分支。
|
||||
*/
|
||||
const QUICK_WHERE_WAYBILL_FUZZY_MIN_LENGTH = 4;
|
||||
|
||||
/**
|
||||
* 打包任务服务层(仓库侧)。
|
||||
@ -48,11 +57,11 @@ class OrderPackingService extends Service {
|
||||
* @param {Object} where 筛选条件对象
|
||||
* @param {Object} [where.status] 任务状态:0=待处理 1=打包中 2=已完成 3=异常
|
||||
* @param {Object} [where.pack_mode] 打包模式:1=散单 2=团购
|
||||
* @param {Object} [where.waybill_code] 运单号(通过 wms_order_express 解析 order_id 后过滤,支持 eq/like)
|
||||
* @param {Object} [where.waybill_code] 运单号(通过 wms_order_express 解析 order_id 后过滤;先剥离一单多件分段后缀,主体运单号等值匹配)
|
||||
* @param {Object} [where.wave_no] 波次号(模糊匹配)
|
||||
* @param {Object} [where.order_id] 订单ID(精确匹配)
|
||||
* @param {Object} [where.workbench_id] 工作台ID(精确匹配)
|
||||
* @param {Object} [where.quickWhere] 快捷搜索:支持波次号模糊匹配;运单号通过 express→order→wave 链路解析(团购按波次号查、散单按订单ID查)
|
||||
* @param {Object} [where.quickWhere] 快捷搜索:支持波次号模糊匹配;运单号通过 express→order→wave 链路解析(团购按波次号查、散单按订单ID查;运单号先剥离分段后缀等值匹配,精确未命中且关键词长度≥4 时才降级模糊,短词禁止降级避免误命中)
|
||||
* @param {Object} [where.start_time] 开始打包时间范围,支持 {operator:'between', value:[start,end]}
|
||||
* @param {Object} [where.start_time_start] 开始打包时间起始(兼容旧写法)
|
||||
* @param {Object} [where.start_time_end] 开始打包时间结束(兼容旧写法)
|
||||
@ -102,20 +111,25 @@ class OrderPackingService extends Service {
|
||||
}
|
||||
|
||||
// 运单号筛选:通过 wms_order_express 表解析 order_id,再按 order_id 过滤(wms_pack_task.waybill_code 通常为空)
|
||||
// 业务规则:先剥离一单多件包裹序号后缀(如 -1-1-),剩余主体运单号必须等值匹配,不得落入模糊 like 分支
|
||||
if (where.waybill_code) {
|
||||
const waybillValue = where.waybill_code?.value ?? where.waybill_code;
|
||||
const waybillOperator = (where.waybill_code?.operator || 'like').toLowerCase();
|
||||
if (waybillValue && String(waybillValue).trim()) {
|
||||
const trimmedValue = String(waybillValue).trim();
|
||||
// 剥离分段后缀得到主体运单号(JDX057941864338-1-1- → JDX057941864338)
|
||||
const baseWaybill = stripWaybillSegmentSuffix(trimmedValue);
|
||||
const escapedBase = escapeLikeWildcards(baseWaybill);
|
||||
// 等值匹配集合:完整单号等值 + 主体单号等值 + 主体单号前缀段(主体相同、分段序号不同)
|
||||
const waybillConditions = [
|
||||
{ waybill_code: trimmedValue },
|
||||
...(baseWaybill !== trimmedValue ? [{ waybill_code: baseWaybill }] : []),
|
||||
...(escapedBase ? [{ waybill_code: { [Op.like]: `${escapedBase}-%` } }] : []),
|
||||
];
|
||||
const expressWhere = {
|
||||
warehouse_id: warehouseId,
|
||||
delete_at: null,
|
||||
[Op.or]: waybillConditions,
|
||||
};
|
||||
if (waybillOperator === 'eq') {
|
||||
expressWhere.waybill_code = trimmedValue;
|
||||
} else {
|
||||
expressWhere.waybill_code = { [Op.like]: `%${trimmedValue}%` };
|
||||
}
|
||||
const expressRecords = await ctx.model.OrderExpress.findAll({
|
||||
where: expressWhere,
|
||||
attributes: [ 'order_id' ],
|
||||
@ -167,15 +181,39 @@ class OrderPackingService extends Service {
|
||||
|
||||
// 2. 运单号解析:express → order → wave
|
||||
// 团购波次 → 按 wave_no 查 packing 表;散单 → 按 order_id 查 packing 表
|
||||
const expressRecords = await ctx.model.OrderExpress.findAll({
|
||||
// 业务规则:先剥离包裹序号后缀(如 -1-1-),主体运单号等值匹配为最高优先级;
|
||||
// 精确未命中时才降级为模糊 like,保留快捷搜索能力
|
||||
const baseKw = stripWaybillSegmentSuffix(kw);
|
||||
const escapedKw = escapeLikeWildcards(baseKw);
|
||||
const exactWaybillConditions = [
|
||||
{ waybill_code: kw },
|
||||
...(baseKw !== kw ? [{ waybill_code: baseKw }] : []),
|
||||
...(escapedKw ? [{ waybill_code: { [Op.like]: `${escapedKw}-%` } }] : []),
|
||||
];
|
||||
let expressRecords = await ctx.model.OrderExpress.findAll({
|
||||
where: {
|
||||
waybill_code: { [Op.like]: `%${kw}%` },
|
||||
[Op.or]: exactWaybillConditions,
|
||||
warehouse_id: warehouseId,
|
||||
delete_at: null,
|
||||
},
|
||||
attributes: [ 'order_id' ],
|
||||
raw: true,
|
||||
});
|
||||
// 精确未命中 → 降级为模糊匹配(兼容快捷搜索输入非完整运单号的场景)
|
||||
// 关键词校验:短词(trim 后长度小于 QUICK_WHERE_WAYBILL_FUZZY_MIN_LENGTH,含纯单字符)
|
||||
// 禁止降级进入 %kw% 模糊分支——运单号通常较长,%短词% 极易误命中大量历史单号,
|
||||
// 违背「不完整短串不得命中订单」的业务约束,此时运单号解析保持空结果
|
||||
if (expressRecords.length === 0 && kw.length >= QUICK_WHERE_WAYBILL_FUZZY_MIN_LENGTH) {
|
||||
expressRecords = await ctx.model.OrderExpress.findAll({
|
||||
where: {
|
||||
waybill_code: { [Op.like]: `%${kw}%` },
|
||||
warehouse_id: warehouseId,
|
||||
delete_at: null,
|
||||
},
|
||||
attributes: [ 'order_id' ],
|
||||
raw: true,
|
||||
});
|
||||
}
|
||||
|
||||
if (expressRecords.length > 0) {
|
||||
const orderIds = [ ...new Set(expressRecords.map(r => r.order_id).filter(Boolean)) ];
|
||||
|
||||
@ -154,7 +154,7 @@ class WaybillService extends MainService {
|
||||
* 主单号通过 SELF JOIN 同 order_id 下 business_key=1 的记录获取。
|
||||
*
|
||||
* @param {Object} [options={}] 查询参数。
|
||||
* @param {Object} [options.where] 筛选条件,支持 waybillCode / mainWaybillCode / orderId / orderNo / logisticsCode / logisticsInterfaceId / platformCode / status / isBack / isIntercept / isGalaxyReceive / packageMaterialId / hasWeighingWeight / keyword / createdAt({operator:'between', value:[start,end]},兼容 createAt 别名)/ createdAtStart / createdAtEnd(旧写法兼容)。
|
||||
* @param {Object} [options.where] 筛选条件,支持 waybillCode(字符串模糊匹配 / 字符串数组精确匹配)/ mainWaybillCode / orderId / orderNo / logisticsCode / logisticsInterfaceId / platformCode / status / isBack / isIntercept / isGalaxyReceive / packageMaterialId / hasWeighingWeight / keyword / createdAt({operator:'between', value:[start,end]},兼容 createAt 别名)/ createdAtStart / createdAtEnd(旧写法兼容)。
|
||||
* @param {Object} [options.pagination] 分页参数 { page, pageSize },默认 page=1, pageSize=20。
|
||||
* @param {Object} [options.sort] 排序参数 { field, order },默认 create_at desc。
|
||||
* @return {Promise<Object>} 运单列表与分页信息 { list, pagination }。
|
||||
@ -222,9 +222,16 @@ class WaybillService extends MainService {
|
||||
}
|
||||
}
|
||||
|
||||
// 运单号模糊搜索
|
||||
// 运单号搜索:数组精确匹配(Op.in),字符串保持模糊匹配(Op.like)
|
||||
const waybillCode = val(where.waybillCode);
|
||||
if (waybillCode && String(waybillCode).trim()) {
|
||||
if (Array.isArray(waybillCode)) {
|
||||
const waybillCodeList = waybillCode
|
||||
.map(item => (item === undefined || item === null ? '' : String(item).trim()))
|
||||
.filter(item => item !== '');
|
||||
if (waybillCodeList.length > 0) {
|
||||
queryWhere.waybill_code = { [Op.in]: waybillCodeList };
|
||||
}
|
||||
} else if (waybillCode && String(waybillCode).trim()) {
|
||||
queryWhere.waybill_code = { [Op.like]: `%${String(waybillCode).trim()}%` };
|
||||
}
|
||||
|
||||
@ -756,8 +763,16 @@ class WaybillService extends MainService {
|
||||
}
|
||||
}
|
||||
|
||||
// 运单号搜索:数组精确匹配(Op.in),字符串保持模糊匹配(Op.like),与 list 保持一致
|
||||
const waybillCodeExport = val(where.waybillCode);
|
||||
if (waybillCodeExport && String(waybillCodeExport).trim()) {
|
||||
if (Array.isArray(waybillCodeExport)) {
|
||||
const waybillCodeListExport = waybillCodeExport
|
||||
.map(item => (item === undefined || item === null ? '' : String(item).trim()))
|
||||
.filter(item => item !== '');
|
||||
if (waybillCodeListExport.length > 0) {
|
||||
queryWhere.waybill_code = { [Op.in]: waybillCodeListExport };
|
||||
}
|
||||
} else if (waybillCodeExport && String(waybillCodeExport).trim()) {
|
||||
queryWhere.waybill_code = { [Op.like]: `%${String(waybillCodeExport).trim()}%` };
|
||||
}
|
||||
const orderIdExport = val(where.orderId);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user