179 lines
6.4 KiB
JavaScript
179 lines
6.4 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 给历史扫码记录**补工作台快照**(按当前配置)
|
||
*
|
||
* 背景:`scan_records.workstation_snapshot` 是 2026-10-18 才加的列,之前的记录没有快照;
|
||
* 查询时只能回落「当前绑定」并打 `snapshotMiss`。这个脚本把指定运单(或全部缺快照的记录)
|
||
* 按**当前**工作台配置补一份写进去,之后查询就走正常路径。
|
||
*
|
||
* ⚠️ 语义提醒:补出来的快照 = **现在**的配置,不是当年的。所以只适合
|
||
* 「设备刚上线、工作台配置没变过」的历史数据。真要严谨,只有当时写过快照的记录才准。
|
||
*
|
||
* 用法(在 backend 目录下):
|
||
* node scripts/backfill-snapshot.js --code=79150895123619 # 只补这个运单(跳过已有快照的)
|
||
* node scripts/backfill-snapshot.js --code=79150895123619 --all # 连已有快照也覆盖成当前配置
|
||
* node scripts/backfill-snapshot.js # 全部缺快照的记录
|
||
* node scripts/backfill-snapshot.js --dry-run # 只打印,不写库
|
||
*
|
||
* 依赖:`scripts/db-config.js`(与其它运维脚本一致,读 config.local.js / 环境变量)
|
||
*/
|
||
|
||
const resolveDbConfig = require('./db-config.js');
|
||
const mysql = require('mysql2/promise');
|
||
const { buildSnapshot } = require('../app/lib/workstation-snapshot.js');
|
||
|
||
/** 解析 `--key=value` */
|
||
function arg(name) {
|
||
const prefix = `--${name}=`;
|
||
const hit = process.argv.slice(2).find(item => item.startsWith(prefix));
|
||
return hit ? hit.slice(prefix.length) : null;
|
||
}
|
||
|
||
function flag(name) {
|
||
return process.argv.slice(2).includes(`--${name}`);
|
||
}
|
||
|
||
(async () => {
|
||
const code = arg('code');
|
||
const includeExisting = flag('all');
|
||
const dryRun = flag('dry-run');
|
||
|
||
const conn = await mysql.createConnection(resolveDbConfig());
|
||
|
||
// ① 目标记录
|
||
const where = [];
|
||
const params = [];
|
||
if (code) {
|
||
where.push('barcode = ?');
|
||
params.push(code);
|
||
}
|
||
if (!includeExisting) where.push('workstation_snapshot IS NULL');
|
||
|
||
const [records] = await conn.query(
|
||
`SELECT id, scanner_id, channel_id, barcode, scan_time
|
||
FROM scan_records
|
||
${where.length ? `WHERE ${where.join(' AND ')}` : ''}
|
||
ORDER BY scan_time ASC, id ASC`,
|
||
params
|
||
);
|
||
|
||
console.log(`目标记录:${records.length} 条${code ? `(运单 ${code})` : ''}`
|
||
+ `${dryRun ? '(dry-run,不写库)' : ''}`);
|
||
if (!records.length) {
|
||
await conn.end();
|
||
return;
|
||
}
|
||
|
||
// ② 当前「通道 → 工作台」绑定(一个通道绑多个工作台时取 workstation_id 最小的,与 service 一致)
|
||
const [links] = await conn.query(
|
||
`SELECT wsc.scanner_id, wsc.channel_id, wsc.channel_name,
|
||
w.id AS workstation_id, w.code, w.name, w.ws_type,
|
||
w.record_before_sec, w.record_after_sec, w.record_max_sec,
|
||
w.canvas_width, w.canvas_height
|
||
FROM workstation_scan_channels wsc
|
||
JOIN workstations w ON w.id = wsc.workstation_id AND w.deleted_at IS NULL
|
||
ORDER BY wsc.workstation_id ASC, wsc.id ASC`
|
||
);
|
||
|
||
const linkMap = new Map(); // `${scanner_id}|${channel_id}` → 绑定
|
||
for (const link of links) {
|
||
const key = `${link.scanner_id}|${link.channel_id}`;
|
||
if (!linkMap.has(key)) linkMap.set(key, link);
|
||
}
|
||
|
||
// ③ 工作台 → 摄像头(含 NVR)
|
||
const [cameraRows] = await conn.query(
|
||
`SELECT wc.workstation_id, wc.sort_no, wc.x, wc.y, wc.width, wc.height, wc.stream_type, wc.z_index,
|
||
c.id AS camera_id, c.name AS camera_name, c.channel_name, c.channel_no, c.nvr_id,
|
||
n.name AS nvr_name, n.host AS nvr_host, n.port AS nvr_port, n.username AS nvr_username
|
||
FROM workstation_cameras wc
|
||
LEFT JOIN cameras c ON c.id = wc.camera_id AND c.deleted_at IS NULL
|
||
LEFT JOIN nvrs n ON n.id = c.nvr_id AND n.deleted_at IS NULL
|
||
ORDER BY wc.workstation_id ASC, wc.sort_no ASC, wc.id ASC`
|
||
);
|
||
|
||
const camerasOf = new Map(); // workstation_id → cameraLinks[]
|
||
for (const row of cameraRows) {
|
||
// 摄像头/NVR 被删了就不进快照(与 service 的 required:false + filter 一致)
|
||
if (!row.camera_id) continue;
|
||
|
||
const list = camerasOf.get(row.workstation_id) || [];
|
||
list.push({
|
||
sortNo: row.sort_no,
|
||
x: row.x,
|
||
y: row.y,
|
||
width: row.width,
|
||
height: row.height,
|
||
streamType: row.stream_type,
|
||
zIndex: row.z_index,
|
||
camera: {
|
||
id: row.camera_id,
|
||
name: row.camera_name,
|
||
channelName: row.channel_name,
|
||
channelNo: row.channel_no,
|
||
nvrId: row.nvr_id,
|
||
nvr: {
|
||
name: row.nvr_name,
|
||
host: row.nvr_host,
|
||
port: row.nvr_port,
|
||
username: row.nvr_username,
|
||
},
|
||
},
|
||
});
|
||
camerasOf.set(row.workstation_id, list);
|
||
}
|
||
|
||
// ④ 逐条补
|
||
let filled = 0;
|
||
let noBinding = 0;
|
||
|
||
for (const record of records) {
|
||
const key = `${record.scanner_id}|${record.channel_id}`;
|
||
const link = linkMap.get(key);
|
||
if (!link) {
|
||
noBinding += 1;
|
||
console.log(` ✗ #${record.id} ${record.scan_time.toISOString?.() || record.scan_time}`
|
||
+ ` ${record.channel_id}:该通道当前**没有**绑定工作台,跳过`);
|
||
continue;
|
||
}
|
||
|
||
const workstation = {
|
||
id: link.workstation_id,
|
||
code: link.code,
|
||
name: link.name,
|
||
wsType: link.ws_type,
|
||
recordBeforeSec: link.record_before_sec,
|
||
recordAfterSec: link.record_after_sec,
|
||
recordMaxSec: link.record_max_sec,
|
||
canvasWidth: link.canvas_width,
|
||
canvasHeight: link.canvas_height,
|
||
cameraLinks: camerasOf.get(link.workstation_id) || [],
|
||
};
|
||
|
||
const snapshot = buildSnapshot(workstation, {
|
||
channelId: record.channel_id,
|
||
channelName: link.channel_name,
|
||
});
|
||
|
||
console.log(` ✓ #${record.id} ${record.channel_id} → 工作台 ${link.name || link.code}`
|
||
+ `(${snapshot.workstation.wsType === 2 ? '团购' : '散单'},摄像头 ${snapshot.cameras.length} 路)`);
|
||
|
||
if (!dryRun) {
|
||
await conn.query(
|
||
'UPDATE scan_records SET workstation_id = ?, workstation_snapshot = ? WHERE id = ?',
|
||
[ link.workstation_id, JSON.stringify(snapshot), record.id ]
|
||
);
|
||
}
|
||
filled += 1;
|
||
}
|
||
|
||
console.log(`\n完成:补了 ${filled} 条,跳过 ${noBinding} 条(通道没绑工作台)`);
|
||
if (dryRun) console.log('(dry-run:一条都没写)');
|
||
|
||
await conn.end();
|
||
})().catch(err => {
|
||
console.error('补快照失败:', err.message);
|
||
process.exit(1);
|
||
});
|