order.packing.list / order.packing.export 新增 where.operator_text(打包员,目标列 wms_pack_task.operator_text,多人姓名逗号拼接快照):
支持 {operator,value,type} 标准结构,value 支持字符串或数组(多值取并集);contains/like 与缺省为 LIKE '%值%'(通配符已转义)、
eq/in 精确、ne 不等、notlike 逐个排除;值为 ''/null/空数组时忽略该条件。
returnUnpack.list / returnUnpack.export 新增 where.unpacker_name(拆包人,目标列 wms_return_unpack.unpacker_name 工号名称快照):
{operator:'like',value} 或直接值为 LIKE '%值%',value 为数组时多值 LIKE 取并集(_resolveConditionValue 的 like 分支增强,标量行为不变);
文本字段循环跳过空值条件,避免生成 unpacker_name IS NULL 的恒空条件。两个 export 与 list 复用同一 _buildListWhere,语义自动对齐。
顺带修复:where.operator_text=null 经 prepareWhereContext 归一为 {operator:'eq',value:null} 后,
原「?.value ?? 原条件对象」写法会 String() 出 '[object Object]' 条件把列表清空,改为「条件对象只取 .value」。
文档:methods/system.json 更新 order.packing.list/export 与 returnUnpack.list/export 共 4 处参数说明(版本 0.3.190→0.3.191),
methods/changes/system.json 追加变更记录 2026.09.26-order-packing-returnUnpack-where-person-filter。
测试:新增 order_packing.list.operatorText(13 例)与 return_unpack.list.unpackerName(6 例),
test/support/where_matcher.js 补充字段级 Op.or/Op.and/Op.not 支持;相关 4 个测试文件 37 passing。
154 lines
5.1 KiB
JavaScript
154 lines
5.1 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* Sequelize where 条件匹配器(测试专用)。
|
||
* 供 service 层测试的 mock 模型复用:在内存中按真实 where 语义过滤 fixture 行,
|
||
* 重点支持 Op.like(含 % _ 与反斜杠转义语义),避免只断言到「假行为」。
|
||
*/
|
||
|
||
const Sequelize = require('sequelize');
|
||
|
||
const { Op } = Sequelize;
|
||
|
||
/**
|
||
* 宽松等值比较:先严格比较,再对数字做数值化比较(兼容字符串/数字混用)。
|
||
* @param {*} left 左值。
|
||
* @param {*} right 右值。
|
||
* @return {boolean} 是否等值。
|
||
*/
|
||
function looseEqual(left, right) {
|
||
if (left === right) return true;
|
||
if (left === null || right === null || left === undefined || right === undefined) return false;
|
||
|
||
const leftNum = Number(left);
|
||
const rightNum = Number(right);
|
||
if (String(left).trim() !== '' && String(right).trim() !== ''
|
||
&& Number.isFinite(leftNum) && Number.isFinite(rightNum)) {
|
||
return leftNum === rightNum;
|
||
}
|
||
|
||
return String(left) === String(right);
|
||
}
|
||
|
||
/**
|
||
* 转义正则元字符。
|
||
* @param {string} ch 单个字符。
|
||
* @return {string} 转义后的字符。
|
||
*/
|
||
function escapeRegExpChar(ch) {
|
||
return ch.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||
}
|
||
|
||
/**
|
||
* 将 SQL LIKE 模式转换为等价正则(支持 \ 转义 % _ \)。
|
||
* @param {string} pattern LIKE 模式。
|
||
* @return {RegExp} 等价正则。
|
||
*/
|
||
function likeToRegExp(pattern) {
|
||
let body = '^';
|
||
for (let index = 0; index < pattern.length; index++) {
|
||
const ch = pattern[index];
|
||
if (ch === '\\') {
|
||
const next = pattern[index + 1];
|
||
if (next !== undefined) {
|
||
body += escapeRegExpChar(next);
|
||
index++;
|
||
} else {
|
||
body += '\\\\';
|
||
}
|
||
} else if (ch === '%') {
|
||
body += '.*';
|
||
} else if (ch === '_') {
|
||
body += '.';
|
||
} else {
|
||
body += escapeRegExpChar(ch);
|
||
}
|
||
}
|
||
body += '$';
|
||
return new RegExp(body);
|
||
}
|
||
|
||
/**
|
||
* 判断字段值是否命中 LIKE 模式。
|
||
* @param {*} value 字段值。
|
||
* @param {string} pattern LIKE 模式。
|
||
* @return {boolean} 是否命中。
|
||
*/
|
||
function matchLike(value, pattern) {
|
||
return likeToRegExp(String(pattern)).test(String(value === null || value === undefined ? '' : value));
|
||
}
|
||
|
||
/**
|
||
* 判断单条条件对象是否命中。
|
||
* @param {Object} row 待匹配行。
|
||
* @param {string} field 字段名。
|
||
* @param {Object} condition 条件对象。
|
||
* @return {boolean} 是否命中。
|
||
*/
|
||
function matchConditionObject(row, field, condition) {
|
||
const symbols = Object.getOwnPropertySymbols(condition);
|
||
if (symbols.length > 0) {
|
||
return symbols.every(symbol => {
|
||
const value = condition[symbol];
|
||
if (symbol === Op.eq) return looseEqual(row[field], value);
|
||
if (symbol === Op.ne) return !looseEqual(row[field], value);
|
||
if (symbol === Op.in) return Array.isArray(value) && value.some(item => looseEqual(row[field], item));
|
||
if (symbol === Op.notIn) return Array.isArray(value) && !value.some(item => looseEqual(row[field], item));
|
||
if (symbol === Op.like || symbol === Op.iLike) return matchLike(row[field], value);
|
||
if (symbol === Op.notLike) return !matchLike(row[field], value);
|
||
if (symbol === Op.gt) return Number(row[field]) > Number(value);
|
||
if (symbol === Op.gte) return Number(row[field]) >= Number(value);
|
||
if (symbol === Op.lt) return Number(row[field]) < Number(value);
|
||
if (symbol === Op.lte) return Number(row[field]) <= Number(value);
|
||
if (symbol === Op.or) return Array.isArray(value) && value.some(item => matchConditionObject(row, field, item));
|
||
if (symbol === Op.and) return Array.isArray(value) && value.every(item => matchConditionObject(row, field, item));
|
||
if (symbol === Op.not) return !matchConditionObject(row, field, value);
|
||
return true;
|
||
});
|
||
}
|
||
|
||
if (Object.keys(condition).length > 0) {
|
||
return matchesWhere(row[field], condition);
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 判断 mock 行数据是否匹配 where 条件(支持 Op.and / Op.or / Op.not / Op.like 等)。
|
||
* @param {Object|*} row 待匹配行。
|
||
* @param {Object|Array|*} where Sequelize where 条件。
|
||
* @return {boolean} 是否匹配。
|
||
*/
|
||
function matchesWhere(row, where) {
|
||
if (where === null || where === undefined) return true;
|
||
if (typeof where !== 'object') return looseEqual(row, where);
|
||
if (Array.isArray(where)) return where.some(item => matchesWhere(row, item));
|
||
|
||
if (Array.isArray(where[Op.and]) && !where[Op.and].every(item => matchesWhere(row, item))) {
|
||
return false;
|
||
}
|
||
if (Array.isArray(where[Op.or]) && !where[Op.or].some(item => matchesWhere(row, item))) {
|
||
return false;
|
||
}
|
||
if (where[Op.not] !== undefined && matchesWhere(row, where[Op.not])) {
|
||
return false;
|
||
}
|
||
|
||
return Object.entries(where).every(([ field, condition ]) => {
|
||
if (condition && typeof condition === 'object' && !Array.isArray(condition)) {
|
||
return matchConditionObject(row, field, condition);
|
||
}
|
||
if (Array.isArray(condition)) {
|
||
return condition.some(item => looseEqual(row[field], item));
|
||
}
|
||
return looseEqual(row[field], condition);
|
||
});
|
||
}
|
||
|
||
module.exports = {
|
||
looseEqual,
|
||
matchLike,
|
||
matchesWhere,
|
||
};
|