From 77661b91b1ad62229a53938984f22eb2ccbe818d Mon Sep 17 00:00:00 2001 From: liufumingren Date: Mon, 19 Oct 2026 22:04:01 +0800 Subject: [PATCH] =?UTF-8?q?feat(backend):=20=E5=BC=95=E5=85=A5=20sequelize?= =?UTF-8?q?=20=E8=BF=81=E7=A7=BB=E4=BD=93=E7=B3=BB=E4=B8=8E=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/.sequelizerc | 13 + backend/database/README.md | 56 +++ backend/database/config.cjs | 46 ++ ...00-create-nvr-camera-workstation-tables.js | 173 ++++++++ ...61018150000-add-channel-name-to-cameras.js | 23 + .../20261018180000-add-has-ptz-to-cameras.js | 23 + .../20261018200000-add-workstation-layout.js | 77 ++++ ...0100-backfill-workstation-camera-layout.js | 61 +++ .../20261018210000-add-workstation-type.js | 26 ++ ...1018220000-add-workstation-record-range.js | 46 ++ .../20261018230000-create-subject-tables.js | 139 ++++++ ...00-add-scan-record-workstation-snapshot.js | 55 +++ .../20261019120000-dedupe-scan-records.js | 47 ++ ...019220000-add-refresh-token-to-scanners.js | 40 ++ backend/package-lock.json | 408 +++++++++++++++++- backend/package.json | 12 +- backend/scripts/backfill-snapshot.js | 178 ++++++++ backend/scripts/dedupe-scan-records.js | 64 +++ .../20261017_add_user_password_changed_at.sql | 7 + backend/sql/schema.sql | 11 +- 20 files changed, 1499 insertions(+), 6 deletions(-) create mode 100644 backend/.sequelizerc create mode 100644 backend/database/README.md create mode 100644 backend/database/config.cjs create mode 100644 backend/database/migrations/20261018100000-create-nvr-camera-workstation-tables.js create mode 100644 backend/database/migrations/20261018150000-add-channel-name-to-cameras.js create mode 100644 backend/database/migrations/20261018180000-add-has-ptz-to-cameras.js create mode 100644 backend/database/migrations/20261018200000-add-workstation-layout.js create mode 100644 backend/database/migrations/20261018200100-backfill-workstation-camera-layout.js create mode 100644 backend/database/migrations/20261018210000-add-workstation-type.js create mode 100644 backend/database/migrations/20261018220000-add-workstation-record-range.js create mode 100644 backend/database/migrations/20261018230000-create-subject-tables.js create mode 100644 backend/database/migrations/20261018240000-add-scan-record-workstation-snapshot.js create mode 100644 backend/database/migrations/20261019120000-dedupe-scan-records.js create mode 100644 backend/database/migrations/20261019220000-add-refresh-token-to-scanners.js create mode 100644 backend/scripts/backfill-snapshot.js create mode 100644 backend/scripts/dedupe-scan-records.js create mode 100644 backend/sql/migrations/20261017_add_user_password_changed_at.sql diff --git a/backend/.sequelizerc b/backend/.sequelizerc new file mode 100644 index 0000000..997dd77 --- /dev/null +++ b/backend/.sequelizerc @@ -0,0 +1,13 @@ +/** + * sequelize-cli 的路径配置 + * + * DDL 只允许通过迁移脚本执行(`npm run db:migrate`),不允许直接连库改表。 + */ +const path = require('path'); + +module.exports = { + config: path.resolve(__dirname, 'database', 'config.cjs'), + 'migrations-path': path.resolve(__dirname, 'database', 'migrations'), + 'seeders-path': path.resolve(__dirname, 'database', 'seeders'), + 'models-path': path.resolve(__dirname, 'app', 'model'), +}; diff --git a/backend/database/README.md b/backend/database/README.md new file mode 100644 index 0000000..b26b114 --- /dev/null +++ b/backend/database/README.md @@ -0,0 +1,56 @@ +# 数据库变更规范(DDL) + +> **所有表结构变更只允许通过 sequelize-cli 迁移脚本执行,禁止直接连库改表。** + +## 为什么 + +- 迁移记录在 `SequelizeMeta` 表里,换环境(开发 / 测试 / 生产)按同一条命令重放,不会漏表、漏字段; +- 直接 `ALTER TABLE` 无法回溯,环境之间会悄悄不一致; +- 回滚有据可依(每个迁移都有 `down`)。 + +## 目录与配置 + +``` +backend/ +├─ .sequelizerc # sequelize-cli 路径配置 +├─ database/ +│ ├─ config.cjs # 连接信息(与环境变量 / config.local.js 同源) +│ ├─ migrations/ # ★ DDL 脚本放这里 +│ └─ seeders/ # 初始数据(可选) +``` + +连接信息由 `scripts/db-config.js` 统一解析:**环境变量 > `config/config.local.js` > 默认值**, +与运行时服务完全一致,避免「迁移改 A 库、服务连 B 库」。 + +## 常用命令 + +```bash +npm run db:migrate # 执行全部未执行的迁移 +npm run db:migrate:status # 查看哪些执行过 +npm run db:migrate:undo # 回滚最后一条 +npm run db:migration:generate -- create_xxx_tables # 生成迁移骨架 +``` + +## 写迁移的约定 + +- 表名/字段名用**下划线**,与既有表保持一致; +- 主键统一 `BIGINT UNSIGNED` 自增; +- 时间字段统一 `created_at` / `updated_at` / `deleted_at`(软删除),`status` 用 `TINYINT UNSIGNED`; +- 不建物理外键(与 `users`、`scanners` 等既有表一致),只建索引; +- `up()` 必须能重复执行前先看 `SequelizeMeta`(由 CLI 保证只跑一次),`down()` 必须能干净回滚。 + +## 已执行过的迁移 + +| 迁移 | 内容 | +|---|---| +| `20261018100000-create-nvr-camera-workstation-tables` | `nvrs` / `cameras` / `workstations` / `workstation_cameras` / `workstation_scan_channels` | +| `20261018150000-add-channel-name-to-cameras` | `cameras.channel_name`(设备通道名,与业务别名分开) | +| `20261018180000-add-has-ptz-to-cameras` | `cameras.has_ptz`(是否支持云台) | +| `20261018200000-add-workstation-layout` | `workstations.canvas_width/height` + `workstation_cameras.x/y/width/height/stream_type/z_index` | +| `20261018200100-backfill-workstation-camera-layout` | 回填已有绑定关系的画布位置(第 1 条铺满 / 第 2 条右上 / 其余左下) | +| `20261018210000-add-workstation-type` | `workstations.ws_type`(1 散单 / 2 团购) | +| `20261018220000-add-workstation-record-range` | `workstations.record_before_sec/record_after_sec/record_max_sec`(录制范围与最大录制时长) | +| `20261018230000-create-subject-tables` | 新表 `subjects` / `subject_members`;`scanners`/`nvrs`/`cameras`/`workstations`/`scan_channels`/`scan_records` 加 `subject_id`,并把已有数据回填到「每人一个主体」 | + +> 历史遗留的 `sql/schema.sql` + `sql/migrations/*.sql`(用 `npm run db:init` 执行)是接入 sequelize-cli 之前的做法, +> 只用于初始化老表;**新变更一律写 `database/migrations/*.js`**。 diff --git a/backend/database/config.cjs b/backend/database/config.cjs new file mode 100644 index 0000000..3bbba1e --- /dev/null +++ b/backend/database/config.cjs @@ -0,0 +1,46 @@ +'use strict'; + +/** + * sequelize-cli 配置入口(`.sequelizerc` 指到这里) + * + * 连接信息与后端运行时**完全同源**:环境变量 > config/config.local.js > 默认值, + * 统一由 scripts/db-config.js 解析,避免迁移改到一个库、服务连另一个库。 + * + * 用法: + * npm run db:migrate # 执行全部未执行的迁移 + * npm run db:migrate:undo # 回滚最后一条 + * npm run db:migrate:status # 查看状态 + */ + +const resolveDbConfig = require('../scripts/db-config'); + +const resolved = resolveDbConfig(); + +const common = { + username: resolved.user, + password: resolved.password, + database: resolved.database, + host: resolved.host, + port: resolved.port, + dialect: 'mysql', + timezone: '+08:00', + charset: 'utf8mb4', + logging: false, + define: { + freezeTableName: true, + underscored: true, + createdAt: 'created_at', + updatedAt: 'updated_at', + }, + dialectOptions: { + // 与运行时保持一致(长文本/JSON 字段不被截断) + supportBigNumbers: true, + bigNumberStrings: true, + }, +}; + +module.exports = { + development: common, + test: common, + production: common, +}; diff --git a/backend/database/migrations/20261018100000-create-nvr-camera-workstation-tables.js b/backend/database/migrations/20261018100000-create-nvr-camera-workstation-tables.js new file mode 100644 index 0000000..8a8c822 --- /dev/null +++ b/backend/database/migrations/20261018100000-create-nvr-camera-workstation-tables.js @@ -0,0 +1,173 @@ +'use strict'; + +/** + * 视频接入相关表(NVR / 摄像头 / 工作台 / 绑定关系) + * + * 业务关系: + * nvrs 录像机(一台 NVR 一个账号一条记录) + * cameras 摄像头(通道)—— 必须挂在某台 NVR 下,唯一键 (nvr_id, channel_no) + * workstations 工作台 + * workstation_cameras 工作台 ↔ 摄像头(多对多,一个工作台可绑多个摄像头) + * workstation_scan_channels 工作台 ↔ 扫码通道(一个工作台可绑多个通道) + * + * 约定与既有表保持一致: + * BIGINT UNSIGNED 自增主键 / created_at / updated_at / deleted_at(软删除)/ status + * 不建物理外键(与 users、scanners 等既有表一致),只建索引,关联由应用层保证。 + * + * ⚠️ DDL 只允许通过本迁移执行(`npm run db:migrate`),不要直接连库改表。 + */ +module.exports = { + async up(queryInterface, Sequelize) { + const { BIGINT, STRING, INTEGER, SMALLINT, TINYINT, DATE, literal } = Sequelize; + + const timestamps = { + created_at: { + type: DATE, + allowNull: false, + defaultValue: literal('CURRENT_TIMESTAMP'), + comment: '创建时间', + }, + updated_at: { + type: DATE, + allowNull: false, + defaultValue: literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), + comment: '更新时间', + }, + }; + + // ---------------- NVR 录像机 ---------------- + await queryInterface.createTable('nvrs', { + id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, + user_id: { type: BIGINT.UNSIGNED, allowNull: true, comment: '归属用户 users.id(空=未绑定)' }, + name: { type: STRING(64), allowNull: false, defaultValue: '', comment: '名称 / 别名' }, + host: { type: STRING(128), allowNull: false, comment: '设备地址(IP 或域名)' }, + port: { type: SMALLINT.UNSIGNED, allowNull: false, defaultValue: 8000, comment: 'SDK 服务端口(默认 8000)' }, + username: { type: STRING(64), allowNull: false, defaultValue: 'admin', comment: '登录账号' }, + password_enc: { type: STRING(512), allowNull: true, comment: '密码密文(AES-256-GCM,禁止存明文)' }, + serial_number: { type: STRING(64), allowNull: true, comment: '设备序列号(登录后回填)' }, + model: { type: STRING(64), allowNull: true, comment: '型号(可选)' }, + channel_count: { type: INTEGER.UNSIGNED, allowNull: false, defaultValue: 0, comment: '最近一次同步到的通道数' }, + last_sync_at: { type: DATE, allowNull: true, comment: '最近一次拉取通道时间' }, + last_error: { type: STRING(255), allowNull: true, comment: '最近一次失败原因(成功后清空)' }, + status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '状态:1正常 0停用' }, + remark: { type: STRING(255), allowNull: true, comment: '备注' }, + ...timestamps, + deleted_at: { type: DATE, allowNull: true, comment: '软删除时间' }, + }, { + engine: 'InnoDB', + charset: 'utf8mb4', + collate: 'utf8mb4_general_ci', + comment: 'NVR 录像机表', + }); + + await queryInterface.addIndex('nvrs', [ 'host', 'port', 'username' ], { + name: 'uk_host_port_username', + unique: true, + }); + await queryInterface.addIndex('nvrs', [ 'user_id' ], { name: 'idx_nvrs_user_id' }); + await queryInterface.addIndex('nvrs', [ 'status' ], { name: 'idx_nvrs_status' }); + + // ---------------- 摄像头(通道) ---------------- + await queryInterface.createTable('cameras', { + id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, + nvr_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属 NVR(nvrs.id)' }, + channel_no: { type: INTEGER.UNSIGNED, allowNull: false, comment: '海康通道号(数字通道通常从 33 开始)' }, + name: { type: STRING(64), allowNull: false, defaultValue: '', comment: '摄像头名称' }, + type: { type: STRING(16), allowNull: false, defaultValue: 'digital', comment: 'analog / digital' }, + ip: { type: STRING(45), allowNull: true, comment: '前端设备 IP(数字通道)' }, + port: { type: INTEGER.UNSIGNED, allowNull: true, comment: '前端设备管理端口' }, + enabled: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '通道是否已配置:1是 0否' }, + status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '状态:1启用 0停用' }, + remark: { type: STRING(255), allowNull: true, comment: '备注' }, + ...timestamps, + deleted_at: { type: DATE, allowNull: true, comment: '软删除时间' }, + }, { + engine: 'InnoDB', + charset: 'utf8mb4', + collate: 'utf8mb4_general_ci', + comment: '摄像头(NVR 通道)表', + }); + + await queryInterface.addIndex('cameras', [ 'nvr_id', 'channel_no' ], { + name: 'uk_nvr_channel', + unique: true, + }); + await queryInterface.addIndex('cameras', [ 'nvr_id' ], { name: 'idx_cameras_nvr_id' }); + await queryInterface.addIndex('cameras', [ 'status' ], { name: 'idx_cameras_status' }); + + // ---------------- 工作台 ---------------- + await queryInterface.createTable('workstations', { + id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, + user_id: { type: BIGINT.UNSIGNED, allowNull: true, comment: '归属用户 users.id' }, + code: { type: STRING(64), allowNull: false, comment: '工作台编号(同一用户下唯一)' }, + name: { type: STRING(64), allowNull: false, defaultValue: '', comment: '工作台名称' }, + status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '状态:1启用 0停用' }, + remark: { type: STRING(255), allowNull: true, comment: '备注' }, + ...timestamps, + deleted_at: { type: DATE, allowNull: true, comment: '软删除时间' }, + }, { + engine: 'InnoDB', + charset: 'utf8mb4', + collate: 'utf8mb4_general_ci', + comment: '工作台表', + }); + + await queryInterface.addIndex('workstations', [ 'user_id', 'code' ], { + name: 'uk_user_code', + unique: true, + }); + await queryInterface.addIndex('workstations', [ 'status' ], { name: 'idx_workstations_status' }); + + // ---------------- 工作台 ↔ 摄像头 ---------------- + await queryInterface.createTable('workstation_cameras', { + id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, + workstation_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '工作台 workstations.id' }, + camera_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '摄像头 cameras.id' }, + sort_no: { type: INTEGER.UNSIGNED, allowNull: false, defaultValue: 0, comment: '排序(越小越靠前)' }, + remark: { type: STRING(255), allowNull: true, comment: '备注' }, + ...timestamps, + }, { + engine: 'InnoDB', + charset: 'utf8mb4', + collate: 'utf8mb4_general_ci', + comment: '工作台-摄像头绑定表', + }); + + await queryInterface.addIndex('workstation_cameras', [ 'workstation_id', 'camera_id' ], { + name: 'uk_workstation_camera', + unique: true, + }); + await queryInterface.addIndex('workstation_cameras', [ 'camera_id' ], { name: 'idx_wc_camera_id' }); + + // ---------------- 工作台 ↔ 扫码通道 ---------------- + await queryInterface.createTable('workstation_scan_channels', { + id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, + workstation_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '工作台 workstations.id' }, + scanner_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '扫码中控 scanners.id' }, + channel_id: { type: STRING(64), allowNull: false, comment: '通道 id(scan_channels.channel_id)' }, + channel_name: { type: STRING(128), allowNull: false, defaultValue: '', comment: '通道名快照' }, + sort_no: { type: INTEGER.UNSIGNED, allowNull: false, defaultValue: 0, comment: '排序(越小越靠前)' }, + remark: { type: STRING(255), allowNull: true, comment: '备注' }, + ...timestamps, + }, { + engine: 'InnoDB', + charset: 'utf8mb4', + collate: 'utf8mb4_general_ci', + comment: '工作台-扫码通道绑定表', + }); + + await queryInterface.addIndex('workstation_scan_channels', [ 'workstation_id', 'scanner_id', 'channel_id' ], { + name: 'uk_workstation_scanner_channel', + unique: true, + }); + await queryInterface.addIndex('workstation_scan_channels', [ 'scanner_id' ], { name: 'idx_wsc_scanner_id' }); + }, + + async down(queryInterface) { + await queryInterface.dropTable('workstation_scan_channels'); + await queryInterface.dropTable('workstation_cameras'); + await queryInterface.dropTable('workstations'); + await queryInterface.dropTable('cameras'); + await queryInterface.dropTable('nvrs'); + }, +}; diff --git a/backend/database/migrations/20261018150000-add-channel-name-to-cameras.js b/backend/database/migrations/20261018150000-add-channel-name-to-cameras.js new file mode 100644 index 0000000..3d90ef8 --- /dev/null +++ b/backend/database/migrations/20261018150000-add-channel-name-to-cameras.js @@ -0,0 +1,23 @@ +'use strict'; + +/** + * cameras 增加 channel_name(设备上的通道名,来自 ISAPI) + * + * 与 name 的区别: + * name = 业务别名(用户可改,重拉通道不覆盖) + * channel_name = 设备上的原始通道名(每次拉取会刷新) + */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.addColumn('cameras', 'channel_name', { + type: Sequelize.STRING(64), + allowNull: true, + after: 'name', + comment: '设备通道名(ISAPI,拉取通道时刷新)', + }); + }, + + async down(queryInterface) { + await queryInterface.removeColumn('cameras', 'channel_name'); + }, +}; diff --git a/backend/database/migrations/20261018180000-add-has-ptz-to-cameras.js b/backend/database/migrations/20261018180000-add-has-ptz-to-cameras.js new file mode 100644 index 0000000..0fdcf28 --- /dev/null +++ b/backend/database/migrations/20261018180000-add-has-ptz-to-cameras.js @@ -0,0 +1,23 @@ +'use strict'; + +/** + * cameras 增加 has_ptz(是否带云台) + * + * 不是所有通道都挂云台(也有固定枪机),由用户在「编辑摄像头」里勾选; + * 预览页面只在勾选后显示云台方向盘(避免给固定机位发云台命令报“设备不支持”)。 + */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.addColumn('cameras', 'has_ptz', { + type: Sequelize.BOOLEAN, + allowNull: false, + defaultValue: false, + after: 'enabled', + comment: '是否支持云台(PTZ)', + }); + }, + + async down(queryInterface) { + await queryInterface.removeColumn('cameras', 'has_ptz'); + }, +}; diff --git a/backend/database/migrations/20261018200000-add-workstation-layout.js b/backend/database/migrations/20261018200000-add-workstation-layout.js new file mode 100644 index 0000000..0e28ae2 --- /dev/null +++ b/backend/database/migrations/20261018200000-add-workstation-layout.js @@ -0,0 +1,77 @@ +'use strict'; + +/** + * 工作台画面布局 + * + * 用途:一个工作台可以绑定多个摄像头,后续要把它们**拼成一路**(录制/合成), + * 所以每条绑定都要记住它在输出画布上的位置和大小,以及取哪路码流。 + * + * - workstations.canvas_width / canvas_height:输出画布(默认 1920×1080) + * - workstation_cameras.x / y / width / height:在该画布上的矩形(像素) + * - workstation_cameras.stream_type:0=主码流 1=子码流 + * - workstation_cameras.z_index:叠放顺序(大的在上层) + */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.addColumn('workstations', 'canvas_width', { + type: Sequelize.INTEGER.UNSIGNED, + allowNull: false, + defaultValue: 1920, + comment: '合成画布宽', + }); + await queryInterface.addColumn('workstations', 'canvas_height', { + type: Sequelize.INTEGER.UNSIGNED, + allowNull: false, + defaultValue: 1080, + comment: '合成画布高', + }); + + await queryInterface.addColumn('workstation_cameras', 'x', { + type: Sequelize.INTEGER, + allowNull: false, + defaultValue: 0, + comment: '画布左上角 x', + }); + await queryInterface.addColumn('workstation_cameras', 'y', { + type: Sequelize.INTEGER, + allowNull: false, + defaultValue: 0, + comment: '画布左上角 y', + }); + await queryInterface.addColumn('workstation_cameras', 'width', { + type: Sequelize.INTEGER, + allowNull: false, + defaultValue: 0, + comment: '宽(0 = 用整张画布)', + }); + await queryInterface.addColumn('workstation_cameras', 'height', { + type: Sequelize.INTEGER, + allowNull: false, + defaultValue: 0, + comment: '高(0 = 用整张画布)', + }); + await queryInterface.addColumn('workstation_cameras', 'stream_type', { + type: Sequelize.TINYINT.UNSIGNED, + allowNull: false, + defaultValue: 0, + comment: '取流类型:0 主码流 1 子码流', + }); + await queryInterface.addColumn('workstation_cameras', 'z_index', { + type: Sequelize.INTEGER, + allowNull: false, + defaultValue: 0, + comment: '叠放顺序(大的在上)', + }); + }, + + async down(queryInterface) { + await queryInterface.removeColumn('workstation_cameras', 'z_index'); + await queryInterface.removeColumn('workstation_cameras', 'stream_type'); + await queryInterface.removeColumn('workstation_cameras', 'height'); + await queryInterface.removeColumn('workstation_cameras', 'width'); + await queryInterface.removeColumn('workstation_cameras', 'y'); + await queryInterface.removeColumn('workstation_cameras', 'x'); + await queryInterface.removeColumn('workstations', 'canvas_height'); + await queryInterface.removeColumn('workstations', 'canvas_width'); + }, +}; diff --git a/backend/database/migrations/20261018200100-backfill-workstation-camera-layout.js b/backend/database/migrations/20261018200100-backfill-workstation-camera-layout.js new file mode 100644 index 0000000..dffa5cf --- /dev/null +++ b/backend/database/migrations/20261018200100-backfill-workstation-camera-layout.js @@ -0,0 +1,61 @@ +'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() { + // 布局是后加的默认值,回滚时不清数据(避免误删用户调整过的位置) + }, +}; diff --git a/backend/database/migrations/20261018210000-add-workstation-type.js b/backend/database/migrations/20261018210000-add-workstation-type.js new file mode 100644 index 0000000..fbab089 --- /dev/null +++ b/backend/database/migrations/20261018210000-add-workstation-type.js @@ -0,0 +1,26 @@ +'use strict'; + +/** + * 工作台类型:散单 / 团购 + * + * - 1 = 散单工作台(默认,兼容已有数据) + * - 2 = 团购工作台 + * + * 用途:后续按类型走不同的业务规则(取流/录制模板、扫码校验等), + * 列表也能按类型筛选。 + */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.addColumn('workstations', 'ws_type', { + type: Sequelize.TINYINT.UNSIGNED, + allowNull: false, + defaultValue: 1, + after: 'name', + comment: '工作台类型:1 散单 2 团购', + }); + }, + + async down(queryInterface) { + await queryInterface.removeColumn('workstations', 'ws_type'); + }, +}; diff --git a/backend/database/migrations/20261018220000-add-workstation-record-range.js b/backend/database/migrations/20261018220000-add-workstation-record-range.js new file mode 100644 index 0000000..d136571 --- /dev/null +++ b/backend/database/migrations/20261018220000-add-workstation-record-range.js @@ -0,0 +1,46 @@ +'use strict'; + +/** + * 工作台录制范围 + * + * - record_before_sec:触发点**之前**录多少秒 + * 团购工作台 = 扫码时间之前 + * 散单工作台 = 任务结束之前 + * - record_after_sec:触发点**之后**录多少秒(同上对应关系) + * - record_max_sec:最大录制时长(默认 240 秒),超出按上限裁切 + * + * 编号 code 不再让用户填(自动生成),列保留给唯一键与历史数据用。 + */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.addColumn('workstations', 'record_before_sec', { + type: Sequelize.INTEGER.UNSIGNED, + allowNull: false, + defaultValue: 60, + after: 'ws_type', + comment: '录制范围:触发点之前秒数', + }); + + await queryInterface.addColumn('workstations', 'record_after_sec', { + type: Sequelize.INTEGER.UNSIGNED, + allowNull: false, + defaultValue: 60, + after: 'record_before_sec', + comment: '录制范围:触发点之后秒数', + }); + + await queryInterface.addColumn('workstations', 'record_max_sec', { + type: Sequelize.INTEGER.UNSIGNED, + allowNull: false, + defaultValue: 240, + after: 'record_after_sec', + comment: '最大录制时长(秒)', + }); + }, + + async down(queryInterface) { + await queryInterface.removeColumn('workstations', 'record_max_sec'); + await queryInterface.removeColumn('workstations', 'record_after_sec'); + await queryInterface.removeColumn('workstations', 'record_before_sec'); + }, +}; diff --git a/backend/database/migrations/20261018230000-create-subject-tables.js b/backend/database/migrations/20261018230000-create-subject-tables.js new file mode 100644 index 0000000..917ea5c --- /dev/null +++ b/backend/database/migrations/20261018230000-create-subject-tables.js @@ -0,0 +1,139 @@ +'use strict'; + +/** + * 主体(租户)体系 + * + * - `subjects`:主体,带管理员(默认创建人)与使用期限 `expire_at` + * - `subject_members`:主体成员(姓名 + 可选账号绑定),一个主体下同一账号只能有一条 + * - 把「设备 / 录像机 / 摄像头 / 工作台 / 扫码记录 / 扫码通道」全部挂到主体上(`subject_id`) + * + * 数据迁移:给**已有数据**的用户各建一个主体(管理员=本人,期限 +365 天), + * 并把其名下数据回填到该主体,保证升级后旧数据仍然可见。 + */ +module.exports = { + async up(queryInterface, Sequelize) { + const { BIGINT, STRING, TINYINT, DATE, INTEGER } = Sequelize; + + const audit = { + created_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') }, + updated_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') }, + deleted_at: { type: DATE, allowNull: true }, + }; + + await queryInterface.createTable('subjects', { + id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false }, + name: { type: STRING(64), allowNull: false, comment: '主体名称' }, + admin_user_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '管理员用户 users.id(默认创建人)' }, + expire_at: { type: DATE, allowNull: false, comment: '使用期限(到期后不可进入)' }, + status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '状态:1启用 0停用' }, + remark: { type: STRING(255), allowNull: true, comment: '备注' }, + ...audit, + }, { comment: '主体表' }); + + await queryInterface.addIndex('subjects', [ 'admin_user_id' ], { name: 'idx_subjects_admin' }); + await queryInterface.addIndex('subjects', [ 'expire_at' ], { name: 'idx_subjects_expire' }); + + await queryInterface.createTable('subject_members', { + id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false }, + subject_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '主体 subjects.id' }, + user_id: { type: BIGINT.UNSIGNED, allowNull: true, comment: '绑定账号 users.id(未绑定为空)' }, + name: { type: STRING(32), allowNull: false, comment: '成员姓名' }, + role: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 2, comment: '角色:1管理员 2成员' }, + wechat_openid: { type: STRING(64), allowNull: true, comment: '绑定的微信 openid' }, + bound_at: { type: DATE, allowNull: true, comment: '成员绑定账号/微信的时间' }, + status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '状态:1正常 0禁用' }, + ...audit, + }, { comment: '主体成员表' }); + + // 同一主体下同一账号只能有一条(NULL 不参与唯一性,所以「未绑定的成员」可以有多条) + await queryInterface.addIndex('subject_members', [ 'subject_id', 'user_id' ], { + name: 'uk_subject_members_subject_user', + unique: true, + }); + await queryInterface.addIndex('subject_members', [ 'user_id' ], { name: 'idx_subject_members_user' }); + + // 业务表挂到主体 + const targets = [ + [ 'scanners', '设备表' ], + [ 'nvrs', 'NVR 录像机表' ], + [ 'cameras', '摄像头表' ], + [ 'workstations', '工作台表' ], + [ 'scan_channels', '扫码通道表' ], + [ 'scan_records', '扫码记录表' ], + ]; + for (const [ table ] of targets) { + await queryInterface.addColumn(table, 'subject_id', { + type: BIGINT.UNSIGNED, + allowNull: true, + comment: '所属主体 subjects.id', + after: 'id', + }); + await queryInterface.addIndex(table, [ 'subject_id' ], { name: `idx_${table}_subject` }); + } + + // ---------------- 回填:给已有数据的用户建主体 ---------------- + const expireExpr = "DATE_ADD(NOW(), INTERVAL 365 DAY)"; + await queryInterface.sequelize.query(` + INSERT INTO subjects (name, admin_user_id, expire_at, status, remark, created_at, updated_at) + SELECT LEFT(CONCAT(COALESCE(NULLIF(u.nickname, ''), NULLIF(u.username, ''), CONCAT('用户', u.id)), ' 的主体'), 64), + u.id, ${expireExpr}, 1, '升级时自动创建的历史主体', NOW(), NOW() + FROM users u + WHERE EXISTS (SELECT 1 FROM nvrs n WHERE n.user_id = u.id AND n.deleted_at IS NULL) + OR EXISTS (SELECT 1 FROM workstations w WHERE w.user_id = u.id AND w.deleted_at IS NULL) + OR EXISTS (SELECT 1 FROM scanners s WHERE s.user_id = u.id AND s.deleted_at IS NULL) + `); + + await queryInterface.sequelize.query(` + INSERT INTO subject_members (subject_id, user_id, name, role, bound_at, status, created_at, updated_at) + SELECT s.id, s.admin_user_id, + LEFT(COALESCE(NULLIF(u.nickname, ''), NULLIF(u.username, ''), CONCAT('用户', u.id)), 32), + 1, NOW(), 1, NOW(), NOW() + FROM subjects s + JOIN users u ON u.id = s.admin_user_id + WHERE NOT EXISTS ( + SELECT 1 FROM subject_members m WHERE m.subject_id = s.id AND m.user_id = s.admin_user_id + ) + `); + + const pairs = [ + [ 'nvrs', 'user_id' ], + [ 'workstations', 'user_id' ], + [ 'scanners', 'user_id' ], + ]; + for (const [ table, column ] of pairs) { + await queryInterface.sequelize.query(` + UPDATE ${table} t + JOIN subjects s ON s.admin_user_id = t.${column} AND s.deleted_at IS NULL + SET t.subject_id = s.id + WHERE t.subject_id IS NULL + `); + } + + await queryInterface.sequelize.query(` + UPDATE cameras c JOIN nvrs n ON n.id = c.nvr_id + SET c.subject_id = n.subject_id + WHERE c.subject_id IS NULL AND n.subject_id IS NOT NULL + `); + + await queryInterface.sequelize.query(` + UPDATE scan_records r JOIN scanners s ON s.id = r.scanner_id + SET r.subject_id = s.subject_id + WHERE r.subject_id IS NULL AND s.subject_id IS NOT NULL + `); + await queryInterface.sequelize.query(` + UPDATE scan_channels c JOIN scanners s ON s.id = c.scanner_id + SET c.subject_id = s.subject_id + WHERE c.subject_id IS NULL AND s.subject_id IS NOT NULL + `); + }, + + async down(queryInterface) { + const targets = [ 'scanners', 'nvrs', 'cameras', 'workstations', 'scan_channels', 'scan_records' ]; + for (const table of targets) { + await queryInterface.removeIndex(table, `idx_${table}_subject`); + await queryInterface.removeColumn(table, 'subject_id'); + } + await queryInterface.dropTable('subject_members'); + await queryInterface.dropTable('subjects'); + }, +}; diff --git a/backend/database/migrations/20261018240000-add-scan-record-workstation-snapshot.js b/backend/database/migrations/20261018240000-add-scan-record-workstation-snapshot.js new file mode 100644 index 0000000..6cec529 --- /dev/null +++ b/backend/database/migrations/20261018240000-add-scan-record-workstation-snapshot.js @@ -0,0 +1,55 @@ +'use strict'; + +/** + * 扫码记录:带上「当时的工作台快照」+ 运单查询要用的索引 + * + * 为什么要快照:录像回放要按**当时的**工作台配置(哪台录像机、哪些摄像头、怎么布局、 + * 录制范围多少秒)去拉流。工作台后来被改/被删了,历史运单照样要能查回来, + * 所以扫码入库时就把配置冻一份 JSON 存这里。 + * + * - `workstation_id`:便于按工作台统计/过滤(快照里也有,这个列只是方便 JOIN) + * - `workstation_snapshot`:JSON,结构见 `app/service/pack.js` 的 `buildSnapshot()` + * + * 索引: + * - `(subject_id, barcode)`:按运单号查(主体内精确匹配) + * - `(scanner_id, channel_id, scan_time)`:散单要取同通道的「上一条/下一条」扫码时间 + */ + +module.exports = { + async up(queryInterface, Sequelize) { + const { BIGINT, JSON: JSONType } = Sequelize; + + await queryInterface.addColumn('scan_records', 'workstation_id', { + type: BIGINT.UNSIGNED, + allowNull: true, + comment: '当时绑定的工作台 workstations.id(快照里有完整信息)', + }); + + await queryInterface.addColumn('scan_records', 'workstation_snapshot', { + type: JSONType, + allowNull: true, + comment: '工作台快照(录像机/摄像头/布局/录制范围),回放按它拉流', + }); + + await queryInterface.addIndex('scan_records', [ 'subject_id', 'barcode' ], { + name: 'idx_scan_subject_barcode', + }); + + await queryInterface.addIndex('scan_records', [ 'scanner_id', 'channel_id', 'scan_time' ], { + name: 'idx_scan_channel_seq', + }); + + // 中控上报时按 (scanner_id, channel_id) 找工作台,没索引会全表扫 + await queryInterface.addIndex('workstation_scan_channels', [ 'scanner_id', 'channel_id' ], { + name: 'idx_wsc_scanner_channel', + }); + }, + + async down(queryInterface) { + await queryInterface.removeIndex('workstation_scan_channels', 'idx_wsc_scanner_channel'); + await queryInterface.removeIndex('scan_records', 'idx_scan_channel_seq'); + await queryInterface.removeIndex('scan_records', 'idx_scan_subject_barcode'); + await queryInterface.removeColumn('scan_records', 'workstation_snapshot'); + await queryInterface.removeColumn('scan_records', 'workstation_id'); + }, +}; diff --git a/backend/database/migrations/20261019120000-dedupe-scan-records.js b/backend/database/migrations/20261019120000-dedupe-scan-records.js new file mode 100644 index 0000000..ff3f0c8 --- /dev/null +++ b/backend/database/migrations/20261019120000-dedupe-scan-records.js @@ -0,0 +1,47 @@ +'use strict'; + +/** + * 清理历史重复运单号:同一个运单号只保留「最后一条」 + * + * 背景: + * - 旧版本的扫码上报规则是「首次扫描即入库、24 小时内再扫直接丢弃」, + * 加上中途换过几次去重键,库里留下了同一运单号的多条记录; + * - 2026-10-19 起后端改成「同运单 24 小时内只保留一条、后扫的覆盖前一条」, + * 但**历史数据不会自动清理**,查询页仍会把重复记录全列出来。 + * + * 清理规则(与 service/scan.js 的 mergeExisting 保持一致): + * - 分组:`subject_id`(空值按 0 归组)+ `barcode`,**跨主体互不影响**; + * - 每组保留:`scan_time` 最新的那一条,时间相同时保留 `id` 最大的(最后写入); + * - 其余行**物理删除**(scan_records 是流水表,没有软删除)。 + * + * 不可逆:down() 不做恢复(被删的都是同一运单的旧记录,没有保留价值)。 + */ + +module.exports = { + async up(queryInterface) { + const [ rows ] = await queryInterface.sequelize.query(` + SELECT id FROM ( + SELECT id, + ROW_NUMBER() OVER ( + PARTITION BY COALESCE(subject_id, 0), barcode + ORDER BY scan_time DESC, id DESC + ) AS rn + FROM scan_records + ) ranked + WHERE rn > 1 + `); + + const ids = rows.map(row => Number(row.id)); + if (!ids.length) { + console.log('[dedupe] scan_records 没有重复运单号,跳过'); + return; + } + + await queryInterface.bulkDelete('scan_records', { id: ids }); + console.log(`[dedupe] 已删除 ${ids.length} 条重复扫码记录(同一运单号只保留最后一条)`); + }, + + async down() { + // 数据清理不可逆:不恢复被删的历史记录 + }, +}; diff --git a/backend/database/migrations/20261019220000-add-refresh-token-to-scanners.js b/backend/database/migrations/20261019220000-add-refresh-token-to-scanners.js new file mode 100644 index 0000000..12f239a --- /dev/null +++ b/backend/database/migrations/20261019220000-add-refresh-token-to-scanners.js @@ -0,0 +1,40 @@ +'use strict'; + +/** + * scanners 增加刷新令牌字段 + * + * 背景:设备长期 JWT 默认 365 天,到期后服务会 401;而续期原本必须重新走 + * 「主体管理员登录 → activate」,对无人值守的扫码服务不友好。 + * + * 方案:激活时同时下发一枚**刷新令牌**(不透明随机串,库里只存 sha256), + * 后台服务在访问令牌快过期时用它换新访问令牌(同时轮换刷新令牌)。 + * - `refresh_token_hash`:刷新令牌摘要(不存原文;索引便于反查) + * - `refresh_expires_at`:刷新令牌过期时间(每次刷新顺延) + */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.addColumn('scanners', 'refresh_token_hash', { + type: Sequelize.STRING(128), + allowNull: true, + after: 'token_expires_at', + comment: '刷新令牌 sha256(不存原文)', + }); + + await queryInterface.addColumn('scanners', 'refresh_expires_at', { + type: Sequelize.DATE, + allowNull: true, + after: 'refresh_token_hash', + comment: '刷新令牌过期时间', + }); + + await queryInterface.addIndex('scanners', [ 'refresh_token_hash' ], { + name: 'idx_scanners_refresh_token_hash', + }); + }, + + async down(queryInterface) { + await queryInterface.removeIndex('scanners', 'idx_scanners_refresh_token_hash'); + await queryInterface.removeColumn('scanners', 'refresh_expires_at'); + await queryInterface.removeColumn('scanners', 'refresh_token_hash'); + }, +}; diff --git a/backend/package-lock.json b/backend/package-lock.json index 74a59dc..0444951 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -23,7 +23,8 @@ }, "devDependencies": { "egg-bin": "^6.13.0", - "egg-mock": "^5.15.2" + "egg-mock": "^5.15.2", + "sequelize-cli": "^6.6.5" }, "engines": { "node": ">=18.0.0" @@ -560,6 +561,13 @@ "node": ">= 8" } }, + "node_modules/@one-ini/wasm": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/@one-ini/wasm/-/wasm-0.1.1.tgz", + "integrity": "sha512-XuySG1E38YScSJoMlqovLru4KTUNSjgVTIjyh7qMX6aNN5HY5Ct5LhRJdxO79JtTzKfzV/bnWpz+zquYrISsvw==", + "dev": true, + "license": "MIT" + }, "node_modules/@paralleldrive/cuid2": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/@paralleldrive/cuid2/-/cuid2-2.3.1.tgz", @@ -1296,6 +1304,16 @@ "node": ">=16.20.0" } }, + "node_modules/abbrev": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-2.0.0.tgz", + "integrity": "sha512-6/mh1E2u2YgEsCHdY0Yx5oW+61gZU+1vXaoiHHrpKeuRNNgFvS+/jrwHiQhB5apAf5oB7UB7E19ol2R2LKH8hQ==", + "dev": true, + "license": "ISC", + "engines": { + "node": "^14.17.0 || ^16.13.0 || >=18.0.0" + } + }, "node_modules/accepts": { "version": "1.3.8", "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz", @@ -1500,6 +1518,16 @@ "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", "license": "MIT" }, + "node_modules/at-least-node": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/at-least-node/-/at-least-node-1.0.0.tgz", + "integrity": "sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==", + "dev": true, + "license": "ISC", + "engines": { + "node": ">= 4.0.0" + } + }, "node_modules/await-event": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/await-event/-/await-event-2.1.0.tgz", @@ -1593,6 +1621,13 @@ "integrity": "sha512-FQSWhFQZmddoqWkwPMFeR5hJo9waZE796MuO7b0poStaPrm0Qr2em9FT4/TF1+0rOA1dRFabX+88rdX8YHxDTA==", "license": "MIT" }, + "node_modules/bluebird": { + "version": "3.7.2", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", + "integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==", + "dev": true, + "license": "MIT" + }, "node_modules/brace-expansion": { "version": "2.1.7", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.1.7.tgz", @@ -2617,6 +2652,17 @@ "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", "license": "MIT" }, + "node_modules/config-chain": { + "version": "1.1.13", + "resolved": "https://registry.npmjs.org/config-chain/-/config-chain-1.1.13.tgz", + "integrity": "sha512-qj+f8APARXHrM0hraqXYb2/bOVSV4PvJQlNZ/DVj0QrmNM2q2euizkeuVckQ57J+W0mRH6Hvi+k50M4Jul2VRQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "ini": "^1.3.4", + "proto-list": "~1.2.1" + } + }, "node_modules/constant-case": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/constant-case/-/constant-case-3.0.4.tgz", @@ -3116,6 +3162,64 @@ "safe-buffer": "^5.0.1" } }, + "node_modules/editorconfig": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/editorconfig/-/editorconfig-1.0.7.tgz", + "integrity": "sha512-e0GOtq/aTQhVdNyDU9e02+wz9oDDM+SIOQxWME2QRjzRX5yyLAuHDE+0aE8vHb9XRC8XD37eO2u57+F09JqFhw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@one-ini/wasm": "0.1.1", + "commander": "^10.0.0", + "minimatch": "^9.0.1", + "semver": "^7.5.3" + }, + "bin": { + "editorconfig": "bin/editorconfig" + }, + "engines": { + "node": ">=14" + } + }, + "node_modules/editorconfig/node_modules/commander": { + "version": "10.0.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-10.0.1.tgz", + "integrity": "sha512-y4Mg2tXshplEbSGzx7amzPwKKOCGuoSRP/CjEdwwk0FOGlUbq6lKuoyDZTNZkmxHdJtp54hdfY/JUrdL7Xfdug==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14" + } + }, + "node_modules/editorconfig/node_modules/minimatch": { + "version": "9.0.9", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.9.tgz", + "integrity": "sha512-OBwBN9AL4dqmETlpS2zasx+vTeWclWzkblfZk7KTA5j3jeOONz/tRCnZomUyvNg83wL5Zv9Ss6HMJXAgL8R2Yg==", + "dev": true, + "license": "ISC", + "dependencies": { + "brace-expansion": "^2.0.2" + }, + "engines": { + "node": ">=16 || 14 >=14.17" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/editorconfig/node_modules/semver": { + "version": "7.8.5", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.5.tgz", + "integrity": "sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA==", + "dev": true, + "license": "ISC", + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -5215,6 +5319,22 @@ "integrity": "sha512-0n+pzCC6ICtVr/WXnN2f03TK/3BfXY7me4cjCAqT8TYXEl0+JBRoqBo94JJHXcyDSLUeWbNX8Fvy5g5RJdAstQ==", "license": "MIT" }, + "node_modules/is-core-module": { + "version": "2.17.0", + "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.17.0.tgz", + "integrity": "sha512-J/vG0zBCbIKOQFfufSwyXdMrsohyJIUNkrnmo6WZGzoM7tr/lsbfW5b2BvisL6zsyMzK9UxV9L6c7AoFbyXHOA==", + "dev": true, + "license": "MIT", + "dependencies": { + "hasown": "^2.0.4" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/is-extendable": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/is-extendable/-/is-extendable-0.1.1.tgz", @@ -5591,6 +5711,73 @@ "node": ">=8" } }, + "node_modules/js-beautify": { + "version": "1.15.4", + "resolved": "https://registry.npmjs.org/js-beautify/-/js-beautify-1.15.4.tgz", + "integrity": "sha512-9/KXeZUKKJwqCXUdBxFJ3vPh467OCckSBmYDwSK/EtV090K+iMJ7zx2S3HLVDIWFQdqMIsZWbnaGiba18aWhaA==", + "dev": true, + "license": "MIT", + "dependencies": { + "config-chain": "^1.1.13", + "editorconfig": "^1.0.4", + "glob": "^10.4.2", + "js-cookie": "^3.0.5", + "nopt": "^7.2.1" + }, + "bin": { + "css-beautify": "js/bin/css-beautify.js", + "html-beautify": "js/bin/html-beautify.js", + "js-beautify": "js/bin/js-beautify.js" + }, + "engines": { + "node": ">=14" + } + }, + "node_modules/js-beautify/node_modules/glob": { + "version": "10.5.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-10.5.0.tgz", + "integrity": "sha512-DfXN8DfhJ7NH3Oe7cFmu3NCu1wKbkReJ8TorzSAFbSKrlNaQSKfIzqYqVY8zlbs2NLBbWpRiU52GX2PbaBVNkg==", + "deprecated": "Old versions of glob are not supported, and contain widely publicized security vulnerabilities, which have been fixed in the current version. Please update. Support for old versions may be purchased (at exorbitant rates) by contacting i@izs.me", + "dev": true, + "license": "ISC", + "dependencies": { + "foreground-child": "^3.1.0", + "jackspeak": "^3.1.2", + "minimatch": "^9.0.4", + "minipass": "^7.1.2", + "package-json-from-dist": "^1.0.0", + "path-scurry": "^1.11.1" + }, + "bin": { + "glob": "dist/esm/bin.mjs" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/js-beautify/node_modules/minimatch": { + "version": "9.0.9", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.9.tgz", + "integrity": "sha512-OBwBN9AL4dqmETlpS2zasx+vTeWclWzkblfZk7KTA5j3jeOONz/tRCnZomUyvNg83wL5Zv9Ss6HMJXAgL8R2Yg==", + "dev": true, + "license": "ISC", + "dependencies": { + "brace-expansion": "^2.0.2" + }, + "engines": { + "node": ">=16 || 14 >=14.17" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/js-cookie": { + "version": "3.0.8", + "resolved": "https://registry.npmjs.org/js-cookie/-/js-cookie-3.0.8.tgz", + "integrity": "sha512-yeJd4aNAdYZQjaon2bpD/Gb0B/omw7HQOsynXXcOiWVCacbBcPlgn8S/d1X6blFSaHao7ozqtW7NZW19xpCtIw==", + "dev": true, + "license": "MIT" + }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", @@ -7236,6 +7423,22 @@ "node": ">=20.0.0" } }, + "node_modules/nopt": { + "version": "7.2.1", + "resolved": "https://registry.npmjs.org/nopt/-/nopt-7.2.1.tgz", + "integrity": "sha512-taM24ViiimT/XntxbPyJQzCG+p4EKOpgD3mxFwW38mGjVUrfERQOeY4EDHjdnptttfHuHQXFx+lTP08Q+mLa/w==", + "dev": true, + "license": "ISC", + "dependencies": { + "abbrev": "^2.0.0" + }, + "bin": { + "nopt": "bin/nopt.js" + }, + "engines": { + "node": "^14.17.0 || ^16.13.0 || >=18.0.0" + } + }, "node_modules/normalize-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", @@ -7625,6 +7828,13 @@ "node": ">=4" } }, + "node_modules/path-parse": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", + "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==", + "dev": true, + "license": "MIT" + }, "node_modules/path-scurry": { "version": "1.11.1", "resolved": "https://registry.npmjs.org/path-scurry/-/path-scurry-1.11.1.tgz", @@ -7678,6 +7888,13 @@ "integrity": "sha512-qR3kGNPBLpCNtz0evbKA0Y/MRFXwSSdT+pTJvYp/bXTcReZbvX1kzF0IyTc1QnxqF7AZbOeBhNL8R5mYQZV/MA==", "license": "MIT" }, + "node_modules/picocolors": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz", + "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==", + "dev": true, + "license": "ISC" + }, "node_modules/picomatch": { "version": "2.3.2", "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.2.tgz", @@ -7793,6 +8010,13 @@ "react-is": "^16.13.1" } }, + "node_modules/proto-list": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/proto-list/-/proto-list-1.2.4.tgz", + "integrity": "sha512-vtK/94akxsTMhe0/cbfpR+syPuszcuwhqVjJq26CuNDgFGj682oRBXOP5MJpv2r7JtE8MsiepGIqvvOTBwn2vA==", + "dev": true, + "license": "ISC" + }, "node_modules/ps-tree": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/ps-tree/-/ps-tree-1.2.0.tgz", @@ -8081,6 +8305,28 @@ "integrity": "sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==", "license": "MIT" }, + "node_modules/resolve": { + "version": "1.22.13", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.13.tgz", + "integrity": "sha512-Dj/cW9zBV2aRj6XzCxuGIs3ESZKv02jixne3q6jFZYZ3/Tg09gHHviSMDL4kObh9pqSahdRHEnDoaP2gn8YLhA==", + "dev": true, + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "is-core-module": "^2.17.0", + "path-parse": "^1.0.7", + "supports-preserve-symlinks-flag": "^1.0.0" + }, + "bin": { + "resolve": "bin/resolve" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/resolve-path": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/resolve-path/-/resolve-path-1.4.0.tgz", @@ -8452,6 +8698,140 @@ } } }, + "node_modules/sequelize-cli": { + "version": "6.6.5", + "resolved": "https://registry.npmjs.org/sequelize-cli/-/sequelize-cli-6.6.5.tgz", + "integrity": "sha512-DqyISCULOaEbTM+rRQH4YvcUWeOC1XDiSKcjsC6TfAnT7W837mNkChJhtB/Z4FdCFHRCojmiP7zsrA4pARmacA==", + "dev": true, + "license": "MIT", + "dependencies": { + "fs-extra": "^9.1.0", + "js-beautify": "1.15.4", + "lodash": "^4.17.21", + "picocolors": "^1.1.1", + "resolve": "^1.22.1", + "umzug": "^2.3.0", + "yargs": "^16.2.0" + }, + "bin": { + "sequelize": "lib/sequelize", + "sequelize-cli": "lib/sequelize" + }, + "engines": { + "node": ">=10.0.0" + } + }, + "node_modules/sequelize-cli/node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dev": true, + "license": "MIT", + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/sequelize-cli/node_modules/cliui": { + "version": "7.0.4", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz", + "integrity": "sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==", + "dev": true, + "license": "ISC", + "dependencies": { + "string-width": "^4.2.0", + "strip-ansi": "^6.0.0", + "wrap-ansi": "^7.0.0" + } + }, + "node_modules/sequelize-cli/node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/sequelize-cli/node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dev": true, + "license": "MIT" + }, + "node_modules/sequelize-cli/node_modules/fs-extra": { + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", + "integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "at-least-node": "^1.0.0", + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/sequelize-cli/node_modules/wrap-ansi": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz", + "integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-styles": "^4.0.0", + "string-width": "^4.1.0", + "strip-ansi": "^6.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/wrap-ansi?sponsor=1" + } + }, + "node_modules/sequelize-cli/node_modules/yargs": { + "version": "16.2.2", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.2.tgz", + "integrity": "sha512-Nt9ZJjXTv5R8MHbqby/wXQ6Gi0Bb3TcYZkR1bzuL4yB2OxWPkXknz513gEF0GoA6tn00UpbPvERW8rzCuWCA6w==", + "dev": true, + "license": "MIT", + "dependencies": { + "cliui": "^7.0.2", + "escalade": "^3.1.1", + "get-caller-file": "^2.0.5", + "require-directory": "^2.1.1", + "string-width": "^4.2.0", + "y18n": "^5.0.5", + "yargs-parser": "^20.2.2" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/sequelize-cli/node_modules/yargs-parser": { + "version": "20.2.9", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.9.tgz", + "integrity": "sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==", + "dev": true, + "license": "ISC", + "engines": { + "node": ">=10" + } + }, "node_modules/sequelize-pool": { "version": "7.1.0", "resolved": "https://registry.npmjs.org/sequelize-pool/-/sequelize-pool-7.1.0.tgz", @@ -9036,6 +9416,19 @@ "node": ">=8" } }, + "node_modules/supports-preserve-symlinks-flag": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz", + "integrity": "sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/table-layout": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/table-layout/-/table-layout-1.0.2.tgz", @@ -9480,6 +9873,19 @@ "node": ">= 0.8" } }, + "node_modules/umzug": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/umzug/-/umzug-2.3.0.tgz", + "integrity": "sha512-Z274K+e8goZK8QJxmbRPhl89HPO1K+ORFtm6rySPhFKfKc5GHhqdzD0SGhSWHkzoXasqJuItdhorSvY7/Cgflw==", + "dev": true, + "license": "MIT", + "dependencies": { + "bluebird": "^3.7.2" + }, + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/undici": { "version": "5.29.0", "resolved": "https://registry.npmjs.org/undici/-/undici-5.29.0.tgz", diff --git a/backend/package.json b/backend/package.json index 0f58388..4c9e6ee 100644 --- a/backend/package.json +++ b/backend/package.json @@ -22,7 +22,8 @@ }, "devDependencies": { "egg-bin": "^6.13.0", - "egg-mock": "^5.15.2" + "egg-mock": "^5.15.2", + "sequelize-cli": "^6.6.5" }, "engines": { "node": ">=18.0.0" @@ -31,11 +32,16 @@ "start": "egg-scripts start --daemon --title=egg-server-pack-service", "stop": "egg-scripts stop --title=egg-server-pack-service", "dev": "egg-bin dev", - "dev:test": "egg-bin dev --port 7300", "debug": "egg-bin debug", "test": "egg-bin test", "cov": "egg-bin cov", "db:init": "node scripts/init-db.js", - "user:pass": "node scripts/set-password.js" + "db:migrate": "sequelize-cli db:migrate", + "db:migrate:undo": "sequelize-cli db:migrate:undo", + "db:migrate:status": "sequelize-cli db:migrate:status", + "db:migration:generate": "sequelize-cli migration:generate --name", + "user:pass": "node scripts/set-password.js", + "backfill:snapshot": "node scripts/backfill-snapshot.js", + "records:dedupe": "node scripts/dedupe-scan-records.js" } } diff --git a/backend/scripts/backfill-snapshot.js b/backend/scripts/backfill-snapshot.js new file mode 100644 index 0000000..61d8a94 --- /dev/null +++ b/backend/scripts/backfill-snapshot.js @@ -0,0 +1,178 @@ +'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); +}); diff --git a/backend/scripts/dedupe-scan-records.js b/backend/scripts/dedupe-scan-records.js new file mode 100644 index 0000000..302ab53 --- /dev/null +++ b/backend/scripts/dedupe-scan-records.js @@ -0,0 +1,64 @@ +'use strict'; + +/** + * 运维脚本:清理重复运单号,同一个运单号只保留最后一条 + * + * 规则与 `database/migrations/20261019120000-dedupe-scan-records.js` 完全一致: + * - 分组:`subject_id`(空值按 0 归组)+ `barcode`,跨主体互不影响; + * - 每组保留 `scan_time` 最新的一条,时间相同保留 `id` 最大的; + * - 其余**物理删除**(scan_records 是流水表,没有软删除)。 + * + * 用途:后端的 24 小时合并窗口之外,同一运单再扫会新增一条记录。 + * 需要把历史数据「只留最后一条」时随时可以再跑本脚本(幂等,可重复执行)。 + * + * 用法: + * npm run records:dedupe # 先看统计 + * npm run records:dedupe -- --yes # 真正删除 + */ + +const mysql = require('mysql2/promise'); +const resolveDbConfig = require('./db-config'); + +const SQL_DUP_IDS = ` + SELECT id FROM ( + SELECT id, + ROW_NUMBER() OVER ( + PARTITION BY COALESCE(subject_id, 0), barcode + ORDER BY scan_time DESC, id DESC + ) AS rn + FROM scan_records + ) ranked + WHERE rn > 1 +`; + +(async () => { + const apply = process.argv.includes('--yes'); + const cfg = resolveDbConfig(); + console.log('数据库:%s@%s:%s', cfg.database, cfg.host, cfg.port); + + const conn = await mysql.createConnection(cfg); + try { + const [[ total ]] = await conn.query('SELECT COUNT(*) AS cnt FROM scan_records'); + const [ rows ] = await conn.query(SQL_DUP_IDS); + const ids = rows.map(row => Number(row.id)); + + console.log('扫码记录共 %d 条,其中重复(同主体同运单号的旧记录)%d 条', total.cnt, ids.length); + if (!ids.length) { + console.log('没有重复运单号,无需处理'); + return; + } + + if (!apply) { + console.log('预览模式:没有做任何修改。加 --yes 才会真正删除。'); + return; + } + + const [ result ] = await conn.query('DELETE FROM scan_records WHERE id IN (?)', [ ids ]); + console.log('已删除 %d 条,剩余 %d 条(每个运单号只保留最后一条)', result.affectedRows, total.cnt - result.affectedRows); + } finally { + await conn.end(); + } +})().catch(err => { + console.error('清理失败:', err.message); + process.exit(1); +}); diff --git a/backend/sql/migrations/20261017_add_user_password_changed_at.sql b/backend/sql/migrations/20261017_add_user_password_changed_at.sql new file mode 100644 index 0000000..20af3e8 --- /dev/null +++ b/backend/sql/migrations/20261017_add_user_password_changed_at.sql @@ -0,0 +1,7 @@ +-- 2026-10-17:users 增加「最近一次设置/重置密码的时间」 +-- +-- 用途:user JWT 是无状态的(默认 7 天)。改密/重置密码之后,**签发时间早于这个时间戳的旧令牌一律失效** +-- (`middleware/api_auth.js` 与 `middleware/internal_auth.js` 里比对 JWT 的 `iat`)。 +-- 老数据保持 NULL = 不限制(升级前签发的令牌继续可用,直到自然过期)。 +ALTER TABLE `users` + ADD COLUMN `password_changed_at` DATETIME DEFAULT NULL COMMENT '最近一次设置/重置密码的时间(用于让旧 JWT 失效)' AFTER `password_hash`; diff --git a/backend/sql/schema.sql b/backend/sql/schema.sql index 0e358f5..88d00b2 100644 --- a/backend/sql/schema.sql +++ b/backend/sql/schema.sql @@ -30,6 +30,7 @@ CREATE TABLE IF NOT EXISTS `users` ( `avatar` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '头像地址', `phone` VARCHAR(20) DEFAULT NULL COMMENT '手机号', `password_hash` VARCHAR(100) DEFAULT NULL COMMENT '密码哈希(bcrypt),登录用', + `password_changed_at` DATETIME DEFAULT NULL COMMENT '最近一次设置/重置密码的时间(用于让旧 JWT 失效)', `gender` TINYINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '性别:0未知 1男 2女', `status` TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '状态:1正常 0禁用', `remark` VARCHAR(255) DEFAULT NULL COMMENT '备注', @@ -169,16 +170,22 @@ CREATE TABLE IF NOT EXISTS `scan_records` ( `barcode` VARCHAR(255) NOT NULL COMMENT '条码内容', `scan_time` DATETIME NOT NULL COMMENT '扫描时间(中控上报,秒级)', `reported_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '服务端接收时间', + `subject_id` BIGINT UNSIGNED NULL COMMENT '所属主体 subjects.id(跟随中控)', + -- ↓ 2026-10-18 新增(迁移 20261018240000):运单回放要按「当时」的工作台配置拉流 + `workstation_id` BIGINT UNSIGNED NULL COMMENT '当时绑定的工作台 workstations.id', + `workstation_snapshot` JSON NULL COMMENT '工作台快照(录像机/摄像头/布局/录制范围)', `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', PRIMARY KEY (`id`), UNIQUE KEY `uk_scan_dedup` (`scanner_id`, `channel_id`, `barcode`, `scan_time`), KEY `idx_scan_time` (`scan_time`), KEY `idx_barcode` (`barcode`), - KEY `idx_channel_scan_time` (`channel_id`, `scan_time`) + KEY `idx_channel_scan_time` (`channel_id`, `scan_time`), + KEY `idx_scan_subject_barcode` (`subject_id`, `barcode`), + KEY `idx_scan_channel_seq` (`scanner_id`, `channel_id`, `scan_time`) ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 - COMMENT = '扫码记录表(同 中控+通道+条码+扫描时间 去重)'; + COMMENT = '扫码记录表(同 中控+通道+条码+扫描时间 去重;带当时的工作台快照)'; -- --------------------------------------------------------------- -- 初始化配置(可选)