| | |
| | | package com.moral.api.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.moral.api.entity.HistoryHourly; |
| | | import com.moral.api.entity.Sensor; |
| | | import com.moral.api.mapper.HistoryHourlyMapper; |
| | | import com.moral.api.mapper.HistoryMinutelyMapper; |
| | | import com.moral.api.service.HistoryHourlyService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.moral.api.service.SensorService; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.constant.RedisConstants; |
| | | import com.moral.util.AmendUtils; |
| | | import com.moral.util.DateUtils; |
| | | |
| | | 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.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.OptionalDouble; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.DoubleStream; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | @Service |
| | | public class HistoryHourlyServiceImpl extends ServiceImpl<HistoryHourlyMapper, HistoryHourly> implements HistoryHourlyService { |
| | | |
| | | @Autowired |
| | | private HistoryHourlyMapper historyHourlyMapper; |
| | | |
| | | @Autowired |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | @Autowired |
| | | private HistoryMinutelyMapper historyMinutelyMapper; |
| | | |
| | | @Autowired |
| | | private SensorService sensorService; |
| | | |
| | | @Override |
| | | public void insertHistoryHourly() { |
| | | //时间格式化:yyyy-MM-dd HH:mm |
| | | String format = DateUtils.yyyy_MM_dd_HH_EN; |
| | | |
| | | //redis获取所有设备mac |
| | | Set<String> macs = redisTemplate.opsForHash().keys(RedisConstants.DEVICE); |
| | | //当前时间,到小时 |
| | | Date now = new Date(); |
| | | String time = DateUtils.dateToDateString(now, format) + ":00:00"; |
| | | |
| | | QueryWrapper<HistoryHourly> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("time", time); |
| | | //查询数据库当前小时的条数,如果比macs小,说明需要数据补充 |
| | | Integer count = historyHourlyMapper.selectCount(queryWrapper); |
| | | if (macs.size() > count) { |
| | | macs.removeIf(mac -> { |
| | | queryWrapper.clear(); |
| | | queryWrapper.eq("time", time); |
| | | queryWrapper.eq("mac", mac); |
| | | Integer num = historyHourlyMapper.selectCount(queryWrapper); |
| | | return num != 0; |
| | | }); |
| | | } |
| | | |
| | | Map<String, Object> params = new HashMap<>(); |
| | | |
| | | //开始时间 |
| | | String dateString = DateUtils.getDateStringOfHour(-1, format); |
| | | Date start = DateUtils.getDate(dateString, format); |
| | | |
| | | //结束时间 |
| | | Date end = DateUtils.dataToTimeStampTime(now, format); |
| | | |
| | | //从分钟表获取数据的分钟表后缀 |
| | | String timeUnits = DateUtils.dateToDateString(start, DateUtils.yyyyMM_EN); |
| | | |
| | | //因子 |
| | | QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>(); |
| | | sensorQueryWrapper.select("code").eq("is_delete", Constants.NOT_DELETE); |
| | | List<Object> sensorCodes = sensorService.listObjs(sensorQueryWrapper); |
| | | |
| | | params.put("timeUnits", timeUnits); |
| | | params.put("start", start); |
| | | params.put("end", end); |
| | | params.put("macs", macs); |
| | | //获取所有设备的1小时内的数据 |
| | | List<Map<String, Object>> hourlyData = historyMinutelyMapper.getHistoryMinutelyData(params); |
| | | if (hourlyData.size() == 0) { |
| | | return; |
| | | } |
| | | |
| | | //按mac分组 |
| | | Map<String, List<Map<String, Object>>> data = hourlyData.parallelStream() |
| | | .collect(Collectors.groupingBy(o -> (String) o.get("mac"))); |
| | | |
| | | //存入数据库的结果集 |
| | | List<HistoryHourly> insertData = new ArrayList<>(); |
| | | |
| | | data.forEach((key, value) -> { |
| | | HistoryHourly historyHourly = new HistoryHourly(); |
| | | historyHourly.setMac(key); |
| | | historyHourly.setTime(end); |
| | | Map<String, Object> jsonMap = new HashMap<>(); |
| | | |
| | | //风向均值计算并修约 |
| | | Object windDirAvg = AmendUtils.getWindDirAvg(value); |
| | | if (windDirAvg != null) { |
| | | jsonMap.put(Constants.SENSOR_CODE_WIND_DIR, windDirAvg); |
| | | } |
| | | |
| | | //除风向外其他因子均值计算 |
| | | sensorCodes.forEach(sensorCode -> { |
| | | OptionalDouble optionalDouble = value.parallelStream() |
| | | .flatMapToDouble(v -> { |
| | | Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class); |
| | | Object sensorValue = dataValue.get(sensorCode.toString()); |
| | | if (ObjectUtils.isEmpty(sensorValue)) { |
| | | return null; |
| | | } |
| | | //风向另外计算 |
| | | if (Constants.SENSOR_CODE_WIND_DIR.equals(sensorCode)) { |
| | | return null; |
| | | } |
| | | return DoubleStream.of(Double.parseDouble(sensorValue.toString())); |
| | | }).average(); |
| | | if (optionalDouble.isPresent()) { |
| | | //银行家算法修约 |
| | | double sciCal = AmendUtils.sciCal(optionalDouble.getAsDouble(), 4); |
| | | jsonMap.put(sensorCode.toString(), sciCal); |
| | | } |
| | | }); |
| | | historyHourly.setValue(JSONObject.toJSONString(jsonMap)); |
| | | historyHourly.setVersion((Integer) value.get(0).get("version")); |
| | | insertData.add(historyHourly); |
| | | }); |
| | | |
| | | //存入小时表 |
| | | historyHourlyMapper.insertHistoryHourly(insertData); |
| | | } |
| | | } |