diff --git a/ems-admin/src/main/java/com/xzzn/web/controller/ems/EmsGeneralQueryController.java b/ems-admin/src/main/java/com/xzzn/web/controller/ems/EmsGeneralQueryController.java index a0f7a76..107e835 100644 --- a/ems-admin/src/main/java/com/xzzn/web/controller/ems/EmsGeneralQueryController.java +++ b/ems-admin/src/main/java/com/xzzn/web/controller/ems/EmsGeneralQueryController.java @@ -9,7 +9,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * @@ -29,12 +31,15 @@ public class EmsGeneralQueryController extends BaseController{ @GetMapping("/getAllDeviceCategory") public AjaxResult getDeviceCategory() { - // 获取所有枚举的中文信息 - List deviceCategoryName = new ArrayList<>(); + // 获取所有枚举的信息 + List> deviceCategoryList = new ArrayList<>(); for (DeviceCategory category : DeviceCategory.values()) { - deviceCategoryName.add(category.getInfo()); + Map categoryMap = new HashMap<>(); + categoryMap.put("name", category.getInfo()); + categoryMap.put("code", category.getCode()); + deviceCategoryList.add(categoryMap); } - return success(deviceCategoryName); + return success(deviceCategoryList); } /** @@ -56,6 +61,7 @@ public class EmsGeneralQueryController extends BaseController{ try { result = iGeneralQueryService.getPointValueList(request); } catch (Exception e) { + logger.error("",e); return error("报错请重试!"); } return success(result); diff --git a/ems-admin/src/main/java/com/xzzn/web/controller/ems/EmsSiteConfigController.java b/ems-admin/src/main/java/com/xzzn/web/controller/ems/EmsSiteConfigController.java index 33beaa9..fc37793 100644 --- a/ems-admin/src/main/java/com/xzzn/web/controller/ems/EmsSiteConfigController.java +++ b/ems-admin/src/main/java/com/xzzn/web/controller/ems/EmsSiteConfigController.java @@ -142,7 +142,7 @@ public class EmsSiteConfigController extends BaseController{ } /** - * 单个站点单个设备点位查询 + * 单个站点单个设备点位查询-点位清单 */ @GetMapping("/getDevicePointList") public TableDataInfo getDevicePointList(@Validated PointDataRequest request) diff --git a/ems-common/src/main/java/com/xzzn/common/enums/DeviceCategory.java b/ems-common/src/main/java/com/xzzn/common/enums/DeviceCategory.java index 4be8b3e..1b7c55f 100644 --- a/ems-common/src/main/java/com/xzzn/common/enums/DeviceCategory.java +++ b/ems-common/src/main/java/com/xzzn/common/enums/DeviceCategory.java @@ -21,7 +21,7 @@ public enum DeviceCategory COOLING("COOLING", "冷却", null), DH("DH", "动环", null), XF("XF", "消防", null), - BATTERY_GROUP("BATTERY_GROUP", "电池组", null),; + BATTERY_GROUP("BATTERY_GROUP", "电池组", null); private final String code; private final String info; diff --git a/ems-system/src/main/java/com/xzzn/ems/domain/vo/DevicePointDataList.java b/ems-system/src/main/java/com/xzzn/ems/domain/vo/DevicePointDataList.java index 90526b5..3f768f2 100644 --- a/ems-system/src/main/java/com/xzzn/ems/domain/vo/DevicePointDataList.java +++ b/ems-system/src/main/java/com/xzzn/ems/domain/vo/DevicePointDataList.java @@ -1,5 +1,6 @@ package com.xzzn.ems.domain.vo; +import java.math.BigDecimal; import java.util.List; /** @@ -13,10 +14,25 @@ public class DevicePointDataList private String parentDeviceId; // 该设备点位数据list private List pointValueList; + // 最大值 + private BigDecimal maxValue; + // 最小值 + private BigDecimal minValue; + // 平均值(保留4位小数) + private BigDecimal avgValue; + // 差值(max - min) + private BigDecimal diffValue; - public DevicePointDataList(String deviceId, List pointValueList) { + public DevicePointDataList(String deviceId, List pointValueList,String parentDeviceId, + BigDecimal maxValue, BigDecimal minValue, + BigDecimal avgValue, BigDecimal diffValue) { this.deviceId = deviceId; this.pointValueList = pointValueList; + this.parentDeviceId = parentDeviceId; + this.maxValue = maxValue; + this.minValue = minValue; + this.avgValue = avgValue; + this.diffValue = diffValue; } public DevicePointDataList() { @@ -46,4 +62,36 @@ public class DevicePointDataList public void setParentDeviceId(String parentDeviceId) { this.parentDeviceId = parentDeviceId; } + + public BigDecimal getMaxValue() { + return maxValue; + } + + public void setMaxValue(BigDecimal maxValue) { + this.maxValue = maxValue; + } + + public BigDecimal getMinValue() { + return minValue; + } + + public void setMinValue(BigDecimal minValue) { + this.minValue = minValue; + } + + public BigDecimal getAvgValue() { + return avgValue; + } + + public void setAvgValue(BigDecimal avgValue) { + this.avgValue = avgValue; + } + + public BigDecimal getDiffValue() { + return diffValue; + } + + public void setDiffValue(BigDecimal diffValue) { + this.diffValue = diffValue; + } } diff --git a/ems-system/src/main/java/com/xzzn/ems/domain/vo/GeneralQueryResponse.java b/ems-system/src/main/java/com/xzzn/ems/domain/vo/GeneralQueryResponse.java index a4e8248..1571ac8 100644 --- a/ems-system/src/main/java/com/xzzn/ems/domain/vo/GeneralQueryResponse.java +++ b/ems-system/src/main/java/com/xzzn/ems/domain/vo/GeneralQueryResponse.java @@ -14,9 +14,13 @@ public class GeneralQueryResponse { // 设备数据 private List deviceList; - public GeneralQueryResponse(String siteId, List deviceList) { + // 点位数据类型,1-瞬时值,2-累计值 + private Long dataType; + + public GeneralQueryResponse(String siteId, List deviceList, Long dataType) { this.siteId = siteId; this.deviceList = deviceList; + this.dataType = dataType; } public GeneralQueryResponse() { @@ -38,4 +42,12 @@ public class GeneralQueryResponse { public void setDeviceList(List deviceList) { this.deviceList = deviceList; } + + public Long getDataType() { + return dataType; + } + + public void setDataType(Long dataType) { + this.dataType = dataType; + } } diff --git a/ems-system/src/main/java/com/xzzn/ems/domain/vo/PointNameRequest.java b/ems-system/src/main/java/com/xzzn/ems/domain/vo/PointNameRequest.java index f2eaa6f..8552c37 100644 --- a/ems-system/src/main/java/com/xzzn/ems/domain/vo/PointNameRequest.java +++ b/ems-system/src/main/java/com/xzzn/ems/domain/vo/PointNameRequest.java @@ -11,7 +11,7 @@ public class PointNameRequest { private List siteIds; - private String categoryName; + private String deviceCategory; private String pointName; @@ -34,12 +34,12 @@ public class PointNameRequest { this.siteIds = siteIds; } - public String getCategoryName() { - return categoryName; + public String getDeviceCategory() { + return deviceCategory; } - public void setCategoryName(String categoryName) { - this.categoryName = categoryName; + public void setDeviceCategory(String deviceCategory) { + this.deviceCategory = deviceCategory; } public String getPointName() { diff --git a/ems-system/src/main/java/com/xzzn/ems/service/impl/GeneralQueryServiceImpl.java b/ems-system/src/main/java/com/xzzn/ems/service/impl/GeneralQueryServiceImpl.java index cb1724e..d424da4 100644 --- a/ems-system/src/main/java/com/xzzn/ems/service/impl/GeneralQueryServiceImpl.java +++ b/ems-system/src/main/java/com/xzzn/ems/service/impl/GeneralQueryServiceImpl.java @@ -11,6 +11,7 @@ import com.xzzn.ems.service.IGeneralQueryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.math.BigDecimal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; @@ -42,8 +43,7 @@ public class GeneralQueryServiceImpl implements IGeneralQueryService return Collections.emptyList(); } - String categoryName = request.getCategoryName(); - String deviceCategory = DeviceCategory.getCodeByInfo(categoryName); + String deviceCategory = request.getDeviceCategory(); if (deviceCategory == null) { return Collections.emptyList(); } @@ -83,8 +83,7 @@ public class GeneralQueryServiceImpl implements IGeneralQueryService List querySiteIds = new ArrayList<>(); List siteIds = request.getSiteIds(); - String categoryName = request.getCategoryName(); - String deviceCategory = DeviceCategory.getCodeByInfo(categoryName); + String deviceCategory = request.getDeviceCategory(); // 根据入参获取点位对应的表和字段 List matchInfo = emsPointMatchMapper.getMatchInfo(siteIds,deviceCategory,request.getPointName()); if (matchInfo == null || matchInfo.size() == 0) { @@ -108,13 +107,13 @@ public class GeneralQueryServiceImpl implements IGeneralQueryService // 不同的site_id根据设备类型和字段,默认取第一个匹配到的表和表字段只会有一个, String tableName = matchInfo.get(0).getMatchTable(); String tableField = matchInfo.get(0).getMatchField(); - + Long dataType = matchInfo.get(0).getDataType(); if (DeviceCategory.BATTERY.getCode().equals(deviceCategory)) { // 单体电池数据特殊处理 - result = generalQueryBatteryData(querySiteIds,tableName,tableField,request,deviceCategory); + result = generalQueryBatteryData(querySiteIds,tableName,tableField,request,deviceCategory,dataType); } else { // 其他设备数据 - result = generalQueryCommonData(querySiteIds,tableName,tableField,request,deviceCategory); + result = generalQueryCommonData(querySiteIds,tableName,tableField,request,deviceCategory,dataType); } } catch (ParseException e) { throw new RuntimeException(e); @@ -122,9 +121,9 @@ public class GeneralQueryServiceImpl implements IGeneralQueryService return result; } - private List generalQueryCommonData(List querySiteIds,String tableName, + private List generalQueryCommonData(List querySiteIds, String tableName, String tableField, PointNameRequest request, - String deviceCategory) throws ParseException { + String deviceCategory, Long dataType) throws ParseException { List result = new ArrayList<>(); List dataVoList = new ArrayList<>(); @@ -146,14 +145,14 @@ public class GeneralQueryServiceImpl implements IGeneralQueryService dataVoList = emsPointMatchMapper.getCommonPointDataByDays(querySiteIds,tableName,tableField,startDate,endDate,deviceId); } - // 数据转换 - result = convertCommonToResultList(dataVoList); + // 数据转换+计算曲线数据最大最小平均和差值 + result = convertCommonToResultList(dataVoList, dataType); return result; } - private List generalQueryBatteryData(List querySiteIds,String tableName, + private List generalQueryBatteryData(List querySiteIds, String tableName, String tableField, PointNameRequest request, - String deviceCategory) throws ParseException { + String deviceCategory, Long dataType) throws ParseException { List result = new ArrayList<>(); List dataVoList = new ArrayList<>(); @@ -179,11 +178,11 @@ public class GeneralQueryServiceImpl implements IGeneralQueryService dataVoList = emsPointMatchMapper.getBatteryPointDataByDays(querySiteIds,tableName,tableField,startDate,endDate,siteDeviceMap,clusterDeviceId); } // 数据转换 - result = convertBatteryToResultList(dataVoList); + result = convertBatteryToResultList(dataVoList,dataType); return result; } - private List convertBatteryToResultList(List dataVoList) { + private List convertBatteryToResultList(List dataVoList, Long dataType) { // 先按siteId分组 return dataVoList.stream() .collect(Collectors.groupingBy(GeneralQueryDataVo::getSiteId)) @@ -218,12 +217,12 @@ public class GeneralQueryServiceImpl implements IGeneralQueryService }) .collect(Collectors.toList()); + // 4. 计算最大最小平均值 + Stats stats = clacStats(pointValueList); + // 4. 构建DeviceItem - DevicePointDataList deviceItem = new DevicePointDataList(); - deviceItem.setDeviceId(deviceId); - deviceItem.setParentDeviceId(parentDeviceId); - deviceItem.setPointValueList(pointValueList); - return deviceItem; + return new DevicePointDataList(deviceId, pointValueList,parentDeviceId, + stats.max, stats.min,stats.avg,stats.diff); })// 关键排序步骤:先按deviceId升序,再按parentDeviceId升序 .sorted( Comparator.comparing(DevicePointDataList::getDeviceId) // 第一排序键:deviceId @@ -232,15 +231,12 @@ public class GeneralQueryServiceImpl implements IGeneralQueryService .collect(Collectors.toList()); // 5. 构建SiteData - GeneralQueryResponse site = new GeneralQueryResponse(); - site.setSiteId(siteId); - site.setDeviceList(deviceList); - return site; + return new GeneralQueryResponse(siteId, deviceList, dataType); }) .collect(Collectors.toList()); } - private List convertCommonToResultList(List dataVoList) { + private List convertCommonToResultList(List dataVoList, Long dataType) { // 数据转换: 先按siteId分组,再按deviceId分组 return dataVoList.stream() // 第一层分组:按siteId分组,得到 @@ -254,18 +250,46 @@ public class GeneralQueryServiceImpl implements IGeneralQueryService List deviceList = siteAllData.stream() .collect(Collectors.groupingBy(GeneralQueryDataVo::getDeviceId)) .entrySet().stream() - .map(deviceEntry -> new DevicePointDataList( - deviceEntry.getKey(), - deviceEntry.getValue() - )) + .map(deviceEntry -> { + String deviceId = deviceEntry.getKey(); + List deviceDataList = deviceEntry.getValue(); + + // 计算当前设备数据的统计值(最大,最小,平均,差值) + Stats stats = clacStats(deviceDataList); + + return new DevicePointDataList(deviceId, deviceDataList,null, + stats.max, stats.min,stats.avg,stats.diff); + }) .collect(Collectors.toList()); - return new GeneralQueryResponse(siteId, deviceList); + return new GeneralQueryResponse(siteId, deviceList, dataType); }) .collect(Collectors.toList()); } + private Stats clacStats(List deviceDataList) { + // 提取有效数值(过滤null) + List values = deviceDataList.stream() + .map(vo -> (BigDecimal) vo.getPointValue()) + .filter(value -> value != null) // 过滤null值 + .collect(Collectors.toList()); + if (values.isEmpty()) { + return new Stats(null, null, null, null); + } + + // 计算最大最小值 + BigDecimal maxValue = values.stream().max(BigDecimal::compareTo).get(); + BigDecimal minValue = values.stream().min(BigDecimal::compareTo).get(); + // 计算平均值,四舍五入保留4为小数 + BigDecimal sum = values.stream().reduce(BigDecimal.ZERO, BigDecimal::add); + BigDecimal avgValue = sum.divide(BigDecimal.valueOf(values.size()), 4, BigDecimal.ROUND_HALF_UP); + // 增量数据,计算差值 + BigDecimal diff = maxValue.subtract(minValue); + + return new Stats(maxValue,minValue,avgValue,diff); + } + private void dealDataTime(PointNameRequest request) { String startDate = request.getStartDate(); String endDate = request.getEndDate(); @@ -441,4 +465,21 @@ public class GeneralQueryServiceImpl implements IGeneralQueryService return minutes; } + + /** + * 内部辅助类:存储统计结果 + */ + private static class Stats { + private final BigDecimal max; + private final BigDecimal min; + private final BigDecimal avg; + private final BigDecimal diff; + + public Stats(BigDecimal max, BigDecimal min, BigDecimal avg, BigDecimal diff) { + this.max = max; + this.min = min; + this.avg = avg; + this.diff = diff; + } + } } diff --git a/ems-system/src/main/resources/mapper/ems/EmsPointMatchMapper.xml b/ems-system/src/main/resources/mapper/ems/EmsPointMatchMapper.xml index f29c14d..4a0f96e 100644 --- a/ems-system/src/main/resources/mapper/ems/EmsPointMatchMapper.xml +++ b/ems-system/src/main/resources/mapper/ems/EmsPointMatchMapper.xml @@ -147,7 +147,8 @@ t.match_table as matchTable, t.match_field as matchField, t.device_category as deviceCategory, - t.point_name as pointName + t.point_name as pointName, + t.data_type as dataType from ems_point_match t where 1=1