Forráskód Böngészése

供水量耗药量统计

wangyangyang 4 éve
szülő
commit
867b83f07d

+ 1 - 1
sms_water/src/main/java/com/huaxu/config/ResourceServerConfig.java

@@ -17,7 +17,7 @@ public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
                 .and()
                 .authorizeRequests()
                 .antMatchers("/swagger-ui.html","/webjars/**", "/webjars/**", "/swagger-resources/**",
-                       "/v2/**","/devicetype/**")
+                       "/v2/**","/devicetype/**","/monitorinfo/**")
                 .permitAll() //配置不需要身份认证的请求路径
                 .anyRequest().authenticated() //其他所有访问路径都需要身份认证
                 .and()

+ 44 - 34
sms_water/src/main/java/com/huaxu/controller/MonitorInfoController.java

@@ -1,9 +1,6 @@
 package com.huaxu.controller;
 
-import com.huaxu.dto.AlarmDetailsDto;
-import com.huaxu.dto.DeviceDto;
-import com.huaxu.dto.MonitorDataDto;
-import com.huaxu.dto.ReportDto;
+import com.huaxu.dto.*;
 import com.huaxu.entity.*;
 import com.huaxu.model.AjaxMessage;
 import com.huaxu.model.LoginUser;
@@ -20,6 +17,7 @@ import org.springframework.stereotype.Controller;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.ui.ModelMap;
 
