diff --git a/app/service/systems/order_packing.js b/app/service/systems/order_packing.js index 370f01a..61cc6df 100644 --- a/app/service/systems/order_packing.js +++ b/app/service/systems/order_packing.js @@ -48,7 +48,7 @@ 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] 运单号(模糊匹配) + * @param {Object} [where.waybill_code] 运单号(通过 wms_order_express 解析 order_id 后过滤,支持 eq/like) * @param {Object} [where.wave_no] 波次号(模糊匹配) * @param {Object} [where.order_id] 订单ID(精确匹配) * @param {Object} [where.workbench_id] 工作台ID(精确匹配) @@ -98,11 +98,33 @@ class OrderPackingService extends Service { } } - // 运单号模糊匹配 + // 运单号筛选:通过 wms_order_express 表解析 order_id,再按 order_id 过滤(wms_pack_task.waybill_code 通常为空) 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()) { - queryWhere.waybill_code = { [Op.like]: `%${String(waybillValue).trim()}%` }; + const trimmedValue = String(waybillValue).trim(); + const expressWhere = { + warehouse_id: warehouseId, + delete_at: null, + }; + 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' ], + raw: true, + }); + const matchedOrderIds = [ ...new Set(expressRecords.map(r => r.order_id).filter(Boolean)) ]; + if (matchedOrderIds.length > 0) { + queryWhere.order_id = { [Op.in]: matchedOrderIds }; + } else { + // 无匹配运单号 → 强制返回空结果 + queryWhere.id = -1; + } } } @@ -250,6 +272,34 @@ class OrderPackingService extends Service { } } + // has_video 筛选:1=有视频 2=无视频 + if (where.has_video !== undefined && where.has_video !== null) { + const hasVideoValue = Number(where.has_video?.value ?? where.has_video); + if (hasVideoValue === 1) { + queryWhere[Op.and] = [ ...(queryWhere[Op.and] || []), + app.Sequelize.literal('EXISTS (SELECT 1 FROM wms_pack_task_video WHERE pack_task_id = pack_task.id AND deleted_at IS NULL)'), + ]; + } else if (hasVideoValue === 2) { + queryWhere[Op.and] = [ ...(queryWhere[Op.and] || []), + app.Sequelize.literal('NOT EXISTS (SELECT 1 FROM wms_pack_task_video WHERE pack_task_id = pack_task.id AND deleted_at IS NULL)'), + ]; + } + } + + // video_enable 筛选:1=录像可用 2=录像不可用 + if (where.video_enable !== undefined && where.video_enable !== null) { + const videoEnableValue = Number(where.video_enable?.value ?? where.video_enable); + if (videoEnableValue === 1) { + queryWhere[Op.and] = [ ...(queryWhere[Op.and] || []), + app.Sequelize.literal("EXISTS (SELECT 1 FROM wms_pack_task_video WHERE pack_task_id = pack_task.id AND deleted_at IS NULL AND video_url != '' AND status != 'failed')"), + ]; + } else if (videoEnableValue === 2) { + queryWhere[Op.and] = [ ...(queryWhere[Op.and] || []), + app.Sequelize.literal("NOT EXISTS (SELECT 1 FROM wms_pack_task_video WHERE pack_task_id = pack_task.id AND deleted_at IS NULL AND video_url != '' AND status != 'failed')"), + ]; + } + } + // 开始打包时间范围 if (where.start_time_start) { const startTimeStart = where.start_time_start?.value ?? where.start_time_start; @@ -309,12 +359,15 @@ class OrderPackingService extends Service { const recordingComputerId = await this._getRecordingComputerId(warehouseId); const list = rows.map(row => { const plain = row.get({ plain: true }); + const rawVideos = plain.videos || []; return { ...plain, workbench_name: plain.workbench?.name || null, workbench_type: plain.workbench?.type || null, workbench_barcode: plain.workbench?.barcode || null, - videos: (plain.videos || []).map(v => ({ + has_video: rawVideos.length > 0 ? 1 : 2, + video_enable: rawVideos.some(v => v.video_url && v.status !== 'failed') ? 1 : 2, + videos: rawVideos.map(v => ({ id: v.id, video_url: this._buildVideoUrl(warehouseId, recordingComputerId, v.video_url), status: v.status || null, @@ -411,12 +464,15 @@ class OrderPackingService extends Service { const plain = record.get({ plain: true }); const recordingComputerId = await this._getRecordingComputerId(warehouseId); + const rawVideos = plain.videos || []; const data = { ...plain, workbench_name: plain.workbench?.name || null, workbench_type: plain.workbench?.type || null, workbench_barcode: plain.workbench?.barcode || null, - videos: (plain.videos || []).map(v => ({ + has_video: rawVideos.length > 0 ? 1 : 2, + video_enable: rawVideos.some(v => v.video_url && v.status !== 'failed') ? 1 : 2, + videos: rawVideos.map(v => ({ id: v.id, video_url: this._buildVideoUrl(warehouseId, recordingComputerId, v.video_url), status: v.status || null, @@ -472,7 +528,14 @@ class OrderPackingService extends Service { return { code: 404, message: '打包任务不存在或无权访问', data: null }; } - const updates = { end_time: new Date(end_time) }; + const newEndTime = new Date(end_time); + + // 结束时间不能早于开始时间 + if (record.start_time && newEndTime < new Date(record.start_time)) { + return { code: 400, message: '结束时间不能早于开始时间', data: null }; + } + + const updates = { end_time: newEndTime }; // 若任务状态不是已完成(2),同步更新为已完成 if (record.status !== 2) { @@ -546,30 +609,37 @@ class OrderPackingService extends Service { // 5. 通知打印服务推送强制录制任务 const printServiceUrl = ctx.app.config.printService.url; + const requestData = { pack_task_id: Number(id) }; + let printServiceResponse = null; + let printServiceError = null; try { - await ctx.curl(`${printServiceUrl}/record/force`, { + const res = await ctx.curl(`${printServiceUrl}/record/force`, { method: 'POST', contentType: 'json', - data: { - pack_task_id: Number(id), - warehouse_id: warehouseId, - computer_id: recordingComputer.id, - camera_ids: packTask.device_ids || [], - }, + data: requestData, dataType: 'json', timeout: 10000, }); + printServiceResponse = { + status: res.status, + headers: res.headers, + data: res.data, + }; } catch (err) { + printServiceError = err.message; ctx.logger.warn(`通知打印服务强制录制 pack_task_id=${id} 失败: ${err.message}`); - return { code: 500, message: `通知打印服务失败: ${err.message}`, data: null }; } return { - code: 200, - message: '强制录制任务已推送', + code: printServiceError ? 500 : 200, + message: printServiceError ? `通知打印服务失败: ${printServiceError}` : '强制录制任务已推送', data: { pack_task_id: Number(id), computer_id: recordingComputer.id, + print_service_url: `${printServiceUrl}/record/force`, + print_service_request: requestData, + print_service_response: printServiceResponse, + print_service_error: printServiceError, }, }; } diff --git a/methods/system.json b/methods/system.json index 0e5f33a..d660c3c 100644 --- a/methods/system.json +++ b/methods/system.json @@ -6453,7 +6453,9 @@ { "name": "workbench_id", "type": "number", "required": false, "description": "工作台ID(精确匹配)" }, { "name": "quickWhere", "type": "string", "required": false, "description": "快捷搜索:波次号模糊匹配 + 运单号通过 express→order→wave 链路解析" }, { "name": "start_time_start", "type": "string", "required": false, "description": "开始打包时间范围起始(ISO 8601)" }, - { "name": "start_time_end", "type": "string", "required": false, "description": "开始打包时间范围结束(ISO 8601)" } + { "name": "start_time_end", "type": "string", "required": false, "description": "开始打包时间范围结束(ISO 8601)" }, + { "name": "has_video", "type": "number", "required": false, "description": "是否包含录像:1=有录像 2=无录像" }, + { "name": "video_enable", "type": "number", "required": false, "description": "录像是否可用:1=可用(存在 video_url 非空且 status!=failed 的记录) 2=不可用" } ] } ], @@ -6462,7 +6464,7 @@ "name": "list", "type": "array", "required": true, - "description": "打包任务列表,每项含 id、order_id、warehouse_id、workbench_id、wave_id、wave_no、waybill_code、pack_quantity、item_quantity、sku_count、pack_mode、status、start_time、end_time、operator_text、device_ids、remark、meta、create_time、update_time、workbench_name、workbench_type、workbench_barcode、videos[]、expresses[]" + "description": "打包任务列表,每项含 id、order_id、warehouse_id、workbench_id、wave_id、wave_no、waybill_code、pack_quantity、item_quantity、sku_count、pack_mode、status、start_time、end_time、operator_text、device_ids、remark、meta、create_time、update_time、workbench_name、workbench_type、workbench_barcode、has_video、video_enable、videos[]、expresses[]" }, { "name": "pagination", @@ -6499,7 +6501,7 @@ "name": "data", "type": "object", "required": true, - "description": "打包任务详情,含 id、order_id、warehouse_id、workbench_id、wave_id、wave_no、waybill_code、pack_quantity、item_quantity、sku_count、pack_mode、status、start_time、end_time、operator_text、device_ids、remark、meta、create_time、update_time、workbench_name、workbench_type、workbench_barcode、videos[]、expresses[]" + "description": "打包任务详情,含 id、order_id、warehouse_id、workbench_id、wave_id、wave_no、waybill_code、pack_quantity、item_quantity、sku_count、pack_mode、status、start_time、end_time、operator_text、device_ids、remark、meta、create_time、update_time、workbench_name、workbench_type、workbench_barcode、has_video、video_enable、videos[]、expresses[]" } ], "errors": [