| 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.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.transaction.annotation.Transactional; | 
| import org.springframework.util.ObjectUtils; | 
| import org.springframework.util.StringUtils; | 
|   | 
| import javax.xml.crypto.Data; | 
| 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> | 
|  * 服务实现类 | 
|  * </p> | 
|  * | 
|  * @author moral | 
|  * @since 2021-06-11 | 
|  */ | 
| @Service | 
| public class HistoryDailyServiceImpl extends ServiceImpl<HistoryDailyMapper, HistoryDaily> implements HistoryDailyService { | 
|   | 
|     @Autowired | 
|     private HistoryDailyMapper historyDailyMapper; | 
|   | 
|     @Autowired | 
|     private SensorService sensorService; | 
|   | 
|     @Autowired | 
|     private HistoryHourlyService historyHourlyService; | 
|   | 
|     @Override | 
|     @Transactional | 
|     public void insertHistoryDaily(String time) { | 
|         String format = DateUtils.yyyy_MM_dd_EN; | 
|         Date now; | 
|         if(StringUtils.isEmpty(time)){ | 
|             now = new Date(); | 
|         } else { | 
|             now = DateUtils.getDate(time,"yyyy-MM-dd"); | 
|         } | 
|         //开始时间,昨日 | 
|         Date start = DateUtils.dataToTimeStampTime(DateUtils.getDateOfDay(now, -1), format); | 
|         //结束时间,今日 | 
|         Date end = DateUtils.dataToTimeStampTime(now, format); | 
|         //因子 | 
|         QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>(); | 
|         sensorQueryWrapper.select("code", "lower", "upper").eq("is_delete", Constants.NOT_DELETE); | 
|         List<Sensor> sensors = sensorService.list(sensorQueryWrapper); | 
|         //获取所有设备小时数据 | 
|         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) { | 
|             System.out.println("没有小时数据"+DateUtils.dateToDateString(start)); | 
|             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((mac, value) -> { | 
|             Map<String, Object> dataMap = new HashMap<>(); | 
|             Map<String, Object> jsonMap = new HashMap<>(); | 
|             dataMap.put("mac", mac); | 
|             dataMap.put("time", start); | 
|   | 
|             Map<String, Object> params = new HashMap<>(); | 
|             params.put("data", value); | 
|             params.put("type", "day"); | 
|             for (Sensor sensor : sensors) { | 
|                 String sensorCode = sensor.getCode(); | 
|   | 
|                 //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()); | 
|                     } | 
|                 } | 
|   | 
|                 //风向上下限 | 
|                 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); | 
|                             //数据有效性标记位 | 
|                             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; | 
|                             } | 
|   | 
|                             //剔除数据超过上下限的数据 | 
|                             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, sciCal); | 
|                 } | 
|             }); | 
|             dataMap.put("value", JSONObject.toJSONString(jsonMap)); | 
|             insertData.add(dataMap); | 
|         }); | 
|         //存入数据库 | 
|         historyDailyMapper.insertHistoryDaily(insertData); | 
|         System.out.println("补偿完成"); | 
|     } | 
| } |