From e605bd80e33b32aabe00a0ffc04bbe381ff8a295 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Fri, 31 Jul 2026 21:37:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20fetchTasks=E4=BA=8B=E5=8A=A1=E9=98=B2?= =?UTF-8?q?=E5=B9=B6=E5=8F=91=E9=87=8D=E5=A4=8D=E5=BD=95=E5=83=8F=E8=AE=B0?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - SELECT ... FOR UPDATE 锁定行防竞态 - 事务内原子化检查+插入,异常回滚 - 查询条件增加 camera_id 区分同任务不同摄像头 --- app/service/hikvision.js | 112 +++++++++++++++++++++------------------ 1 file changed, 61 insertions(+), 51 deletions(-) diff --git a/app/service/hikvision.js b/app/service/hikvision.js index 8aa7f42..407a53a 100644 --- a/app/service/hikvision.js +++ b/app/service/hikvision.js @@ -82,63 +82,73 @@ class HikvisionService extends BaseService { deviceIds = []; } - for (const deviceId of deviceIds) { - // 按设备ID匹配摄像头 - const matchedCamera = cameras.find(c => c.id === deviceId) - || cameras.find(c => c.custom_name && c.custom_name.includes(`${task.workbench_id}号`)) - || cameras[0]; + // 事务:原子化检查+插入,防止并发重复 + const conn = await app.mysql.get('wms').beginTransaction(); + try { + for (const deviceId of deviceIds) { + // 按设备ID匹配摄像头 + const matchedCamera = cameras.find(c => c.id === deviceId) + || cameras.find(c => c.custom_name && c.custom_name.includes(`${task.workbench_id}号`)) + || cameras[0]; - if (!matchedCamera) continue; + if (!matchedCamera) continue; - const matchedServer = servers.find(s => s.id === matchedCamera.server_id) || servers[0]; - if (!matchedServer) continue; + const matchedServer = servers.find(s => s.id === matchedCamera.server_id) || servers[0]; + if (!matchedServer) continue; - // 先创建录像记录(状态=created),防止重复拉取 - const existingVideo = await app.mysql.get('wms').get('wms_pack_task_video', { - pack_task_id: task.id, - deleted_at: null, - }); - if (!existingVideo) { - await app.mysql.get('wms').insert('wms_pack_task_video', { + // 事务内 SELECT ... FOR UPDATE 锁定检查,防止并发重复 + const [existing] = await conn.query( + `SELECT id FROM wms_pack_task_video + WHERE pack_task_id = ? AND camera_id = ? AND deleted_at IS NULL + FOR UPDATE`, + [task.id, matchedCamera.id] + ); + if (!existing || existing.length === 0) { + await conn.insert('wms_pack_task_video', { + pack_task_id: task.id, + camera_id: matchedCamera.id, + video_url: '', + status: 'created', + }); + console.log(`[Hikvision] 创建录像记录: pack_task=${task.id}, camera=${matchedCamera.id}`); + } + + result.push({ pack_task_id: task.id, - camera_id: matchedCamera.id, - video_url: '', - status: 'created', + device_id: deviceId, + uniqueCode: `${task.id}_${deviceId}`, + order_id: task.order_id, + warehouse_id: task.warehouse_id, + workbench_id: task.workbench_id, + wave_no: task.wave_no, + waybill_code: task.waybill_code, + task_status: task.task_status, + start_time: this._toISO(task.start_time, -5000), + end_time: this._toISO(task.end_time), + operator: task.operator_text, + camera: { + id: matchedCamera.id, + channel_id: matchedCamera.channel_id, + channel_name: matchedCamera.channel_name, + ip: matchedCamera.ip, + port: matchedCamera.port, + model: matchedCamera.model, + custom_name: matchedCamera.custom_name, + }, + server: { + id: matchedServer.id, + brand: matchedServer.brand, + internal_ip: matchedServer.internal_ip, + internal_port: matchedServer.internal_port || 80, + username: matchedServer.username, + password: matchedServer.password, + }, }); - console.log(`[Hikvision] 创建录像记录: pack_task=${task.id}, camera=${matchedCamera.id}`); } - - result.push({ - pack_task_id: task.id, - device_id: deviceId, - uniqueCode: `${task.id}_${deviceId}`, - order_id: task.order_id, - warehouse_id: task.warehouse_id, - workbench_id: task.workbench_id, - wave_no: task.wave_no, - waybill_code: task.waybill_code, - task_status: task.task_status, - start_time: this._toISO(task.start_time, -5000), - end_time: this._toISO(task.end_time), - operator: task.operator_text, - camera: { - id: matchedCamera.id, - channel_id: matchedCamera.channel_id, - channel_name: matchedCamera.channel_name, - ip: matchedCamera.ip, - port: matchedCamera.port, - model: matchedCamera.model, - custom_name: matchedCamera.custom_name, - }, - server: { - id: matchedServer.id, - brand: matchedServer.brand, - internal_ip: matchedServer.internal_ip, - internal_port: matchedServer.internal_port || 80, - username: matchedServer.username, - password: matchedServer.password, - }, - }); + await conn.commit(); + } catch (txError) { + await conn.rollback(); + throw txError; } }