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 = [];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user