liujie
2026-04-01 2f74a0e6c101edc69de6d5e19e7c071e09a9c3ea
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ClientServiceImpl.java
@@ -1,22 +1,15 @@
package com.ruoyi.system.service.impl;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
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.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.service.ClientService;
import com.ruoyi.system.service.ProjectCategoryCalculator;
@@ -29,7 +22,6 @@
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
@@ -49,9 +41,13 @@
    private final ProjectMapper projectMapper;
    private final PlanMapper planMapper;
    private final CheckMapper checkMapper;
    private final TaskMapper taskMapper;
    private final DeviceUsageMapper deviceUsageMapper;
    private final RecordMapper recordMapper;
    private final ProjectCategoryCalculator projectCategoryCalculator;
    private final ClientParamMapper clientParamMapper;
    private final DeviceMapper deviceMapper;
    private final ErrorReviewMapper errorReviewMapper;
    @Override
    public ClientDashboardVO getClientDashboard(ClientDashboardQuery query) {
@@ -88,7 +84,14 @@
        // 查询在建工程项目(usage_type 表示项目用途类型,假设 0 表示在建工程)
        LambdaQueryWrapper<Project> projectWrapper = new LambdaQueryWrapper<>();
        projectWrapper.eq(Project::getDeleted, false)
                .eq(Project::getUsageType, 0); // 在建工程
                .ne(Project::getType, 2) // 在建工程
                .and(wrapper -> wrapper
                    .eq(Project::getStatus, 1)  // 在建项目
                    .or(w -> w
                        .eq(Project::getStatus, 2)  // 已完成项目
                        .between(Project::getSuccessTime, monthStart,monthEnd)  // 完成时间 >= 开始日期// 完成时间 <= 结束日期
                    )
                );
        if (clientId != null) {
            projectWrapper.eq(Project::getClientId, clientId);
        }
@@ -101,41 +104,87 @@
        vo.setHousingProjectCount((int) housingCount);
        vo.setMunicipalProjectCount((int) municipalCount);
        // 计算检查覆盖率
        // 计算检查覆盖率(仅当查询时间范围为当月时才计算)
        List<Integer> projectIds = projects.stream().map(Project::getId).collect(Collectors.toList());
        if (!projectIds.isEmpty()) {
            LambdaQueryWrapper<Check> checkWrapper = new LambdaQueryWrapper<>();
            checkWrapper.in(Check::getProjectId, projectIds);
            List<Check> checks = checkMapper.selectList(checkWrapper);
        if (!projectIds.isEmpty() && isCurrentMonth(monthStart, monthEnd)) {
            // 1. 先查询符合条件的 Task
            LambdaQueryWrapper<Task> taskWrapper = new LambdaQueryWrapper<>();
            taskWrapper.in(Task::getProjectId, projectIds)
                    .ge(Task::getDate, monthStart.toInstant().atZone(ZoneId.systemDefault()).toLocalDate())
                    .le(Task::getDate, monthEnd.toInstant().atZone(ZoneId.systemDefault()).toLocalDate())
                    .eq(Task::getDeleted, false);
            List<Task> tasks = taskMapper.selectList(taskWrapper);
            List<Integer> checkedProjectIds = checks.stream()
                    .map(Check::getProjectId)
                    .distinct()
                    .collect(Collectors.toList());
            if (!tasks.isEmpty()) {
                // 2. 获取 Task ID 列表
                List<Integer> taskIds = tasks.stream().map(Task::getId).collect(Collectors.toList());
            // 房建检查覆盖率
            List<Integer> housingProjectIds = projects.stream()
                    .filter(p -> p.getType() != null && p.getType() == 0)
                    .map(Project::getId)
                    .toList();
            long checkedHousingCount = checkedProjectIds.stream()
                    .filter(housingProjectIds::contains)
                    .count();
            vo.setHousingInspectionCoverage(
                    housingCount > 0 ? (int) Math.round(checkedHousingCount * 100.0 / housingCount) : 0
            );
                // 3. 建立 Task ID 到 Project ID 的映射
                Map<Integer, Integer> taskToProjectMap = tasks.stream()
                        .collect(Collectors.toMap(Task::getId, Task::getProjectId));
            // 市政检查覆盖率
            List<Integer> municipalProjectIds = projects.stream()
                    .filter(p -> p.getType() != null && p.getType() == 1)
                    .map(Project::getId)
                    .toList();
            long checkedMunicipalCount = checkedProjectIds.stream()
                    .filter(municipalProjectIds::contains)
                    .count();
            vo.setMunicipalInspectionCoverage(
                    municipalCount > 0 ? (int) Math.round(checkedMunicipalCount * 100.0 / municipalCount) : 0
            );
                // 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
                List<Integer> checkedProjectIds = projectRecords.stream()
                        .map(Record::getCheckTaskId)
                        .map(taskToProjectMap::get)
                        .filter(Objects::nonNull)
                        .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)
                        .map(Project::getId)
                        .toList();
                long checkedHousingCount = checkedProjectIds.stream()
                        .filter(housingProjectIds::contains)
                        .count();
                // 使用配置的房建项目总数,如果没有配置则使用实际数量
                int configuredHousingNum = (clientParam != null && clientParam.getHousingProjectNum() != null)
                        ? clientParam.getHousingProjectNum()
                        : (int) housingCount;
                vo.setHousingInspectionCoverage(
                        configuredHousingNum > 0 ? (int) Math.round(checkedHousingCount * 100.0 / configuredHousingNum) : 0
                );
                // 市政检查覆盖率
                List<Integer> municipalProjectIds = projects.stream()
                        .filter(p -> p.getType() != null && p.getType() == 1)
                        .map(Project::getId)
                        .toList();
                long checkedMunicipalCount = checkedProjectIds.stream()
                        .filter(municipalProjectIds::contains)
                        .count();
                // 使用配置的市政项目总数,如果没有配置则使用实际数量
                int configuredMunicipalNum = (clientParam != null && clientParam.getMunicipalProjectNum() != null)
                        ? clientParam.getMunicipalProjectNum()
                        : (int) municipalCount;
                vo.setMunicipalInspectionCoverage(
                        configuredMunicipalNum > 0 ? (int) Math.round(checkedMunicipalCount * 100.0 / configuredMunicipalNum) : 0
                );
            } else {
                vo.setHousingInspectionCoverage(0);
                vo.setMunicipalInspectionCoverage(0);
            }
        } else {
            vo.setHousingInspectionCoverage(0);
            vo.setMunicipalInspectionCoverage(0);
@@ -145,7 +194,23 @@
        calculateMonthlyInspectionForOngoing(vo, projects, projectIds, monthStart, monthEnd);
        // 3. 统计大型机械设备数
        vo.setMachineryStatistics(calculateMachineryStatistics(projectIds));
        List<MachineryStatisticsVO> machineryStatistics = calculateMachineryStatistics(projectIds, monthStart, monthEnd);
        vo.setMachineryStatistics(machineryStatistics);
        // 汇总计算设备总数统计
        int machineryTotal = machineryStatistics.stream()
                .mapToInt(MachineryStatisticsVO::getTotal)
                .sum();
        int inspectedMachineryTotal = machineryStatistics.stream()
                .mapToInt(MachineryStatisticsVO::getInspectedCount)
                .sum();
        int uninspectedMachineryTotal = machineryStatistics.stream()
                .mapToInt(MachineryStatisticsVO::getUninspectedCount)
                .sum();
        vo.setMachineryTotal(machineryTotal);
        vo.setInspectedMachineryTotal(inspectedMachineryTotal);
        vo.setUninspectedMachineryTotal(uninspectedMachineryTotal);
        // 4. 统计重大隐患数
        calculateHazardStatistics(vo, projectIds);
@@ -162,10 +227,17 @@
    private FixedAssetDashboardVO calculateFixedAssetStatistics(Long clientId, Date monthStart, Date monthEnd) {
        FixedAssetDashboardVO vo = new FixedAssetDashboardVO();
        // 查询固定资产项目(usage_type 表示项目用途类型,假设 1 表示固定资产)
        // 查询固定资产项目
        LambdaQueryWrapper<Project> projectWrapper = new LambdaQueryWrapper<>();
        projectWrapper.eq(Project::getDeleted, false)
                .eq(Project::getUsageType, 1); // 固定资产
                .eq(Project::getType, 2) // 在建工程
                .and(wrapper -> wrapper
                        .eq(Project::getStatus, 1)  // 在建项目
                        .or(w -> w
                                .eq(Project::getStatus, 2)  // 已完成项目
                                .between(Project::getSuccessTime, monthStart,monthEnd)  // 完成时间 >= 开始日期// 完成时间 <= 结束日期
                        )
                );
        if (clientId != null) {
            projectWrapper.eq(Project::getClientId, clientId);
        }
@@ -174,23 +246,7 @@
        // 1. 统计项目总数
        vo.setProjectTotal(projects.size());
        // 计算检查覆盖率
        List<Integer> projectIds = projects.stream().map(Project::getId).collect(Collectors.toList());
        if (!projectIds.isEmpty()) {
            LambdaQueryWrapper<Check> checkWrapper = new LambdaQueryWrapper<>();
            checkWrapper.in(Check::getProjectId, projectIds);
            List<Check> checks = checkMapper.selectList(checkWrapper);
            long checkedProjectCount = checks.stream()
                    .map(Check::getProjectId)
                    .distinct()
                    .count();
            vo.setInspectionCoverage(
                    projects.size() > 0 ? (int) Math.round(checkedProjectCount * 100.0 / projects.size()) : 0
            );
        } else {
            vo.setInspectionCoverage(0);
        }
        // 2. 统计本月检查情况
        calculateMonthlyInspectionForFixed(vo, projects, projectIds, monthStart, monthEnd);
@@ -219,15 +275,44 @@
            return;
        }
        // 查询本月有检查记录的项目
        LambdaQueryWrapper<Check> checkWrapper = new LambdaQueryWrapper<>();
        checkWrapper.in(Check::getProjectId, projectIds)
                .ge(Check::getTime, monthStart)
                .le(Check::getTime, monthEnd);
        List<Check> monthlyChecks = checkMapper.selectList(checkWrapper);
        // 1. 先查询符合条件的 Task(本月的任务)
        LambdaQueryWrapper<Task> taskWrapper = new LambdaQueryWrapper<>();
        taskWrapper.in(Task::getProjectId, projectIds)
                .ge(Task::getDate, monthStart.toInstant().atZone(ZoneId.systemDefault()).toLocalDate())
                .le(Task::getDate, monthEnd.toInstant().atZone(ZoneId.systemDefault()).toLocalDate())
                .eq(Task::getDeleted, false);
        List<Task> monthlyTasks = taskMapper.selectList(taskWrapper);
        List<Integer> inspectedProjectIds = monthlyChecks.stream()
                .map(Check::getProjectId)
        if (monthlyTasks.isEmpty()) {
            vo.setInspectedCount(0);
            vo.setInspectedHousingCount(0);
            vo.setInspectedMunicipalCount(0);
            vo.setUninspectedCount(projects.size());
            vo.setUninspectedHousingCount(vo.getHousingProjectCount());
            vo.setUninspectedMunicipalCount(vo.getMunicipalProjectCount());
            return;
        }
        // 2. 获取 Task ID 列表
        List<Integer> taskIds = monthlyTasks.stream().map(Task::getId).collect(Collectors.toList());
        // 3. 建立 Task ID 到 Project ID 的映射
        Map<Integer, Integer> taskToProjectMap = monthlyTasks.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> monthlyRecords = recordMapper.selectList(recordWrapper);
        // 5. 通过 Task 映射获取有检查记录的项目 ID
        List<Integer> inspectedProjectIds = monthlyRecords.stream()
                .map(Record::getCheckTaskId)
                .map(taskToProjectMap::get)
                .filter(Objects::nonNull)
                .distinct()
                .toList();
@@ -265,15 +350,40 @@
            return;
        }
        // 查询本月有检查记录的项目
        LambdaQueryWrapper<Check> checkWrapper = new LambdaQueryWrapper<>();
        checkWrapper.in(Check::getProjectId, projectIds)
                .ge(Check::getTime, monthStart)
                .le(Check::getTime, monthEnd);
        List<Check> monthlyChecks = checkMapper.selectList(checkWrapper);
        // 1. 先查询符合条件的 Task(本月的任务)
        LambdaQueryWrapper<Task> taskWrapper = new LambdaQueryWrapper<>();
        taskWrapper.in(Task::getProjectId, projectIds)
                .ge(Task::getDate, monthStart.toInstant().atZone(ZoneId.systemDefault()).toLocalDate())
                .le(Task::getDate, monthEnd.toInstant().atZone(ZoneId.systemDefault()).toLocalDate())
                .eq(Task::getDeleted, false);
        List<Task> monthlyTasks = taskMapper.selectList(taskWrapper);
        long inspectedCount = monthlyChecks.stream()
                .map(Check::getProjectId)
        if (monthlyTasks.isEmpty()) {
            vo.setInspectedCount(0);
            vo.setUninspectedCount(projects.size());
            return;
        }
        // 2. 获取 Task ID 列表
        List<Integer> taskIds = monthlyTasks.stream().map(Task::getId).collect(Collectors.toList());
        // 3. 建立 Task ID 到 Project ID 的映射
        Map<Integer, Integer> taskToProjectMap = monthlyTasks.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> monthlyRecords = recordMapper.selectList(recordWrapper);
        // 5. 通过 Task 映射获取有检查记录的项目 ID
        long inspectedCount = monthlyRecords.stream()
                .map(Record::getCheckTaskId)
                .map(taskToProjectMap::get)
                .filter(Objects::nonNull)
                .distinct()
                .count();
@@ -284,7 +394,7 @@
    /**
     * 统计大型机械设备数(含已检查/未检查)
     */
    private List<MachineryStatisticsVO> calculateMachineryStatistics(List<Integer> projectIds) {
    private List<MachineryStatisticsVO> calculateMachineryStatistics(List<Integer> projectIds, Date monthStart, Date monthEnd) {
        if (projectIds.isEmpty()) {
            return new ArrayList<>();
        }
@@ -293,28 +403,79 @@
        LambdaQueryWrapper<DeviceUsage> deviceWrapper = new LambdaQueryWrapper<>();
        deviceWrapper.eq(DeviceUsage::getDeleted, false)
                .in(DeviceUsage::getProjectId, projectIds);
        List<DeviceUsage> devices = deviceUsageMapper.selectList(deviceWrapper);
        List<DeviceUsage> deviceUsages = deviceUsageMapper.selectList(deviceWrapper);
        // 按设备名称分组统计
        Map<String, List<DeviceUsage>> deviceGroupMap = devices.stream()
                .filter(d -> d.getName() != null && !d.getName().isEmpty())
                .collect(Collectors.groupingBy(DeviceUsage::getName));
        // 批量查询 Device 信息
        List<Integer> deviceIds = deviceUsages.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 = deviceUsages.stream()
                .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());
        // 建立 device.id 到 device_usage.id 的映射
        Map<Integer, List<Integer>> deviceToUsageMap = deviceUsages.stream()
                .filter(d -> d.getDeviceId() != null)
                .collect(Collectors.groupingBy(
                        DeviceUsage::getDeviceId,
                        Collectors.mapping(DeviceUsage::getId, Collectors.toList())
                ));
        Map<Integer, Boolean> deviceInspectionMap = new HashMap<>();
        if (!deviceIds.isEmpty()) {
            LambdaQueryWrapper<Record> recordWrapper = new LambdaQueryWrapper<>();
            recordWrapper.eq(Record::getDeleted, false)
                    .in(Record::getDeviceId, deviceIds);
            recordWrapper.eq(Record::getRecordType, "error")
                    .isNotNull(Record::getDeviceId)
                    .in(Record::getDeviceId, deviceIds)
                    .eq(Record::getDeleted, false)
                    .between(Record::getCreateTime, monthStart, monthEnd);
            List<Record> records = recordMapper.selectList(recordWrapper);
            // 获取已检查的 device.id 集合
            Set<Integer> inspectedDeviceIds = records.stream()
                    .map(Record::getDeviceId)
                    .collect(Collectors.toSet());
            for (Integer deviceId : deviceIds) {
                deviceInspectionMap.put(deviceId, inspectedDeviceIds.contains(deviceId));
            // 通过映射关系,将 device.id 的检查状态映射到 device_usage.id
            for (Map.Entry<Integer, List<Integer>> entry : deviceToUsageMap.entrySet()) {
                Integer deviceId = entry.getKey();
                boolean isInspected = inspectedDeviceIds.contains(deviceId);
                // 将该设备的检查状态应用到所有使用该设备的 device_usage 记录
                for (Integer usageId : entry.getValue()) {
                    deviceInspectionMap.put(usageId, isInspected);
                }
            }
        }
@@ -324,6 +485,16 @@
            MachineryStatisticsVO vo = new MachineryStatisticsVO();
            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))
@@ -349,10 +520,11 @@
        }
        // 查询检查任务
        LambdaQueryWrapper<Check> checkWrapper = new LambdaQueryWrapper<>();
        checkWrapper.in(Check::getProjectId, projectIds);
        List<Check> checks = checkMapper.selectList(checkWrapper);
        List<Integer> checkTaskIds = checks.stream().map(Check::getId).collect(Collectors.toList());
        LambdaQueryWrapper<Task> taskWrapper = new LambdaQueryWrapper<>();
        taskWrapper.in(Task::getProjectId, projectIds)
                .eq(Task::getDeleted, false);
        List<Task> tasks = taskMapper.selectList(taskWrapper);
        List<Integer> checkTaskIds = tasks.stream().map(Task::getId).collect(Collectors.toList());
        if (checkTaskIds.isEmpty()) {
            vo.setMajorHazardTotal(0);
@@ -361,22 +533,35 @@
            return;
        }
        // 查询重大隐患记录(level 表示隐患等级,假设 3 表示重大隐患)
        // 查询重大隐患记录(level 表示隐患等级,0 表示重大隐患)
        LambdaQueryWrapper<Record> recordWrapper = new LambdaQueryWrapper<>();
        recordWrapper.eq(Record::getDeleted, false)
                .in(Record::getCheckTaskId, checkTaskIds)
                .eq(Record::getLevel, 3); // 重大隐患等级
                .eq(Record::getLevel, 0); // 重大隐患等级
        List<Record> majorHazards = recordMapper.selectList(recordWrapper);
        vo.setMajorHazardTotal(majorHazards.size());
        // 统计已整改和未整改数量
        long rectifiedCount = majorHazards.stream()
                .filter(r -> r.getReformed() != null && r.getReformed())
                .count();
        long unrectifiedCount = majorHazards.stream()
                .filter(r -> r.getReformed() == null || !r.getReformed())
                .count();
        // 1. 收集所有非空的 errorReviewId
        List<Integer> errorReviewIds = majorHazards.stream()
                .map(Record::getErrorReviewId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        // 2. 查询 ErrorReview 中 state=2 的记录
        long rectifiedCount = 0;
        if (!errorReviewIds.isEmpty()) {
            LambdaQueryWrapper<ErrorReview> reviewWrapper = new LambdaQueryWrapper<>();
            reviewWrapper.in(ErrorReview::getId, errorReviewIds)
                    .eq(ErrorReview::getState, 2)  // state=2 表示已整改
                    .eq(ErrorReview::getDeleted, false);
            rectifiedCount = errorReviewMapper.selectCount(reviewWrapper);
        }
        // 3. 未整改数量 = 总数 - 已整改数量
        long unrectifiedCount = majorHazards.size() - rectifiedCount;
        vo.setRectifiedHazardCount((int) rectifiedCount);
        vo.setUnrectifiedHazardCount((int) unrectifiedCount);
@@ -394,10 +579,11 @@
        }
        // 查询检查任务
        LambdaQueryWrapper<Check> checkWrapper = new LambdaQueryWrapper<>();
        checkWrapper.in(Check::getProjectId, projectIds);
        List<Check> checks = checkMapper.selectList(checkWrapper);
        List<Integer> checkTaskIds = checks.stream().map(Check::getId).collect(Collectors.toList());
        LambdaQueryWrapper<Task> taskWrapper = new LambdaQueryWrapper<>();
        taskWrapper.in(Task::getProjectId, projectIds)
                .eq(Task::getDeleted, false);
        List<Task> tasks = taskMapper.selectList(taskWrapper);
        List<Integer> checkTaskIds = tasks.stream().map(Task::getId).collect(Collectors.toList());
        if (checkTaskIds.isEmpty()) {
            vo.setMajorHazardTotal(0);
@@ -406,22 +592,35 @@
            return;
        }
        // 查询重大隐患记录(level 表示隐患等级,假设 3 表示重大隐患)
        // 查询重大隐患记录(level 表示隐患等级,0 表示重大隐患)
        LambdaQueryWrapper<Record> recordWrapper = new LambdaQueryWrapper<>();
        recordWrapper.eq(Record::getDeleted, false)
                .in(Record::getCheckTaskId, checkTaskIds)
                .eq(Record::getLevel, 3); // 重大隐患等级
                .eq(Record::getLevel, 0); // 重大隐患等级
        List<Record> majorHazards = recordMapper.selectList(recordWrapper);
        vo.setMajorHazardTotal(majorHazards.size());
        // 统计已整改和未整改数量
        long rectifiedCount = majorHazards.stream()
                .filter(r -> r.getReformed() != null && r.getReformed())
                .count();
        long unrectifiedCount = majorHazards.stream()
                .filter(r -> r.getReformed() == null || !r.getReformed())
                .count();
        // 1. 收集所有非空的 errorReviewId
        List<Integer> errorReviewIds = majorHazards.stream()
                .map(Record::getErrorReviewId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        // 2. 查询 ErrorReview 中 state=2 的记录
        long rectifiedCount = 0;
        if (!errorReviewIds.isEmpty()) {
            LambdaQueryWrapper<ErrorReview> reviewWrapper = new LambdaQueryWrapper<>();
            reviewWrapper.in(ErrorReview::getId, errorReviewIds)
                    .eq(ErrorReview::getState, 2)  // state=2 表示已整改
                    .eq(ErrorReview::getDeleted, false);
            rectifiedCount = errorReviewMapper.selectCount(reviewWrapper);
        }
        // 3. 未整改数量 = 总数 - 已整改数量
        long unrectifiedCount = majorHazards.size() - rectifiedCount;
        vo.setRectifiedHazardCount((int) rectifiedCount);
        vo.setUnrectifiedHazardCount((int) unrectifiedCount);
@@ -439,12 +638,12 @@
        Map<Integer, String> categoryMap;
        if (!projects.isEmpty()) {
            // 判断项目类型:在建工程 or 固定资产
            Integer usageType = projects.get(0).getUsageType();
            if (usageType != null && usageType == 0) {
            Integer usageType = projects.get(0).getType();
            if (usageType != null && usageType != 2) {
                // 在建工程
                categoryMap = projectCategoryCalculator.calculateOngoingProjectCategories(
                        projects, clientId, startDate, endDate);
            } else if (usageType != null && usageType == 1) {
            } else if (usageType != null && usageType == 2) {
                // 固定资产
                categoryMap = projectCategoryCalculator.calculateFixedAssetCategories(
                        projects, clientId, startDate, endDate);
@@ -465,6 +664,7 @@
                // 解析 address_info JSON 字符串
                JSONObject addressJson = JSON.parseObject(p.getAddressInfo());
                JSONArray gps = addressJson.getJSONArray("gps");
                String lonLat = addressJson.getString("lonLat");
                if (gps != null && gps.size() >= 2) {
                    ProjectMapPointVO vo = new ProjectMapPointVO();
@@ -475,7 +675,24 @@
                    // gps 数组格式:[经度, 纬度]
                    vo.setLongitude(gps.getString(0));
                    vo.setLatitude(gps.getString(1));
                    //位置信息
                    vo.setAddressInfo(addressJson);
                    // 设置项目类别
                    vo.setCategory(categoryMap.get(p.getId()));
                    result.add(vo);
                }else if(org.apache.commons.lang3.StringUtils.isNotBlank(lonLat)){
                    String[] split = lonLat.split(",");
                    ProjectMapPointVO vo = new ProjectMapPointVO();
                    vo.setProjectId(p.getId());
                    vo.setProjectName(p.getName());
                    vo.setProjectType(p.getType());
                    // gps 数组格式:[经度, 纬度]
                    vo.setLongitude(split[0]);
                    vo.setLatitude(split[1]);
                    //位置信息
                    vo.setAddressInfo(addressJson);
                    // 设置项目类别
                    vo.setCategory(categoryMap.get(p.getId()));
@@ -495,11 +712,12 @@
     */
    private Date getStartDate(Date startDate) {
        if (startDate != null) {
            return startDate;
            return DateUtil.beginOfDay(startDate);
        }
        // 默认为当前月份第一天
        YearMonth currentMonth = YearMonth.now();
        return Date.from(currentMonth.atDay(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
        Date date = new Date();
        return DateUtil.beginOfMonth(date);
    }
    /**
@@ -507,10 +725,33 @@
     */
    private Date getEndDate(Date endDate) {
        if (endDate != null) {
            return endDate;
            return DateUtil.endOfDay(endDate);
        }
        // 默认为当前月份最后一天
        Date date = new Date();
        return DateUtil.endOfMonth(date);
    }
    /**
     * 判断给定的日期范围是否为当前月份
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return 如果日期范围在当前月份内则返回 true,否则返回 false
     */
    private boolean isCurrentMonth(Date startDate, Date endDate) {
        if (startDate == null || endDate == null) {
            return false;
        }
        YearMonth currentMonth = YearMonth.now();
        return Date.from(currentMonth.atEndOfMonth().atTime(23, 59, 59).atZone(ZoneId.systemDefault()).toInstant());
        LocalDate start = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate end = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        YearMonth startMonth = YearMonth.from(start);
        YearMonth endMonth = YearMonth.from(end);
        // 判断开始和结束日期是否都在当前月份
        return startMonth.equals(currentMonth) && endMonth.equals(currentMonth);
    }
}