浏览代码

日报表处理

wangli 4 年之前
父节点
当前提交
687714e964

+ 2 - 0
sms_water/src/main/java/com/huaxu/dao/MonitorDataMapper.java

@@ -21,4 +21,6 @@ public interface MonitorDataMapper {
     void batchInsertYearReport(@Param("year") Integer year, @Param("month")Integer month);
     void batchInsertMonthReport(@Param("year") Integer year, @Param("month")Integer month, @Param("day")Integer day);
     void batchInsertDayReport( @Param("dayReportEntities") List<DayReportEntity> dayReportEntities);
+
+    List<Integer> checkReportDataExit(@Param("year") Integer year, @Param("month")Integer month, @Param("day")Integer day, @Param("hour")Integer hour, @Param("deviceIds") List<Integer> deviceIds);
 }

+ 2 - 2
sms_water/src/main/java/com/huaxu/service/MonitorDataService.java

@@ -48,13 +48,13 @@ public interface MonitorDataService {
      */
     MonitorDataEntity getDeviceMonitorInfoByDeviceCode(String deviceCode);
 
-    List<DayReportEntity> getMonitorDataGroupByHour(LocalDateTime dateTime);
+    List<DayReportEntity> getMonitorDataGroupByHour(LocalDateTime dateTime,List<Integer> deviceIds);
 
     /**
      * 生成每小时数据的日报
      * @return
      */
-    List<DayReportEntity> getMonitorDataReportByHour();
+    void getMonitorDataReportByHour();
     /**
      * 生成每天数据的月报
      * @return

+ 42 - 40
sms_water/src/main/java/com/huaxu/service/impl/MonitorDataServiceImpl.java

@@ -120,7 +120,7 @@ public class MonitorDataServiceImpl implements MonitorDataService , Initializing
     }
 
     @Override
-    public List<DayReportEntity> getMonitorDataGroupByHour(LocalDateTime dateTime ){
+    public List<DayReportEntity> getMonitorDataGroupByHour(LocalDateTime dateTime ,List<Integer> deviceIds ){
 
         if(dateTime == null ){
             return null;
@@ -130,6 +130,9 @@ public class MonitorDataServiceImpl implements MonitorDataService , Initializing
         criteria.and("month").is(dateTime.getMonthValue());
         criteria.and("day").is(dateTime.getDayOfMonth());
         criteria.and("hour").is(dateTime.getHour());
+        if(deviceIds!=null && deviceIds.size()>0){
+            criteria.and("deviceId").in(deviceIds);
+        }
         AggregationOptions aggregationOptions = AggregationOptions.builder().allowDiskUse(true).build();
         Aggregation agg = Aggregation.newAggregation(
                 Aggregation.match(criteria),    //查询条件
@@ -153,60 +156,59 @@ public class MonitorDataServiceImpl implements MonitorDataService , Initializing
     }
 
     @Override
-    public List<DayReportEntity> getMonitorDataReportByHour(){
+    public void getMonitorDataReportByHour(){
         //取前一个小时的时间
         LocalDateTime dateTime = LocalDateTime.now().plusHours(-1);
 
-        List<DayReportEntity> hourDatas = getMonitorDataGroupByHour(dateTime);
-        List<DayReportEntity> lastHourDatas = getMonitorDataGroupByHour(dateTime.plusHours(-1));
+        List<DayReportEntity> hourDatas = getMonitorDataGroupByHour(dateTime,null);
+        List<DayReportEntity> lastHourDatas = getMonitorDataGroupByHour(dateTime.plusHours(-1),null);
 
         Map<String,DayReportEntity> lastHourDataMap = lastHourDatas.stream().collect(Collectors.toMap(DayReportEntity::getMapkey, a -> a,(k1, k2)->k1));
         //保存日报表数据
-        if(hourDatas.size()>0){
+        saveReportDataByHour(hourDatas, lastHourDataMap);
+
+        //设备id
+        List<Integer> deviceIds = lastHourDataMap.values().stream().map(d -> d.getDeviceId().intValue()).distinct().collect(Collectors.toList());
+        //补数据,前6小时没有统计数据的
+        if(deviceIds.size()>0){
+            for(int i=1;i<7;i++){
+                dateTime = LocalDateTime.now().plusHours(-i);
+                //前几小时有统计数据的设备id
+                List<Integer> deviceIdsIsExit = monitorDataMapper.checkReportDataExit(dateTime.getYear(),dateTime.getMonthValue(),dateTime.getDayOfMonth(),dateTime.getHour(),deviceIds);
+                //需要补充数据的设备
+                deviceIds = deviceIds.stream().filter(deviceId -> !deviceIdsIsExit.contains(deviceId)).collect(Collectors.toList());
+
+                //查询需要补充数据的设备数据信息
+                hourDatas = new ArrayList<>(lastHourDataMap.values());
+
+                lastHourDatas = getMonitorDataGroupByHour(dateTime.plusHours(-i),deviceIds);
+                lastHourDataMap = lastHourDatas.stream().collect(Collectors.toMap(DayReportEntity::getMapkey, a -> a,(k1, k2)->k1));
+                //保存日报表数据
+                saveReportDataByHour(hourDatas, lastHourDataMap);
+            }
+        }
+
+    }
+    //保存日报表数据
+    private void saveReportDataByHour(List<DayReportEntity> hourDatas, Map<String, DayReportEntity> lastHourDataMap) {
+        if (hourDatas.size() > 0) {
             //计算累计值
-            for(DayReportEntity dayReportEntity : hourDatas){
-                if(dayReportEntity.getLatestValue()!=null && lastHourDataMap.containsKey(dayReportEntity.getMapkey())){
+            for (DayReportEntity dayReportEntity : hourDatas) {
+                if (dayReportEntity.getLatestValue() != null && lastHourDataMap.containsKey(dayReportEntity.getMapkey())) {
                     //上一个小时有值
-                    if(  lastHourDataMap.get(dayReportEntity.getMapkey()).getLatestValue() != null){
+                    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());
+                    } else {
+                        if (dayReportEntity.getFirstValue() != null) {//上一个小时没有值,取本小时的初始值
+                            dayReportEntity.setSumValue(new BigDecimal(dayReportEntity.getLatestValue().toString()).subtract(new BigDecimal(dayReportEntity.getFirstValue().toString())).doubleValue());
+                        }
+                        lastHourDataMap.remove(dayReportEntity.getMapkey());
                     }
 
                 }
             }
             monitorDataMapper.batchInsertDayReport(hourDatas);
         }
-        return hourDatas;
-
-//        Map<String,DayReportEntity> hourDataMap = hourDatas.stream().collect(Collectors.toMap(DayReportEntity::getMapkey, a -> a,(k1, k2)->k1));
-//        //需要统计的属性值(租户——场景——设备——属性)
-//        List<DayReportEntity> dayReportEntities = monitorDataMapper.getReportBaseInfo();
-//        //保存日报表数据
-//        if(hourDatas.size()>0 && dayReportEntities.size()>0){
-//           //填充数据
-//
-//            dayReportEntities =  dayReportEntities.stream()
-//                            .filter(d ->StringUtils.isNotBlank(d.getMapkey()) && hourDataMap.containsKey(d.getMapkey()))
-//                            .map(dayReportEntity ->{
-//                                DayReportEntity hourData = hourDataMap.get(dayReportEntity.getMapkey());
-//                                dayReportEntity.setYear(hourData.getYear());
-//                                dayReportEntity.setMonth(hourData.getMonth());
-//                                dayReportEntity.setDay(hourData.getDay());
-//                                dayReportEntity.setHour(hourData.getHour());
-//                                dayReportEntity.setMinValue(hourData.getMinValue());
-//                                dayReportEntity.setMaxValue(hourData.getMaxValue());
-//                                dayReportEntity.setAvgValue(hourData.getAvgValue());
-//                                dayReportEntity.setCollectDate(hourData.getCollectDate());
-//                                dayReportEntity.setLatestValue(hourData.getLatestValue());
-//                                if(lastHourDataMap.containsKey(dayReportEntity.getMapkey()) && lastHourDataMap.get(dayReportEntity.getMapkey()).getLatestValue() != null){
-//                                    dayReportEntity.setSumValue(new BigDecimal(hourDataMap.get(dayReportEntity.getMapkey()).getLatestValue().toString()).subtract(new BigDecimal(lastHourDataMap.get(dayReportEntity.getMapkey()).getLatestValue().toString())).doubleValue());
-//                                }
-//                                return dayReportEntity;
-//                            }).collect(Collectors.toList());
-//            monitorDataMapper.batchInsertDayReport(dayReportEntities);
-//        }
-//        return dayReportEntities;
     }
 
     @Override

+ 1 - 1
sms_water/src/main/resources/mapper/HomePageReportMapper.xml

@@ -103,7 +103,7 @@
         <if test="companyOrgId != null">
             and r.COMPANY_ORG_ID = #{companyOrgId}
         </if>
-        and year(r.COLLECT_DATE) =year(CURDATE()) and month(r.COLLECT_DATE) = year(CURDATE())
+        and year(r.COLLECT_DATE) =year(CURDATE()) and month(r.COLLECT_DATE) = month(CURDATE())
         <if test="userType!=null and userType!=-999 and userType!=-9999 and  programItems != null and programItems.size() > 0">
             and r.COMPANY_ORG_ID in
             <foreach collection="programItems" item="item" open="(" close=")" separator=",">

+ 13 - 0
sms_water/src/main/resources/mapper/MonitorDataMapper.xml

@@ -71,4 +71,17 @@
         where year = #{year} and month =#{month} and day=#{day}
         group by TENANT_ID, DEVICE_ID, DEVICE_NAME, DEVICE_CODE, ATTRIBUTE_ID, ATTRIBUTE_NAME,  YEAR, MONTH, DAY
     </insert>
+
+
+    <select id="checkReportDataExit" resultType="java.lang.Integer">
+        select
+            distinct device_id
+        from sms_day_report
+        where year=#{year} and month = #{month} and day =#{day} and hour =#{hour}
+        and device_id in
+        <foreach collection="deviceIds" item="item" index="index" open="(" separator="," close=")">
+             #{item}
+        </foreach>
+
+    </select>
 </mapper>