kaiyu
2020-11-18 8beab91730f3e3cd6b53e4d498f763ff9dc65ef5
src/main/java/com/moral/service/impl/HistoryMinutelyServiceImpl.java
@@ -1,6 +1,10 @@
package com.moral.service.impl;
import com.moral.mapper.*;
import io.swagger.models.auth.In;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
@@ -36,6 +40,7 @@
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.jasper.compiler.JspUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
@@ -63,7 +68,9 @@
import com.moral.mapper.MonitorPointMapper;
import com.moral.mapper.ProvinceMapper;
import com.moral.mapper.SensorMapper;
import com.moral.mapper.ShAreaMapper;
import com.moral.service.HistoryMinutelyService;
import com.sun.org.apache.bcel.internal.generic.ANEWARRAY;
import static com.moral.common.bean.Constants.NULL_VALUE;
import static org.springframework.util.ObjectUtils.isEmpty;
@@ -98,6 +105,7 @@
    @Resource
    private MonitorPointMapper monitorPointMapper;
    @Override
    public Map<String, Object> getDayAQIByDevice(Map<String, Object> parameters) {
@@ -380,6 +388,15 @@
            parameters.put("timeUnits", "10min");
        }
        return historyMinutelyMapper.getMonitorPointOrDeviceAvgData(parameters);
    }
    @Override
    public List<Map<String, Object>> get(Map<String, Object> parameters) throws Exception {
        convertQueryParam(parameters);
        if (!ObjectUtils.isEmpty(parameters.get("compensate"))) {
            parameters.put("timeUnits", "10min");
        }
        return historyMinutelyMapper.getMultiDeviceSensorData(parameters);
    }
    @Override
