|  |  | 
 |  |  |  | 
 |  |  | 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; | 
 |  |  | 
 |  |  |     private HistoryDailyService historyDailyService; | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     @Transactional | 
 |  |  |     public void insertHistoryMonthly() { | 
 |  |  |  | 
 |  |  |         Date now = new Date(); | 
 |  |  |         //开始时间,上月1号 | 
 |  |  |         Date start = DateUtils.getFirstDayOfLastMonth(); | 
 |  |  |         //结束时间,本月1号 | 
 |  |  |         Date end = DateUtils.addMonths(start, 1); | 
 |  |  |         //因子 | 
 |  |  |         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<HistoryDaily> historyDailyQueryWrapper = new QueryWrapper<>(); | 
 |  |  |         historyDailyQueryWrapper.ge("time", DateUtils.dateToDateString(start)).lt("time", DateUtils.dateToDateString(now)); | 
 |  |  |         historyDailyQueryWrapper.ge("time", DateUtils.dateToDateString(start)).lt("time", DateUtils.dateToDateString(end)); | 
 |  |  |         List<Map<String, Object>> weeklyData = historyDailyService.listMaps(historyDailyQueryWrapper); | 
 |  |  |         if (weeklyData.size() == 0) { | 
 |  |  |             return; | 
 |  |  | 
 |  |  |             dataMap.put("time", start); | 
 |  |  |  | 
 |  |  |             //风向均值计算并修约 | 
 |  |  |             Object windDirAvg = AmendUtils.getWindDirAvg(value); | 
 |  |  |             if (windDirAvg != null) { | 
 |  |  |                 jsonMap.put(Constants.SENSOR_CODE_WIND_DIR, windDirAvg); | 
 |  |  |             Map<String, Object> params = new HashMap<>(); | 
 |  |  |             params.put("data", value); | 
 |  |  |             params.put("type", "month"); | 
 |  |  |             for (Sensor sensor : sensors) { | 
 |  |  |                 String sensorCode = sensor.getCode(); | 
 |  |  |                 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()); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 if (sensorCode.equals(Constants.SENSOR_CODE_CO)) { | 
 |  |  |                     if (sensor.getUpper() != null) { | 
 |  |  |                         params.put("coUpper", sensor.getUpper()); | 
 |  |  |                     } | 
 |  |  |                     if (sensor.getLower() != null) { | 
 |  |  |                         params.put("coLower", sensor.getLower()); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 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()); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |             } | 
 |  |  |             //风向计算 | 
 |  |  |             Map<String, Object> windDirAvg = AmendUtils.getWindDirAvg(params); | 
 |  |  |             if (!ObjectUtils.isEmpty(windDirAvg)) { | 
 |  |  |                 jsonMap.putAll(windDirAvg); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             //CO 95百分位计算并修约 | 
 |  |  |             Object coAvg = AmendUtils.getCOAvgOfWeek(value); | 
 |  |  |             if (coAvg != null) { | 
 |  |  |                 jsonMap.put(Constants.SENSOR_CODE_CO, coAvg); | 
 |  |  |             Map<String, Object> coAvgOfWeekOrMonth = AmendUtils.getCOAvgOfWeekOrMonth(params); | 
 |  |  |             if (!ObjectUtils.isEmpty(coAvgOfWeekOrMonth)) { | 
 |  |  |                 jsonMap.putAll(coAvgOfWeekOrMonth); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             //O3 90百分位计算并修约 | 
 |  |  |             Object o3Avg = AmendUtils.getO3AvgOfWeek(value); | 
 |  |  |             if (o3Avg != null) { | 
 |  |  |                 jsonMap.put(Constants.SENSOR_CODE_O3, o3Avg); | 
 |  |  |             Map<String, Object> o3AvgOfWeekOrMonth = AmendUtils.getO3AvgOfWeekOrMonth(params); | 
 |  |  |             if (!ObjectUtils.isEmpty(o3AvgOfWeekOrMonth)) { | 
 |  |  |                 jsonMap.putAll(o3AvgOfWeekOrMonth); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             sensorCodes.forEach(sensorCode -> { | 
 |  |  |             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); | 
 |  |  |                             if (ObjectUtils.isEmpty(sensorValue)) { | 
 |  |  |                                 return null; | 
 |  |  |                             } | 
 |  |  | 
 |  |  |                             if (Constants.SENSOR_CODE_O3.equals(sensorCode)) { | 
 |  |  |                                 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()) { | 
 |  |  |                     //银行家算法修约 | 
 |  |  |                     double sciCal = AmendUtils.sciCal(optionalDouble.getAsDouble(), 4); | 
 |  |  |                     jsonMap.put(sensorCode.toString(), sciCal); | 
 |  |  |                     jsonMap.put(sensorCode, sciCal); | 
 |  |  |                 } | 
 |  |  |             }); | 
 |  |  |             dataMap.put("value", JSONObject.toJSONString(jsonMap)); | 
 |  |  | 
 |  |  |  | 
 |  |  |         //存入数据库 | 
 |  |  |         historyMonthlyMapper.insertHistoryMonthly(insertData); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     } | 
 |  |  | } |