jinpengyong
2022-12-22 c96aba58f5bf3991e18cb0adaa71e7bec339cb4b
screen-job/src/main/java/com/moral/api/service/impl/HistoryDailyServiceImpl.java
@@ -3,7 +3,6 @@
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;
@@ -16,6 +15,7 @@
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;
@@ -48,6 +48,7 @@
    private HistoryHourlyService historyHourlyService;
    @Override
    @Transactional
    public void insertHistoryDaily() {
        String format = DateUtils.yyyy_MM_dd_EN;
        Date now = new Date();
@@ -57,13 +58,17 @@
        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;
        }
@@ -74,47 +79,101 @@
        //存入数据库的结果集
        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小时滑动平均值计算并修约
            Object o3AvgOfDay = AmendUtils.getO3AvgOfDay(value);
            if (o3AvgOfDay != null) {
                jsonMap.put(Constants.SENSOR_CODE_O3, o3AvgOfDay);
                //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());
                    }
                }
            }
            //除臭氧外其他因子均值计算
            tempValue.removeIf(map -> ((Date) map.get("time")).getTime() == end.getTime());
            //臭氧8小时滑动平均值计算并修约
            Map<String, Object> o3AvgOfDay = AmendUtils.getO3AvgOfDay(params);
            if (!ObjectUtils.isEmpty(o3AvgOfDay)) {
                jsonMap.putAll(o3AvgOfDay);
            }
            //风向均值计算并修约
            Object windDirAvg = AmendUtils.getWindDirAvg(value);
            if (windDirAvg != null) {
                jsonMap.put(Constants.SENSOR_CODE_WIND_DIR, windDirAvg);
            Map<String, Object> windDirAvg = AmendUtils.getWindDirAvg(params);
            if (!ObjectUtils.isEmpty(windDirAvg)) {
                jsonMap.putAll(windDirAvg);
            }
            sensorCodes.forEach(sensorCode -> {
                OptionalDouble optionalDouble = tempValue.parallelStream()
            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()) {
                    //银行家算法修约
                    double sciCal = AmendUtils.sciCal(optionalDouble.getAsDouble(), 4);
                    jsonMap.put(sensorCode.toString(), sciCal);
                    jsonMap.put(sensorCode, sciCal);
                }
            });
            dataMap.put("value", JSONObject.toJSONString(jsonMap));