| | |
| | | //从缓存中获取 |
| | | Map<String, Object> sensorValues = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DATA_FIVE_MINUTES, mac); |
| | | //如果没有数据从数据库查询 |
| | | HistoryFiveMinutely dbHistoryFiveMinutely = queryLastDataByMac(mac); |
| | | if(ObjectUtils.isEmpty(dbHistoryFiveMinutely)) |
| | | continue; |
| | | String dbDataStr = dbHistoryFiveMinutely.getValue(); |
| | | if (ObjectUtils.isEmpty(sensorValues)) { |
| | | HistoryFiveMinutely dbHistoryFiveMinutely = queryLastDataByMac(mac); |
| | | if (ObjectUtils.isEmpty(dbHistoryFiveMinutely)) |
| | | continue; |
| | | else |
| | | sensorValues = JSON.parseObject(dbHistoryFiveMinutely.getValue(),Map.class); |
| | | } |
| | | String dbDataStr = JSON.toJSONString(sensorValues); |
| | | sensorValues = JSON.parseObject(dbDataStr, HashMap.class); |
| | | Map<String,Object> sensorValue = new HashMap<>(); |
| | | if (sensorValues!=null&&sensorValues.get(sensorCode) != null) |
| | | sensorValue.put(sensorCode,sensorValues.get(sensorCode)); |
| | | Map<String, Object> sensorValue = new HashMap<>(); |
| | | if (sensorValues != null && sensorValues.get(sensorCode) != null) |
| | | sensorValue.put(sensorCode, sensorValues.get(sensorCode)); |
| | | else |
| | | sensorValue.put(sensorCode,null); |
| | | sensorValue.put(sensorCode, null); |
| | | dto.setDevice(device); |
| | | dto.setSensorValue(sensorValue); |
| | | dtos.add(dto); |
| | |
| | | @Override |
| | | public HistoryFiveMinutely queryLastDataByMac(String mac) { |
| | | QueryWrapper<HistoryFiveMinutely> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("mac",mac); |
| | | queryWrapper.eq("mac", mac); |
| | | queryWrapper.orderByDesc("time"); |
| | | queryWrapper.last("limit 0,1"); |
| | | List<HistoryFiveMinutely> historyFiveMinutelies = historyFiveMinutelyMapper.selectList(queryWrapper); |
| | | if(ObjectUtils.isEmpty(historyFiveMinutelies)) |
| | | //获取当月的表名 |
| | | Date date = new Date(); |
| | | List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(date, date, SeparateTableType.MONTH); |
| | | List<HistoryFiveMinutely> datas = multiTableQuery(queryWrapper, tableNames); |
| | | if (ObjectUtils.isEmpty(datas)) |
| | | return null; |
| | | return historyFiveMinutelies.get(0); |
| | | return datas.get(0); |
| | | } |
| | | |
| | | |
| | |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/9/23 |
| | | */ |
| | | public List<HistoryFiveMinutely> getValueByMacAndTime(String mac, Date startDate, Date endDate){ |
| | | public List<HistoryFiveMinutely> getValueByMacAndTime(String mac, Date startDate, Date endDate) { |
| | | QueryWrapper<HistoryFiveMinutely> wrapper = new QueryWrapper<>(); |
| | | wrapper.eq("mac",mac); |
| | | wrapper.between("time",startDate,endDate); |
| | | wrapper.eq("mac", mac); |
| | | wrapper.between("time", startDate, endDate); |
| | | List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(startDate, endDate, SeparateTableType.MONTH); |
| | | List<HistoryFiveMinutely> datas = multiTableQuery(wrapper, tableNames); |
| | | return datas; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @Description: 多表查询,传入表名集合,以及条件wrapper,返回数据 |
| | |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/9/23 |
| | | */ |
| | | private List<HistoryFiveMinutely> multiTableQuery(QueryWrapper<HistoryFiveMinutely> wrapper,List<String> tableNames){ |
| | | private List<HistoryFiveMinutely> multiTableQuery(QueryWrapper<HistoryFiveMinutely> wrapper, List<String> tableNames) { |
| | | List<HistoryFiveMinutely> result = new ArrayList<>(); |
| | | for (String tableName : tableNames) { |
| | | MybatisPlusConfig.tableName.set(tableName); |
| | | List<HistoryFiveMinutely> datas = historyFiveMinutelyMapper.selectList(wrapper); |
| | | result.addAll(datas); |
| | | } |
| | | MybatisPlusConfig.tableName.remove(); |
| | | return result; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } |