From c69984929a8c8e1d89b4af309f742687067265db Mon Sep 17 00:00:00 2001 From: Your Name <2499342078@qq.com> Date: Wed, 2 Jul 2025 15:25:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=97=A0=E5=88=86=E7=B1=BB?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E9=9D=A2=E6=9D=BFbug=E5=92=8C=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/server/src/tasks/init/gendev.service.ts | 109 +----------------- .../app/main/devicepage/dashboard/page.tsx | 72 +++++++----- .../main/devicepage/select/Device-select.tsx | 15 ++- config/nginx/conf.d/web.conf | 4 +- 4 files changed, 61 insertions(+), 139 deletions(-) diff --git a/apps/server/src/tasks/init/gendev.service.ts b/apps/server/src/tasks/init/gendev.service.ts index 22b38de..d134f5f 100755 --- a/apps/server/src/tasks/init/gendev.service.ts +++ b/apps/server/src/tasks/init/gendev.service.ts @@ -57,7 +57,7 @@ export class GenDevService { this.logger.log('初始化设备分类'); // 创建网系类型分类 let systemTypeTaxonomy = await db.taxonomy.findFirst({ - where: { slug: 'network_type' }, // 改名为 network_type + where: { slug: 'network_type' }, }); if (!systemTypeTaxonomy) { systemTypeTaxonomy = await db.taxonomy.create({ @@ -97,112 +97,7 @@ export class GenDevService { }); } - this.logger.log('创建三层分类记录'); - - // 定义三层分类结构 - const networkTypes = [ - { - name: '办公网络', - systemTypes: [ - { - name: '文印系统', - faultTypes: ['电源故障', '主板故障', '内存故障', '硬盘故障'], - }, - { - name: '内网系统', - faultTypes: ['系统崩溃', '应用程序错误', '病毒感染', '驱动问题'], - }, - ], - }, - { - name: '安全网络', - systemTypes: [ - { - name: '监控系统', - faultTypes: ['摄像头故障', '录像异常', '存储故障', '网络断开'], - }, - { - name: '门禁系统', - faultTypes: ['读卡器故障', '门锁故障', '系统异常', '网络故障'], - }, - ], - }, - ]; - - // 创建三层分类结构 - for (const networkTypeData of networkTypes) { - // 创建网系类型 - const networkType = await db.term.findFirst({ - where: { - name: networkTypeData.name, - taxonomyId: systemTypeTaxonomy.id, - }, - }); - - let networkTypeId; - if (!networkType) { - const newNetworkType = await db.term.create({ - data: { - name: networkTypeData.name, - taxonomyId: systemTypeTaxonomy.id, - hasChildren: true, - }, - }); - networkTypeId = newNetworkType.id; - } else { - networkTypeId = networkType.id; - } - - // 为每个网系类型创建系统类型 - for (const systemTypeData of networkTypeData.systemTypes) { - const systemType = await db.term.findFirst({ - where: { - name: systemTypeData.name, - taxonomyId: systemSubTypeTaxonomy.id, - parentId: networkTypeId, - }, - }); - - let systemTypeId; - if (!systemType) { - const newSystemType = await db.term.create({ - data: { - name: systemTypeData.name, - taxonomyId: systemSubTypeTaxonomy.id, - parentId: networkTypeId, - hasChildren: true, - }, - }); - systemTypeId = newSystemType.id; - } else { - systemTypeId = systemType.id; - } - - // 为每个系统类型创建故障类型 - for (const faultTypeName of systemTypeData.faultTypes) { - const faultType = await db.term.findFirst({ - where: { - name: faultTypeName, - taxonomyId: deviceTypeTaxonomy.id, - parentId: systemTypeId, - }, - }); - - if (!faultType) { - await db.term.create({ - data: { - name: faultTypeName, - taxonomyId: deviceTypeTaxonomy.id, - parentId: systemTypeId, - hasChildren: false, - }, - }); - } - } - } - } - - this.logger.log('初始化三层设备分类完成'); + this.logger.log('初始化设备分类完成'); } private async calculateCounts() { this.counts = await getCounts(); diff --git a/apps/web/src/app/main/devicepage/dashboard/page.tsx b/apps/web/src/app/main/devicepage/dashboard/page.tsx index d17a650..8a5ff5f 100755 --- a/apps/web/src/app/main/devicepage/dashboard/page.tsx +++ b/apps/web/src/app/main/devicepage/dashboard/page.tsx @@ -78,7 +78,7 @@ const DashboardPage = () => { // 初始化选中的网系和系统类型(选择故障最多的) useEffect(() => { - if (devices && networkTypeTerms && systemTypeTerms && !selectedNetworkType) { + if (devices && networkTypeTerms && systemTypeTerms && networkTypeTerms.length > 0 && !selectedNetworkType) { // 找到故障最多的网系类型 const networkCounts = {}; networkTypeTerms.forEach(type => { @@ -93,33 +93,37 @@ const DashboardPage = () => { } }); - const maxNetworkTypeId = Object.keys(networkCounts).reduce((a, b) => - networkCounts[a] > networkCounts[b] ? a : b - ); - - setSelectedNetworkType(maxNetworkTypeId); - - // 找到该网系下故障最多的系统类型 - const systemCounts = {}; - const relevantSystemTypes = systemTypeTerms.filter(st => st.parentId === maxNetworkTypeId); - - relevantSystemTypes.forEach(type => { - systemCounts[type.id] = 0; - }); - - devices.forEach(device => { - const deviceTerms = getDeviceTerms(device); - const systemType = systemTypeTerms.find(t => t.name === deviceTerms.systemType); - if (systemType && Object.prototype.hasOwnProperty.call(systemCounts, systemType.id)) { - systemCounts[systemType.id]++; - } - }); - - if (Object.keys(systemCounts).length > 0) { - const maxSystemTypeId = Object.keys(systemCounts).reduce((a, b) => - systemCounts[a] > systemCounts[b] ? a : b + const networkCountKeys = Object.keys(networkCounts); + if (networkCountKeys.length > 0) { + const maxNetworkTypeId = networkCountKeys.reduce((a, b) => + networkCounts[a] > networkCounts[b] ? a : b ); - setSelectedSystemType(maxSystemTypeId); + + setSelectedNetworkType(maxNetworkTypeId); + + // 找到该网系下故障最多的系统类型 + const systemCounts = {}; + const relevantSystemTypes = systemTypeTerms.filter(st => st.parentId === maxNetworkTypeId); + + relevantSystemTypes.forEach(type => { + systemCounts[type.id] = 0; + }); + + devices.forEach(device => { + const deviceTerms = getDeviceTerms(device); + const systemType = systemTypeTerms.find(t => t.name === deviceTerms.systemType); + if (systemType && Object.prototype.hasOwnProperty.call(systemCounts, systemType.id)) { + systemCounts[systemType.id]++; + } + }); + + const systemCountKeys = Object.keys(systemCounts); + if (systemCountKeys.length > 0) { + const maxSystemTypeId = systemCountKeys.reduce((a, b) => + systemCounts[a] > systemCounts[b] ? a : b + ); + setSelectedSystemType(maxSystemTypeId); + } } } }, [devices, networkTypeTerms, systemTypeTerms, selectedNetworkType]); @@ -233,7 +237,9 @@ const DashboardPage = () => { // 准备三层分类统计数据 const prepareHierarchicalFaultData = () => { - if (!devices || !networkTypeTerms) return { names: [], values: [] }; + if (!devices || !networkTypeTerms || networkTypeTerms.length === 0) { + return { names: [], values: [] }; + } const hierarchicalCounts = {}; const totalDevices = devices.length; @@ -263,7 +269,10 @@ const DashboardPage = () => { // 修改系统类型分布数据(基于选中的网系类型,支持交互) const prepareGroupedSystemTypeDistribution = () => { - if (!devices || !systemTypeTerms || !networkTypeTerms) return { groups: [], values: [] }; + if (!devices || !systemTypeTerms || !networkTypeTerms || + systemTypeTerms.length === 0 || networkTypeTerms.length === 0) { + return { groups: [], values: [] }; + } const networkGroups: Record = {}; const systemTypeCounts: Record = {}; @@ -273,6 +282,11 @@ const DashboardPage = () => { ? [networkTypeTerms.find(t => t.id === selectedNetworkType)].filter(Boolean) : networkTypeTerms; + // 如果没有找到目标网系类型,返回空数据 + if (targetNetworkTypes.length === 0) { + return { groups: [], values: [] }; + } + // 初始化网系分组 targetNetworkTypes.forEach(networkType => { networkGroups[networkType.name] = []; diff --git a/apps/web/src/app/main/devicepage/select/Device-select.tsx b/apps/web/src/app/main/devicepage/select/Device-select.tsx index e131119..fcfca79 100755 --- a/apps/web/src/app/main/devicepage/select/Device-select.tsx +++ b/apps/web/src/app/main/devicepage/select/Device-select.tsx @@ -1,6 +1,6 @@ import { Select, Spin } from "antd"; import { api } from "@nice/client"; -import { useEffect, useState } from "react"; +import { useEffect, useState, useRef } from "react"; import React from "react"; interface DeviceTypeSelectProps { @@ -40,6 +40,19 @@ export default function DeviceTypeSelect({ orderBy: { order: "asc" }, }); + // 使用 useRef 来跟踪之前的 systemTypeId + const prevSystemTypeIdRef = useRef(systemTypeId); + + // 当系统类型改变时,清空当前选择 + useEffect(() => { + if (prevSystemTypeIdRef.current !== systemTypeId) { + prevSystemTypeIdRef.current = systemTypeId; + if (onChange && value) { + onChange(undefined); + } + } + }, [systemTypeId]); + // 处理选项数据 useEffect(() => { if (deviceTypes) { diff --git a/config/nginx/conf.d/web.conf b/config/nginx/conf.d/web.conf index 269f3d1..718b7ff 100755 --- a/config/nginx/conf.d/web.conf +++ b/config/nginx/conf.d/web.conf @@ -2,7 +2,7 @@ server { # 监听80端口 listen 80; # 服务器域名/IP地址,使用环境变量 - server_name 192.168.122.194; + server_name 192.168.77.194; # 基础性能优化配置 # 启用tcp_nopush以优化数据发送 @@ -100,7 +100,7 @@ server { # 仅供内部使用 internal; # 代理到认证服务 - proxy_pass http://192.168.122.194:3000/auth/file; + proxy_pass http://192.168.77.194:3000/auth/file; # 请求优化:不传递请求体 proxy_pass_request_body off;