kaiyu
2021-09-24 378c3936c402732a6c8dd84d018e48fe69ba7a03
screen-api
修改获取五分钟数据代码
2 files modified
44 ■■■■■ changed files
screen-api/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java 43 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java 1 ●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java
@@ -71,16 +71,20 @@
            //从缓存中获取
            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);
@@ -91,13 +95,16 @@
    @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);
    }
@@ -301,14 +308,15 @@
     * @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,返回数据
@@ -317,17 +325,16 @@
     * @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;
    }
}
screen-api/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java
@@ -83,6 +83,7 @@
            List<HistoryHourly> datas = historyHourlyMapper.selectList(wrapper);
            result.addAll(datas);
        }
        MybatisPlusConfig.tableName.remove();
        return result;
    }