| | |
| | | package com.moral.api.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.ISqlSegment; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | 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.mapper.HistoryFiveMinutelyMapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | |
| | | import com.moral.api.service.HistoryFiveMinutelyService; |
| | | import com.moral.api.service.MonitorPointService; |
| | | import com.moral.constant.RedisConstants; |
| | | import com.moral.constant.SeparateTableType; |
| | | import com.moral.util.DateUtils; |
| | | |
| | | import com.moral.util.MybatisPLUSUtils; |
| | | import io.lettuce.core.GeoCoordinates; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | |
| | | @Autowired |
| | | MonitorPointService monitorPointService; |
| | | |
| | | @Autowired |
| | | RedisTemplate redisTemplate; |
| | | |
| | | @Autowired |
| | | private HistoryFiveMinutelyMapper historyFiveMinutelyMapper; |
| | | HistoryFiveMinutelyMapper historyFiveMinutelyMapper; |
| | | |
| | | @Override |
| | | public List<DeviceAndFiveMinuteDataDTO> queryDeviceAndFiveMinuteData(QueryDeviceAndFiveMinuteDataForm form) { |
| | |
| | | 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); |
| | | Map<String, Object> value = new HashMap<>(); |
| | | //如果没有数据从数据库查询 |
| | | 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) |
| | | value.put(sensorCode, sensorValues.get(sensorCode)); |
| | | sensorValue.put(sensorCode, sensorValues.get(sensorCode)); |
| | | else |
| | | value.put(sensorCode, null); |
| | | sensorValue.put(sensorCode, null); |
| | | dto.setDevice(device); |
| | | dto.setSensorValue(value); |
| | | dto.setSensorValue(sensorValue); |
| | | dtos.add(dto); |
| | | } |
| | | return dtos; |
| | | } |
| | | |
| | | @Override |
| | | public HistoryFiveMinutely queryLastDataByMac(String mac) { |
| | | QueryWrapper<HistoryFiveMinutely> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("mac", mac); |
| | | queryWrapper.orderByDesc("time"); |
| | | queryWrapper.last("limit 0,1"); |
| | | //获取当月的表名 |
| | | 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 datas.get(0); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public List<Object> getAreaWindData(Map<String, Object> params) { |
| | |
| | | list.add(laLaMap); |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 查询一段时间内某一mac的数据 |
| | | * @Param: [mac, startDate, endDate] |
| | | * @return: java.util.List<com.moral.api.entity.HistoryHourly> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/9/23 |
| | | */ |
| | | public List<HistoryFiveMinutely> getValueByMacAndTime(String mac, Date startDate, Date endDate) { |
| | | QueryWrapper<HistoryFiveMinutely> wrapper = new QueryWrapper<>(); |
| | | 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,返回数据 |
| | | * @Param: [wrapper, tableNames] |
| | | * @return: java.util.List<com.moral.api.entity.HistoryHourly> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/9/23 |
| | | */ |
| | | 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; |
| | | } |
| | | |
| | | |
| | | } |