+import java.time.LocalDateTime;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -53,45 +51,57 @@ public class MonitorInfoController {
     @ApiOperation(value = "按场景ID查询供水量出水量耗药量耗电量")
     @RequestMapping(value = "/findTotalUsageBySceneId",method = RequestMethod.GET)
     @ResponseBody
-    public  AjaxMessage<ReportDto> findTotalUsageBySceneId(@ApiParam(value = "场景ID", required = true) @RequestParam Long id) {
-       //查询场景下的所有设备信息
+    public  AjaxMessage<SceneUsageDto> findTotalUsageBySceneId(@ApiParam(value = "场景ID", required = true) @RequestParam Long id) {
+        SceneUsageDto sceneUsageDto = new SceneUsageDto();
+        //查询场景下的所有设备信息
         List<DeviceDto> devices = new ArrayList<>();
         DeviceDto deviceDto = new DeviceDto();
         deviceDto.setSceneIds(sceneService.findByParentIdsLike(id));
         devices.addAll(deviceService.selectList(deviceDto));
-
-        for(DeviceDto item : devices) {
+        //取前一个小时的时间
+        LocalDateTime dateTime = LocalDateTime.now().plusDays(-1);
+        for (DeviceDto item : devices) {
+            //设备的几个参数值
+            SceneDeviceAttributeDto sceneDeviceAttributeDto = new SceneDeviceAttributeDto();
+            sceneDeviceAttributeDto.setDeviceId(item.getId());
+            sceneDeviceAttributeDto.setYear(dateTime.getYear());
+            sceneDeviceAttributeDto.setMonth(dateTime.getMonthValue());
+            sceneDeviceAttributeDto.setDay(dateTime.getDayOfMonth());
+            List<SceneDeviceAttributeDto> sceneDeviceAttributeDtos = monitorInfoService.findAttributeList(sceneDeviceAttributeDto);
             //取缓存里的数据
             byte[] bytes = redisUtil.get(("sms_water_" + item.getDeviceCode()).getBytes());
             if (bytes != null && bytes.length > 0) {
                 MonitorDataEntity monitorDataEntity = (MonitorDataEntity) ByteArrayUtils.bytesToObject(bytes).get();
                 //筛选该设备相同属性的值
-//                List<MonitorDataValueEntity> attributeEntities = monitorDataEntity.getDataValues().stream().filter((MonitorDataValueEntity m)
-//                        -> m.getAttributeId().equals(monitorData.getAttributeId())).collect(Collectors.toList());
-//                MonitorDataValueEntity  attributeEntity=attributeEntities.size()>0?attributeEntities.get(0):null;
-//                if (attributeEntity != null) {
-//                    Double attributeDiffValue =0d;
-//                    if(attributeEntity.getDataValue() != null && monitorData.getLatestValue() != null){
-//                        attributeDiffValue=attributeEntity.getDataValue() - monitorData.getLatestValue();
-//                    }else if(attributeEntity.getDataValue() != null && monitorData.getLatestValue() == null) {
-//                        attributeDiffValue=attributeEntity.getDataValue();
-//                    }
-//                    Double attributeValue = monitorData.getSumValue() != null ? monitorData.getSumValue() + attributeDiffValue : attributeDiffValue;
-//                    switch (monitorData.getAttributeType()) {
-//                        case "3":
-//                            monDataCol.setYieldWaterUsage(monDataCol.getYieldWaterUsage() != null ? monDataCol.getYieldWaterUsage() + attributeValue : attributeValue);
-//                            break;
-//                        case "4":
-//                            monDataCol.setIntakeWaterUsage(monDataCol.getIntakeWaterUsage() != null ? monDataCol.getIntakeWaterUsage() + attributeValue : attributeValue);
-//                            break;
-//                        case "5":
-//                            monDataCol.setPowerUsage(monDataCol.getPowerUsage() != null ? monDataCol.getPowerUsage() + attributeValue : attributeValue);
-//                            break;
-//                        case "6":
-//                            monDataCol.setDrugUsage(monDataCol.getDrugUsage() != null ? monDataCol.getDrugUsage() + attributeValue : attributeValue);
-//                            break;
-//                    }
-//                }
+                Map<Long, MonitorDataValueEntity> map = new HashMap<>();
+                //将缓存中的实时数据放到map中方便进行遍历
+                for (MonitorDataValueEntity dateValue : monitorDataEntity.getDataValues()) {
+                    map.put(dateValue.getAttributeId(), dateValue);
+                }
+                for (SceneDeviceAttributeDto itemAttribute : sceneDeviceAttributeDtos) {
+                    Double attributeDiffValue = 0d;
+                    if(!map.containsKey(itemAttribute.getAttributeId()))
+                        continue;
+                    if (map.get(itemAttribute.getAttributeId()).getDataValue() != null && itemAttribute.getLatestValue() != null) {
+                        attributeDiffValue = map.get(itemAttribute.getAttributeId()).getDataValue() - itemAttribute.getLatestValue();
+                    } else if (map.get(itemAttribute.getAttributeId()).getDataValue() != null && itemAttribute.getLatestValue() == null) {
+                        attributeDiffValue = map.get(itemAttribute.getAttributeId()).getDataValue();
+                    }
+                    switch (itemAttribute.getAttributeType()) {
+                        case "3":
+                            sceneUsageDto.setYieldWaterUsage(sceneUsageDto.getYieldWaterUsage() != null ? sceneUsageDto.getYieldWaterUsage() + attributeDiffValue : attributeDiffValue);
+                            break;
+                        case "4":
+                            sceneUsageDto.setIntakeWaterUsage(sceneUsageDto.getIntakeWaterUsage() != null ? sceneUsageDto.getIntakeWaterUsage() + attributeDiffValue : attributeDiffValue);
+                            break;
+                        case "5":
+                            sceneUsageDto.setPowerUsage(sceneUsageDto.getPowerUsage() != null ? sceneUsageDto.getPowerUsage() + attributeDiffValue : attributeDiffValue);
+                            break;
+                        case "6":
+                            sceneUsageDto.setDrugUsage(sceneUsageDto.getDrugUsage() != null ? sceneUsageDto.getDrugUsage() + attributeDiffValue : attributeDiffValue);
+                            break;
+                    }
+                }
             }
         }
         return new AjaxMessage<>(ResultStatus.OK);

+ 4 - 0
sms_water/src/main/java/com/huaxu/dao/MonitorInfoMapper.java

@@ -1,5 +1,7 @@
 package com.huaxu.dao;
 
+import com.huaxu.dto.ReportDto;
+import com.huaxu.dto.SceneDeviceAttributeDto;
 import com.huaxu.entity.MonitorInfoEntity;
 import java.io.Serializable;
 import java.util.List;
@@ -30,5 +32,7 @@ public interface MonitorInfoMapper extends BaseMapper<MonitorInfoEntity> {
 
      List<MonitorInfoEntity> findList(MonitorInfoEntity monitorInfoEntity);
 
+    List<SceneDeviceAttributeDto> findAttributeList(@Param("report") SceneDeviceAttributeDto reportDto);
+
      /**删除相关方法  使用mybatis-plus集成的 **/
 }

+ 41 - 0
sms_water/src/main/java/com/huaxu/dto/SceneDeviceAttributeDto.java

@@ -8,11 +8,27 @@ import lombok.Data;
 @Data
 @ApiModel(value = "设备属性信息")
 public class SceneDeviceAttributeDto {
+    /** 设备信息 */
+    @ApiModelProperty(value = "设备信息")
+    private Long deviceId;
     /**
      * 设备属性名称
      */
     @ApiModelProperty("设备属性名称")
     private String attributeName;
+
+    /** 年 */
+    @ApiModelProperty(value = "年")
+    private Integer year;
+
+    /** 月 */
+    @ApiModelProperty(value = "月")
+    private Integer month;
+
+    /** 日 */
+    @ApiModelProperty(value = "日")
+    private Integer day;
+
     /**
      * 设备属性ID
      */
@@ -22,5 +38,30 @@ public class SceneDeviceAttributeDto {
     @ApiModelProperty(value = "属性类型标记",hidden = true)
     @JsonIgnore
     private String attributeType;
+    /**
+     * 最小值
+     */
+    @ApiModelProperty(value = "最小值")
+    private Double minValue;
+    /**
+     * 最大值
+     */
+    @ApiModelProperty(value = "最大值")
+    private Double maxValue;
+    /**
+     * 平均值
+     */
+    @ApiModelProperty(value = "平均值")
+    private Double avgValue;
+    /**
+     * 合计值
+     */
+    @ApiModelProperty(value = "合计值")
+    private Double sumValue;
+    /**
+     * 最新值
+     */
+    @ApiModelProperty(value = "最新值")
+    private Double latestValue;
 
 }

+ 18 - 0
sms_water/src/main/java/com/huaxu/dto/SceneUsageDto.java

@@ -0,0 +1,18 @@
+package com.huaxu.dto;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+@Data
+@ApiModel(value = "场景属性汇总值")
+public class SceneUsageDto {
+    @ApiModelProperty("今日供水量or今日出水量")
+    private Double yieldWaterUsage;
+    @ApiModelProperty("今日取水量or今日进水量")
+    private Double intakeWaterUsage;
+    @ApiModelProperty("今日耗电量")
+    private Double powerUsage;
+    @ApiModelProperty("今日耗药量")
+    private Double drugUsage;
+}

+ 4 - 4
sms_water/src/main/java/com/huaxu/service/DayReportService.java

@@ -68,7 +68,7 @@ public class DayReportService extends ServiceImpl<DayReportMapper, DayReportEnti
         return dayReportMapper.findDayReportById(id);
     }
 
-    public Page<ReportDto> findPage(IPage<ReportDto> page, Long[] ids, String year, String month, String day) {
+    public Page<ReportDto> findPage(IPage<ReportDto> page, Long[] ids, Integer year, Integer month, Integer day) {
         LoginUser currentUser = UserUtil.getCurrentUser();
         Page<ReportDto> reportPage = new Page<>();
         //查询场景下的所有设备信息
@@ -80,9 +80,9 @@ public class DayReportService extends ServiceImpl<DayReportMapper, DayReportEnti
         }
         //根据设备ID查询报表测点信息
         DayReportEntity dayReportEntity = new DayReportEntity();
-        dayReportEntity.setYear(Integer.valueOf(year));
-        dayReportEntity.setMonth(Integer.valueOf(month));
-        dayReportEntity.setDay(Integer.valueOf(day));
+        dayReportEntity.setYear(year);
+        dayReportEntity.setMonth(month);
+        dayReportEntity.setDay(day);
         dayReportEntity.setTenantId(currentUser.getTenantId());
         dayReportEntity.setParentSceneIds(ids);
         dayReportEntity.setDeviceIds(devices);

+ 6 - 0
sms_water/src/main/java/com/huaxu/service/MonitorInfoService.java

@@ -2,6 +2,7 @@ package com.huaxu.service;
 
 
 import com.huaxu.dao.MonitorInfoMapper;
+import com.huaxu.dto.SceneDeviceAttributeDto;
 import com.huaxu.entity.MonitorInfoEntity;
 import com.huaxu.model.LoginUser;
 import com.huaxu.util.UserUtil;
@@ -96,4 +97,9 @@ public class MonitorInfoService extends ServiceImpl<MonitorInfoMapper, MonitorIn
     public MonitorInfoEntity findMonitorInfoById(Long id) {
         return monitorInfoMapper.findMonitorInfoById(id);
     }
+
+    public List<SceneDeviceAttributeDto> findAttributeList(SceneDeviceAttributeDto sceneDeviceAttributeDto)
+    {
+        return monitorInfoMapper.findAttributeList(sceneDeviceAttributeDto);
+    }
 }

+ 3 - 3
sms_water/src/main/java/com/huaxu/service/MonthReportService.java

@@ -65,7 +65,7 @@ public class MonthReportService extends ServiceImpl<MonthReportMapper, MonthRepo
         return monthReportMapper.findMonthReportById(id);
     }
 
-    public Page<ReportDto> findPage(IPage<ReportDto> page, Long[] ids, String year, String month, String day) {
+    public Page<ReportDto> findPage(IPage<ReportDto> page, Long[] ids, Integer year, Integer month, Integer day) {
         LoginUser currentUser = UserUtil.getCurrentUser();
         Page<ReportDto> reportPage = new Page<>();
         //查询场景下的所有设备信息
@@ -76,8 +76,8 @@ public class MonthReportService extends ServiceImpl<MonthReportMapper, MonthRepo
             devices.addAll(deviceService.selectList(deviceDto));
         }
         MonthReportEntity monthReportEntity = new MonthReportEntity();
-        monthReportEntity.setYear(Integer.valueOf(year));
-        monthReportEntity.setMonth(Integer.valueOf(month));
+        monthReportEntity.setYear(year);
+        monthReportEntity.setMonth(month);
         monthReportEntity.setTenantId(currentUser.getTenantId());
         monthReportEntity.setParentSceneIds(ids);
         monthReportEntity.setDeviceIds(devices);

+ 1 - 1
sms_water/src/main/java/com/huaxu/service/ReportService.java

@@ -30,7 +30,7 @@ public class ReportService {
      * @param day
      * @param reportType
      */
-    public IPage<ReportDto> getReportBySceneIds(IPage<ReportDto> page, Long[] ids, String year, String month, String day, Integer reportType) {
+    public IPage<ReportDto> getReportBySceneIds(IPage<ReportDto> page, Long[] ids, Integer year, Integer month, Integer day, Integer reportType) {
         Page<ReportDto> reportPage = new Page<>();
         if (reportType.equals(0))//日报
         {

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

@@ -66,7 +66,7 @@ public class YearReportService extends ServiceImpl<YearReportMapper, YearReportE
         return yearReportMapper.findYearReportById(id);
     }
 
-    public Page<ReportDto> findPage(IPage<ReportDto> page, Long[] ids, String year, String month, String day) {
+    public Page<ReportDto> findPage(IPage<ReportDto> page, Long[] ids, Integer year, Integer month, Integer day) {
         LoginUser currentUser = UserUtil.getCurrentUser();
         Page<ReportDto> reportPage = new Page<>();
         //查询场景下的所有设备信息
@@ -77,7 +77,7 @@ public class YearReportService extends ServiceImpl<YearReportMapper, YearReportE
             devices.addAll(deviceService.selectList(deviceDto));
         }
         YearReportEntity yearReportEntity = new YearReportEntity();
-        yearReportEntity.setYear(Integer.valueOf(year));
+        yearReportEntity.setYear(year);
         yearReportEntity.setTenantId(currentUser.getTenantId());
         yearReportEntity.setParentSceneIds(ids);
         yearReportEntity.setDeviceIds(devices);

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

@@ -176,6 +176,6 @@
         <if test="report.day != null ">and c.`DAY`=#{report.day}</if>
         <if test="report.hour != null ">and c.`HOUR`=#{report.hour}</if>
         <if test="report.parentSceneId != null ">and c.PARENT_SCENE_ID=#{report.parentSceneId}</if>
-        and (b.ATTRIBUTE_TYPE =3 or b.ATTRIBUTE_TYPE =4 or b.ATTRIBUTE_TYPE =5 or b.ATTRIBUTE_TYPE !=6)) tab
+        and (b.ATTRIBUTE_TYPE =3 or b.ATTRIBUTE_TYPE =4 or b.ATTRIBUTE_TYPE =5 or b.ATTRIBUTE_TYPE =6)) tab
     </select>
 </mapper>

+ 7 - 19
sms_water/src/main/resources/mapper/MonitorInfoMapper.xml

@@ -94,33 +94,21 @@
             <if test="monitorInfo.monitorType != null ">and a.monitor_type = #{monitorInfo.monitorType}</if>
         </where>
     </select>
-    <select id="findAttributeList" resultType="com.huaxu.dto.ReportAttributeDto">
-        SELECT tab.attributeName,tab.ATTRIBUTE_TYPE,min(tab.minValue) as "minValue",
-        max(tab.maxValue) as "maxValue",AVG(tab.avgValue)as "avgValue",
-        AVG(tab.sumValue)as "sumValue",AVG(tab.latestValue) as "latestValue"
-        from
-        (SELECT IFNULL(a.REMARK,b.`NAME`) AS "attributeName",
+    <select id="findAttributeList" resultType="com.huaxu.dto.SceneDeviceAttributeDto">
+       SELECT IFNULL(a.REMARK,b.`NAME`) AS "attributeName",
         c.min_value as "minValue" ,c.max_value as "maxValue" ,c.avg_value as "avgValue" ,
         c.sum_value as "sumValue" ,c.latest_value as "latestValue",
-        b.ATTRIBUTE_TYPE
+        b.ATTRIBUTE_TYPE as "attributeType"
         FROM sms_device_parm a
         INNER JOIN sms_device_attribute b on a.ATTRIBUTE_ID=b.ID
         INNER JOIN sms_day_report c on a.ATTRIBUTE_ID=c.ATTRIBUTE_ID and a.DEVICE_ID=c.DEVICE_ID
         where
-        a.IS_REPORT=1
-        <if test="report.deviceIds != null and report.deviceIds.size() > 0">
-            and a.DEVICE_ID in
-            <foreach collection="report.deviceIds" item="dramaId" open="(" close=")" separator=",">
-                #{dramaId.id}
-            </foreach>
-        </if>
+        a.DEVICE_ID = #{report.deviceId}
         <if test="report.year != null ">and c.`YEAR`=#{report.year}</if>
         <if test="report.month != null ">and c.`MONTH`=#{report.month}</if>
         <if test="report.day != null ">and c.`DAY`=#{report.day}</if>
-        <if test="report.hour != null ">and c.`HOUR`=#{report.hour}</if>
-        <if test="report.parentSceneId != null ">and c.PARENT_SCENE_ID=#{report.parentSceneId}</if>
-        ORDER BY a.SEQ
-        ) as tab
-        group by tab.attributeName,tab.ATTRIBUTE_TYPE
+        and (b.ATTRIBUTE_TYPE =3 or b.ATTRIBUTE_TYPE =4 or b.ATTRIBUTE_TYPE =5 or b.ATTRIBUTE_TYPE =6)
+        ORDER BY c.collect_date desc
+        limit 1
     </select>
 </mapper>

+ 3 - 3
sms_water/src/main/resources/mapper/MonthReportMapper.xml

@@ -126,7 +126,7 @@
         b.ATTRIBUTE_TYPE
         FROM sms_device_parm a
         INNER JOIN sms_device_attribute b on a.ATTRIBUTE_ID=b.ID
-        INNER JOIN sms_day_report c on a.ATTRIBUTE_ID=c.ATTRIBUTE_ID and a.DEVICE_ID=c.DEVICE_ID
+        INNER JOIN sms_month_report c on a.ATTRIBUTE_ID=c.ATTRIBUTE_ID and a.DEVICE_ID=c.DEVICE_ID
         where
         a.IS_REPORT=1
         <if test="report.deviceIds != null and report.deviceIds.size() > 0">
@@ -160,7 +160,7 @@
         c.latest_value end as "drugUsage"
         FROM sms_device_parm a
         INNER JOIN sms_device_attribute b on a.ATTRIBUTE_ID=b.ID
-        INNER JOIN sms_day_report c on a.ATTRIBUTE_ID=c.ATTRIBUTE_ID and a.DEVICE_ID=c.DEVICE_ID
+        INNER JOIN sms_month_report c on a.ATTRIBUTE_ID=c.ATTRIBUTE_ID and a.DEVICE_ID=c.DEVICE_ID
         where
         <if test="report.deviceIds != null and report.deviceIds.size() > 0">
             a.DEVICE_ID in
@@ -172,6 +172,6 @@
         <if test="report.month != null ">and c.`MONTH`=#{report.month}</if>
         <if test="report.day != null ">and c.`DAY`=#{report.day}</if>
         <if test="report.parentSceneId != null ">and c.PARENT_SCENE_ID=#{report.parentSceneId}</if>
-        and (b.ATTRIBUTE_TYPE =3 or b.ATTRIBUTE_TYPE =4 or b.ATTRIBUTE_TYPE =5 or b.ATTRIBUTE_TYPE !=6)) tab
+        and (b.ATTRIBUTE_TYPE =3 or b.ATTRIBUTE_TYPE =4 or b.ATTRIBUTE_TYPE =5 or b.ATTRIBUTE_TYPE =6)) tab
     </select>
 </mapper>

+ 3 - 3
sms_water/src/main/resources/mapper/YearReportMapper.xml

@@ -124,7 +124,7 @@
         b.ATTRIBUTE_TYPE
         FROM sms_device_parm a
         INNER JOIN sms_device_attribute b on a.ATTRIBUTE_ID=b.ID
-        INNER JOIN sms_day_report c on a.ATTRIBUTE_ID=c.ATTRIBUTE_ID and a.DEVICE_ID=c.DEVICE_ID
+        INNER JOIN sms_year_report c on a.ATTRIBUTE_ID=c.ATTRIBUTE_ID and a.DEVICE_ID=c.DEVICE_ID
         where
         a.IS_REPORT=1
         <if test="report.deviceIds != null and report.deviceIds.size() > 0">
@@ -157,7 +157,7 @@
         c.latest_value end as "drugUsage"
         FROM sms_device_parm a
         INNER JOIN sms_device_attribute b on a.ATTRIBUTE_ID=b.ID
-        INNER JOIN sms_day_report c on a.ATTRIBUTE_ID=c.ATTRIBUTE_ID and a.DEVICE_ID=c.DEVICE_ID
+        INNER JOIN sms_year_report c on a.ATTRIBUTE_ID=c.ATTRIBUTE_ID and a.DEVICE_ID=c.DEVICE_ID
         where
         <if test="report.deviceIds != null and report.deviceIds.size() > 0">
             a.DEVICE_ID in
@@ -168,6 +168,6 @@
         <if test="report.year != null ">and c.`YEAR`=#{report.year}</if>
         <if test="report.month != null ">and c.`MONTH`=#{report.month}</if>
         <if test="report.parentSceneId != null ">and c.PARENT_SCENE_ID=#{report.parentSceneId}</if>
-        and (b.ATTRIBUTE_TYPE =3 or b.ATTRIBUTE_TYPE =4 or b.ATTRIBUTE_TYPE =5 or b.ATTRIBUTE_TYPE !=6)) tab
+        and (b.ATTRIBUTE_TYPE =3 or b.ATTRIBUTE_TYPE =4 or b.ATTRIBUTE_TYPE =5 or b.ATTRIBUTE_TYPE =6)) tab
     </select>
 </mapper>