From b485f8e9f1abd9613f93012630e6826eb30b6410 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Mon, 27 Jul 2026 03:46:51 +0800 Subject: [PATCH] =?UTF-8?q?feat(camera):=20=E6=96=B0=E5=A2=9E=E6=91=84?= =?UTF-8?q?=E5=83=8F=E5=A4=B4/=E5=BD=95=E5=88=B6=E6=A8=A1=E5=9D=97=20(came?= =?UTF-8?q?ra/camera=5Fserver/pack=5Ftask=5Fvideo)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 model: camera, camera_server - 新增 services: camera, cameraServer (systemMethods + systems) - 新增 pack_task_video 表及录制状态管理 - 7 条迁移: 建表→设备字段→端口拆分→状态枚举 --- app/model/camera.js | 115 ++++++++++++++++ app/model/camera_server.js | 100 ++++++++++++++ app/service/systemMethods/camera.js | 54 ++++++++ app/service/systemMethods/cameraServer.js | 54 ++++++++ app/service/systems/camera.js | 126 +++++++++++++++++ app/service/systems/cameraServer.js | 127 ++++++++++++++++++ ...20260715220000-create-wms-camera-server.js | 98 ++++++++++++++ .../20260715220001-create-wms-camera.js | 88 ++++++++++++ ...260715230000-create-wms-pack-task-video.js | 72 ++++++++++ ...20260717150000-add-camera-device-fields.js | 55 ++++++++ ...20260718160000-split-camera-server-port.js | 89 ++++++++++++ ...0000-update-pack-task-video-status-enum.js | 59 ++++++++ ...00-add-recording-status-pack-task-video.js | 26 ++++ 13 files changed, 1063 insertions(+) create mode 100644 app/model/camera.js create mode 100644 app/model/camera_server.js create mode 100644 app/service/systemMethods/camera.js create mode 100644 app/service/systemMethods/cameraServer.js create mode 100644 app/service/systems/camera.js create mode 100644 app/service/systems/cameraServer.js create mode 100644 database/migrations/20260715220000-create-wms-camera-server.js create mode 100644 database/migrations/20260715220001-create-wms-camera.js create mode 100644 database/migrations/20260715230000-create-wms-pack-task-video.js create mode 100644 database/migrations/20260717150000-add-camera-device-fields.js create mode 100644 database/migrations/20260718160000-split-camera-server-port.js create mode 100644 database/migrations/20260726120000-update-pack-task-video-status-enum.js create mode 100644 database/migrations/20260726130000-add-recording-status-pack-task-video.js diff --git a/app/model/camera.js b/app/model/camera.js new file mode 100644 index 0000000..755d013 --- /dev/null +++ b/app/model/camera.js @@ -0,0 +1,115 @@ +'use strict'; + +module.exports = app => { + const { STRING, BIGINT, INTEGER, DATE, TINYINT } = app.Sequelize; + + const Camera = app.model.define('camera', { + id: { + type: BIGINT(20).UNSIGNED, + primaryKey: true, + autoIncrement: true, + comment: '主键ID', + }, + warehouse_id: { + type: BIGINT(20).UNSIGNED, + allowNull: false, + comment: '仓库ID', + }, + server_id: { + type: BIGINT(20).UNSIGNED, + allowNull: false, + comment: '所属录像服务器ID,关联 wms_camera_server.id', + }, + channel_id: { + type: INTEGER.UNSIGNED, + allowNull: false, + comment: '通道编号(DVR/NVR 上的物理通道号)', + }, + channel_name: { + type: STRING(100), + allowNull: true, + comment: '通道原始名称', + }, + ip: { + type: STRING(50), + allowNull: true, + comment: '摄像头IP地址', + }, + port: { + type: INTEGER.UNSIGNED, + allowNull: true, + comment: 'SDK管理端口(通常2020/8000)', + }, + model: { + type: STRING(100), + allowNull: true, + comment: '摄像头型号', + }, + serial_number: { + type: STRING(100), + allowNull: true, + comment: '摄像头序列号', + }, + stream_type: { + type: STRING(20), + allowNull: true, + comment: '码流类型(auto/main/sub)', + }, + custom_name: { + type: STRING(100), + allowNull: true, + comment: '自定义摄像头名称', + }, + status: { + type: TINYINT, + allowNull: false, + defaultValue: 1, + comment: '状态:0-禁用 1-启用', + }, + remark: { + type: STRING(500), + allowNull: true, + comment: '备注', + }, + created_at: { + type: DATE, + allowNull: false, + comment: '创建时间', + }, + updated_at: { + type: DATE, + allowNull: false, + comment: '更新时间', + }, + deleted_at: { + type: DATE, + allowNull: true, + comment: '软删除时间', + }, + }, { + tableName: 'wms_camera', + timestamps: true, + createdAt: 'created_at', + updatedAt: 'updated_at', + paranoid: true, + deletedAt: 'deleted_at', + comment: '摄像头表', + indexes: [ + { name: 'idx_warehouse_id', fields: ['warehouse_id'] }, + { name: 'idx_server_id', fields: ['server_id'] }, + { name: 'uk_server_channel', unique: true, fields: ['server_id', 'channel_id', 'deleted_at'] }, + { name: 'idx_status', fields: ['status'] }, + { name: 'idx_deleted_at', fields: ['deleted_at'] }, + ], + }); + + Camera.associate = () => { + app.model.Camera.belongsTo(app.model.CameraServer, { + foreignKey: 'server_id', + targetKey: 'id', + as: 'server', + }); + }; + + return Camera; +}; diff --git a/app/model/camera_server.js b/app/model/camera_server.js new file mode 100644 index 0000000..7b4712c --- /dev/null +++ b/app/model/camera_server.js @@ -0,0 +1,100 @@ +'use strict'; + +module.exports = app => { + const { STRING, BIGINT, INTEGER, DATE, TINYINT } = app.Sequelize; + + const CameraServer = app.model.define('camera_server', { + id: { + type: BIGINT(20).UNSIGNED, + primaryKey: true, + autoIncrement: true, + comment: '主键ID', + }, + warehouse_id: { + type: BIGINT(20).UNSIGNED, + allowNull: false, + comment: '仓库ID', + }, + name: { + type: STRING(100), + allowNull: false, + comment: '服务器名称', + }, + brand: { + type: STRING(50), + allowNull: true, + comment: '品牌(如 Hikvision/Dahua)', + }, + public_ip: { + type: STRING(50), + allowNull: true, + comment: '公网IP', + }, + internal_ip: { + type: STRING(50), + allowNull: true, + comment: '内网IP', + }, + public_port: { + type: INTEGER.UNSIGNED, + allowNull: true, + comment: '远程/公网端口', + }, + internal_port: { + type: INTEGER.UNSIGNED, + allowNull: true, + comment: '本地/内网端口', + }, + username: { + type: STRING(100), + allowNull: true, + comment: '登录账号', + }, + password: { + type: STRING(200), + allowNull: true, + comment: '登录密码', + }, + status: { + type: TINYINT, + allowNull: false, + defaultValue: 1, + comment: '状态:0-禁用 1-启用', + }, + remark: { + type: STRING(500), + allowNull: true, + comment: '备注', + }, + created_at: { + type: DATE, + allowNull: false, + comment: '创建时间', + }, + updated_at: { + type: DATE, + allowNull: false, + comment: '更新时间', + }, + deleted_at: { + type: DATE, + allowNull: true, + comment: '软删除时间', + }, + }, { + tableName: 'wms_camera_server', + timestamps: true, + createdAt: 'created_at', + updatedAt: 'updated_at', + paranoid: true, + deletedAt: 'deleted_at', + comment: '录像服务器表', + indexes: [ + { name: 'idx_warehouse_id', fields: ['warehouse_id'] }, + { name: 'idx_status', fields: ['status'] }, + { name: 'idx_deleted_at', fields: ['deleted_at'] }, + ], + }); + + return CameraServer; +}; diff --git a/app/service/systemMethods/camera.js b/app/service/systemMethods/camera.js new file mode 100644 index 0000000..050c059 --- /dev/null +++ b/app/service/systemMethods/camera.js @@ -0,0 +1,54 @@ +'use strict'; + +const MainService = require('../main'); + +class CameraService extends MainService { + constructor(ctx) { + super(ctx); + const _noNeedLogin = ''; + const _noNeedRight = 'list,detail'; + const _noNeedLog = '*'; + const _noNeedAuth = 'list,detail'; + this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth); + } + + async list(data) { + try { + const result = await this.service.systems.camera.list(data || {}); + return this.success('获取成功', result); + } catch (e) { return this.fail(e.message); } + } + + async detail(data) { + try { + const { id } = data || {}; + if (!id) return this.fail('ID不能为空'); + return this.success('获取成功', await this.service.systems.camera.detail(parseInt(id))); + } catch (e) { return this.fail(e.message); } + } + + async create(data) { + try { + return this.success('创建成功', await this.service.systems.camera.create(data)); + } catch (e) { return this.fail(e.message); } + } + + async update(data) { + try { + const { id, ...fields } = data || {}; + if (!id) return this.fail('ID不能为空'); + return this.success('更新成功', await this.service.systems.camera.update(parseInt(id), fields)); + } catch (e) { return this.fail(e.message); } + } + + async delete(data) { + try { + const { id } = data || {}; + if (!id) return this.fail('ID不能为空'); + await this.service.systems.camera.delete(parseInt(id)); + return this.success('删除成功'); + } catch (e) { return this.fail(e.message); } + } +} + +module.exports = CameraService; diff --git a/app/service/systemMethods/cameraServer.js b/app/service/systemMethods/cameraServer.js new file mode 100644 index 0000000..5b8ff72 --- /dev/null +++ b/app/service/systemMethods/cameraServer.js @@ -0,0 +1,54 @@ +'use strict'; + +const MainService = require('../main'); + +class CameraServerService extends MainService { + constructor(ctx) { + super(ctx); + const _noNeedLogin = ''; + const _noNeedRight = 'list,detail'; + const _noNeedLog = '*'; + const _noNeedAuth = 'list,detail'; + this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth); + } + + async list(data) { + try { + const result = await this.service.systems.cameraServer.list(data || {}); + return this.success('获取成功', result); + } catch (e) { return this.fail(e.message); } + } + + async detail(data) { + try { + const { id } = data || {}; + if (!id) return this.fail('ID不能为空'); + return this.success('获取成功', await this.service.systems.cameraServer.detail(parseInt(id))); + } catch (e) { return this.fail(e.message); } + } + + async create(data) { + try { + return this.success('创建成功', await this.service.systems.cameraServer.create(data)); + } catch (e) { return this.fail(e.message); } + } + + async update(data) { + try { + const { id, ...fields } = data || {}; + if (!id) return this.fail('ID不能为空'); + return this.success('更新成功', await this.service.systems.cameraServer.update(parseInt(id), fields)); + } catch (e) { return this.fail(e.message); } + } + + async delete(data) { + try { + const { id } = data || {}; + if (!id) return this.fail('ID不能为空'); + await this.service.systems.cameraServer.delete(parseInt(id)); + return this.success('删除成功'); + } catch (e) { return this.fail(e.message); } + } +} + +module.exports = CameraServerService; diff --git a/app/service/systems/camera.js b/app/service/systems/camera.js new file mode 100644 index 0000000..94658ba --- /dev/null +++ b/app/service/systems/camera.js @@ -0,0 +1,126 @@ +'use strict'; + +const Service = require('egg').Service; + +class CameraService extends Service { + async list(params = {}) { + const { ctx } = this; + const { Op } = ctx.app.Sequelize; + const warehouseId = ctx.user.store_id; + const { pagination = {}, where = {} } = params; + + const page = Math.max(1, parseInt(pagination.page) || 1); + const pageSize = Math.min(200, Math.max(1, parseInt(pagination.pageSize) || 20)); + + const queryWhere = { warehouse_id: warehouseId }; + if (where.server_id) queryWhere.server_id = parseInt(where.server_id); + if (where.custom_name) queryWhere.custom_name = { [Op.like]: `%${where.custom_name}%` }; + if (where.status !== undefined && where.status !== null && where.status !== '') { + queryWhere.status = parseInt(where.status); + } + + const { count, rows } = await ctx.model.Camera.findAndCountAll({ + where: queryWhere, + include: [{ + model: ctx.model.CameraServer, + as: 'server', + attributes: ['id', 'name', 'brand', 'public_ip', 'internal_ip', 'public_port', 'internal_port', 'status'], + required: false, + }], + order: [['created_at', 'DESC']], + offset: (page - 1) * pageSize, + limit: pageSize, + }); + + return { list: rows, pagination: { page, pageSize, total: count, totalPages: Math.ceil(count / pageSize), hasPrevious: page > 1, hasNext: page * pageSize < count } }; + } + + async detail(id) { + const { ctx } = this; + const record = await ctx.model.Camera.findOne({ + where: { id, warehouse_id: ctx.user.store_id }, + include: [{ + model: ctx.model.CameraServer, + as: 'server', + attributes: ['id', 'name', 'brand', 'public_ip', 'internal_ip', 'public_port', 'internal_port', 'status'], + required: false, + }], + }); + if (!record) throw new Error('摄像头不存在或无权访问'); + return record; + } + + async create(data) { + const { ctx } = this; + if (!data.server_id) throw new Error('所属服务器ID不能为空'); + if (data.channel_id === undefined || data.channel_id === null) throw new Error('通道编号不能为空'); + if (data.custom_name !== undefined && data.custom_name !== null && String(data.custom_name).trim() === '') { + throw new Error('自定义名称不能为空'); + } + return await ctx.model.Camera.create({ + warehouse_id: ctx.user.store_id, + server_id: parseInt(data.server_id), + channel_id: parseInt(data.channel_id), + channel_name: data.channel_name || null, + ip: data.ip || null, + port: data.port !== undefined && data.port !== null ? parseInt(data.port) : null, + model: data.model || null, + serial_number: data.serial_number || null, + stream_type: data.stream_type || null, + custom_name: data.custom_name || null, + status: data.status !== undefined ? parseInt(data.status) : 1, + remark: data.remark || null, + }); + } + + async update(id, data) { + const { ctx } = this; + const record = await ctx.model.Camera.findOne({ where: { id, warehouse_id: ctx.user.store_id } }); + if (!record) throw new Error('摄像头不存在或无权操作'); + + const fields = {}; + if (data.server_id !== undefined) fields.server_id = parseInt(data.server_id); + if (data.channel_id !== undefined) fields.channel_id = parseInt(data.channel_id); + if (data.channel_name !== undefined) fields.channel_name = data.channel_name; + if (data.ip !== undefined) fields.ip = data.ip; + if (data.port !== undefined) fields.port = data.port !== null ? parseInt(data.port) : null; + if (data.model !== undefined) fields.model = data.model; + if (data.serial_number !== undefined) fields.serial_number = data.serial_number; + if (data.stream_type !== undefined) fields.stream_type = data.stream_type; + if (data.custom_name !== undefined) { + if (data.custom_name !== null && String(data.custom_name).trim() === '') { + throw new Error('自定义名称不能为空'); + } + fields.custom_name = data.custom_name; + } + if (data.status !== undefined) fields.status = parseInt(data.status); + if (data.remark !== undefined) fields.remark = data.remark; + if (Object.keys(fields).length === 0) throw new Error('没有需要更新的字段'); + + await record.update(fields); + return record; + } + + async delete(id) { + const { ctx } = this; + const warehouseId = ctx.user.store_id; + const record = await ctx.model.Camera.findOne({ where: { id, warehouse_id: warehouseId } }); + if (!record) throw new Error('摄像头不存在或无权操作'); + + // 检查是否被工作台绑定 + const boundWorkbenches = await ctx.model.Workbench.findAll({ + where: { warehouse_id: warehouseId }, + attributes: ['id', 'name'], + }); + for (const wb of boundWorkbenches) { + const deviceIds = Array.isArray(wb.device_ids) ? wb.device_ids : []; + if (deviceIds.includes(id)) { + throw new Error(`摄像头已被工作台「${wb.name}」绑定,请先解绑后再删除`); + } + } + + await record.destroy(); + } +} + +module.exports = CameraService; diff --git a/app/service/systems/cameraServer.js b/app/service/systems/cameraServer.js new file mode 100644 index 0000000..6437e65 --- /dev/null +++ b/app/service/systems/cameraServer.js @@ -0,0 +1,127 @@ +'use strict'; + +const Service = require('egg').Service; + +class CameraServerService extends Service { + async list(params = {}) { + const { ctx } = this; + const { Op } = ctx.app.Sequelize; + const warehouseId = ctx.user.store_id; + const { pagination = {}, where = {} } = params; + + const page = Math.max(1, parseInt(pagination.page) || 1); + const pageSize = Math.min(200, Math.max(1, parseInt(pagination.pageSize) || 20)); + + const queryWhere = { warehouse_id: warehouseId }; + if (where.name) queryWhere.name = { [Op.like]: `%${where.name}%` }; + if (where.status !== undefined && where.status !== null && where.status !== '') { + queryWhere.status = parseInt(where.status); + } + + const { count, rows } = await ctx.model.CameraServer.findAndCountAll({ + where: queryWhere, + order: [['created_at', 'DESC']], + offset: (page - 1) * pageSize, + limit: pageSize, + }); + + return { list: rows, pagination: { page, pageSize, total: count, totalPages: Math.ceil(count / pageSize), hasPrevious: page > 1, hasNext: page * pageSize < count } }; + } + + async detail(id) { + const { ctx } = this; + const record = await ctx.model.CameraServer.findOne({ where: { id, warehouse_id: ctx.user.store_id } }); + if (!record) throw new Error('录像服务器不存在或无权访问'); + return record; + } + + async create(data) { + const { ctx } = this; + if (!data.name || !String(data.name).trim()) throw new Error('服务器名称不能为空'); + + // 规范化 IP 和端口 + const publicIp = data.public_ip ? String(data.public_ip).trim() : ''; + const internalIp = data.internal_ip ? String(data.internal_ip).trim() : ''; + const publicPort = data.public_port !== undefined && data.public_port !== null ? parseInt(data.public_port) : null; + const internalPort = data.internal_port !== undefined && data.internal_port !== null ? parseInt(data.internal_port) : null; + + // IP-端口配对校验:有 IP 则端口必填 + if (publicIp && !publicPort) throw new Error('公网端口不能为空'); + if (internalIp && !internalPort) throw new Error('内网端口不能为空'); + + // 至少一组 IP+端口 完整 + const hasPublic = !!(publicIp && publicPort); + const hasInternal = !!(internalIp && internalPort); + if (!hasPublic && !hasInternal) throw new Error('至少需要填写一组公网IP+端口或内网IP+端口'); + + return await ctx.model.CameraServer.create({ + warehouse_id: ctx.user.store_id, + name: String(data.name).trim(), + brand: data.brand || null, + public_ip: publicIp || null, + internal_ip: internalIp || null, + public_port: publicPort, + internal_port: internalPort, + username: data.username || null, + password: data.password || null, + status: data.status !== undefined ? parseInt(data.status) : 1, + remark: data.remark || null, + }); + } + + async update(id, data) { + const { ctx } = this; + const record = await ctx.model.CameraServer.findOne({ where: { id, warehouse_id: ctx.user.store_id } }); + if (!record) throw new Error('录像服务器不存在或无权操作'); + + const fields = {}; + if (data.name !== undefined) fields.name = String(data.name).trim(); + if (data.brand !== undefined) fields.brand = data.brand; + if (data.public_ip !== undefined) fields.public_ip = data.public_ip ? String(data.public_ip).trim() : null; + if (data.internal_ip !== undefined) fields.internal_ip = data.internal_ip ? String(data.internal_ip).trim() : null; + if (data.public_port !== undefined) fields.public_port = data.public_port !== null ? parseInt(data.public_port) : null; + if (data.internal_port !== undefined) fields.internal_port = data.internal_port !== null ? parseInt(data.internal_port) : null; + if (data.username !== undefined) fields.username = data.username; + if (data.password !== undefined) fields.password = data.password; + if (data.status !== undefined) fields.status = parseInt(data.status); + if (data.remark !== undefined) fields.remark = data.remark; + if (Object.keys(fields).length === 0) throw new Error('没有需要更新的字段'); + + // 合并新旧值,校验最终状态 + const mergedPublicIp = data.public_ip !== undefined ? fields.public_ip : record.public_ip; + const mergedInternalIp = data.internal_ip !== undefined ? fields.internal_ip : record.internal_ip; + const mergedPublicPort = data.public_port !== undefined ? fields.public_port : record.public_port; + const mergedInternalPort = data.internal_port !== undefined ? fields.internal_port : record.internal_port; + + // IP-端口配对校验 + if (mergedPublicIp && !mergedPublicPort) throw new Error('公网端口不能为空'); + if (mergedInternalIp && !mergedInternalPort) throw new Error('内网端口不能为空'); + + // 至少一组 IP+端口 完整 + const hasPublic = !!(mergedPublicIp && mergedPublicPort); + const hasInternal = !!(mergedInternalIp && mergedInternalPort); + if (!hasPublic && !hasInternal) throw new Error('至少需要填写一组公网IP+端口或内网IP+端口'); + + await record.update(fields); + return record; + } + + async delete(id) { + const { ctx } = this; + const warehouseId = ctx.user.store_id; + const record = await ctx.model.CameraServer.findOne({ where: { id, warehouse_id: warehouseId } }); + if (!record) throw new Error('录像服务器不存在或无权操作'); + + // 检查是否有关联摄像头 + const cameraCount = await ctx.model.Camera.count({ + where: { server_id: id, warehouse_id: warehouseId }, + }); + if (cameraCount > 0) { + throw new Error(`录像服务器下存在 ${cameraCount} 个摄像头,请先删除摄像头后再删除服务器`); + } + + await record.destroy(); + } +} + +module.exports = CameraServerService; diff --git a/database/migrations/20260715220000-create-wms-camera-server.js b/database/migrations/20260715220000-create-wms-camera-server.js new file mode 100644 index 0000000..5ad66f3 --- /dev/null +++ b/database/migrations/20260715220000-create-wms-camera-server.js @@ -0,0 +1,98 @@ +'use strict'; + +const TABLE_NAME = 'wms_camera_server'; + +module.exports = { + up: async (queryInterface, Sequelize) => { + const tables = await queryInterface.showAllTables(); + const exists = tables.some(t => (typeof t === 'string' ? t : t.tableName) === TABLE_NAME); + if (exists) return; + + await queryInterface.createTable(TABLE_NAME, { + id: { + type: Sequelize.BIGINT(20).UNSIGNED, + primaryKey: true, + autoIncrement: true, + allowNull: false, + comment: '主键ID', + }, + warehouse_id: { + type: Sequelize.BIGINT(20).UNSIGNED, + allowNull: false, + comment: '仓库ID', + }, + name: { + type: Sequelize.STRING(100), + allowNull: false, + comment: '服务器名称', + }, + brand: { + type: Sequelize.STRING(50), + allowNull: true, + comment: '品牌(如 Hikvision/Dahua)', + }, + public_ip: { + type: Sequelize.STRING(50), + allowNull: true, + comment: '公网IP', + }, + internal_ip: { + type: Sequelize.STRING(50), + allowNull: true, + comment: '内网IP', + }, + port: { + type: Sequelize.INTEGER.UNSIGNED, + allowNull: true, + defaultValue: 80, + comment: '端口号', + }, + username: { + type: Sequelize.STRING(100), + allowNull: true, + comment: '登录账号', + }, + password: { + type: Sequelize.STRING(200), + allowNull: true, + comment: '登录密码', + }, + status: { + type: Sequelize.TINYINT, + allowNull: false, + defaultValue: 1, + comment: '状态:0-禁用 1-启用', + }, + remark: { + type: Sequelize.STRING(500), + allowNull: true, + comment: '备注', + }, + created_at: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), + comment: '创建时间', + }, + updated_at: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), + comment: '更新时间', + }, + deleted_at: { + type: Sequelize.DATE, + allowNull: true, + comment: '软删除时间', + }, + }); + + await queryInterface.addIndex(TABLE_NAME, ['warehouse_id'], { name: 'idx_warehouse_id' }); + await queryInterface.addIndex(TABLE_NAME, ['status'], { name: 'idx_status' }); + await queryInterface.addIndex(TABLE_NAME, ['deleted_at'], { name: 'idx_deleted_at' }); + }, + + down: async queryInterface => { + await queryInterface.dropTable(TABLE_NAME); + }, +}; diff --git a/database/migrations/20260715220001-create-wms-camera.js b/database/migrations/20260715220001-create-wms-camera.js new file mode 100644 index 0000000..6a6e894 --- /dev/null +++ b/database/migrations/20260715220001-create-wms-camera.js @@ -0,0 +1,88 @@ +'use strict'; + +const TABLE_NAME = 'wms_camera'; + +module.exports = { + up: async (queryInterface, Sequelize) => { + const tables = await queryInterface.showAllTables(); + const exists = tables.some(t => (typeof t === 'string' ? t : t.tableName) === TABLE_NAME); + if (exists) return; + + await queryInterface.createTable(TABLE_NAME, { + id: { + type: Sequelize.BIGINT(20).UNSIGNED, + primaryKey: true, + autoIncrement: true, + allowNull: false, + comment: '主键ID', + }, + warehouse_id: { + type: Sequelize.BIGINT(20).UNSIGNED, + allowNull: false, + comment: '仓库ID', + }, + server_id: { + type: Sequelize.BIGINT(20).UNSIGNED, + allowNull: false, + comment: '所属录像服务器ID,关联 wms_camera_server.id', + }, + channel_id: { + type: Sequelize.INTEGER.UNSIGNED, + allowNull: false, + comment: '通道编号(DVR/NVR 上的物理通道号)', + }, + channel_name: { + type: Sequelize.STRING(100), + allowNull: true, + comment: '通道原始名称', + }, + custom_name: { + type: Sequelize.STRING(100), + allowNull: true, + comment: '自定义摄像头名称', + }, + status: { + type: Sequelize.TINYINT, + allowNull: false, + defaultValue: 1, + comment: '状态:0-禁用 1-启用', + }, + remark: { + type: Sequelize.STRING(500), + allowNull: true, + comment: '备注', + }, + created_at: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), + comment: '创建时间', + }, + updated_at: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), + comment: '更新时间', + }, + deleted_at: { + type: Sequelize.DATE, + allowNull: true, + comment: '软删除时间', + }, + }); + + await queryInterface.addIndex(TABLE_NAME, ['warehouse_id'], { name: 'idx_warehouse_id' }); + await queryInterface.addIndex(TABLE_NAME, ['server_id'], { name: 'idx_server_id' }); + // 同一服务器下通道号唯一 + await queryInterface.addIndex(TABLE_NAME, ['server_id', 'channel_id', 'deleted_at'], { + unique: true, + name: 'uk_server_channel', + }); + await queryInterface.addIndex(TABLE_NAME, ['status'], { name: 'idx_status' }); + await queryInterface.addIndex(TABLE_NAME, ['deleted_at'], { name: 'idx_deleted_at' }); + }, + + down: async queryInterface => { + await queryInterface.dropTable(TABLE_NAME); + }, +}; diff --git a/database/migrations/20260715230000-create-wms-pack-task-video.js b/database/migrations/20260715230000-create-wms-pack-task-video.js new file mode 100644 index 0000000..ceeeffa --- /dev/null +++ b/database/migrations/20260715230000-create-wms-pack-task-video.js @@ -0,0 +1,72 @@ +'use strict'; + +const TABLE_NAME = 'wms_pack_task_video'; + +module.exports = { + up: async (queryInterface, Sequelize) => { + const tables = await queryInterface.showAllTables(); + const exists = tables.some(t => (typeof t === 'string' ? t : t.tableName) === TABLE_NAME); + if (exists) return; + + await queryInterface.createTable(TABLE_NAME, { + id: { + type: Sequelize.BIGINT(20).UNSIGNED, + primaryKey: true, + autoIncrement: true, + allowNull: false, + comment: '主键ID', + }, + pack_task_id: { + type: Sequelize.BIGINT(20).UNSIGNED, + allowNull: false, + comment: '打包任务ID,关联 wms_pack_task.id', + }, + camera_id: { + type: Sequelize.BIGINT(20).UNSIGNED, + allowNull: true, + comment: '摄像头设备ID,关联 wms_camera.id', + }, + video_url: { + type: Sequelize.STRING(500), + allowNull: false, + comment: '视频地址(录像回放URL)', + }, + status: { + type: Sequelize.TINYINT, + allowNull: false, + defaultValue: 1, + comment: '状态:0-无效 1-有效', + }, + remark: { + type: Sequelize.STRING(500), + allowNull: true, + comment: '备注', + }, + created_at: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), + comment: '创建时间', + }, + updated_at: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), + comment: '更新时间', + }, + deleted_at: { + type: Sequelize.DATE, + allowNull: true, + comment: '软删除时间', + }, + }); + + await queryInterface.addIndex(TABLE_NAME, ['pack_task_id'], { name: 'idx_pack_task_id' }); + await queryInterface.addIndex(TABLE_NAME, ['camera_id'], { name: 'idx_camera_id' }); + await queryInterface.addIndex(TABLE_NAME, ['deleted_at'], { name: 'idx_deleted_at' }); + }, + + down: async queryInterface => { + await queryInterface.dropTable(TABLE_NAME); + }, +}; diff --git a/database/migrations/20260717150000-add-camera-device-fields.js b/database/migrations/20260717150000-add-camera-device-fields.js new file mode 100644 index 0000000..6acfe27 --- /dev/null +++ b/database/migrations/20260717150000-add-camera-device-fields.js @@ -0,0 +1,55 @@ +'use strict'; + +const TABLE_NAME = 'wms_camera'; + +/** + * 新增摄像头设备信息字段:IP、SDK端口、型号、序列号、码流类型。 + * 用于保存 NVR 服务器返回的摄像头详细设备信息。 + */ +module.exports = { + up: async (queryInterface, Sequelize) => { + const columns = [ + { + name: 'ip', + def: { type: Sequelize.STRING(50), allowNull: true, comment: '摄像头IP地址', after: 'channel_name' }, + }, + { + name: 'port', + def: { type: Sequelize.INTEGER.UNSIGNED, allowNull: true, comment: 'SDK管理端口(通常2020/8000)', after: 'ip' }, + }, + { + name: 'model', + def: { type: Sequelize.STRING(100), allowNull: true, comment: '摄像头型号', after: 'port' }, + }, + { + name: 'serial_number', + def: { type: Sequelize.STRING(100), allowNull: true, comment: '摄像头序列号', after: 'model' }, + }, + { + name: 'stream_type', + def: { type: Sequelize.STRING(20), allowNull: true, comment: '码流类型(auto/main/sub)', after: 'serial_number' }, + }, + ]; + + for (const col of columns) { + const tableInfo = await queryInterface.describeTable(TABLE_NAME); + if (!tableInfo[col.name]) { + await queryInterface.addColumn(TABLE_NAME, col.name, col.def); + console.log(`[Migration] wms_camera.${col.name} 已添加`); + } else { + console.log(`[Migration] wms_camera.${col.name} 已存在,跳过`); + } + } + }, + + down: async queryInterface => { + const columns = ['stream_type', 'serial_number', 'model', 'port', 'ip']; + for (const col of columns) { + const tableInfo = await queryInterface.describeTable(TABLE_NAME); + if (tableInfo[col]) { + await queryInterface.removeColumn(TABLE_NAME, col); + console.log(`[Migration] wms_camera.${col} 已移除`); + } + } + }, +}; diff --git a/database/migrations/20260718160000-split-camera-server-port.js b/database/migrations/20260718160000-split-camera-server-port.js new file mode 100644 index 0000000..e3f4b10 --- /dev/null +++ b/database/migrations/20260718160000-split-camera-server-port.js @@ -0,0 +1,89 @@ +'use strict'; + +const TABLE_NAME = 'wms_camera_server'; + +/** + * 将 wms_camera_server.port 拆分为 public_port(远程/公网端口)和 internal_port(本地/内网端口)。 + * 原因:远程访问端口和本地访问端口通常不同(如 NAT 映射),需要分别存储。 + */ +module.exports = { + up: async (queryInterface, Sequelize) => { + const tableInfo = await queryInterface.describeTable(TABLE_NAME); + + // 1. 添加 public_port + if (!tableInfo.public_port) { + await queryInterface.addColumn(TABLE_NAME, 'public_port', { + type: Sequelize.INTEGER.UNSIGNED, + allowNull: true, + comment: '远程/公网端口', + after: 'internal_ip', + }); + console.log('[Migration] wms_camera_server.public_port 已添加'); + } else { + console.log('[Migration] wms_camera_server.public_port 已存在,跳过'); + } + + // 2. 添加 internal_port + if (!tableInfo.internal_port) { + await queryInterface.addColumn(TABLE_NAME, 'internal_port', { + type: Sequelize.INTEGER.UNSIGNED, + allowNull: true, + comment: '本地/内网端口', + after: 'public_port', + }); + console.log('[Migration] wms_camera_server.internal_port 已添加'); + } else { + console.log('[Migration] wms_camera_server.internal_port 已存在,跳过'); + } + + // 3. 将旧 port 数据迁移到 public_port 和 internal_port(保留旧值,后续可按需调整) + if (tableInfo.port) { + await queryInterface.sequelize.query( + `UPDATE ${TABLE_NAME} SET public_port = port, internal_port = port WHERE port IS NOT NULL` + ); + console.log('[Migration] wms_camera_server.port 数据已复制到 public_port 和 internal_port'); + + // 4. 删除旧 port 列 + await queryInterface.removeColumn(TABLE_NAME, 'port'); + console.log('[Migration] wms_camera_server.port 列已删除'); + } else { + console.log('[Migration] wms_camera_server.port 列不存在,跳过迁移和删除'); + } + }, + + down: async (queryInterface, Sequelize) => { + const tableInfo = await queryInterface.describeTable(TABLE_NAME); + + // 恢复 port 列 + if (!tableInfo.port) { + await queryInterface.addColumn(TABLE_NAME, 'port', { + type: Sequelize.INTEGER.UNSIGNED, + allowNull: true, + defaultValue: 80, + comment: '端口号', + after: 'internal_ip', + }); + console.log('[Migration] wms_camera_server.port 列已恢复'); + } + + // 将 public_port 数据回写到 port(优先使用 internal_port) + if (tableInfo.public_port || tableInfo.internal_port) { + await queryInterface.sequelize.query( + `UPDATE ${TABLE_NAME} SET port = COALESCE(internal_port, public_port) WHERE internal_port IS NOT NULL OR public_port IS NOT NULL` + ); + console.log('[Migration] port 数据已从 public_port/internal_port 回写'); + } + + // 删除 public_port + if (tableInfo.public_port) { + await queryInterface.removeColumn(TABLE_NAME, 'public_port'); + console.log('[Migration] wms_camera_server.public_port 列已删除'); + } + + // 删除 internal_port + if (tableInfo.internal_port) { + await queryInterface.removeColumn(TABLE_NAME, 'internal_port'); + console.log('[Migration] wms_camera_server.internal_port 列已删除'); + } + }, +}; diff --git a/database/migrations/20260726120000-update-pack-task-video-status-enum.js b/database/migrations/20260726120000-update-pack-task-video-status-enum.js new file mode 100644 index 0000000..087eaa6 --- /dev/null +++ b/database/migrations/20260726120000-update-pack-task-video-status-enum.js @@ -0,0 +1,59 @@ +'use strict'; + +/** + * wms_pack_task_video.status 字段改为 ENUM 类型。 + * + * 变更前:TINYINT(无明确语义) + * 变更后:ENUM('created', 'completed', 'failed') + * - created = 已创建 + * - completed = 已完成 + * - failed = 失败 + * + * 现有数据映射:status=1 → 'created' + */ + +module.exports = { + async up(queryInterface, Sequelize) { + // 新增临时列 + await queryInterface.sequelize.query( + "ALTER TABLE wms_pack_task_video ADD COLUMN status_new ENUM('created','completed','failed') NOT NULL DEFAULT 'created' COMMENT '状态:已创建/已完成/失败' AFTER status" + ); + + // 迁移现有数据:TINYINT 1 → 'created' + await queryInterface.sequelize.query( + "UPDATE wms_pack_task_video SET status_new = 'created' WHERE status = 1" + ); + + // 删除旧列 + await queryInterface.sequelize.query( + 'ALTER TABLE wms_pack_task_video DROP COLUMN status' + ); + + // 新列改名为 status + await queryInterface.sequelize.query( + "ALTER TABLE wms_pack_task_video CHANGE COLUMN status_new status ENUM('created','completed','failed') NOT NULL DEFAULT 'created' COMMENT '状态:已创建/已完成/失败'" + ); + }, + + async down(queryInterface, Sequelize) { + // 新增临时 TINYINT 列 + await queryInterface.sequelize.query( + 'ALTER TABLE wms_pack_task_video ADD COLUMN status_new TINYINT NOT NULL DEFAULT 1 AFTER status' + ); + + // 回退数据:'created' → 1 + await queryInterface.sequelize.query( + "UPDATE wms_pack_task_video SET status_new = 1 WHERE status = 'created'" + ); + + // 删除 ENUM 列 + await queryInterface.sequelize.query( + 'ALTER TABLE wms_pack_task_video DROP COLUMN status' + ); + + // 改名为 status + await queryInterface.sequelize.query( + 'ALTER TABLE wms_pack_task_video CHANGE COLUMN status_new status TINYINT NOT NULL DEFAULT 1' + ); + }, +}; diff --git a/database/migrations/20260726130000-add-recording-status-pack-task-video.js b/database/migrations/20260726130000-add-recording-status-pack-task-video.js new file mode 100644 index 0000000..4bf006d --- /dev/null +++ b/database/migrations/20260726130000-add-recording-status-pack-task-video.js @@ -0,0 +1,26 @@ +'use strict'; + +/** + * wms_pack_task_video.status 增加「录制中」状态。 + * + * 变更前:ENUM('created','completed','failed') + * 变更后:ENUM('created','recording','completed','failed') + * - created = 已创建 + * - recording = 录制中(新增) + * - completed = 已完成 + * - failed = 失败 + */ + +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.sequelize.query( + "ALTER TABLE wms_pack_task_video MODIFY COLUMN status ENUM('created','recording','completed','failed') NOT NULL DEFAULT 'created' COMMENT '状态:已创建/录制中/已完成/失败'" + ); + }, + + async down(queryInterface, Sequelize) { + await queryInterface.sequelize.query( + "ALTER TABLE wms_pack_task_video MODIFY COLUMN status ENUM('created','completed','failed') NOT NULL DEFAULT 'created' COMMENT '状态:已创建/已完成/失败'" + ); + }, +};