Ver código fonte

修复已知BUG

lihui007 4 anos atrás
pai
commit
acb81aa7f3

+ 1 - 0
sms_water/src/main/java/com/huaxu/entity/ReportWaterPumpStateEntity.java

@@ -81,4 +81,5 @@ public class ReportWaterPumpStateEntity {
         return Md5Util.hash(this.tenantId + this.deviceId + this.deviceCode + this.attributeId + this.year + this.month);
     }
 
+
 }

+ 34 - 9
sms_water/src/main/java/com/huaxu/rabbitmq/ReportWaterPumpStateHandler.java

@@ -1,8 +1,12 @@
 package com.huaxu.rabbitmq;
 
+import cn.hutool.core.collection.CollectionUtil;
 import com.alibaba.fastjson.JSONObject;
+import com.huaxu.dao.DeviceAttributeSpecsMapper;
 import com.huaxu.dao.ReportWaterPumpStateMapper;
+import com.huaxu.dto.AlarmDetailsDto;
 import com.huaxu.dto.watePump.ReprotWaterPumpQueryDto;
+import com.huaxu.entity.DeviceAttributeSpecsEntity;
 import com.huaxu.entity.MonitorDataEntity;
 import com.huaxu.entity.MonitorDataValueEntity;
 import com.huaxu.entity.ReportWaterPumpStateEntity;
@@ -14,9 +18,8 @@ import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Component;
 
 import java.math.BigDecimal;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
