fix: fetchTasks事务防并发重复录像记录
- SELECT ... FOR UPDATE 锁定行防竞态 - 事务内原子化检查+插入,异常回滚 - 查询条件增加 camera_id 区分同任务不同摄像头
This commit is contained in:
parent
a216aad2ce
commit
e605bd80e3
@ -82,63 +82,73 @@ class HikvisionService extends BaseService {
|
|||||||
deviceIds = [];
|
deviceIds = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const deviceId of deviceIds) {
|
// 事务:原子化检查+插入,防止并发重复
|
||||||
// 按设备ID匹配摄像头
|
const conn = await app.mysql.get('wms').beginTransaction();
|
||||||
const matchedCamera = cameras.find(c => c.id === deviceId)
|
try {
|
||||||
|| cameras.find(c => c.custom_name && c.custom_name.includes(`${task.workbench_id}号`))
|
for (const deviceId of deviceIds) {
|
||||||
|| cameras[0];
|
// 按设备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];
|
const matchedServer = servers.find(s => s.id === matchedCamera.server_id) || servers[0];
|
||||||
if (!matchedServer) continue;
|
if (!matchedServer) continue;
|
||||||
|
|
||||||
// 先创建录像记录(状态=created),防止重复拉取
|
// 事务内 SELECT ... FOR UPDATE 锁定检查,防止并发重复
|
||||||
const existingVideo = await app.mysql.get('wms').get('wms_pack_task_video', {
|
const [existing] = await conn.query(
|
||||||
pack_task_id: task.id,
|
`SELECT id FROM wms_pack_task_video
|
||||||
deleted_at: null,
|
WHERE pack_task_id = ? AND camera_id = ? AND deleted_at IS NULL
|
||||||
});
|
FOR UPDATE`,
|
||||||
if (!existingVideo) {
|
[task.id, matchedCamera.id]
|
||||||
await app.mysql.get('wms').insert('wms_pack_task_video', {
|
);
|
||||||
|
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,
|
pack_task_id: task.id,
|
||||||
camera_id: matchedCamera.id,
|
device_id: deviceId,
|
||||||
video_url: '',
|
uniqueCode: `${task.id}_${deviceId}`,
|
||||||
status: 'created',
|
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}`);
|
|
||||||
}
|
}
|
||||||
|
await conn.commit();
|
||||||
result.push({
|
} catch (txError) {
|
||||||
pack_task_id: task.id,
|
await conn.rollback();
|
||||||
device_id: deviceId,
|
throw txError;
|
||||||
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,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user