Browse Source

每小时数据统计时,前一个小时没数据,取当本时段的初始值

wangli 4 years ago
parent
commit
bbd3bbb798

+ 3 - 0
sms_water/src/main/java/com/huaxu/entity/DayReportEntity.java

@@ -121,6 +121,9 @@ public class DayReportEntity{
     /** 最新值 */
     @ApiModelProperty(value = "最新值")
     private Double latestValue;
+    /** 最新值 */
+    @ApiModelProperty(value = "初始值")
+    private Double firstValue;
 
     /** 采集时间 */
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" , timezone = "GMT+8")

+ 9 - 3
sms_water/src/main/java/com/huaxu/service/impl/MonitorDataServiceImpl.java

@@ -140,6 +140,7 @@ public class MonitorDataServiceImpl implements MonitorDataService , Initializing
                         .max("$dataValues.dataValue").as("maxValue")
                         .avg("$dataValues.dataValue").as("avgValue")
 //                        .sum("$dataValues.dataValue").as("sumValue")
+                        .first("$dataValues.dataValue").as("firstValue")
                         .last("$dataValues.dataValue").as("latestValue")
                         .last("$collectDate").as("collectDate")
                         .count().as("countNum")
@@ -164,9 +165,14 @@ public class MonitorDataServiceImpl implements MonitorDataService , Initializing
         if(hourDatas.size()>0){
             //计算累计值
             for(DayReportEntity dayReportEntity : hourDatas){
-                if(dayReportEntity.getLatestValue()!=null && lastHourDataMap.containsKey(dayReportEntity.getMapkey())
-                        && lastHourDataMap.get(dayReportEntity.getMapkey()).getLatestValue() != null){
-                    dayReportEntity.setSumValue(new BigDecimal(dayReportEntity.getLatestValue().toString()).subtract(new BigDecimal(lastHourDataMap.get(dayReportEntity.getMapkey()).getLatestValue().toString())).doubleValue());
+                if(dayReportEntity.getLatestValue()!=null && lastHourDataMap.containsKey(dayReportEntity.getMapkey())){
+                    //上一个小时有值
+                    if(  lastHourDataMap.get(dayReportEntity.getMapkey()).getLatestValue() != null){
+                        dayReportEntity.setSumValue(new BigDecimal(dayReportEntity.getLatestValue().toString()).subtract(new BigDecimal(lastHourDataMap.get(dayReportEntity.getMapkey()).getLatestValue().toString())).doubleValue());
+                    }else if(dayReportEntity.getFirstValue() != null){//上一个小时没有值,取本小时的初始值
+                        dayReportEntity.setSumValue(new BigDecimal(dayReportEntity.getLatestValue().toString()).subtract(new BigDecimal(dayReportEntity.getFirstValue().toString())).doubleValue());
+                    }
+
                 }
             }
             monitorDataMapper.batchInsertDayReport(hourDatas);