diff --git a/app/service/systemMethods/order/base.js b/app/service/systemMethods/order/base.js index 7f7c006..9b6354f 100644 --- a/app/service/systemMethods/order/base.js +++ b/app/service/systemMethods/order/base.js @@ -910,6 +910,102 @@ class OrderBaseService extends MainService { }; } + /** + * 提取买家留言虚拟筛选条件 buyer_message_has。 + * 覆盖 buyer_message / seller_message / remark 三个字段,任一有值即为"有留言"。 + * 将其从 where 中移除并解析为布尔值,后续在 buildWhere 之后注入 Sequelize 条件。 + * @param {Object} whereInput 标准化后的 where 条件。 + * @return {{ where: Object, buyerMessageHas: boolean|null }} 清洗后的 where 与买家留言标记。 + */ + extractBuyerMessageHasVirtualFilter(whereInput = {}) { + const normalizedWhere = this.isPlainObject(whereInput) + ? { ...whereInput } + : {}; + const buyerMessageHasCondition = normalizedWhere.buyer_message_has; + + delete normalizedWhere.buyer_message_has; + + if (!buyerMessageHasCondition) { + return { where: normalizedWhere, buyerMessageHas: null }; + } + + const rawValue = this.isPlainObject(buyerMessageHasCondition) + && Object.prototype.hasOwnProperty.call(buyerMessageHasCondition, 'value') + ? buyerMessageHasCondition.value + : buyerMessageHasCondition; + + // 兼容布尔值、字符串 'true'/'false'、'Y'/'N'、数字 1/0、以及前端 select 组件值 'has'/'empty'/'none' + const normalizedRawValue = typeof rawValue === 'string' ? rawValue.trim().toLowerCase() : rawValue; + if (rawValue === true || normalizedRawValue === 'true' || normalizedRawValue === 'y' || normalizedRawValue === 'has' || rawValue === 1) { + return { where: normalizedWhere, buyerMessageHas: true }; + } + if (rawValue === false || normalizedRawValue === 'false' || normalizedRawValue === 'n' || normalizedRawValue === 'empty' || normalizedRawValue === 'none' || rawValue === 0) { + return { where: normalizedWhere, buyerMessageHas: false }; + } + + return { where: normalizedWhere, buyerMessageHas: null }; + } + + /** + * 构建 buyer_message_has 虚拟字段的 Sequelize where 条件。 + * 覆盖三个留言/备注字段:buyer_message、seller_message、remark。 + * @param {boolean} hasMessage true=任一字段有留言,false=三个字段全部为空。 + * @return {Object|null} Sequelize where 条件,无效时返回 null。 + */ + buildBuyerMessageHasCondition(hasMessage) { + if (typeof hasMessage !== 'boolean') { + return null; + } + const { Op } = this.app.Sequelize; + const hasContent = col => ({ + [Op.and]: [ + { [col]: { [Op.not]: null } }, + { [col]: { [Op.ne]: '' } }, + ], + }); + const isEmpty = col => ({ + [Op.or]: [ + { [col]: { [Op.is]: null } }, + { [col]: { [Op.eq]: '' } }, + ], + }); + const fields = [ 'buyer_message', 'seller_message', 'remark' ]; + if (hasMessage) { + return { [Op.or]: fields.map(col => hasContent(col)) }; + } + return { [Op.and]: fields.map(col => isEmpty(col)) }; + } + + /** + * 将附加条件并入 queryWhere,兼容 flat 对象与 { [Op.and]: [...] } 结构。 + * @param {Object} queryWhere 现有 Sequelize where 条件。 + * @param {Object} extraCondition 待并入的条件。 + * @return {Object} 合并后的 where 条件。 + */ + appendConditionToWhere(queryWhere = {}, extraCondition = null) { + if (!extraCondition || !this.isPlainObject(extraCondition) + || (Object.keys(extraCondition).length === 0 && Object.getOwnPropertySymbols(extraCondition).length === 0)) { + return queryWhere; + } + const { Op } = this.app.Sequelize; + + if (!this.isPlainObject(queryWhere) + || (Object.keys(queryWhere).length === 0 && Object.getOwnPropertySymbols(queryWhere).length === 0)) { + return extraCondition; + } + + if (Array.isArray(queryWhere[Op.and])) { + return { + ...queryWhere, + [Op.and]: [ ...queryWhere[Op.and], extraCondition ], + }; + } + + return { + [Op.and]: [ queryWhere, extraCondition ], + }; + } + /** * 规范化运单号筛选输入,兼容字符串、数组与 { value } 结构。 * @param {*} condition 运单号筛选条件。 @@ -1150,7 +1246,8 @@ class OrderBaseService extends MainService { }, }); const { where: sanitizedWhere, expressNumbers } = this.extractOrderListExpressVirtualFilter(where); - const mergedWhere = this.mergeWhereObjects(this.getDefaultWhere(moduleName), sanitizedWhere); + const { where: whereAfterBuyerMessage, buyerMessageHas } = this.extractBuyerMessageHasVirtualFilter(sanitizedWhere); + const mergedWhere = this.mergeWhereObjects(this.getDefaultWhere(moduleName), whereAfterBuyerMessage); let queryWhere = this.buildWhere(mergedWhere, contextWarehouseId, { specialFieldMap: { quickWhere: { @@ -1164,6 +1261,11 @@ class OrderBaseService extends MainService { queryWhere = this.appendOrderIdsFilterToWhere(queryWhere, orderIds); } + if (buyerMessageHas !== null) { + const buyerMessageCondition = this.buildBuyerMessageHasCondition(buyerMessageHas); + queryWhere = this.appendConditionToWhere(queryWhere, buyerMessageCondition); + } + const model = await this.getOrderModel(moduleName, contextWarehouseId); const order = this.buildListOrder(requestData.sort, moduleName); const offset = (page - 1) * pageSize; diff --git a/app/service/systemMethods/print.js b/app/service/systemMethods/print.js index 7cdc50a..e0135f6 100644 --- a/app/service/systemMethods/print.js +++ b/app/service/systemMethods/print.js @@ -971,6 +971,18 @@ class PrintService extends MainService { printIds: Array.isArray(expressSendResult.printIds) ? expressSendResult.printIds : [], }); } + // 异步检查是否需要解绑拣货车(不阻塞返回) + const expressWaveIds = this.extractUniqueWaveIds(data.waveGroups); + for (const wid of expressWaveIds) { + this.tryUnbindCartAfterPrintComplete(wid).catch(err => { + this.ctx.logger.error('[print.send] 异步解绑车标异常', { + waveId: wid, + warehouseId, + error: err && err.message ? err.message : `${err}`, + }); + }); + } + this.ctx.logger.info('[print.send] 最终返回 success', { warehouseId, type, @@ -1194,6 +1206,23 @@ class PrintService extends MainService { return Array.from(platformCodeSet); } + /** + * 从 waveGroups 中提取去重后的 wave_id 列表。 + * @param {Array} waveGroups 波次分组数据。 + * @return {Array} 去重后的波次ID列表。 + */ + extractUniqueWaveIds(waveGroups = []) { + const normalizedWaveGroups = Array.isArray(waveGroups) ? waveGroups : []; + const waveIdSet = new Set(); + for (const waveGroup of normalizedWaveGroups) { + const waveId = Number(waveGroup && waveGroup.wave_id || 0); + if (Number.isInteger(waveId) && waveId > 0) { + waveIdSet.add(waveId); + } + } + return Array.from(waveIdSet); + } + /** * 从波次平台数据中提取订单ID列表。 * @@ -2750,6 +2779,68 @@ class PrintService extends MainService { return this.success('success', { data: null, code: 0 }); } + + async tryUnbindCartAfterPrintComplete(waveId, warehouseId) { + const { ctx } = this; + const resolvedWarehouseId = Number(warehouseId || ctx.user && ctx.user.store_id || 0); + let taskId = 0; + let waveType = 0; + try { + const waveBatch = await ctx.model.WaveBatch.findOne({ where: { wave_id: waveId }, attributes: [ 'wave_type' ], raw: true }); + if (!waveBatch) return; + waveType = Number(waveBatch.wave_type); + if (![ 1, 2 ].includes(waveType)) return; + const taskWaveRows = await ctx.model.query( + 'SELECT task_id FROM wms_wave_pick_task_wave WHERE wave_id = :waveId AND warehouse_id = :warehouseId LIMIT 1', + { replacements: { waveId, warehouseId: resolvedWarehouseId }, type: ctx.model.QueryTypes.SELECT } + ); + const taskWave = Array.isArray(taskWaveRows) && taskWaveRows.length > 0 ? taskWaveRows[0] : null; + if (!taskWave || !taskWave.task_id) return; + taskId = Number(taskWave.task_id); + if (!taskId) return; + + if (waveType === 1) { + const taskRows = await ctx.model.query( + 'SELECT pick_mode FROM wms_wave_pick_task WHERE id = :taskId LIMIT 1', + { replacements: { taskId }, type: ctx.model.QueryTypes.SELECT } + ); + const task = Array.isArray(taskRows) && taskRows.length > 0 ? taskRows[0] : null; + const pickMode = task ? Number(task.pick_mode) : 0; + if (pickMode !== 1) return; + } + + // 团购 / 散单统一按订单维度检查:所有订单均已打印时才解绑 + const taskOrders = await ctx.model.query( + 'SELECT DISTINCT order_id FROM wms_wave_pick_task_order WHERE task_id = :taskId', + { replacements: { taskId }, type: ctx.model.QueryTypes.SELECT } + ); + const orderIds = (Array.isArray(taskOrders) ? taskOrders : []).map(r => Number(r && r.order_id)).filter(id => id > 0); + if (orderIds.length === 0) return; + const orderDeliveryFactory = ctx.model && ctx.model.OrderDeliveryFactory; + if (!orderDeliveryFactory || typeof orderDeliveryFactory.getCurrentModel !== 'function') { + this.ctx.logger.warn('[print.unbindCart] OrderDeliveryFactory 不可用,跳过检查'); + return; + } + const orderDeliveryModel = await orderDeliveryFactory.getCurrentModel(resolvedWarehouseId); + const unprintedCount = await orderDeliveryModel.count({ + where: { id: { [ctx.model.Sequelize.Op.in]: orderIds }, print_status: 'none' }, + }); + if (unprintedCount > 0) { + this.ctx.logger.info('[print.unbindCart] 尚有未打印订单,跳过解绑', { waveId, warehouseId, taskId, waveType, waveTypeLabel: waveType === 1 ? '团购' : '散单', totalOrders: orderIds.length, unprintedCount }); + return; + } + + const operator = this.getPrintOrderOperationOperator(); + const unbindUserId = operator.id || null; + await ctx.model.query( + 'UPDATE wms_pick_task_cart_binding SET is_active = 0, unbind_user_id = :unbindUserId, unbind_time = NOW(), update_time = NOW() WHERE task_id = :taskId AND is_active = 1', + { replacements: { taskId, unbindUserId } } + ); + this.ctx.logger.info('[print.unbindCart] 车标解绑成功', { waveId, warehouseId: resolvedWarehouseId, taskId, waveType, waveTypeLabel: waveType === 1 ? '团购' : '散单', unbindUserId }); + } catch (error) { + this.ctx.logger.error('[print.unbindCart] 解绑车标检查异常', { waveId, warehouseId: resolvedWarehouseId, taskId, waveType, error: error && error.message ? error.message : String(error) }); + } + } } module.exports = PrintService; diff --git a/app/service/systems/order_export.js b/app/service/systems/order_export.js index 2c24b8c..4c66a92 100644 --- a/app/service/systems/order_export.js +++ b/app/service/systems/order_export.js @@ -615,9 +615,10 @@ class OrderExportService extends OrderBaseService { }, }); const { where: sanitizedWhere, expressNumbers } = this.extractOrderListExpressVirtualFilter(where); + const { where: whereAfterBuyerMessage, buyerMessageHas } = this.extractBuyerMessageHasVirtualFilter(sanitizedWhere); const mergedWhere = this.mergeWhereObjects( target.defaultWhereModuleName ? this.getDefaultWhere(target.defaultWhereModuleName) : {}, - sanitizedWhere + whereAfterBuyerMessage ); let queryWhere = this.buildWhere(mergedWhere, warehouseId, { specialFieldMap: { @@ -632,6 +633,11 @@ class OrderExportService extends OrderBaseService { queryWhere = this.appendOrderIdsFilterToWhere(queryWhere, orderIds); } + if (buyerMessageHas !== null) { + const buyerMessageCondition = this.buildBuyerMessageHasCondition(buyerMessageHas); + queryWhere = this.appendConditionToWhere(queryWhere, buyerMessageCondition); + } + const orderModel = await this.getOrderModelByTableType(target.tableType, warehouseId); const pageSize = this.getExportPageSize(); const order = this.buildListOrder(requestData.sort, target.sortModuleName);