完善项目

This commit is contained in:
xuqiuyun
2025-09-28 17:58:43 +08:00
parent ec3f472641
commit 5b615473e0
59 changed files with 5428 additions and 593 deletions

View File

@@ -7,7 +7,7 @@ const express = require('express');
const router = express.Router();
const { verifyToken, checkRole } = require('../middleware/auth');
const { requirePermission } = require('../middleware/permission');
const { IotXqClient, IotJbqServer, IotJbqClient } = require('../models');
const { IotXqClient, IotJbqServer, IotJbqClient, Farm } = require('../models');
const { Op } = require('sequelize');
const { createSuccessResponse, createErrorResponse, createPaginatedResponse, SUCCESS_MESSAGES, ERROR_CODES } = require('../utils/apiResponse');
@@ -1366,6 +1366,14 @@ router.get('/hosts', verifyToken, requirePermission('smart_host:view'), async (r
]
});
// 批量查询牧场信息,构建映射表
const orgIds = [...new Set(rows.map(h => h.org_id).filter(id => id !== null && id !== undefined))];
let farmMap = {};
if (orgIds.length > 0) {
const farms = await Farm.findAll({ where: { id: orgIds } });
farmMap = Object.fromEntries(farms.map(f => [f.id, f.name]));
}
// 格式化数据以匹配前端UI需求
const hosts = rows.map(host => ({
id: host.id,
@@ -1383,6 +1391,9 @@ router.get('/hosts', verifyToken, requirePermission('smart_host:view'), async (r
state: host.state, // 设备状态
title: host.title, // 设备标题
org_id: host.org_id, // 组织ID
farmId: host.org_id, // 牧场ID与组织ID一致
farmName: farmMap[host.org_id] || '-', // 牧场名称
farm: { id: host.org_id, name: farmMap[host.org_id] || '-' },
uid: host.uid, // 用户ID
fence_id: host.fence_id, // 围栏ID
source_id: host.source_id, // 数据源ID
@@ -1449,6 +1460,19 @@ router.get('/hosts/:id', verifyToken, requirePermission('smart_host:view'), asyn
}
// 格式化数据
// 牧场信息
let farmId = host.org_id;
let farmName = '-';
if (farmId !== null && farmId !== undefined) {
try {
const farm = await Farm.findByPk(farmId);
if (farm && farm.name) {
farmName = farm.name;
}
} catch (e) {
// 牧场查询失败时忽略,不影响主机详情返回
}
}
const hostData = {
id: host.id,
deviceNumber: host.sid,
@@ -1465,6 +1489,9 @@ router.get('/hosts/:id', verifyToken, requirePermission('smart_host:view'), asyn
state: host.state,
title: host.title,
org_id: host.org_id,
farmId: farmId,
farmName: farmName,
farm: { id: farmId, name: farmName },
uid: host.uid,
fence_id: host.fence_id,
source_id: host.source_id,