+import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * @ClassName ReportWaterPumpStateHandler
@@ -36,28 +39,40 @@ public class ReportWaterPumpStateHandler {
     @Autowired
     private ReportWaterPumpStateMapper reportWaterPumpStateMapper;
 
+    @Autowired
+    private DeviceAttributeSpecsMapper deviceAttributeSpecsMapper;
+
     @Async
     public void handler(MonitorDataEntity monitorDataEntity, JSONObject receiveData, Date receiveDateTime){
         log.info("【水泵运行状态报表:{}】开始处理", monitorDataEntity.getDeviceCode());
-        BigDecimal stateValue = null;
+        // 0 运行, 1停止
+        int state = 0;
         // 执行批量插入操作
+        DeviceAttributeSpecsEntity entity = new DeviceAttributeSpecsEntity();
         List<ReportWaterPumpStateEntity> insertList = new ArrayList<>();
         ReprotWaterPumpQueryDto queryDto = getReprotWaterPumpQueryDto(monitorDataEntity);
         // 转换常量,保证获取是同一个对象
         // 避免同一时间内同一个code上报多条数据,保证数据更新和插入一致性
+        // TODO 需要采用分布式锁解决,避免部署多台服务器
         synchronized (monitorDataEntity.getDeviceCode().intern()) {
             for (MonitorDataValueEntity valueEntity : monitorDataEntity.getDataValues()) {
                 if (!receiveData.containsKey(valueEntity.getIdentifier())){
                     continue;
                 }
-                stateValue  = new BigDecimal(receiveData.getDouble(valueEntity.getIdentifier()));
-                // 目前不知道哪些Identifier属性的值来表示state,所以统一用0或者1表示
-                if (stateValue == null || (stateValue.compareTo(new BigDecimal("0")) != 0 &&
-                        stateValue.compareTo(new BigDecimal("1")) != 0)) {
+                entity.setAttributeId(valueEntity.getAttributeId());
+                // 查找属性ID是否有设置运行、停止状态
+                List<DeviceAttributeSpecsEntity> list = deviceAttributeSpecsMapper.findList(entity);
+                if (list == null ) {
+                    continue;
+                }
+                Map<String,DeviceAttributeSpecsEntity> map = toMap(list);
+                if ((map.get("运行1") == null || map.get("停止1") == null)){
                     continue;
                 }
+                BigDecimal stateValue  = new BigDecimal(receiveData.getDouble(valueEntity.getIdentifier()));
+                state = stateValue.toString().equals(map.get("运行1").getSpecsValue()) ? 0 : 1;
                 queryDto.setAttributeId(valueEntity.getAttributeId());
-                addOrUpdate(queryDto.getMd5Query(), monitorDataEntity, valueEntity, insertList, stateValue.intValue(), receiveDateTime);
+                addOrUpdate(queryDto.getMd5Query(), monitorDataEntity, valueEntity, insertList, state, receiveDateTime);
             }
             // 最后一次全部插入
             if (insertList.size() > 0 ){
@@ -67,6 +82,16 @@ public class ReportWaterPumpStateHandler {
         log.info("【水泵运行状态报表:{}】结束处理,新增:{}", monitorDataEntity.getDeviceCode(),insertList.size());
     }
 
+    private Map<String,DeviceAttributeSpecsEntity> toMap(List<DeviceAttributeSpecsEntity> list){
+         String key = null;
+         Map<String,DeviceAttributeSpecsEntity> map = new HashMap<>();
+         for (DeviceAttributeSpecsEntity deviceAttributeSpecsEntity: list) {
+            key = deviceAttributeSpecsEntity.getSpecsName() + deviceAttributeSpecsEntity.getStatus();
+            map.put(key, deviceAttributeSpecsEntity);
+         }
+         return map;
+    }
+
     /**
     * @Author lihui
     * @Description 如果查询到数据,根据返回的state来判断数据

+ 100 - 49
sms_water/src/main/java/com/huaxu/service/impl/MonitorDataReportServiceImpl.java

@@ -146,7 +146,11 @@ public class MonitorDataReportServiceImpl implements MonitorDataReportService {
         List<MonitorDataChartReportDeviceDto> monitorDataChartReportDeviceDtos = monitorDataReportMapper.DeviceAlarmReport(dateType,sceneIds,beginDate,endDate,
                 loginUser.getTenantId(),loginUser.getType(),loginUser.getPermissonType(),loginUser.getProgramItemList());
 
-        FillLoseData(monitorDataChartReportDeviceDtos,type,year,month,day);
+        if (monitorDataChartReportDeviceDtos != null && type == 4){
+            FillLoseData(startTime, endTime, monitorDataChartReportDeviceDtos);
+        } else  if (monitorDataChartReportDeviceDtos != null){
+            FillLoseData(monitorDataChartReportDeviceDtos,type,year,month,day);
+        }
         return monitorDataChartReportDeviceDtos;
     }
 
@@ -215,7 +219,8 @@ public class MonitorDataReportServiceImpl implements MonitorDataReportService {
         // 其他场景需要转换为单位“万”
         for(MonitorDataChartReportDeviceDto monitorDataChartReportDeviceDto : monitorDataChartReportDeviceDtos){
             for(MonitorDataChartReportAttributeDto monitorDataChartReportAttributeDto:monitorDataChartReportDeviceDto.getAttributeData()){
-                monitorDataChartReportAttributeDto.setUnit("万"+monitorDataChartReportAttributeDto.getUnit() == null ? "" : monitorDataChartReportAttributeDto.getUnit());
+                String unit = monitorDataChartReportAttributeDto.getUnit() == null ? "" : monitorDataChartReportAttributeDto.getUnit();
+                monitorDataChartReportAttributeDto.setUnit("万"+ unit);
                 for(MonitorDataChartReportValueDto monitorDataChartReportValueDto: monitorDataChartReportAttributeDto.getMonitorDataChartReportValue()){
                     monitorDataChartReportValueDto.setData(new BigDecimal(monitorDataChartReportValueDto.getData()).divide(BigDecimal.valueOf(10000),3, RoundingMode.HALF_UP).doubleValue());
                     monitorDataChartReportValueDto.setMonitorData(monitorDataChartReportValueDto.getData()+monitorDataChartReportAttributeDto.getUnit());
@@ -360,58 +365,20 @@ public class MonitorDataReportServiceImpl implements MonitorDataReportService {
                             maxValue=12;
                         }
                         for(int i=0;i<maxValue;i++){
-                            if(i == monitorDataChartReportValueDtos.size() || !monitorDataChartReportValueDtos.get(i).getDateLabel().equals(i+growingBase)){
+                            if ( i == monitorDataChartReportValueDtos.size() || !monitorDataChartReportValueDtos.get(i).getDateLabel().equals(i+growingBase)){
                                 MonitorDataChartReportValueDto monitorDataChartReportValueDto = new MonitorDataChartReportValueDto();
                                 monitorDataChartReportValueDto.setData(0.0);
-                                LocalDateTime dateTime=localDateTime.now();
-                                if(type != null && type ==3){
-                                    dateTime= localDateTime.plusHours(i);
-                                    monitorDataChartReportValueDto.setDateStringLabel(
-                                            dateTime.getYear()+"-"+
-                                                    String.format("%02d",dateTime.getMonthValue())+"-"+
-                                                    String.format("%02d",dateTime.getDayOfMonth())+" "+
-                                                    String.format("%02d",dateTime.getHour())+":00");
-                                }
-                                if(type != null && type ==2){
-                                    dateTime= localDateTime.plusDays(i);
-                                    monitorDataChartReportValueDto.setDateStringLabel(
-                                            dateTime.getYear()+"-"+
-                                                    String.format("%02d",dateTime.getMonthValue())+"-"+
-                                                    String.format("%02d",dateTime.getDayOfMonth()));
-                                }
-                                if(type != null && type ==1){
-                                    dateTime= localDateTime.plusMonths(i);
-                                    monitorDataChartReportValueDto.setDateStringLabel(
-                                            dateTime.getYear()+"-"+
-                                                    String.format("%02d",dateTime.getMonthValue()));
-                                }
+                                LocalDateTime dateTime = localDateTime.now();
+                                setDateStringLabel(localDateTime, dateTime, i, monitorDataChartReportValueDto, type, true);
                                 monitorDataChartReportValueDto.setDate(dateTime);
-
                                 monitorDataChartReportValueDto.setDateLabel(i+growingBase);
                                 monitorDataChartReportValueDto.setMonitorData("");
                                 monitorDataChartReportValueDtos.add(i,monitorDataChartReportValueDto);
-                            }else{
+                            } else {
                                 MonitorDataChartReportValueDto monitorDataChartReportValueDto =monitorDataChartReportValueDtos.get(i);
                                 monitorDataChartReportValueDto.setMonitorData(monitorDataChartReportValueDto.getData()+""+unit);
                                 LocalDateTime dateTime = monitorDataChartReportValueDto.getDate();
-                                if(type != null && type ==3){
-                                    monitorDataChartReportValueDto.setDateStringLabel(
-                                            dateTime.getYear()+"-"+
-                                                    String.format("%02d",dateTime.getMonthValue())+"-"+
-                                                    String.format("%02d",dateTime.getDayOfMonth())+" "+
-                                                    String.format("%02d",dateTime.getHour())+":00");
-                                }
-                                if(type != null && type ==2){
-                                    monitorDataChartReportValueDto.setDateStringLabel(
-                                            dateTime.getYear()+"-"+
-                                                    String.format("%02d",dateTime.getMonthValue())+"-"+
-                                                    String.format("%02d",dateTime.getDayOfMonth()));
-                                }
-                                if(type != null && type ==1){
-                                    monitorDataChartReportValueDto.setDateStringLabel(
-                                            dateTime.getYear()+"-"+
-                                                    String.format("%02d",dateTime.getMonthValue()));
-                                }
+                                setDateStringLabel(localDateTime, dateTime, i , monitorDataChartReportValueDto, type, false);
                             }
                         }
                     }
@@ -420,6 +387,81 @@ public class MonitorDataReportServiceImpl implements MonitorDataReportService {
         }
     }
 
+    private static void FillLoseData(List<MonitorDataChartReportDeviceDto> monitorDataChartReportDeviceDtos,Integer type,Integer year,Integer month
+                ,int startTime, int endTime) {
+        LocalDateTime localDateTime = LocalDateTime.of(year, month,1,0,0,0);
+        for(MonitorDataChartReportDeviceDto monitorDataChartReportDeviceDto: monitorDataChartReportDeviceDtos){
+            if(monitorDataChartReportDeviceDto == null || monitorDataChartReportDeviceDto.getAttributeData() == null) {
+                continue;
+            }
+            for(MonitorDataChartReportAttributeDto monitorDataChartReportAttributeDto:monitorDataChartReportDeviceDto.getAttributeData()){
+                if (monitorDataChartReportAttributeDto == null ||
+                        monitorDataChartReportAttributeDto.getMonitorDataChartReportValue() == null) {
+                    continue;
+                }
+                Integer growingBase = 1;
+
+                String unit = monitorDataChartReportAttributeDto.getUnit();
+                unit = StringUtils.isNotBlank(unit) ? unit : "";
+                List<MonitorDataChartReportValueDto> monitorDataChartReportValueDtos = monitorDataChartReportAttributeDto.getMonitorDataChartReportValue();
+                Map<String,MonitorDataChartReportValueDto> mmp = toMap(monitorDataChartReportValueDtos);
+                for (int i = startTime; i <= endTime; i++ ) {
+                    MonitorDataChartReportValueDto monitorDataChartReportValueDto =  mmp.get(year + "" + month + "" + i);
+                    if (monitorDataChartReportValueDto == null){
+                        monitorDataChartReportValueDto = new MonitorDataChartReportValueDto();
+                        monitorDataChartReportValueDto.setData(0.0);
+                        LocalDateTime dateTime = localDateTime.now();
+                        setDateStringLabel(localDateTime, dateTime, i-1, monitorDataChartReportValueDto, type, true);
+                        monitorDataChartReportValueDto.setDate(dateTime);
+                        monitorDataChartReportValueDto.setDateLabel(i);
+                        monitorDataChartReportValueDto.setMonitorData("");
+                        monitorDataChartReportValueDtos.add(growingBase-1,monitorDataChartReportValueDto);
+                    } else {
+                        monitorDataChartReportValueDto.setMonitorData(monitorDataChartReportValueDto.getData()+""+unit);
+                        LocalDateTime dateTime = monitorDataChartReportValueDto.getDate();
+                        setDateStringLabel(localDateTime, dateTime, i , monitorDataChartReportValueDto, type, false);
+                    }
+                    growingBase++;
+                }
+            }
+        }
+    }
+
+    private static Map<String, MonitorDataChartReportValueDto> toMap (List<MonitorDataChartReportValueDto> monitorDataChartReportValueDtos) {
+        LocalDateTime localDateTime = null;
+        Map<String, MonitorDataChartReportValueDto> result = new HashMap<>();
+        for (MonitorDataChartReportValueDto monitorDataChartReportValueDto:monitorDataChartReportValueDtos) {
+            if (StringUtils.isEmpty(monitorDataChartReportValueDto.getDateStringLabel())) {
+                localDateTime = monitorDataChartReportValueDto.getDate();
+            } else {
+                localDateTime = LocalDateTime.parse(monitorDataChartReportValueDto.getDateStringLabel() + " 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
+            }
+            result.put(localDateTime.getYear() + "" +  localDateTime.getMonthValue() +"" + localDateTime.getDayOfMonth() , monitorDataChartReportValueDto);
+        }
+        return result;
+    }
+
+    private static void setDateStringLabel(LocalDateTime localDateTime,LocalDateTime dateTime, int i , MonitorDataChartReportValueDto monitorDataChartReportValueDto,
+                    Integer type, boolean isNull) {
+        if (type == null) {
+            return;
+        }
+        if(type ==3){
+            dateTime= isNull ? localDateTime.plusHours(i) : dateTime;
+            monitorDataChartReportValueDto.setDateStringLabel(
+                    dateTime.getYear()+"-"+ String.format("%02d",dateTime.getMonthValue())+"-"+  String.format("%02d",dateTime.getDayOfMonth())
+                            + " "+String.format("%02d",dateTime.getHour())+":00");
+        }
+        if(type ==2){
+            dateTime = isNull ? localDateTime.plusDays(i): dateTime;
+            monitorDataChartReportValueDto.setDateStringLabel(dateTime.getYear()+"-" + String.format("%02d",dateTime.getMonthValue())+
+                    "-"+ String.format("%02d",dateTime.getDayOfMonth()));
+        }
+        if (type ==1) {
+            dateTime = isNull ? localDateTime.plusMonths(i) : dateTime;
+            monitorDataChartReportValueDto.setDateStringLabel(dateTime.getYear()+"-" + String.format("%02d", dateTime.getMonthValue()));
+        }
+    }
 
 
     @Override
@@ -1410,9 +1452,15 @@ public class MonitorDataReportServiceImpl implements MonitorDataReportService {
     private void FillLoseData(String startTime, String endTime,List<MonitorDataChartReportDeviceDto> monitorDataChartReportDeviceDtos){
         LocalDateTime  startDate =  LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
         LocalDateTime  endDate   =  LocalDateTime.parse(endTime,   DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
+        int startDay = 0;
+        int endDay   = 0;
         if (startDate.getYear() == endDate.getYear()){
-            for (int i = startDate.getMonth().getValue();i <= endDate.getMonth().getValue(); i++){
-                FillLoseData(monitorDataChartReportDeviceDtos,2, startDate.getYear(), i, 1);
+            boolean sameMonth = startDate.getMonth().getValue() == endDate.getMonth().getValue();
+            for (int i = startDate.getMonth().getValue(); i <= endDate.getMonth().getValue(); i++){
+                startDay = (sameMonth || i == startDate.getMonth().getValue()) ? startDate.getDayOfMonth() : 1;
+                endDay   = sameMonth  || i == endDate.getMonth().getValue() ? endDate.getDayOfMonth() :
+                        endDate.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
+                FillLoseData(monitorDataChartReportDeviceDtos,2, startDate.getYear(), i, startDay, endDay);
             }
         } else {
             int start = 0;
@@ -1420,8 +1468,11 @@ public class MonitorDataReportServiceImpl implements MonitorDataReportService {
             for (int i = startDate.getYear(); i <= endDate.getYear(); i++){
                 start = i == startDate.getYear() ? startDate.getMonth().getValue() : 1;
                 end   = i == endDate.getYear()   ? endDate.getMonthValue() : 12;
-                for (int m = start;m <= end; m++){
-                    FillLoseData(monitorDataChartReportDeviceDtos,2, i , m,1);
+                for (int m = start; m <= end; m++){
+                    startDay =  i == startDate.getYear() && m == start ? startDate.getDayOfMonth() : 1;
+                    endDay   =  i == endDate.getYear() && m ==  endDate.getMonth().getValue() ?
+                            endDate.getDayOfMonth() : startDate.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
+                    FillLoseData(monitorDataChartReportDeviceDtos,2, i, m, startDay, endDay);
                 }
             }
         }

+ 2 - 0
sms_water/src/main/resources/mapper/MonitorDataReportMapper.xml

@@ -17,6 +17,8 @@
 
     <resultMap id="MonitorDataChartReportValue" type="com.huaxu.dto.MonitorDataChartReportValueDto">
         <result property="dateLabel" column="dateLabel" />
+        <result property="dateStringLabel" column="dateStringLabel"/>
+        <result property="monitorData" column="monitorData"/>
         <result property="data" column="data"/>
         <result property="date" column="date"/>
     </resultMap>