From 5ea0d97a1bf9030945fab70cd8e21aa6e98c7950 Mon Sep 17 00:00:00 2001
From: 无关风月 <443237572@qq.com>
Date: 星期六, 24 一月 2026 09:35:14 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ClientServiceImpl.java |  142 ++++++++++++++++++++++++++---------------------
 1 files changed, 79 insertions(+), 63 deletions(-)

diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ClientServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ClientServiceImpl.java
index 45264b2..80c1689 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ClientServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ClientServiceImpl.java
@@ -6,20 +6,10 @@
 import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.ruoyi.system.mapper.CheckMapper;
-import com.ruoyi.system.mapper.ClientMapper;
-import com.ruoyi.system.mapper.DeviceUsageMapper;
-import com.ruoyi.system.mapper.PlanMapper;
-import com.ruoyi.system.mapper.ProjectMapper;
-import com.ruoyi.system.mapper.RecordMapper;
-import com.ruoyi.system.mapper.TaskMapper;
-import com.ruoyi.system.model.Check;
-import com.ruoyi.system.model.Client;
-import com.ruoyi.system.model.DeviceUsage;
-import com.ruoyi.system.model.Plan;
-import com.ruoyi.system.model.Project;
+import com.ruoyi.system.enums.DeviceType;
+import com.ruoyi.system.mapper.*;
+import com.ruoyi.system.model.*;
 import com.ruoyi.system.model.Record;
-import com.ruoyi.system.model.Task;
 import com.ruoyi.system.service.ClientService;
 import com.ruoyi.system.service.ProjectCategoryCalculator;
 import com.ruoyi.system.vo.*;
@@ -55,6 +45,8 @@
     private final DeviceUsageMapper deviceUsageMapper;
     private final RecordMapper recordMapper;
     private final ProjectCategoryCalculator projectCategoryCalculator;
