diff --git a/app/service/systemMethods/order/wave.js b/app/service/systemMethods/order/wave.js index 34f3143..ce962ea 100644 --- a/app/service/systemMethods/order/wave.js +++ b/app/service/systemMethods/order/wave.js @@ -1613,11 +1613,354 @@ class WaveService extends OrderBaseService { throw new Error(`不支持的分配状态筛选值: ${normalizedStatusFilter}`); } + /** + * 判断波次列表 where 字段条件是否为空值。 + * + * 空值(undefined/null/空串/空数组)不参与 SQL 拼接; + * 例外:显式声明 isnull/isnotnull 的标准结构即使不带 value 也视为有效条件。 + * + * @param {*} rawCondition 原始条件值。 + * @return {boolean} 是否为空值。 + */ + isEmptyBatchListWhereValue(rawCondition) { + if (rawCondition === undefined || rawCondition === null) { + return true; + } + + if (typeof rawCondition === 'string') { + return rawCondition.trim() === ''; + } + + if (Array.isArray(rawCondition)) { + return rawCondition.length === 0; + } + + if (this.isPlainObject(rawCondition)) { + const hasStandardKeys = [ 'value', 'operator', 'type', 'field', 'values' ] + .some(key => Object.prototype.hasOwnProperty.call(rawCondition, key)); + if (!hasStandardKeys) { + return false; + } + + const operator = String(rawCondition.operator || '').toLowerCase(); + if (operator === 'isnull' || operator === 'isnotnull') { + return false; + } + + const value = Object.prototype.hasOwnProperty.call(rawCondition, 'value') + ? rawCondition.value + : rawCondition.values; + if (value === undefined || value === null) { + return true; + } + if (typeof value === 'string' && value.trim() === '') { + return true; + } + if (Array.isArray(value) && value.length === 0) { + return true; + } + return false; + } + + return false; + } + + /** + * 解析波次列表单字段条件(兼容裸值与 {operator,value,type} 标准结构)。 + * + * - 标准结构(含 value/operator/type/field/values 任一键):显式读取 operator,缺省回落 defaultOperator; + * - 裸值:数组按 in 处理,其余按 defaultOperator(保持历史语义,如 wave_no 默认模糊匹配)。 + * + * @param {*} rawCondition 原始条件值。 + * @param {string} [defaultOperator='eq'] 未显式传 operator 时的默认操作符。 + * @return {{ operator: string, value: *, hasExplicitOperator: boolean }} 解析结果。 + */ + resolveBatchListFieldCondition(rawCondition, defaultOperator = 'eq') { + const isStandardCondition = this.isPlainObject(rawCondition) + && [ 'value', 'operator', 'type', 'field', 'values' ] + .some(key => Object.prototype.hasOwnProperty.call(rawCondition, key)); + + if (isStandardCondition) { + const normalized = this.normalizeWhereCondition('__batch_list_where_field__', rawCondition); + return { + operator: String(normalized.operator || defaultOperator).toLowerCase(), + value: normalized.value, + hasExplicitOperator: true, + }; + } + + return { + operator: Array.isArray(rawCondition) ? 'in' : String(defaultOperator || 'eq').toLowerCase(), + value: rawCondition, + hasExplicitOperator: false, + }; + } + + /** + * 将波次列表单字段条件构建为 SQL 片段与占位符参数。 + * + * 支持操作符:eq/ne/gt/lt/ge/le/in/notin/between/notbetween/like/contains/ + * notlike/startswith/endswith/isnull/isnotnull(调用方通过 allowedOperators 限定)。 + * 数组值:like/contains/startswith/endswith 多值按 OR 展开,notlike 多值按 AND 展开。 + * + * @param {Object} options 构建参数。 + * @param {string} options.fieldKey 字段名(用于错误提示)。 + * @param {string} options.columnName 目标 SQL 列名(服务端白名单常量)。 + * @param {*} options.rawCondition 原始条件值。 + * @param {string} [options.defaultOperator='eq'] 默认操作符。 + * @param {'text'|'integer'|'number'} [options.valueType='text'] 值类型。 + * @param {boolean} [options.allowZero=true] integer 类型是否允许 0。 + * @param {Array} [options.allowedOperators=[]] 允许的操作符集合,空数组表示不限制。 + * @param {Function} [options.valueNormalizer=null] 自定义值归一化函数,优先级最高。 + * @param {string} [options.replacementKeyBase='whereField'] 占位符前缀。 + * @param {Object} [options.replacements={}] 占位符参数收集对象。 + * @return {string} SQL 条件片段(不含前导 AND),无有效条件时返回空字符串。 + */ + buildBatchListFieldConditionSql(options = {}) { + const { + fieldKey, + columnName, + rawCondition, + defaultOperator = 'eq', + valueType = 'text', + allowZero = true, + allowedOperators = [], + valueNormalizer = null, + replacementKeyBase = 'whereField', + replacements = {}, + } = options; + + if (this.isEmptyBatchListWhereValue(rawCondition)) { + return ''; + } + + const resolved = this.resolveBatchListFieldCondition(rawCondition, defaultOperator); + const rawValue = resolved.value; + let operator = String(resolved.operator || defaultOperator).toLowerCase(); + + if (allowedOperators.length > 0 && !allowedOperators.includes(operator)) { + throw new Error(`${fieldKey} 不支持操作符: ${operator},仅支持: ${allowedOperators.join('、')}`); + } + + const normalizeSingleValue = item => { + if (typeof valueNormalizer === 'function') { + return valueNormalizer(item); + } + + if (valueType === 'number') { + const numberValue = Number(item); + if (!Number.isFinite(numberValue)) { + throw new Error(`${fieldKey} 参数不合法`); + } + return numberValue; + } + + if (valueType === 'integer') { + const numberValue = Number(item); + const isValidInteger = Number.isInteger(numberValue) && (allowZero ? numberValue >= 0 : numberValue > 0); + if (!isValidInteger) { + throw new Error(`${fieldKey} 参数不合法`); + } + return numberValue; + } + + const textValue = String(item === undefined || item === null ? '' : item).trim(); + if (!textValue) { + throw new Error(`${fieldKey} 参数不合法`); + } + return textValue; + }; + + const normalizeValueList = () => { + const sourceList = Array.isArray(rawValue) ? rawValue : [ rawValue ]; + const valueList = sourceList.filter(item => item !== undefined && item !== null && String(item).trim() !== ''); + if (valueList.length === 0) { + throw new Error(`${fieldKey} 参数不合法`); + } + return valueList.map(item => normalizeSingleValue(item)); + }; + + // eq + 数组等价于 in(与 Sequelize 字段数组语义保持一致) + if (operator === 'eq' && Array.isArray(rawValue)) { + operator = 'in'; + } + + switch (operator) { + case 'eq': + case 'ne': { + const singleValue = normalizeSingleValue(Array.isArray(rawValue) ? rawValue[0] : rawValue); + replacements[replacementKeyBase] = singleValue; + return `${columnName} ${operator === 'eq' ? '=' : '<>'} :${replacementKeyBase}`; + } + + case 'gt': + case 'lt': + case 'ge': + case 'le': { + const comparators = { + gt: '>', + lt: '<', + ge: '>=', + le: '<=', + }; + const singleValue = normalizeSingleValue(Array.isArray(rawValue) ? rawValue[0] : rawValue); + replacements[replacementKeyBase] = singleValue; + return `${columnName} ${comparators[operator]} :${replacementKeyBase}`; + } + + case 'like': + case 'contains': + case 'startswith': + case 'endswith': { + const valueList = normalizeValueList(); + const useMultipleKeys = valueList.length > 1; + const clauses = valueList.map((textValue, index) => { + const replacementKey = useMultipleKeys ? `${replacementKeyBase}${index}` : replacementKeyBase; + replacements[replacementKey] = operator === 'startswith' + ? `${textValue}%` + : operator === 'endswith' + ? `%${textValue}` + : `%${textValue}%`; + return `${columnName} LIKE :${replacementKey}`; + }); + return clauses.length > 1 ? `(${clauses.join(' OR ')})` : clauses[0]; + } + + case 'notlike': { + const valueList = normalizeValueList(); + const useMultipleKeys = valueList.length > 1; + const clauses = valueList.map((textValue, index) => { + const replacementKey = useMultipleKeys ? `${replacementKeyBase}${index}` : replacementKeyBase; + replacements[replacementKey] = `%${textValue}%`; + return `${columnName} NOT LIKE :${replacementKey}`; + }); + return clauses.length > 1 ? `(${clauses.join(' AND ')})` : clauses[0]; + } + + case 'in': + case 'notin': { + const valueList = normalizeValueList(); + replacements[replacementKeyBase] = valueList; + return `${columnName} ${operator === 'in' ? 'IN' : 'NOT IN'} (:${replacementKeyBase})`; + } + + case 'between': + case 'notbetween': { + let betweenValue = rawValue; + if (this.isPlainObject(betweenValue)) { + const start = betweenValue.start ?? betweenValue.from ?? betweenValue.begin; + const end = betweenValue.end ?? betweenValue.to ?? betweenValue.finish; + betweenValue = [ start, end ]; + } + if (!Array.isArray(betweenValue) || betweenValue.length !== 2) { + throw new Error(`${fieldKey} 的 ${operator} 操作符要求 value 为长度为2的数组`); + } + + const startValue = normalizeSingleValue(betweenValue[0]); + const endValue = normalizeSingleValue(betweenValue[1]); + replacements[`${replacementKeyBase}From`] = startValue; + replacements[`${replacementKeyBase}To`] = endValue; + + return operator === 'between' + ? `${columnName} BETWEEN :${replacementKeyBase}From AND :${replacementKeyBase}To` + : `(${columnName} < :${replacementKeyBase}From OR ${columnName} > :${replacementKeyBase}To)`; + } + + case 'isnull': + return `${columnName} IS NULL`; + + case 'isnotnull': + return `${columnName} IS NOT NULL`; + + default: + throw new Error(`${fieldKey} 不支持操作符: ${operator}`); + } + } + + /** + * 归一化波次列表 distinguish_express 条件值(布尔/数字/文本统一为 0/1)。 + * + * @param {*} value 原始值。 + * @return {number} 0 或 1。 + */ + normalizeBatchListDistinguishExpressValue(value) { + if (typeof value === 'boolean') { + return value ? 1 : 0; + } + + if (typeof value === 'number') { + if (value !== 0 && value !== 1) { + throw new Error('distinguish_express 参数不合法'); + } + return value; + } + + const boolText = String(value === undefined || value === null ? '' : value).trim().toLowerCase(); + if ([ '1', 'true', 'yes', 'y', 'on' ].includes(boolText)) { + return 1; + } + if ([ '0', 'false', 'no', 'n', 'off' ].includes(boolText)) { + return 0; + } + + throw new Error('distinguish_express 参数不合法'); + } + + /** + * 解析波次列表 store_id 筛选条件。 + * + * 兼容单值/数组/{operator,value}:默认按 in 匹配(返回包含任一筛选货主订单的波次,含混合货主波次); + * operator=ne/notin 时反向匹配(排除包含所选货主订单的波次)。 + * + * @param {*} rawCondition 原始条件。 + * @return {{ operator: 'in'|'notin', ids: number[] }} 归一化结果。 + */ + normalizeBatchListStoreIdCondition(rawCondition) { + if (rawCondition === undefined || rawCondition === null || rawCondition === '') { + return { + operator: 'in', + ids: [], + }; + } + + const resolved = this.resolveBatchListFieldCondition(rawCondition, 'in'); + const operator = String(resolved.operator || 'in').toLowerCase(); + if (![ 'eq', 'in', 'ne', 'notin' ].includes(operator)) { + throw new Error(`store_id 不支持操作符: ${operator},仅支持: eq、in、ne、notin`); + } + + const rawValue = resolved.value; + const sourceList = Array.isArray(rawValue) ? rawValue : [ rawValue ]; + const normalizedIds = []; + + for (const item of sourceList) { + if (item === undefined || item === null || item === '') { + continue; + } + + const numberValue = Number(String(item).trim()); + if (!Number.isInteger(numberValue) || numberValue <= 0) { + throw new Error('store_id 参数不合法'); + } + + normalizedIds.push(numberValue); + } + + return { + operator: operator === 'in' || operator === 'eq' ? 'in' : 'notin', + ids: [ ...new Set(normalizedIds) ], + }; + } + /** * 构建波次列表 where 条件(白名单字段)。 * + * 每个白名单字段均支持裸值与 `{ operator, value, type }` 标准结构; + * 未显式传 operator 时按字段默认语义(wave_no/creator/logistics_name/item_part/ + * item_snapshot_key/item_detail_machine 默认 contains 模糊,其余默认 eq)。 + * * @param {Object} whereData 查询 where 入参。 - * @return {{ sql: string, replacements: Object, storageAssignmentStatus: string, expressAssignmentStatus: string, printAssignmentStatus: string, reviewAssignmentStatus: string, storeIds: number[] }} SQL 条件片段、参数与分配状态筛选值。 + * @return {{ sql: string, replacements: Object, storageAssignmentStatus: string, expressAssignmentStatus: string, printAssignmentStatus: string, reviewAssignmentStatus: string, storeIds: number[], storeIdOperator: 'in'|'notin' }} SQL 条件片段、参数与分配状态筛选值。 */ buildBatchListWhereClause(whereData = {}) { const normalizedWhere = this.isPlainObject(whereData) ? whereData : {}; @@ -1640,8 +1983,10 @@ class WaveService extends OrderBaseService { 'item_detail_machine', 'distinguish_express', 'source', + 'total_orders', 'total_orders_min', 'total_orders_max', + 'processed_orders', 'processed_orders_min', 'processed_orders_max', 'storage_assignment_status', @@ -1659,162 +2004,199 @@ class WaveService extends OrderBaseService { const conditions = []; const replacements = {}; - const appendIntegerEqualCondition = (fieldKey, columnName, replacementKey, allowZero = true) => { - const value = normalizedWhere[fieldKey]; - if (value === undefined || value === null || value === '') { - return; + const textSearchOperators = [ 'eq', 'ne', 'like', 'contains', 'notlike', 'startswith', 'endswith', 'in', 'notin', 'isnull', 'isnotnull' ]; + const comparableOperators = [ 'eq', 'ne', 'gt', 'lt', 'ge', 'le', 'in', 'notin', 'between', 'notbetween', 'isnull', 'isnotnull' ]; + + const fieldSpecs = [ + { + fieldKey: 'wave_id', + columnName: 'wave_id', + defaultOperator: 'eq', + valueType: 'integer', + allowZero: false, + allowedOperators: comparableOperators, + replacementKeyBase: 'whereWaveId', + }, + { + fieldKey: 'wave_no', + columnName: 'wave_no', + defaultOperator: 'like', + allowedOperators: textSearchOperators, + replacementKeyBase: 'whereWaveNo', + }, + { + fieldKey: 'wave_type', + columnName: 'wave_type', + defaultOperator: 'eq', + valueType: 'integer', + allowedOperators: comparableOperators, + replacementKeyBase: 'whereWaveType', + }, + { + fieldKey: 'wave_status', + columnName: 'wave_status', + defaultOperator: 'eq', + valueType: 'integer', + allowedOperators: comparableOperators, + replacementKeyBase: 'whereWaveStatus', + }, + { + fieldKey: 'source', + columnName: 'source', + defaultOperator: 'eq', + valueType: 'integer', + allowedOperators: comparableOperators, + replacementKeyBase: 'whereSource', + }, + { + fieldKey: 'creator', + columnName: 'creator', + defaultOperator: 'like', + allowedOperators: textSearchOperators, + replacementKeyBase: 'whereCreator', + }, + { + fieldKey: 'logistics_name', + columnName: 'logistics_name', + defaultOperator: 'like', + allowedOperators: textSearchOperators, + replacementKeyBase: 'whereLogisticsName', + }, + { + fieldKey: 'item_part_type', + columnName: 'item_part_type', + defaultOperator: 'eq', + allowedOperators: textSearchOperators, + replacementKeyBase: 'whereItemPartType', + }, + { + fieldKey: 'item_part', + columnName: 'item_part', + defaultOperator: 'like', + allowedOperators: textSearchOperators, + replacementKeyBase: 'whereItemPart', + }, + { + fieldKey: 'item_snapshot_key', + columnName: 'item_snapshot_key', + defaultOperator: 'like', + allowedOperators: textSearchOperators, + replacementKeyBase: 'whereItemSnapshotKey', + }, + { + fieldKey: 'item_detail_machine', + columnName: 'item_detail_machine', + defaultOperator: 'like', + allowedOperators: textSearchOperators, + replacementKeyBase: 'whereItemDetailMachine', + }, + { + fieldKey: 'distinguish_express', + columnName: 'distinguish_express', + defaultOperator: 'eq', + valueType: 'integer', + valueNormalizer: value => this.normalizeBatchListDistinguishExpressValue(value), + allowedOperators: [ 'eq', 'ne', 'in', 'notin' ], + replacementKeyBase: 'whereDistinguishExpress', + }, + { + fieldKey: 'total_orders', + columnName: 'total_orders', + defaultOperator: 'eq', + valueType: 'number', + allowedOperators: comparableOperators, + replacementKeyBase: 'whereTotalOrders', + }, + { + fieldKey: 'total_orders_min', + columnName: 'total_orders', + defaultOperator: 'ge', + valueType: 'number', + allowedOperators: comparableOperators, + replacementKeyBase: 'whereTotalOrdersMin', + }, + { + fieldKey: 'total_orders_max', + columnName: 'total_orders', + defaultOperator: 'le', + valueType: 'number', + allowedOperators: comparableOperators, + replacementKeyBase: 'whereTotalOrdersMax', + }, + { + fieldKey: 'processed_orders', + columnName: 'processed_orders', + defaultOperator: 'eq', + valueType: 'number', + allowedOperators: comparableOperators, + replacementKeyBase: 'whereProcessedOrders', + }, + { + fieldKey: 'processed_orders_min', + columnName: 'processed_orders', + defaultOperator: 'ge', + valueType: 'number', + allowedOperators: comparableOperators, + replacementKeyBase: 'whereProcessedOrdersMin', + }, + { + fieldKey: 'processed_orders_max', + columnName: 'processed_orders', + defaultOperator: 'le', + valueType: 'number', + allowedOperators: comparableOperators, + replacementKeyBase: 'whereProcessedOrdersMax', + }, + ]; + + for (const fieldSpec of fieldSpecs) { + if (!Object.prototype.hasOwnProperty.call(normalizedWhere, fieldSpec.fieldKey)) { + continue; } - const numberValue = Number(value); - const isValid = Number.isInteger(numberValue) && (allowZero ? numberValue >= 0 : numberValue > 0); - if (!isValid) { - throw new Error(`${fieldKey} 参数不合法`); + const fieldSql = this.buildBatchListFieldConditionSql({ + ...fieldSpec, + rawCondition: normalizedWhere[fieldSpec.fieldKey], + replacements, + }); + if (fieldSql) { + conditions.push(fieldSql); } - - conditions.push(`${columnName} = :${replacementKey}`); - replacements[replacementKey] = numberValue; - }; - - const appendLikeCondition = (fieldKey, columnName, replacementKey) => { - const value = normalizedWhere[fieldKey]; - if (value === undefined || value === null || value === '') { - return; - } - - const normalizedValue = String(value).trim(); - if (!normalizedValue) { - return; - } - - conditions.push(`${columnName} LIKE :${replacementKey}`); - replacements[replacementKey] = `%${normalizedValue}%`; - }; - - const appendMinCondition = (fieldKey, columnName, replacementKey) => { - const value = normalizedWhere[fieldKey]; - if (value === undefined || value === null || value === '') { - return; - } - - const numberValue = Number(value); - if (!Number.isFinite(numberValue)) { - throw new Error(`${fieldKey} 参数不合法`); - } - - conditions.push(`${columnName} >= :${replacementKey}`); - replacements[replacementKey] = numberValue; - }; - - const appendMaxCondition = (fieldKey, columnName, replacementKey) => { - const value = normalizedWhere[fieldKey]; - if (value === undefined || value === null || value === '') { - return; - } - - const numberValue = Number(value); - if (!Number.isFinite(numberValue)) { - throw new Error(`${fieldKey} 参数不合法`); - } - - conditions.push(`${columnName} <= :${replacementKey}`); - replacements[replacementKey] = numberValue; - }; - - appendIntegerEqualCondition('wave_id', 'wave_id', 'whereWaveId', false); - appendLikeCondition('wave_no', 'wave_no', 'whereWaveNo'); - appendIntegerEqualCondition('wave_type', 'wave_type', 'whereWaveType'); - appendIntegerEqualCondition('wave_status', 'wave_status', 'whereWaveStatus'); - appendIntegerEqualCondition('source', 'source', 'whereSource', true); - appendLikeCondition('creator', 'creator', 'whereCreator'); - appendLikeCondition('logistics_name', 'logistics_name', 'whereLogisticsName'); - appendLikeCondition('item_part', 'item_part', 'whereItemPart'); - appendLikeCondition('item_snapshot_key', 'item_snapshot_key', 'whereItemSnapshotKey'); - appendLikeCondition('item_detail_machine', 'item_detail_machine', 'whereItemDetailMachine'); - - const itemPartType = normalizedWhere.item_part_type; - if (itemPartType !== undefined && itemPartType !== null && itemPartType !== '') { - const normalizedItemPartType = String(itemPartType).trim(); - if (!normalizedItemPartType) { - throw new Error('item_part_type 参数不合法'); - } - - conditions.push('item_part_type = :whereItemPartType'); - replacements.whereItemPartType = normalizedItemPartType; } - const distinguishExpress = normalizedWhere.distinguish_express; - if (distinguishExpress !== undefined && distinguishExpress !== null && distinguishExpress !== '') { - let normalizedDistinguishExpress; + // store_id 货主筛选:支持单值/数组/{operator,value},默认 in(含混合货主波次),ne/notin 为排除语义。 + const storeIdCondition = this.normalizeBatchListStoreIdCondition(normalizedWhere.store_id); - if (typeof distinguishExpress === 'boolean') { - normalizedDistinguishExpress = distinguishExpress ? 1 : 0; - } else if (typeof distinguishExpress === 'number') { - if (distinguishExpress !== 0 && distinguishExpress !== 1) { - throw new Error('distinguish_express 参数不合法'); - } - normalizedDistinguishExpress = distinguishExpress; - } else { - const boolText = String(distinguishExpress).trim().toLowerCase(); - if ([ '1', 'true', 'yes', 'y', 'on' ].includes(boolText)) { - normalizedDistinguishExpress = 1; - } else if ([ '0', 'false', 'no', 'n', 'off' ].includes(boolText)) { - normalizedDistinguishExpress = 0; - } else { - throw new Error('distinguish_express 参数不合法'); - } + // 分配状态类筛选:由聚合统计判定,仅支持等值;显式传入其他操作符直接报错。 + const resolveAssignmentStatusFilter = (fieldKey, statusNormalizer) => { + const rawCondition = normalizedWhere[fieldKey]; + if (this.isEmptyBatchListWhereValue(rawCondition)) { + return ''; } - conditions.push('distinguish_express = :whereDistinguishExpress'); - replacements.whereDistinguishExpress = normalizedDistinguishExpress; - } - - appendMinCondition('total_orders_min', 'total_orders', 'whereTotalOrdersMin'); - appendMaxCondition('total_orders_max', 'total_orders', 'whereTotalOrdersMax'); - appendMinCondition('processed_orders_min', 'processed_orders', 'whereProcessedOrdersMin'); - appendMaxCondition('processed_orders_max', 'processed_orders', 'whereProcessedOrdersMax'); - - // store_id 货主筛选:支持单值/数组/{operator,value} 结构,归一化为正整数数组(去重)。 - // 过滤语义:返回包含任一筛选货主订单的波次(含混合货主波次)。 - const normalizeStoreIdList = value => { - if (value === undefined || value === null || value === '') { - return []; + const { operator, value } = this.resolveBatchListFieldCondition(rawCondition, 'eq'); + if (operator !== 'eq') { + throw new Error(`${fieldKey} 不支持操作符: ${operator},仅支持: eq`); } - const rawValue = this.isPlainObject(value) && Object.prototype.hasOwnProperty.call(value, 'value') - ? value.value - : value; - const sourceList = Array.isArray(rawValue) ? rawValue : [ rawValue ]; - const normalizedIds = []; - - for (const item of sourceList) { - if (item === undefined || item === null || item === '') { - continue; - } - - const numberValue = Number(String(item).trim()); - if (!Number.isInteger(numberValue) || numberValue <= 0) { - throw new Error('store_id 参数不合法'); - } - - normalizedIds.push(numberValue); - } - - return [ ...new Set(normalizedIds) ]; + return statusNormalizer(value); }; - const storeIds = normalizeStoreIdList(normalizedWhere.store_id); - - const storageAssignmentStatus = this.normalizeBatchListStorageAssignmentStatusFilter( - normalizedWhere.storage_assignment_status + const storageAssignmentStatus = resolveAssignmentStatusFilter( + 'storage_assignment_status', + value => this.normalizeBatchListStorageAssignmentStatusFilter(value) ); - const expressAssignmentStatus = this.normalizeBatchListExpressAssignmentStatusFilter( - normalizedWhere.express_assignment_status + const expressAssignmentStatus = resolveAssignmentStatusFilter( + 'express_assignment_status', + value => this.normalizeBatchListExpressAssignmentStatusFilter(value) ); - const printAssignmentStatus = this.normalizeBatchListPrintAssignmentStatusFilter( - normalizedWhere.print_assignment_status + const printAssignmentStatus = resolveAssignmentStatusFilter( + 'print_assignment_status', + value => this.normalizeBatchListPrintAssignmentStatusFilter(value) ); - const reviewAssignmentStatus = this.normalizeBatchListReviewAssignmentStatusFilter( - normalizedWhere.review_assignment_status + const reviewAssignmentStatus = resolveAssignmentStatusFilter( + 'review_assignment_status', + value => this.normalizeBatchListReviewAssignmentStatusFilter(value) ); return { @@ -1824,7 +2206,8 @@ class WaveService extends OrderBaseService { expressAssignmentStatus, printAssignmentStatus, reviewAssignmentStatus, - storeIds, + storeIds: storeIdCondition.ids, + storeIdOperator: storeIdCondition.operator, }; } @@ -4657,7 +5040,8 @@ class WaveService extends OrderBaseService { ${whereClause.sql} `; const storeIdFilter = Array.isArray(whereClause.storeIds) ? whereClause.storeIds : []; - const ownerFilterJoinSql = storeIdFilter.length > 0 ? ` + const storeIdExcludeFilter = storeIdFilter.length > 0 && whereClause.storeIdOperator === 'notin'; + const ownerFilterJoinSql = storeIdFilter.length > 0 && !storeIdExcludeFilter ? ` INNER JOIN ( SELECT candidate_wave.wave_id FROM ( @@ -4670,6 +5054,14 @@ class WaveService extends OrderBaseService { GROUP BY candidate_wave.wave_id ) wave_owner_filter ON wave_owner_filter.wave_id = wb.wave_id ` : ''; + // store_id 排除语义(ne/notin):排除包含所选货主订单的波次(无货主订单的波次保留) + const ownerExcludeFilterSql = storeIdExcludeFilter ? ` + AND wb.wave_id NOT IN ( + SELECT DISTINCT od.wave_id + FROM ${orderTableName} od + WHERE od.wave_id IS NOT NULL + AND od.store_id IN (:whereStoreIds) + )` : ''; const batchListFromSql = ` FROM ( SELECT @@ -4722,6 +5114,7 @@ class WaveService extends OrderBaseService { ${ownerFilterJoinSql} LEFT JOIN wms_express e ON e.code = wb.logistics_name WHERE 1 = 1 + ${ownerExcludeFilterSql} ${storageAssignmentFilterClause.sql} ${expressAssignmentFilterClause.sql} ${printAssignmentFilterClause.sql} @@ -9539,6 +9932,7 @@ class WaveService extends OrderBaseService { allocated_order_count: 0, skipped_intercepted_order_ids: skippedInterceptedOrderIds, skipped_locked_order_ids: skippedLockedOrderIds, + skipped_need_oms_waybill_order_ids: [], duration_ms: durationMs, message: skippedInterceptedOrderIds.length > 0 || skippedLockedOrderIds.length > 0 ? `拦截/锁定订单已跳过,未找到可分配快递号的订单(拦截 ${skippedInterceptedOrderIds.length} 单,锁定 ${skippedLockedOrderIds.length} 单)` @@ -9594,6 +9988,9 @@ class WaveService extends OrderBaseService { allocated_order_count: normalizedOrders.length, skipped_intercepted_order_ids: skippedInterceptedOrderIds, skipped_locked_order_ids: skippedLockedOrderIds, + skipped_need_oms_waybill_order_ids: pipelineResult && Array.isArray(pipelineResult.skipped_need_oms_waybill_order_ids) + ? pipelineResult.skipped_need_oms_waybill_order_ids + : [], duration_ms: durationMs, message: String(pipelineResult && pipelineResult.message || '分配快递任务已提交').trim(), },