@@ -708,7 +725,7 @@
            }
            parameters.put("timeList", timeList);
        } else if ("day".equals(parameters.get("type"))) {
            String time = parameters.get("time") + "T00:00:00";
            String time = parameters.get("time") + "T01:00:00";
            LocalDateTime value = LocalDateTime.parse(time);
            LocalDateTime end = value.plusHours(23);
            parameters.put("timeUnits", "hourly");
@@ -866,4 +883,214 @@
        }
        return resultList;
    }
    @Override
    public List<Map<String, Object>> getMultiDeviceSensorData(Map<String, Object> parameters) throws Exception {
        String sensorKey = parameters.get("sensorKey").toString();
        String[] macs = parameters.get("macs").toString().split(",");
        List<String> sensorKeys = new ArrayList<>();
        sensorKeys.add(sensorKey);
        parameters.put("sensorKeys", sensorKeys);
        parameters.put("sensors", sensorKeys);
        String type = parameters.get("type").toString();
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < 31; i++) {
            list.add(null);
        }
        for (String mac : macs) {
            parameters.put("mac", mac);
            List<Map<String, Object>> data = getMonitorPointOrDeviceAvgData(parameters);
            List<Map<String, Object>> l = new ArrayList<>();
            for (int i = 0; i < 31; i++) {
                l.add(null);
            }
            for (Map<String, Object> dataMap : data) {
                String time = dataMap.get("time").toString();
                Integer t = Integer.valueOf(time.substring(time.length() - 2));
                dataMap.put("time", t);
                if ("day".equals(type)) {
                    l.set(t, dataMap);
                } else {
                    l.set(t - 1, dataMap);
                }
            }
            for (int i = 0; i < l.size(); i++) {
                if (l.get(i) == null) {
                    Map<String, Object> m = new HashMap<>();
                    List<String> v = new ArrayList<>();
                    v.add("");
                    if ("day".equals(type)) {
                        m.put("time", i);
                    } else {
                        m.put("time", i + 1);
                    }
                    m.put("values", v);
                    l.set(i, m);
                }
            }
            data = l;
            for (Map<String, Object> map : data) {
                Map<String, Object> hashMap = new HashMap<>();
                if (map != null) {
                    int t = Integer.valueOf(map.get("time").toString());
                    hashMap.put("time", t);
                    String value;
                    if (map.get(sensorKey) == null) {
                        value = "";
                    } else {
                        value = map.get(sensorKey).toString();
                    }
                    List<String> values;
                    if ("day".equals(type)) {
                        t = t + 1;
                    }
                    if (list.get(t - 1) != null) {
                        values = (ArrayList<String>) list.get(t - 1).get("values");
                    } else {
                        values = new ArrayList<>();
                    }
                    values.add(value);
                    hashMap.put("values", values);
                    list.set(t - 1, hashMap);
                }
            }
        }
        boolean flag = false;
        Iterator<Map<String, Object>> iterator = list.iterator();
        int digit = 0;
        while (iterator.hasNext()) {
            Map<String, Object> next = iterator.next();
            ArrayList<String> values = (ArrayList<String>) next.get("values");
            for (String value : values) {
                if ("".equals(value)) {
                    flag = true;
                } else {
                    digit = value.split("\\.")[1].length();
                    flag = false;
                    break;
                }
            }
            if (flag) {
                iterator.remove();
            }
        }
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(digit);
        for (Map<String, Object> map : list) {
            int time = Integer.valueOf(map.get("time").toString());
            ArrayList<String> values = (ArrayList<String>) map.get("values");
            if (values.size() > 1) {
                int length = 0;
                double sum = 0.0;
                for (String value : values) {
                    if (!"".equals(value)) {
                        Double v = Double.valueOf(value);
                        length += 1;
                        sum += v;
                    }
                }
                Double avg = sum / length;
                String format = nf.format(avg);
                values.add(format);
                map.put("values", values);
            }
        }
        return list;
    }
    @Override
    public List<Map<String, Object>> getAllDeviceDataToExcel(Map<String, Object> parameters) throws Exception {
        Calendar cal = Calendar.getInstance();
        int length = ((String) parameters.get("startTime")).split("-").length;
        String time = parameters.remove("startTime").toString();
        String timeb;
        if (parameters.get("endTime") == null) {
            timeb = time;
        } else {
            timeb = parameters.remove("endTime").toString();
        }
        String dateFormat = "";
        String typeFormat = "";
        String timeUnits = "daily";
        int i = 0;
        if (length == 1) {
            dateFormat = "yyyy";
            typeFormat = "%Y";
            i = Calendar.YEAR;
        } else if (length == 2) {
            dateFormat = "yyyy-MM";
            typeFormat = "%Y-%m";
            i = Calendar.MONTH;
        } else if (length == 3) {
            dateFormat = "yyyy-MM-dd";
            typeFormat = "%Y-%m-%d";
            i = Calendar.DAY_OF_MONTH;
        } else if (length == 4) {
            dateFormat = "yyyy-MM-dd HH";
            typeFormat = "%Y-%m-%d %H";
            timeUnits = "hourly";
            i = Calendar.HOUR_OF_DAY;
            time = time.substring(0, 10) + " " + time.substring(11, 13);
            timeb = timeb.substring(0, 10) + " " + timeb.substring(11, 13);
        }
        parameters.put("time", time);
        parameters.put("timeb", timeb);
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        cal.setTime(sdf.parse(timeb));
        cal.add(i, 1);
        Date endTime = cal.getTime();
        parameters.put("format", dateFormat);
        parameters.put("typeFormat", typeFormat);
        parameters.put("timeUnits", timeUnits);
        parameters.put("field", i);
        cal.setTime(sdf.parse(time));
        List<String> times = new ArrayList<>();
        for (long d = cal.getTimeInMillis(); d < endTime.getTime(); cal.set(i, cal.get(i) + 1), d = cal.getTimeInMillis()) {
            String format = sdf.format(d);
            times.add(format);
        }
        String[] sensorKeys = parameters.get("sensorKey").toString().split(",");
        List<String> keys = Arrays.asList(sensorKeys);
        parameters.put("sensors", keys);
        parameters.put("sensorKeys", keys);
        parameters.put("timeb", sdf.format(endTime));
        int mpId = Integer.valueOf(parameters.get("monitorPoint").toString());
        String monitorPointName = monitorPointMapper.getMonitorName(mpId);
        List<Map<String, Object>> devices = deviceMapper.getDevicesByMpId(mpId);
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (Map<String, Object> map : devices) {
            String mac = map.get("mac").toString();
            parameters.put("mac", mac);
            String name = map.get("name").toString();
            List<Map<String, Object>> data = getMonitorPointOrDeviceAvgData(parameters);
            for (String sensorKey : keys) {
                Map<String, Object> sensor = sensorMapper.getSensorBySensorKey(sensorKey);
                if (sensor == null) {
                    continue;
                }
                String description = sensor.get("description").toString();
                String unit = sensor.get("unit").toString();
                Map<String, Object> hashMap = new LinkedHashMap<>();
                for (String t : times) {
                    hashMap.put("monitorPointName", monitorPointName);
                    hashMap.put("name", name);
                    hashMap.put("sensor", description + "(" + unit + ")");
                    hashMap.put(t, "");
                }
                if (data.size() != 0) {
                    for (Map<String, Object> dataMap : data) {
                        String t = dataMap.get("time").toString();
                        if (dataMap.get(sensorKey) != null) {
                            String value = dataMap.get(sensorKey).toString();
                            hashMap.put(t, value);
                        }
                    }
                }
                resultList.add(hashMap);
            }
        }
        return resultList;
    }
}