From 2e53762e348ddc10407e6a0367b56ed8bfd4d31b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=85=B4=E7=90=B3?= <1911390090@qq.com> Date: Thu, 21 Nov 2024 09:08:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=AA=E9=98=B3=E8=83=BD=E6=9D=BF=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=9B=91=E6=B5=8B=E5=B0=8F=E6=97=B6=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/DcDeviceController.java | 64 ++++++++++++------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/zc-business/src/main/java/com/zc/business/controller/DcDeviceController.java b/zc-business/src/main/java/com/zc/business/controller/DcDeviceController.java index 9aae11bf..054097b5 100644 --- a/zc-business/src/main/java/com/zc/business/controller/DcDeviceController.java +++ b/zc-business/src/main/java/com/zc/business/controller/DcDeviceController.java @@ -416,35 +416,34 @@ private VideoController videoController; dateList.add(DateUtil.beginOfDay(new Date()).toString()); dateList.add(DateUtil.endOfDay(new Date()).toString()); // 将日期列表以逗号分隔并设置为查询条件的值 - props.put("terms[0].value", String.join(UniversalEnum.COMMA.getValue(), dateList)); + props.put("terms[0].value", String.join(",", dateList)); props.put("paging", false); props.put("sorts[0].order", "asc"); props.put("sorts[0].name", "timestamp"); - Map parameter =new HashMap<>(); - String[] endStakeMark={"208","979"}; - String[] startStakeMark={"054","378"}; - parameter.put("deviceType","15"); - parameter.put("endStakeMark",endStakeMark); - parameter.put("startStakeMark",startStakeMark); - // parameter.put("deviceState","1"); + Map parameter = new HashMap<>(); + String[] endStakeMark = {"208", "979"}; + String[] startStakeMark = {"054", "378"}; + parameter.put("deviceType", "15"); + parameter.put("endStakeMark", endStakeMark); + parameter.put("startStakeMark", startStakeMark); + // parameter.put("deviceState","1"); // 创建一个映射来存储按小时汇总的数据 Map allDevicesHourlyAggregates = new TreeMap<>(); List dcDevices = dcDeviceService.devicePileNumberQueryDevice(parameter); for (DcDevice dcDevice : dcDevices) { AjaxResult ajaxResult = queryDeviceProperties(dcDevice.getIotDeviceId(), propertyId, props); - if (!ajaxResult.get("code").equals(UniversalEnum.TWO_HUNDRED.getNumber())) { - return ajaxResult; - } + if (!ajaxResult.get("code").equals(UniversalEnum.TWO_HUNDRED.getNumber())) { + return ajaxResult; + } // 定义时间格式 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - Object data = JSON.parseObject(queryDeviceProperties(dcDevice.getIotDeviceId(), propertyId, props).get("data").toString()).get("data"); - JSONArray dataArray = JSON.parseArray(data.toString()); + Object data = JSON.parseObject(ajaxResult.get("data").toString()).get("data"); + JSONArray dataArray = JSON.parseArray(data.toString()); // 创建一个映射来存储单个设备按小时汇总的数据 - Map deviceHourlyAggregates = new TreeMap<>(); + Map> deviceHourlyLatestData = new TreeMap<>(); - List> list = new ArrayList<>(); DecimalFormat decimalFormat = new DecimalFormat("#.##"); decimalFormat.setRoundingMode(RoundingMode.HALF_UP); @@ -469,24 +468,43 @@ private VideoController videoController; calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), hour); - //转换为 double 类型,去除单位 + // 转换为 double 类型,去除单位 String valueWithoutUnit = formatValueStr.replaceAll("[^\\d.]", ""); - // double formatValue = Double.parseDouble(valueWithoutUnit); double parsedValue = Double.parseDouble(valueWithoutUnit); // 保留两位小数 double formatValue = Double.parseDouble(decimalFormat.format(parsedValue)); - // 汇总每个小时的数据 - deviceHourlyAggregates.merge(key, formatValue, (oldVal, newVal) -> Double.parseDouble(decimalFormat.format(oldVal + newVal))); } -// 将设备的数据合并到所有设备的汇总数据中 - for (String key : deviceHourlyAggregates.keySet()) { - allDevicesHourlyAggregates.merge(key, deviceHourlyAggregates.get(key), (oldVal, newVal) -> Double.parseDouble(decimalFormat.format(oldVal + newVal))); + + // 检查当前小时是否有数据,如果没有则直接添加 + if (!deviceHourlyLatestData.containsKey(key)) { + Map dataPoint = new HashMap<>(); + dataPoint.put("value", formatValue); + dataPoint.put("timestamp", timestamp); + deviceHourlyLatestData.put(key, dataPoint); + } else { + // 如果已经有数据,比较时间戳,保留最新的数据 + long existingTimestamp = (long) deviceHourlyLatestData.get(key).get("timestamp"); + if (timestamp > existingTimestamp) { + Map dataPoint = new HashMap<>(); + dataPoint.put("value", formatValue); + dataPoint.put("timestamp", timestamp); + deviceHourlyLatestData.put(key, dataPoint); + } + } } + // 将设备的数据合并到所有设备的汇总数据中 + for (String key : deviceHourlyLatestData.keySet()) { + double latestValue = (double) deviceHourlyLatestData.get(key).get("value"); + allDevicesHourlyAggregates.merge(key, latestValue, (oldVal, newVal) -> { + double sum = oldVal + newVal; + return Double.parseDouble(decimalFormat.format(sum)); + }); + } } - return AjaxResult.success(allDevicesHourlyAggregates); } + @ApiOperation("太阳能重点数据") @GetMapping("/properties/latestOne") public AjaxResult getDeviceLatest() throws HttpException, IOException {