| | |
| | | package com.moral.api.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.moral.api.entity.HistoryDaily; |
| | | import com.moral.api.entity.HistoryHourly; |
| | | import com.moral.api.entity.Sensor; |
| | | import com.moral.api.mapper.HistoryDailyMapper; |
| | | import com.moral.api.service.HistoryDailyService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.moral.api.service.HistoryHourlyService; |
| | | import com.moral.api.service.SensorService; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.util.AmendUtils; |
| | | import com.moral.util.DateUtils; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | 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.stream.Collectors; |
| | | import java.util.stream.DoubleStream; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | @Autowired |
| | | private HistoryDailyMapper historyDailyMapper; |
| | | |
| | | @Autowired |
| | | private SensorService sensorService; |
| | | |
| | | @Autowired |
| | | private HistoryHourlyService historyHourlyService; |
| | | |
| | | @Override |
| | | public void insertHistoryDaily(List<HistoryDaily> list) { |
| | | System.out.println(list); |
| | | historyDailyMapper.insertHistoryDaily(list); |
| | | public void insertHistoryDaily() { |
| | | String format = DateUtils.yyyy_MM_dd_EN; |
| | | Date now = new Date(); |
| | | //开始时间 |
| | | Date start = DateUtils.dataToTimeStampTime(DateUtils.getDateOfDay(now, -1), format); |
| | | //结束时间 |
| | | Date end = DateUtils.dataToTimeStampTime(now, format); |
| | | //因子 |
| | | QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>(); |
| | | sensorQueryWrapper.select("code").eq("is_delete", Constants.NOT_DELETE); |
| | | List<Object> sensorCodes = sensorService.listObjs(sensorQueryWrapper); |
| | | |
| | | //获取所有设备小时数据 |
| | | QueryWrapper<HistoryHourly> historyHourlyQueryWrapper = new QueryWrapper<>(); |
| | | historyHourlyQueryWrapper.ge("time", DateUtils.dateToDateString(start)).le("time", DateUtils.dateToDateString(end)); |
| | | List<Map<String, Object>> dailyData = historyHourlyService.listMaps(historyHourlyQueryWrapper); |
| | | |
| | | //按mac分组 |
| | | Map<String, List<Map<String, Object>>> data = dailyData.parallelStream().collect(Collectors.groupingBy(o -> (String) o.get("mac"))); |
| | | |
| | | //存入数据库的结果集 |
| | | List<Map<String, Object>> insertData = new ArrayList<>(); |
| | | |
| | | data.forEach((key, value) -> { |
| | | Map<String, Object> dataMap = new HashMap<>(); |
| | | Map<String, Object> jsonMap = new HashMap<>(); |
| | | dataMap.put("mac", key); |
| | | dataMap.put("time", start); |
| | | |
| | | //中间变量,用于计算除臭氧外其它因子 |
| | | List<Map<String, Object>> tempValue = new ArrayList<>(value); |
| | | |
| | | value.removeIf(map -> ((Date) map.get("time")).getTime() == start.getTime()); |
| | | //臭氧8小时滑动平均值计算并修约 |
| | | Object o3AvgOfDay = AmendUtils.getO3AvgOfDay(value); |
| | | if (o3AvgOfDay != null) { |
| | | jsonMap.put(Constants.SENSOR_CODE_O3, o3AvgOfDay); |
| | | } |
| | | //除臭氧外其他因子均值计算 |
| | | tempValue.removeIf(map -> ((Date) map.get("time")).getTime() == end.getTime()); |
| | | |
| | | //风向均值计算并修约 |
| | | Object windDirAvg = AmendUtils.getWindDirAvg(value); |
| | | if (windDirAvg != null) { |
| | | jsonMap.put(Constants.SENSOR_CODE_WIND_DIR, windDirAvg); |
| | | } |
| | | |
| | | sensorCodes.forEach(sensorCode -> { |
| | | OptionalDouble optionalDouble = tempValue.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 (sensorCode.equals(Constants.SENSOR_CODE_O3)) { |
| | | 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); |
| | | } |
| | | }); |
| | | dataMap.put("value", JSONObject.toJSONString(jsonMap)); |
| | | insertData.add(dataMap); |
| | | }); |
| | | |
| | | //存入数据库 |
| | | historyDailyMapper.insertHistoryDaily(insertData); |
| | | } |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | List<Integer> list = new ArrayList<>(); |
| | | list.add(8);//1点 |
| | | list.add(12);//2 |
| | | list.add(10);//3 |
| | | list.add(18);//4 |
| | | list.add(16);//5 |
| | | list.add(22);//6 |
| | | list.add(4);//7 |
| | | list.add(12); |
| | | list.add(28); |
| | | list.add(26); |
| | | list.add(25); |
| | | list.add(21); |
| | | list.add(6); |
| | | list.add(18); |
| | | list.add(28); |
| | | list.add(18); |
| | | list.add(16); |
| | | list.add(15); |
| | | list.add(12); |
| | | list.add(14); |
| | | list.add(12); |
| | | list.add(10); |
| | | list.add(5); |
| | | list.add(88);//24 |
| | | Integer max = 0; |
| | | for (int i = 7; i < list.size(); i++) { |
| | | Integer sum = 0; |
| | | for (int j = i - 7; j <= i; j++) { |
| | | Integer b = list.get(j); |
| | | sum = sum + b; |
| | | } |
| | | if (sum > max) { |
| | | max = sum; |
| | | } |
| | | } |
| | | System.out.println((max / 8F)); |
| | | } |
| | | } |