|  |  | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.segments.NormalSegmentList; | 
 |  |  | import com.moral.api.config.mybatis.MybatisPlusConfig; | 
 |  |  | import com.moral.api.entity.Device; | 
 |  |  | import com.moral.api.entity.HistoryFiveMinutely; | 
 |  |  | import com.moral.api.entity.HistoryHourly; | 
 |  |  | import com.moral.api.entity.MonitorPoint; | 
 |  |  | import com.moral.api.entity.*; | 
 |  |  | import com.moral.api.mapper.HistoryFiveMinutelyMapper; | 
 |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
 |  |  | import com.moral.api.pojo.dto.Wind.WindData; | 
 |  |  | import com.moral.api.pojo.dto.historyFiveMinutely.DeviceAndFiveMinuteDataDTO; | 
 |  |  | import com.moral.api.pojo.form.device.MonitorPointQueryForm; | 
 |  |  | import com.moral.api.pojo.form.historyFiveMinutely.QueryDeviceAndFiveMinuteDataForm; | 
 |  |  | import com.moral.api.service.DeviceService; | 
 |  |  | import com.moral.api.service.HistoryFiveMinutelyService; | 
 |  |  | import com.moral.api.service.MonitorPointService; | 
 |  |  | import com.moral.api.utils.UnitConvertUtils; | 
 |  |  | import com.moral.constant.Constants; | 
 |  |  | import com.moral.constant.RedisConstants; | 
 |  |  | import com.moral.constant.SeparateTableType; | 
 |  |  | import com.moral.util.DateUtils; | 
 |  |  | 
 |  |  |     RedisTemplate redisTemplate; | 
 |  |  |     @Autowired | 
 |  |  |     HistoryFiveMinutelyMapper historyFiveMinutelyMapper; | 
 |  |  |     @Autowired | 
 |  |  |     DeviceService deviceService; | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public List<DeviceAndFiveMinuteDataDTO> queryDeviceAndFiveMinuteData(QueryDeviceAndFiveMinuteDataForm form) { | 
 |  |  | 
 |  |  |         List<Device> devices = new ArrayList<>(); | 
 |  |  |         for (MonitorPoint monitorPoint : monitorPoints) { | 
 |  |  |             List<Device> monitorPointDevices = monitorPoint.getDevices(); | 
 |  |  |             if (!ObjectUtils.isEmpty(monitorPointDevices)) | 
 |  |  |                 devices.addAll(monitorPointDevices); | 
 |  |  |             if (!ObjectUtils.isEmpty(monitorPointDevices)) { | 
 |  |  |                 //过滤掉线设备 | 
 |  |  |                 for (Device device : monitorPointDevices) { | 
 |  |  |                     if (!device.getState().equals(Constants.DEVICE_STATE_OFFLINE)) | 
 |  |  |                         devices.add(device); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |         } | 
 |  |  |         //查询所有设备对应的数据 | 
 |  |  |         List<DeviceAndFiveMinuteDataDTO> dtos = new ArrayList<>(); | 
 |  |  |         for (Device device : devices) { | 
 |  |  |             DeviceAndFiveMinuteDataDTO dto = new DeviceAndFiveMinuteDataDTO(); | 
 |  |  |             String mac = device.getMac(); | 
 |  |  |             //从缓存中获取 | 
 |  |  |             Map<String, Object> sensorValues = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DATA_FIVE_MINUTES, mac); | 
 |  |  |             //如果没有数据从数据库查询 | 
 |  |  |             if (ObjectUtils.isEmpty(sensorValues)) { | 
 |  |  |                 HistoryFiveMinutely dbHistoryFiveMinutely = queryLastDataByMac(mac); | 
 |  |  |                 if (ObjectUtils.isEmpty(dbHistoryFiveMinutely)) | 
 |  |  |                     continue; | 
 |  |  |                 else | 
 |  |  |                     sensorValues = JSON.parseObject(dbHistoryFiveMinutely.getValue(),Map.class); | 
 |  |  |             } | 
 |  |  |             Map<String, Object> sensorValues = queryLastDataByMac(mac); | 
 |  |  |             String dbDataStr = JSON.toJSONString(sensorValues); | 
 |  |  |             sensorValues = JSON.parseObject(dbDataStr, HashMap.class); | 
 |  |  |             Map<String, Object> sensorValue = new HashMap<>(); | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public HistoryFiveMinutely queryLastDataByMac(String mac) { | 
 |  |  |     public Map<String, Object> queryLastDataByMac(String mac) { | 
 |  |  |         //从缓存中获取 | 
 |  |  |         Map<String, Object> sensorValues = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DATA_FIVE_MINUTES, mac); | 
 |  |  |         //如果没有数据从数据库查询 | 
 |  |  |         if (sensorValues != null) | 
 |  |  |             return sensorValues; | 
 |  |  |         QueryWrapper<HistoryFiveMinutely> queryWrapper = new QueryWrapper<>(); | 
 |  |  |         queryWrapper.eq("mac", mac); | 
 |  |  |         queryWrapper.orderByDesc("time"); | 
 |  |  | 
 |  |  |         List<HistoryFiveMinutely> datas = multiTableQuery(queryWrapper, tableNames); | 
 |  |  |         if (ObjectUtils.isEmpty(datas)) | 
 |  |  |             return null; | 
 |  |  |         return datas.get(0); | 
 |  |  |         HistoryFiveMinutely historyFiveMinutely = datas.get(0); | 
 |  |  |         sensorValues = JSON.parseObject(historyFiveMinutely.getValue(), Map.class); | 
 |  |  |         sensorValues.put("dataTime", DateUtils.dateToDateString(historyFiveMinutely.getTime(), "yyyy-MM-dd HH:mm:ss")); | 
 |  |  |         return sensorValues; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  | 
 |  |  |         return getWindData(windData); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //获取五分钟弹窗数据 | 
 |  |  |     @Override | 
 |  |  |     public Map<String, Object> getPopDataByMac(String mac) { | 
 |  |  |         Map<String, Object> datas = queryLastDataByMac(mac); | 
 |  |  |         //构建返回对象 | 
 |  |  |         Map<String, Object> result = new HashMap<>(); | 
 |  |  |         //创建返回因子code集合 | 
 |  |  |         List<String> sensorCodes = Arrays.asList("a34004", "a34002", "a21005", "a21004", "a21026", "a05024", "a99054"); | 
 |  |  |         //获取设备信息 | 
 |  |  |         Device device = deviceService.getDeviceUnitAlramInforByMac(mac); | 
 |  |  |         //转换数据单位和名称 | 
 |  |  |         for (Sensor sensor : device.getVersion().getSensors()) { | 
 |  |  |             if (!sensorCodes.contains(sensor.getCode())) | 
 |  |  |                 continue; | 
 |  |  |             Object dataO = datas.get(sensor.getCode()); | 
 |  |  |             if (dataO != null) { | 
 |  |  |                 String data = String.valueOf(dataO); | 
 |  |  |                 //转换单位 | 
 |  |  |                 if (!sensor.getShowUnitKey().equals(sensor.getUnitKey()) ) | 
 |  |  |                     data = UnitConvertUtils.calculate(data, sensor.getFormula()); | 
 |  |  |                 //拼接单位 | 
 |  |  |                 data = data +" "+ sensor.getShowUnit(); | 
 |  |  |                 result.put(sensor.getCode(), data); | 
 |  |  |             }else{ | 
 |  |  |                 result.put(sensor.getCode(),"-"); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         //添加设备名称 | 
 |  |  |         result.put("name",device.getName()); | 
 |  |  |         //添加时间 | 
 |  |  |         result.put("dataTime",datas.get("dataTime")); | 
 |  |  |         //排序 | 
 |  |  |         return orderSixParam(result); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //对六参以及时间进行排序 | 
 |  |  |     private Map<String,Object> orderSixParam(Map<String,Object> data){ | 
 |  |  |         LinkedHashMap result = new LinkedHashMap(); | 
 |  |  |         Object PM2_5 = data.get("a34004"); | 
 |  |  |         Object PM10 = data.get("a34002"); | 
 |  |  |         Object SO2 = data.get("a21026"); | 
 |  |  |         Object NO2 = data.get("a21004"); | 
 |  |  |         Object CO = data.get("a21005"); | 
 |  |  |         Object O3 = data.get("a05024"); | 
 |  |  |         Object name = data.get("name"); | 
 |  |  |         Object time = data.get("dataTime"); | 
 |  |  |         if(name!=null) | 
 |  |  |             result.put("名称",name); | 
 |  |  |         if(PM2_5!=null) | 
 |  |  |             result.put("PM2.5",PM2_5); | 
 |  |  |         if(PM10!=null) | 
 |  |  |             result.put("PM10",PM10); | 
 |  |  |         if(SO2!=null) | 
 |  |  |             result.put("二氧化硫(SO₂)",SO2); | 
 |  |  |         if(NO2!=null) | 
 |  |  |             result.put("二氧化氮(NO₂)",NO2); | 
 |  |  |         if(CO!=null) | 
 |  |  |             result.put("一氧化碳(CO)",CO); | 
 |  |  |         if(O3!=null) | 
 |  |  |             result.put("臭氧(O₃)",O3); | 
 |  |  |         if(time!=null) | 
 |  |  |             result.put("时间",time); | 
 |  |  |         return result; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 根据输入的地点坐标计算中心点 |