liujie
2026-01-29 fc8c8356f5e95cb8317069c9986120b1ecb88851
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;
@@ -86,10 +86,10 @@
                .ne(Project::getType, 2) // 在建工程
                .and(wrapper -> wrapper
                    .eq(Project::getStatus, 1)  // 在建项目
                        .between(Project::getCreateTime, monthStart, monthEnd)
                    .or(w -> w
                        .eq(Project::getStatus, 2)  // 已完成项目
                        .ge(Project::getSuccessTime, monthStart)  // 完成时间 >= 开始日期
                        .le(Project::getSuccessTime, monthEnd)    // 完成时间 <= 结束日期
                        .between(Project::getSuccessTime, monthStart,monthEnd)  // 完成时间 >= 开始日期// 完成时间 <= 结束日期
                    )
                );
        if (clientId != null) {
@@ -110,6 +110,8 @@
            // 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);
@@ -192,7 +194,7 @@
        calculateMonthlyInspectionForOngoing(vo, projects, projectIds, monthStart, monthEnd);
        // 3. 统计大型机械设备数
        List<MachineryStatisticsVO> machineryStatistics = calculateMachineryStatistics(projectIds);
        List<MachineryStatisticsVO> machineryStatistics = calculateMachineryStatistics(projectIds, monthStart, monthEnd);
        vo.setMachineryStatistics(machineryStatistics);
        // 汇总计算设备总数统计
@@ -225,17 +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)  // 已完成项目
                        .ge(Project::getSuccessTime, monthStart)  // 完成时间 >= 开始日期
                        .le(Project::getSuccessTime, monthEnd)    // 完成时间 <= 结束日期
                    )
                        .eq(Project::getStatus, 1)  // 在建项目
                        .between(Project::getCreateTime, monthStart, monthEnd)
                        .or(w -> w
                                .eq(Project::getStatus, 2)  // 已完成项目
                                .between(Project::getSuccessTime, monthStart,monthEnd)  // 完成时间 >= 开始日期// 完成时间 <= 结束日期
                        )
                );
        if (clientId != null) {
            projectWrapper.eq(Project::getClientId, clientId);
@@ -393,7 +395,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 +404,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 +429,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 -> {
@@ -443,24 +445,38 @@
        log.debug("设备按类型分组完成,共 {} 种类型", deviceGroupMap.size());
        // 查询设备检查记录
        List<Integer> deviceUsageIds = 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 (!deviceUsageIds.isEmpty()) {
        if (!deviceIds.isEmpty()) {
            LambdaQueryWrapper<Record> recordWrapper = new LambdaQueryWrapper<>();
            recordWrapper.eq(Record::getRecordType, "error")
                    .isNotNull(Record::getDeviceId)
                    .in(Record::getDeviceId, deviceUsageIds)
                    .eq(Record::getDeleted, false);
                    .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 deviceUsageId : deviceUsageIds) {
                deviceInspectionMap.put(deviceUsageId, inspectedDeviceIds.contains(deviceUsageId));
            // 通过映射关系,将 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);
                }
            }
        }
@@ -470,6 +486,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))
@@ -643,11 +669,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);
    }
    /**
@@ -655,11 +682,11 @@
     */
    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);
    }
    /**