“mitao”
2026-04-02 0225ef3c66a2cc57dfc572b85848178bedd22fa3
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ClientServiceImpl.java
@@ -1,6 +1,7 @@
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;
@@ -21,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;
@@ -47,6 +47,7 @@
    private final ProjectCategoryCalculator projectCategoryCalculator;
    private final ClientParamMapper clientParamMapper;
    private final DeviceMapper deviceMapper;
    private final ErrorReviewMapper errorReviewMapper;
    @Override
    public ClientDashboardVO getClientDashboard(ClientDashboardQuery query) {
@@ -88,7 +89,7 @@
                    .eq(Project::getStatus, 1)  // 在建项目
                    .or(w -> w
                        .eq(Project::getStatus, 2)  // 已完成项目
                        .ge(Project::getSuccessTime, monthStart)  // 完成时间 >= 开始日期// 完成时间 <= 结束日期
                        .between(Project::getSuccessTime, monthStart,monthEnd)  // 完成时间 >= 开始日期// 完成时间 <= 结束日期
                    )
                );
        if (clientId != null) {
@@ -97,11 +98,29 @@
        List<Project> projects = projectMapper.selectList(projectWrapper);
        // 1. 统计项目总数及分类
        vo.setProjectTotal(projects.size());
        long housingCount = projects.stream().filter(p -> p.getType() != null && p.getType() == 0).count();
        long municipalCount = projects.stream().filter(p -> p.getType() != null && p.getType() == 1).count();
        vo.setHousingProjectCount((int) housingCount);
        vo.setMunicipalProjectCount((int) municipalCount);
        ClientParam clientParam = null;
        if (clientId != null) {
            clientParam = clientParamMapper.selectOne(
                    new LambdaQueryWrapper<ClientParam>()
                            .eq(ClientParam::getClientId, clientId.intValue())
            );
        }
        int effectiveHousingCount = resolveDisplayProjectCount(
                clientParam != null ? clientParam.getHousingProjectNum() : null,
                housingCount
        );
        int effectiveMunicipalCount = resolveDisplayProjectCount(
                clientParam != null ? clientParam.getMunicipalProjectNum() : null,
                municipalCount
        );
        vo.setProjectTotal(effectiveHousingCount + effectiveMunicipalCount);
        vo.setHousingProjectCount(effectiveHousingCount);
        vo.setMunicipalProjectCount(effectiveMunicipalCount);
        // 计算检查覆盖率(仅当查询时间范围为当月时才计算)
        List<Integer> projectIds = projects.stream().map(Project::getId).collect(Collectors.toList());
@@ -138,15 +157,6 @@
                        .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)
@@ -156,12 +166,8 @@
                        .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
                        effectiveHousingCount > 0 ? (int) Math.round(checkedHousingCount * 100.0 / effectiveHousingCount) : 0
                );
                // 市政检查覆盖率
@@ -173,12 +179,8 @@
                        .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
                        effectiveMunicipalCount > 0 ? (int) Math.round(checkedMunicipalCount * 100.0 / effectiveMunicipalCount) : 0
                );
            } else {
                vo.setHousingInspectionCoverage(0);
@@ -193,7 +195,7 @@
        calculateMonthlyInspectionForOngoing(vo, projects, projectIds, monthStart, monthEnd);
        // 3. 统计大型机械设备数
        List<MachineryStatisticsVO> machineryStatistics = calculateMachineryStatistics(projectIds);
        List<MachineryStatisticsVO> machineryStatistics = calculateMachineryStatistics(projectIds, monthStart, monthEnd);
        vo.setMachineryStatistics(machineryStatistics);
        // 汇总计算设备总数统计
@@ -229,13 +231,13 @@
        // 查询固定资产项目
        LambdaQueryWrapper<Project> projectWrapper = new LambdaQueryWrapper<>();
        projectWrapper.eq(Project::getDeleted, false)
                .eq(Project::getType, 2) // 固定资产
                .eq(Project::getType, 2) // 在建工程
                .and(wrapper -> wrapper
                    .eq(Project::getStatus, 1)  // 在建项目
                    .or(w -> w
                        .eq(Project::getStatus, 2)  // 已完成项目
                        .ge(Project::getSuccessTime, monthStart)  // 完成时间 >= 开始日期
                    )
                        .eq(Project::getStatus, 1)  // 在建项目
                        .or(w -> w
                                .eq(Project::getStatus, 2)  // 已完成项目
                                .between(Project::getSuccessTime, monthStart,monthEnd)  // 完成时间 >= 开始日期// 完成时间 <= 结束日期
                        )
                );
        if (clientId != null) {
            projectWrapper.eq(Project::getClientId, clientId);
@@ -268,9 +270,7 @@
            vo.setInspectedCount(0);
            vo.setInspectedHousingCount(0);
            vo.setInspectedMunicipalCount(0);
            vo.setUninspectedCount(projects.size());
            vo.setUninspectedHousingCount(vo.getHousingProjectCount());
            vo.setUninspectedMunicipalCount(vo.getMunicipalProjectCount());
            fillUninspectedProjectCounts(vo);
            return;
        }
@@ -286,9 +286,7 @@
            vo.setInspectedCount(0);
            vo.setInspectedHousingCount(0);
            vo.setInspectedMunicipalCount(0);
            vo.setUninspectedCount(projects.size());
            vo.setUninspectedHousingCount(vo.getHousingProjectCount());
            vo.setUninspectedMunicipalCount(vo.getMunicipalProjectCount());
            fillUninspectedProjectCounts(vo);
            return;
        }
