jinpengyong
2021-08-17 6f4e852b84c577454a4876f83c7085bd360fe4fb
screen-job/src/main/java/com/moral/api/service/impl/HistoryDailyServiceImpl.java
@@ -1,15 +1,32 @@
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.transaction.annotation.Transactional;
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>
@@ -25,9 +42,157 @@
    @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);
    @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", "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);
        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) -> {
            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);
            //移除第一天数据(0点的),O3滑动值第一天数据是从1点-8点
            value.removeIf(map -> ((Date) map.get("time")).getTime() == start.getTime());
            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);
            }
            //除臭氧外其他因子均值计算
            tempValue.removeIf(o -> ((Date) o.get("time")).getTime() == end.getTime());
            //风向均值计算并修约
            params.put("data", tempValue);
            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 = tempValue.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)) {
                                System.out.println(456);
                                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;
                                }
                            }
                            if ("a00e12".equals(sensorCode)) {
                                System.out.println(key + "==" + sensorCode + "==" + v.get("time") + "==" + aDouble);
                            }
                            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);
    }
}