- 新增 model: camera, camera_server - 新增 services: camera, cameraServer (systemMethods + systems) - 新增 pack_task_video 表及录制状态管理 - 7 条迁移: 建表→设备字段→端口拆分→状态枚举
128 lines
5.7 KiB
JavaScript
128 lines
5.7 KiB
JavaScript
'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;
|