+    private final ClientParamMapper clientParamMapper;
+    private final DeviceMapper deviceMapper;
 
     @Override
     public ClientDashboardVO getClientDashboard(ClientDashboardQuery query) {
@@ -145,6 +137,15 @@
                         .distinct()
                         .toList();
 
+                // 查询配置的项目总数
+                ClientParam clientParam = null;
+                if (clientId != null) {
+                    clientParam = clientParamMapper.selectOne(
+                        new LambdaQueryWrapper<ClientParam>()
+                            .eq(ClientParam::getClientId, clientId.intValue())
+                    );
+                }
+
                 // 房建检查覆盖率
                 List<Integer> housingProjectIds = projects.stream()
                         .filter(p -> p.getType() != null && p.getType() == 0)
@@ -153,8 +154,13 @@
                 long checkedHousingCount = checkedProjectIds.stream()
                         .filter(housingProjectIds::contains)
                         .count();
+
+                // 使用配置的房建项目总数,如果没有配置则使用实际数量
+                int configuredHousingNum = (clientParam != null && clientParam.getHousingProjectNum() != null)
+                        ? clientParam.getHousingProjectNum()
+                        : (int) housingCount;
                 vo.setHousingInspectionCoverage(
-                        housingCount > 0 ? (int) Math.round(checkedHousingCount * 100.0 / housingCount) : 0
+                        configuredHousingNum > 0 ? (int) Math.round(checkedHousingCount * 100.0 / configuredHousingNum) : 0
                 );
 
                 // 市政检查覆盖率
@@ -165,8 +171,13 @@
                 long checkedMunicipalCount = checkedProjectIds.stream()
                         .filter(municipalProjectIds::contains)
                         .count();
+
+                // 使用配置的市政项目总数,如果没有配置则使用实际数量
+                int configuredMunicipalNum = (clientParam != null && clientParam.getMunicipalProjectNum() != null)
+                        ? clientParam.getMunicipalProjectNum()
+                        : (int) municipalCount;
                 vo.setMunicipalInspectionCoverage(
-                        municipalCount > 0 ? (int) Math.round(checkedMunicipalCount * 100.0 / municipalCount) : 0
+                        configuredMunicipalNum > 0 ? (int) Math.round(checkedMunicipalCount * 100.0 / configuredMunicipalNum) : 0
                 );
             } else {
                 vo.setHousingInspectionCoverage(0);
@@ -234,47 +245,7 @@
         // 1. 统计项目总数
         vo.setProjectTotal(projects.size());
 
-        // 计算检查覆盖率
         List<Integer> projectIds = projects.stream().map(Project::getId).collect(Collectors.toList());
-        if (!projectIds.isEmpty()) {
-            // 1. 先查询符合条件的 Task
-            LambdaQueryWrapper<Task> taskWrapper = new LambdaQueryWrapper<>();
-            taskWrapper.in(Task::getProjectId, projectIds)
-                    .eq(Task::getDeleted, false);
-            List<Task> tasks = taskMapper.selectList(taskWrapper);
-
-            if (!tasks.isEmpty()) {
-                // 2. 获取 Task ID 列表
-                List<Integer> taskIds = tasks.stream().map(Task::getId).collect(Collectors.toList());
-
-                // 3. 建立 Task ID 到 Project ID 的映射
-                Map<Integer, Integer> taskToProjectMap = tasks.stream()
-                        .collect(Collectors.toMap(Task::getId, Task::getProjectId));
-
-                // 4. 查询 record 表中 record_type='error' 且 device_id 为 null 的记录(项目检查)
-                LambdaQueryWrapper<Record> recordWrapper = new LambdaQueryWrapper<>();
-                recordWrapper.eq(Record::getRecordType, "error")
-                        .isNull(Record::getDeviceId)
-                        .in(Record::getCheckTaskId, taskIds)
-                        .eq(Record::getDeleted, false);
-                List<Record> projectRecords = recordMapper.selectList(recordWrapper);
-
-                // 5. 通过 Task 映射获取有检查记录的项目 ID
-                long checkedProjectCount = projectRecords.stream()
-                        .map(Record::getCheckTaskId)
-                        .map(taskToProjectMap::get)
-                        .filter(Objects::nonNull)
-                        .distinct()
-                        .count();
-                vo.setInspectionCoverage(
-                        projects.size() > 0 ? (int) Math.round(checkedProjectCount * 100.0 / projects.size()) : 0
-                );
-            } else {
-                vo.setInspectionCoverage(0);
-            }
-        } else {
-            vo.setInspectionCoverage(0);
-        }
 
         // 2. 统计本月检查情况
         calculateMonthlyInspectionForFixed(vo, projects, projectIds, monthStart, monthEnd);
@@ -433,19 +404,54 @@
                 .in(DeviceUsage::getProjectId, projectIds);
         List<DeviceUsage> devices = deviceUsageMapper.selectList(deviceWrapper);
 
-        // 按设备名称分组统计
+        // 批量查询 Device 信息
+        List<Integer> deviceIds = devices.stream()
+                .map(DeviceUsage::getDeviceId)
+                .filter(Objects::nonNull)
+                .distinct()
+                .collect(Collectors.toList());
+
+        Map<Integer, Device> deviceMap = new HashMap<>();
+        if (!deviceIds.isEmpty()) {
+            List<Device> deviceList = deviceMapper.selectBatchIds(deviceIds);
+            deviceMap = deviceList.stream()
+                    .collect(Collectors.toMap(Device::getId, d -> d));
+            log.debug("批量查询到 {} 个设备信息", deviceMap.size());
+
+            // 检查是否有设备信息查询失败
+            if (deviceIds.size() != deviceMap.size()) {
+                log.warn("部分设备信息查询失败,期望 {} 个,实际查询到 {} 个",
+                         deviceIds.size(), deviceMap.size());
+            }
+        }
+
+        // 按设备类型名称分组统计
+        final Map<Integer, Device> finalDeviceMap = deviceMap;
         Map<String, List<DeviceUsage>> deviceGroupMap = devices.stream()
-                .filter(d -> d.getName() != null && !d.getName().isEmpty())
-                .collect(Collectors.groupingBy(DeviceUsage::getName));
+                .filter(d -> d.getDeviceId() != null)
+                .filter(d -> finalDeviceMap.containsKey(d.getDeviceId()))
+                .collect(Collectors.groupingBy(d -> {
+                    try {
+                        Device device = finalDeviceMap.get(d.getDeviceId());
+                        return device.getDeviceType().getDesc();
+                    } catch (Exception e) {
+                        log.error("获取设备类型失败,deviceId: {}", d.getDeviceId(), e);
+                        return DeviceType.OTHER.getDesc();
+                    }
+                }));
+
+        log.debug("设备按类型分组完成,共 {} 种类型", deviceGroupMap.size());
 
         // 查询设备检查记录
-        List<Integer> deviceIds = devices.stream().map(DeviceUsage::getId).collect(Collectors.toList());
+        List<Integer> deviceUsageIds = devices.stream()
+                .map(DeviceUsage::getId)
+                .collect(Collectors.toList());
         Map<Integer, Boolean> deviceInspectionMap = new HashMap<>();
-        if (!deviceIds.isEmpty()) {
+        if (!deviceUsageIds.isEmpty()) {
             LambdaQueryWrapper<Record> recordWrapper = new LambdaQueryWrapper<>();
             recordWrapper.eq(Record::getRecordType, "error")
                     .isNotNull(Record::getDeviceId)
-                    .in(Record::getDeviceId, deviceIds)
+                    .in(Record::getDeviceId, deviceUsageIds)
                     .eq(Record::getDeleted, false);
             List<Record> records = recordMapper.selectList(recordWrapper);
 
@@ -453,8 +459,8 @@
                     .map(Record::getDeviceId)
                     .collect(Collectors.toSet());
 
-            for (Integer deviceId : deviceIds) {
-                deviceInspectionMap.put(deviceId, inspectedDeviceIds.contains(deviceId));
+            for (Integer deviceUsageId : deviceUsageIds) {
+                deviceInspectionMap.put(deviceUsageId, inspectedDeviceIds.contains(deviceUsageId));
             }
         }
 
@@ -465,6 +471,16 @@
             vo.setName(entry.getKey());
             vo.setTotal(entry.getValue().size());
 
+            // 设置设备类型字段
+            // 从当前分组的第一个设备使用记录中获取设备类型
+            if (!entry.getValue().isEmpty()) {
+                DeviceUsage firstUsage = entry.getValue().get(0);
+                Device device = finalDeviceMap.get(firstUsage.getDeviceId());
+                if (device != null && device.getType() != null) {
+                    vo.setType(device.getType());
+                }
+            }
+
             long inspectedCount = entry.getValue().stream()
                     .filter(d -> deviceInspectionMap.getOrDefault(d.getId(), false))
                     .count();

--
Gitblit v1.7.1