'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'); }, };