packServer/backend/database/migrations/20261018200100-backfill-workstation-camera-layout.js

62 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 为已有的工作台摄像头绑定补默认布局
*
* 新列默认 width/height = 0,前端会把 0 当成“铺满画布”,
* 已经绑定多个摄像头的工作台就会全部叠在一起。这里按排序补一遍:
* 第 1 个:铺满画布(1920×1080)
* 第 2 个:720 宽,贴右上角
* 后面的:480 宽,贴左下角(依次叠放)
*/
module.exports = {
async up(queryInterface) {
const [ rows ] = await queryInterface.sequelize.query(`
SELECT wc.id, wc.workstation_id, wc.width, wc.height,
w.canvas_width AS canvasW, w.canvas_height AS canvasH
FROM workstation_cameras wc
JOIN workstations w ON w.id = wc.workstation_id
ORDER BY wc.workstation_id ASC, wc.sort_no ASC, wc.id ASC
`);
let currentWorkstation = null;
let slot = 0;
for (const row of rows) {
if (row.workstation_id !== currentWorkstation) {
currentWorkstation = row.workstation_id;
slot = 0;
}
const canvasW = Number(row.canvasW) || 1920;
const canvasH = Number(row.canvasH) || 1080;
let layout;
if (slot === 0) {
layout = { x: 0, y: 0, width: canvasW, height: canvasH };
} else if (slot === 1) {
const width = Math.min(720, canvasW);
const height = Math.round(width * canvasH / canvasW);
layout = { x: canvasW - width, y: 0, width, height };
} else {
const width = Math.min(480, canvasW);
const height = Math.round(width * canvasH / canvasW);
layout = { x: 0, y: canvasH - height, width, height };
}
if (!Number(row.width) || !Number(row.height)) {
await queryInterface.sequelize.query(
'UPDATE workstation_cameras SET x = ?, y = ?, width = ?, height = ?, z_index = ? WHERE id = ?',
{ replacements: [ layout.x, layout.y, layout.width, layout.height, slot, row.id ] }
);
}
slot += 1;
}
},
async down() {
// 布局是后加的默认值,回滚时不清数据(避免误删用户调整过的位置)
},
};