@@ -326,16 +324,7 @@
        vo.setInspectedMunicipalCount((int) inspectedProjects.stream()
                .filter(p -> p.getType() != null && p.getType() == 1).count());
        // 未检查项目统计
        List<Project> uninspectedProjects = projects.stream()
                .filter(p -> !inspectedProjectIds.contains(p.getId()))
                .toList();
        vo.setUninspectedCount(uninspectedProjects.size());
        vo.setUninspectedHousingCount((int) uninspectedProjects.stream()
                .filter(p -> p.getType() != null && p.getType() == 0).count());
        vo.setUninspectedMunicipalCount((int) uninspectedProjects.stream()
                .filter(p -> p.getType() != null && p.getType() == 1).count());
        fillUninspectedProjectCounts(vo);
    }
    /**
@@ -393,7 +382,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<>();
        }
@@ -402,10 +391,10 @@
        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);
        // 批量查询 Device 信息
        List<Integer> deviceIds = devices.stream()
        List<Integer> deviceIds = deviceUsages.stream()
                .map(DeviceUsage::getDeviceId)
                .filter(Objects::nonNull)
                .distinct()
@@ -427,7 +416,7 @@
        // 按设备类型名称分组统计
        final Map<Integer, Device> finalDeviceMap = deviceMap;
        Map<String, List<DeviceUsage>> deviceGroupMap = devices.stream()
        Map<String, List<DeviceUsage>> deviceGroupMap = deviceUsages.stream()
                .filter(d -> d.getDeviceId() != null)
                .filter(d -> finalDeviceMap.containsKey(d.getDeviceId()))
                .collect(Collectors.groupingBy(d -> {
@@ -444,7 +433,7 @@
        // 查询设备检查记录
        // 建立 device.id 到 device_usage.id 的映射
        Map<Integer, List<Integer>> deviceToUsageMap = devices.stream()
        Map<Integer, List<Integer>> deviceToUsageMap = deviceUsages.stream()
                .filter(d -> d.getDeviceId() != null)
                .collect(Collectors.groupingBy(
                        DeviceUsage::getDeviceId,
@@ -457,7 +446,8 @@
            recordWrapper.eq(Record::getRecordType, "error")
                    .isNotNull(Record::getDeviceId)
                    .in(Record::getDeviceId, deviceIds)
                    .eq(Record::getDeleted, false);
                    .eq(Record::getDeleted, false)
                    .between(Record::getCreateTime, monthStart, monthEnd);
            List<Record> records = recordMapper.selectList(recordWrapper);
            // 获取已检查的 device.id 集合
@@ -541,12 +531,25 @@
        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);
@@ -587,12 +590,25 @@
        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);
@@ -636,6 +652,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();
@@ -646,7 +663,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()));
@@ -666,11 +700,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);
    }
    /**
@@ -678,11 +713,27 @@
     */
    private Date getEndDate(Date endDate) {
        if (endDate != null) {
            return endDate;
            return DateUtil.endOfDay(endDate);
        }
        // 默认为当前月份最后一天
        YearMonth currentMonth = YearMonth.now();
        return Date.from(currentMonth.atEndOfMonth().atTime(23, 59, 59).atZone(ZoneId.systemDefault()).toInstant());
        Date date = new Date();
        return DateUtil.endOfMonth(date);
    }
    private int resolveDisplayProjectCount(Integer configuredCount, long actualCount) {
        int actualProjectCount = (int) actualCount;
        if (configuredCount == null) {
            return actualProjectCount;
        }
        return Math.max(configuredCount, actualProjectCount);
    }
    private void fillUninspectedProjectCounts(OngoingProjectDashboardVO vo) {
        int uninspectedHousingCount = Math.max(vo.getHousingProjectCount() - vo.getInspectedHousingCount(), 0);
        int uninspectedMunicipalCount = Math.max(vo.getMunicipalProjectCount() - vo.getInspectedMunicipalCount(), 0);
        vo.setUninspectedHousingCount(uninspectedHousingCount);
        vo.setUninspectedMunicipalCount(uninspectedMunicipalCount);
        vo.setUninspectedCount(uninspectedHousingCount + uninspectedMunicipalCount);
    }
    /**