| | |
| | | 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 java.time.LocalDate; |
| | | import java.time.YearMonth; |
| | | import java.time.ZoneId; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | private final ProjectCategoryCalculator projectCategoryCalculator; |
| | | private final ClientParamMapper clientParamMapper; |
| | | private final DeviceMapper deviceMapper; |
| | | private final ErrorReviewMapper errorReviewMapper; |
| | | |
| | | @Override |
| | | public ClientDashboardVO getClientDashboard(ClientDashboardQuery query) { |
| | |
| | | .eq(Project::getStatus, 1) // 在建项目 |
| | | .or(w -> w |
| | | .eq(Project::getStatus, 2) // 已完成项目 |
| | | .ge(Project::getSuccessTime, monthStart) // 完成时间 >= 开始日期// 完成时间 <= 结束日期 |
| | | .between(Project::getSuccessTime, monthStart,monthEnd) // 完成时间 >= 开始日期// 完成时间 <= 结束日期 |
| | | ) |
| | | ); |
| | | if (clientId != null) { |
| | |
| | | // 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); |
| | | |
| | |
| | | calculateMonthlyInspectionForOngoing(vo, projects, projectIds, monthStart, monthEnd); |
| | | |
| | | // 3. 统计大型机械设备数 |
| | | List<MachineryStatisticsVO> machineryStatistics = calculateMachineryStatistics(projectIds); |
| | | List<MachineryStatisticsVO> machineryStatistics = calculateMachineryStatistics(projectIds, monthStart, monthEnd); |
| | | vo.setMachineryStatistics(machineryStatistics); |
| | | |
| | | // 汇总计算设备总数统计 |
| | |
| | | // 查询固定资产项目 |
| | | 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); |
| | |
| | | /** |
| | | * 统计大型机械设备数(含已检查/未检查) |
| | | */ |
| | | private List<MachineryStatisticsVO> calculateMachineryStatistics(List<Integer> projectIds) { |
| | | private List<MachineryStatisticsVO> calculateMachineryStatistics(List<Integer> projectIds, Date monthStart, Date monthEnd) { |
| | | if (projectIds.isEmpty()) { |
| | | return new ArrayList<>(); |
| | | } |
| | |
| | | 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() |
| | |
| | | |
| | | // 按设备类型名称分组统计 |
| | | 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 -> { |
| | |
| | | |
| | | // 查询设备检查记录 |
| | | // 建立 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, |
| | |
| | | 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 集合 |
| | |
| | | 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); |
| | |
| | | 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); |
| | |
| | | // gps 数组格式:[经度, 纬度] |
| | | vo.setLongitude(gps.getString(0)); |
| | | vo.setLatitude(gps.getString(1)); |
| | | |
| | | //位置信息 |
| | | vo.setAddressInfo(addressJson); |
| | | // 设置项目类别 |
| | | vo.setCategory(categoryMap.get(p.getId())); |
| | | |
| | |
| | | */ |
| | | 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); |
| | | |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | 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); |
| | | } |
| | | |
| | | /** |