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