|  |  |  | 
|---|
|  |  |  | 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.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.transaction.annotation.Transactional; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private HistoryHourlyService historyHourlyService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | sensorQueryWrapper.select("code", "lower", "upper").eq("is_delete", Constants.NOT_DELETE); | 
|---|
|  |  |  | List<Sensor> sensors = sensorService.list(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); | 
|---|
|  |  |  | Map<String, Object> prop = new HashMap<>(); | 
|---|
|  |  |  | String timeUnits = DateUtils.dateToDateString(start, DateUtils.yyyyMM_EN); | 
|---|
|  |  |  | prop.put("timeUnits", timeUnits); | 
|---|
|  |  |  | prop.put("start", DateUtils.dateToDateString(start)); | 
|---|
|  |  |  | prop.put("end", DateUtils.dateToDateString(end)); | 
|---|
|  |  |  | List<Map<String, Object>> dailyData = historyHourlyService.selectDailyData(prop); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (dailyData.size() == 0) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //按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) -> { | 
|---|
|  |  |  | data.forEach((mac, value) -> { | 
|---|
|  |  |  | Map<String, Object> dataMap = new HashMap<>(); | 
|---|
|  |  |  | Map<String, Object> jsonMap = new HashMap<>(); | 
|---|
|  |  |  | dataMap.put("mac", key); | 
|---|
|  |  |  | dataMap.put("mac", mac); | 
|---|
|  |  |  | dataMap.put("time", start); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //中间变量,用于计算除臭氧外其它因子 | 
|---|
|  |  |  | List<Map<String, Object>> tempValue = new ArrayList<>(value); | 
|---|
|  |  |  | Map<String, Object> params = new HashMap<>(); | 
|---|
|  |  |  | params.put("data", value); | 
|---|
|  |  |  | params.put("type", "day"); | 
|---|
|  |  |  | for (Sensor sensor : sensors) { | 
|---|
|  |  |  | String sensorCode = sensor.getCode(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | value.removeIf(map -> ((Date) map.get("time")).getTime() == start.getTime()); | 
|---|
|  |  |  | //臭氧8小时滑动值 | 
|---|
|  |  |  | double o3AvgOfDay = getO3AvgOfDay(value); | 
|---|
|  |  |  | jsonMap.put(Constants.SENSOR_CODE_O3, o3AvgOfDay); | 
|---|
|  |  |  | System.out.println("臭氧==" + value); | 
|---|
|  |  |  | //O3上下限 | 
|---|
|  |  |  | if (sensorCode.equals(Constants.SENSOR_CODE_O3)) { | 
|---|
|  |  |  | if (sensor.getUpper() != null) { | 
|---|
|  |  |  | params.put("o3Upper", sensor.getUpper()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (sensor.getLower() != null) { | 
|---|
|  |  |  | params.put("o3Lower", sensor.getLower()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //除臭氧外日均值计算 | 
|---|
|  |  |  | tempValue.removeIf(map -> ((Date) map.get("time")).getTime() == end.getTime()); | 
|---|
|  |  |  | System.out.println("其他因子==" + tempValue); | 
|---|
|  |  |  | sensorCodes.forEach(sensorCode -> { | 
|---|
|  |  |  | OptionalDouble optionalDouble = tempValue.parallelStream() | 
|---|
|  |  |  | //风向上下限 | 
|---|
|  |  |  | if (sensorCode.equals(Constants.SENSOR_CODE_WIND_DIR)) { | 
|---|
|  |  |  | if (sensor.getUpper() != null) { | 
|---|
|  |  |  | params.put("windDirUpper", sensor.getUpper()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (sensor.getLower() != null) { | 
|---|
|  |  |  | params.put("windDirLower", sensor.getLower()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //风速上下限 | 
|---|
|  |  |  | if (sensorCode.equals(Constants.SENSOR_CODE_WIND_SPEED)) { | 
|---|
|  |  |  | if (sensor.getUpper() != null) { | 
|---|
|  |  |  | params.put("windSpeedUpper", sensor.getUpper()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (sensor.getLower() != null) { | 
|---|
|  |  |  | params.put("windSpeedLower", sensor.getLower()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //臭氧8小时滑动平均值计算并修约 | 
|---|
|  |  |  | Map<String, Object> o3AvgOfDay = AmendUtils.getO3AvgOfDay(params); | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(o3AvgOfDay)) { | 
|---|
|  |  |  | jsonMap.putAll(o3AvgOfDay); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //风向均值计算并修约 | 
|---|
|  |  |  | Map<String, Object> windDirAvg = AmendUtils.getWindDirAvg(params); | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(windDirAvg)) { | 
|---|
|  |  |  | jsonMap.putAll(windDirAvg); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | sensors.forEach(sensor -> { | 
|---|
|  |  |  | String sensorCode = sensor.getCode(); | 
|---|
|  |  |  | Double upper = sensor.getUpper(); | 
|---|
|  |  |  | Double lower = sensor.getLower(); | 
|---|
|  |  |  | OptionalDouble optionalDouble = value.parallelStream() | 
|---|
|  |  |  | .flatMapToDouble(v -> { | 
|---|
|  |  |  | Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class); | 
|---|
|  |  |  | Object sensorValue = dataValue.get(sensorCode.toString()); | 
|---|
|  |  |  | Object sensorValue = dataValue.get(sensorCode); | 
|---|
|  |  |  | //数据有效性标记位 | 
|---|
|  |  |  | Object flag = dataValue.get(sensorCode + "-" + Constants.MARKER_BIT_KEY); | 
|---|
|  |  |  | if (!Constants.MARKER_BIT_TRUE.equals(flag)) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(sensorValue)) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //O3单独计算 | 
|---|
|  |  |  | if (sensorCode.equals(Constants.SENSOR_CODE_O3)) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return DoubleStream.of(Double.parseDouble(sensorValue.toString())); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //剔除数据超过上下限的数据 | 
|---|
|  |  |  | double aDouble = Double.parseDouble(sensorValue.toString()); | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(upper)) { | 
|---|
|  |  |  | if (aDouble > upper) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(lower)) { | 
|---|
|  |  |  | if (aDouble < lower) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return DoubleStream.of(aDouble); | 
|---|
|  |  |  | }).average(); | 
|---|
|  |  |  | if (optionalDouble.isPresent()) { | 
|---|
|  |  |  | jsonMap.put(sensorCode.toString(), Double.parseDouble(String.format("%.4f", optionalDouble.getAsDouble()))); | 
|---|
|  |  |  | //银行家算法修约 | 
|---|
|  |  |  | double sciCal = AmendUtils.sciCal(optionalDouble.getAsDouble(), 4); | 
|---|
|  |  |  | jsonMap.put(sensorCode, sciCal); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | dataMap.put("value", JSONObject.toJSONString(jsonMap)); | 
|---|
|  |  |  | insertData.add(dataMap); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //臭氧8小时滑动值 | 
|---|
|  |  |  | System.out.println(insertData); | 
|---|
|  |  |  | //存入数据库 | 
|---|
|  |  |  | historyDailyMapper.insertHistoryDaily(insertData); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //臭氧8小时滑动值 | 
|---|
|  |  |  | private double getO3AvgOfDay(List<Map<String, Object>> list) { | 
|---|
|  |  |  | double max = 0d; | 
|---|
|  |  |  | for (int i = 8; i <= 24; i++) { | 
|---|
|  |  |  | List<Double> data = new ArrayList<>(); | 
|---|
|  |  |  | for (Map<String, Object> dataMap : list) { | 
|---|
|  |  |  | Map<String, Object> dataValue = JSONObject.parseObject((String) dataMap.get("value"), Map.class); | 
|---|
|  |  |  | Double o3 = Double.parseDouble(dataValue.get(Constants.SENSOR_CODE_O3).toString()); | 
|---|
|  |  |  | int hour = DateUtils.getHour((Date) dataMap.get("time")); | 
|---|
|  |  |  | if (hour == 0) { | 
|---|
|  |  |  | hour = 24; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (hour <= i && hour >= i - 7) { | 
|---|
|  |  |  | data.add(o3); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | double average = data.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage(); | 
|---|
|  |  |  | if (max < average) { | 
|---|
|  |  |  | max = average; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return Double.parseDouble(String.format("%.4f", max)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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);//8 | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | 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)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|