Просмотр исходного кода

pms 瑞恒看板 近7天的设备利用率 折线图

zhangcl 16 часов назад
Родитель
Сommit
ec3f84728f

+ 144 - 0
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/controller/admin/stat/IotStaticController.java

@@ -1,6 +1,8 @@
 package cn.iocoder.yudao.module.pms.controller.admin.stat;
 
 import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.date.DatePattern;
+import cn.hutool.core.date.LocalDateTimeUtil;
 import cn.hutool.core.util.NumberUtil;
 import cn.hutool.core.util.ObjUtil;
 import cn.hutool.core.util.StrUtil;
@@ -1322,6 +1324,148 @@ public class IotStaticController {
 
     }
 
+    @Operation(summary = "近1周的设备利用率")
+    @GetMapping("/rh/device/sevenDayUtilization")
+    @PermitAll
+    public CommonResult<Map<String, Object>> sevenDayUtilization() {
+
+        List<String> lastSevenDays = getLastSevenDays();
+        String first = lastSevenDays.get(0);
+        String last = lastSevenDays.get(lastSevenDays.size() - 1);
+        LocalDateTime startOfDay = getStartOfDay(last);
+        LocalDateTime endOfDay = getEndOfDay(first);
+        LocalDateTime[] createTime = new LocalDateTime[]{startOfDay, endOfDay};
+
+        IotRhDailyReportPageReqVO iotRhDailyReportPageReqVO = new IotRhDailyReportPageReqVO();
+        iotRhDailyReportPageReqVO.setCreateTime(createTime);
+        iotRhDailyReportPageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
+        PageResult<IotRhDailyReportDO> pageReports = iotRhDailyReportMapper.selectPage(iotRhDailyReportPageReqVO);
+        if (ObjUtil.isEmpty(pageReports)) {
+            return buildEmptyResult(lastSevenDays);
+        }
+        List<IotRhDailyReportDO> reports = pageReports.getList();
+        if (CollUtil.isEmpty(reports)) {
+            return buildEmptyResult(lastSevenDays);
+        }
+        List<Map<String, Object>> fills = reports.stream().map(e -> {
+            Map<String, Object> abc = new HashMap<>();
+            abc.put("id", e.getId());
+            abc.put("createTime", e.getCreateTime());
+            abc.put("today", e.getDailyGasInjection());
+            return abc;
+        }).collect(Collectors.toList());
+
+        // 计算最近7天公司整体的设备利用率
+        // 筛选瑞恒 主设备 rq_iot_rh_main_device_category
+        Set<Long> mainDeviceCategoryIds = new HashSet<>();
+        List<DictDataDO> constructionStatusDictData = dictDataService.getDictDataListByDictType("rq_iot_rh_main_device_category");
+        if (CollUtil.isNotEmpty(constructionStatusDictData)) {
+            constructionStatusDictData.forEach(data -> {
+                String value = data.getValue();
+                if (NumberUtil.isNumber(value)) {
+                    Long longValue = Long.valueOf(value);
+                    mainDeviceCategoryIds.add(longValue);
+                }
+            });
+        }
+        Set<Long> allRhChildDeptIds = deptService.getChildDeptIdListFromCache(157l);
+        // 包含设备的部门id集合
+        Set<Long> haveDeviceDeptIds = new HashSet<>();
+        IotDevicePageReqVO deviceReqVO = new IotDevicePageReqVO();
+        deviceReqVO.setDeptIds(new ArrayList<>(allRhChildDeptIds));
+        List<IotDeviceDO> devices = iotDeviceMapper.selectListAlone(deviceReqVO);
+        if (CollUtil.isNotEmpty(devices)) {
+            devices.forEach(device -> {
+                // 20260605 筛选包含主设备的队伍
+                if (mainDeviceCategoryIds.contains(device.getAssetClass())) {
+                    haveDeviceDeptIds.add(device.getDeptId());
+                }
+            });
+        }
+        Map<Long, DeptDO> allDeptPair = deptService.getDeptMap(allRhChildDeptIds);
+        Set<Long> teamIds = new HashSet<>();
+        if (CollUtil.isNotEmpty(allDeptPair)) {
+            allDeptPair.forEach((deptId, dept) -> {
+                // 队伍
+                if ("3".equals(dept.getType()) && haveDeviceDeptIds.contains(deptId)) {
+                    teamIds.add(dept.getId());
+                }
+            });
+        }
+        // key日期2026-06-08   value日期对应的日报数量
+        Map<String, Integer> dateReportCountPair = new HashMap<>();
+        reports.forEach(report -> {
+            // 按照日期维度统计各 工作量数据 累计用电量 累计油耗 累计注气量 累计注水量
+            LocalDateTime reportLocalDate = report.getCreateTime();
+            // 将日期格式转换成 字符串
+            String reportDateStr = LocalDateTimeUtil.format(reportLocalDate, DatePattern.NORM_DATE_PATTERN);
+            Long deptId = report.getDeptId();
+            if (haveDeviceDeptIds.contains(deptId)) {
+                if (dateReportCountPair.containsKey(reportDateStr)) {
+                    Integer tempNum = dateReportCountPair.get(reportDateStr);
+                    dateReportCountPair.put(reportDateStr, ++tempNum);
+                } else {
+                    dateReportCountPair.put(reportDateStr, 1);
+                }
+            }
+        });
+        // 计算每天的设备利用率
+        Map<String, BigDecimal> dateReportUtilizationPair = new HashMap<>();
+        if (CollUtil.isNotEmpty(dateReportCountPair)) {
+            dateReportCountPair.forEach((reportDateStr, reportNum) -> {
+                if (CollUtil.isNotEmpty(teamIds)) {
+                    BigDecimal deviceUtilization = new BigDecimal((double) reportNum / (teamIds.size() * 1))
+                            .setScale(4, RoundingMode.HALF_UP);
+                    dateReportUtilizationPair.put(reportDateStr, deviceUtilization);
+                }
+            });
+        }
+        // 5. 按日期升序补全最近7天,缺失填0
+        List<String> sortedDays = lastSevenDays.stream()
+                .sorted()                                      // yyyy-MM-dd 字符串自然升序
+                .collect(Collectors.toList());
+        LinkedHashMap<String, BigDecimal> fillMap = new LinkedHashMap<>();
+        sortedDays.forEach(day ->
+                fillMap.put(day, dateReportUtilizationPair.getOrDefault(day, BigDecimal.ZERO))
+        );
+        /* LinkedHashMap<String, BigDecimal> fillMap = dateReportUtilizationPair.entrySet().stream()
+                .sorted(Map.Entry.comparingByKey())      // yyyy-MM-dd 字符串自然序即升序
+                .collect(Collectors.toMap(
+                        Map.Entry::getKey,
+                        Map.Entry::getValue,
+                        (e1, e2) -> e1,
+                        LinkedHashMap::new
+                )); */
+
+        // LinkedHashMap<String, Long> fillMap = sumTotalByDate(fills, 7,"today");
+        LinkedList<Object> xAxis = new LinkedList<>(fillMap.keySet());
+        LinkedList<Object> fillData = new LinkedList<>(fillMap.values());
+        /* fillMap.forEach( (k,v)->{
+            xAxis.add(k);
+            fillData.add(v);
+        }); */
+
+        ImmutableMap<String, Serializable> fillResult = ImmutableMap.of("name", "设备利用率~~en**device utilization", "data", fillData);
+        return success(ImmutableMap.of("xAxis", xAxis, "series", ImmutableList.of(fillResult)));
+
+    }
+
+    /**
+     * 无日报时直接返回全0序列
+     */
+    private CommonResult<Map<String, Object>> buildEmptyResult(List<String> lastSevenDays) {
+        List<String> sortedDays = lastSevenDays.stream().sorted().collect(Collectors.toList());
+        LinkedList<Object> xAxis = new LinkedList<>(sortedDays);
+        LinkedList<Object> fillData = sortedDays.stream()
+                .map(d -> BigDecimal.ZERO)
+                .collect(Collectors.toCollection(LinkedList::new));
+        ImmutableMap<String, Serializable> fillResult = ImmutableMap.of(
+                "name", "设备利用率~~en**device utilization",
+                "data", fillData
+        );
+        return success(ImmutableMap.of("xAxis", xAxis, "series", ImmutableList.of(fillResult)));
+    }
+
     @Operation(summary = "瑞恒当日注气量 累计注气量")
     @GetMapping("/rh/zql")
     @PermitAll