xufenglei
2018-03-06 db0bd262f7b07e43155db4e0fdf5c52e91610edf
报表优化
1 files modified
140 ■■■■■ changed files
src/main/java/com/moral/service/impl/HistoryMinutelyServiceImpl.java 140 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/impl/HistoryMinutelyServiceImpl.java
@@ -8,6 +8,7 @@
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
@@ -32,6 +33,8 @@
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.moral.common.util.CalculateUtils;
import com.moral.common.util.ResourceUtil;
import com.moral.common.util.ValidateUtil;
@@ -43,6 +46,7 @@
import com.moral.service.HistoryMinutelyService;
@Service
@SuppressWarnings({ "unchecked", "rawtypes", "unused" })
public class HistoryMinutelyServiceImpl implements HistoryMinutelyService {
    @Resource
@@ -134,108 +138,76 @@
    @Override
    public Map<String, List<Object>> getCompareReport(Map<String, Object> parameters) throws Exception {
        Map<String, List<Object>> resultMap = new HashMap<String, List<Object>>();
        Map<String, Object> parametersA = new HashMap<String, Object>();
        Map<String, Object> parametersB = new HashMap<String, Object>();
        if (parameters.containsKey("time") && parameters.containsKey("monitorPoint")) {
            parametersA.put("part", "A");
            parametersA.putAll(parameters);
            parametersA.remove("timeb");
        }
        if (parameters.containsKey("timeb") && parameters.containsKey("monitorPointb")) {
            parametersB.putAll(parameters);
            parametersB.put("part", "B");
            parametersB.put("time", parametersB.get("timeb"));
            parametersB.put("monitorPoint", parametersB.get("monitorPointb"));
            if (parameters.containsKey("macb")) {
                parametersB.put("mac", parametersB.get("macb"));
            }
            parametersB.remove("timeb");
        }
        List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
        list.add(parametersA);
        list.add(parametersB);
        List<Map<String, Object>> list = JSON.parseObject((String)parameters.get("items"), new TypeReference<List<Map<String, Object>>>() {});
        parameters.remove("items");
        ExecutorService threadPool = Executors.newCachedThreadPool();
        CompletionService<Map<String, List<Object>>> cs = new ExecutorCompletionService<Map<String, List<Object>>>(threadPool);
        String type = (String) parameters.get("type");
        if ("month".equals(type)) {
        for (Map<String, Object> map : list) {
                int timeLength = Integer.valueOf(parameters.get("timeLength").toString());
                String[] formatTime = map.get("formatTime").toString().split("-");
                LocalDate localDate = LocalDate.of(Integer.valueOf(formatTime[0]), Integer.valueOf(formatTime[1]), 1);
                int lengthOfMonth = localDate.lengthOfMonth();
                if (lengthOfMonth > timeLength) {
                    parameters.put("timeLength", lengthOfMonth);
                }
            }
        }
        List<Object> timeList = new ArrayList<Object>();
        for (int i = 0; i <Integer.valueOf(parameters.get("timeLength").toString()); i++) {
            timeList.add(i, String.format("%02d", "day".equals(type) || "hour".equals(type) ? i : i + 1));
        }
        parameters.put("timeList", timeList);
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i);
            map.put("part", i);
            if (ObjectUtils.isEmpty(map.get("mac"))) {
                map.remove("mac");
            }
            map.put("time", map.get("formatTime"));
            map.remove("formatTime");
            map.putAll(parameters);
            cs.submit(new Callable<Map<String, List<Object>>>() {
                @Override
                public Map<String, List<Object>> call() throws Exception {
                    return getMonitorPointOrDeviceAvgData4Compare(map);
                }
            });
        }
            
        }
        List<Object> dataList = new ArrayList<Object>();
        for (Map<String, Object> map : list) {
            resultMap.putAll(cs.take().get());
            dataList.add(cs.take().get());
        }
        if (resultMap.containsKey("timeA") && resultMap.containsKey("timeB")) {
            List<Object> timeA = resultMap.get("timeA");
            List<Object> timeB = resultMap.get("timeB");
            List<Object> time = new ArrayList<Object>(timeA);
            List<Object> time1 = new ArrayList<Object>(timeB);
            List<Object> dataA = resultMap.get("dataA");
            List<Object> dataB = resultMap.get("dataB");
            List<Object> resultA = new ArrayList<Object>();
            List<Object> resultB = new ArrayList<Object>();
            time1.removeAll(time);
            time.addAll(time1);
            time.sort(new Comparator<Object>() {
                @Override
                public int compare(Object o1, Object o2) {
                    return Integer.compare(Integer.valueOf(o1.toString()), Integer.valueOf(o2.toString()));
                }
            });
            for (int i = 0; i < time.size(); i++) {
                Object object = time.get(i);
                int indexA = timeA.indexOf(object);
                if (indexA > -1) {
                    resultA.add(i, dataA.get(indexA));
                }else {
                    resultA.add("-");
                }
                int indexB = timeB.indexOf(object);
                if (indexB > -1) {
                    resultB.add(i, dataB.get(indexB));
                }else {
                    resultB.add("-");
        Map[] maps = new HashMap[list.size()];
        for (Object object : dataList) {
            Map<String, Object> map = (Map<String, Object>)object;
            for (String key : map.keySet()) {
                int index = Integer.valueOf(key.replace("data", ""));
                maps[index] = map;
                }
            }
            resultMap.put("dataA", resultA);
            resultMap.put("dataB", resultB);
            resultMap.put("time", time);
        } else if (resultMap.containsKey("timeA")) {
            resultMap.put("time", resultMap.get("timeA"));
        } else {
            resultMap.put("time", resultMap.get("timeB"));
        }
        resultMap.put("time", timeList);
        resultMap.put("data", Arrays.asList(maps));
        return resultMap;
    }
    
    public Map<String, List<Object>> getMonitorPointOrDeviceAvgData4Compare(Map<String, Object> parameters) throws Exception {
        Map<String, List<Object>> resultMap = new HashMap<String, List<Object>>();
        if (ObjectUtils.isEmpty(parameters)) {
            resultMap.put("c", null);
            return resultMap;
        }
        List<Map<String, Object>> resultList = getMonitorPointOrDeviceAvgData(parameters);
        
        List<Object> timeList = new ArrayList<Object>();
        List<Object> timeList = (List<Object>) parameters.get("timeList");
        List<Object> dataList = new ArrayList<Object>();
        Set<String> sensors ;
        if (parameters.containsKey("sensorKey")) {
            String sensorKey = (String) parameters.get("sensorKey");
//            Map<String, List<Double>> doubleMap = new HashMap<String, List<Double>>();
//            List<Double> doubles = new ArrayList<Double>();
//            for (Map<String, Object> map : resultList) {
//                String time = map.get("time").toString();
//                time =time.substring(time.length() - 2);
//                timeList.add(time);
//                // dataList.add(map.get(sensorKey));
//                doubles.add((Double) map.get(sensorKey));
//            }
//            doubleMap.put(sensorKey, doubles);
//            dataList.add(doubleMap);
            Sensor sensor = new Sensor();
            sensor.setSensorKey(sensorKey);
            sensor = sensorMapper.selectOne(sensor );
@@ -250,31 +222,29 @@
            });
            sensors.addAll((Set<String>)parameters.get("sensors"));
        }
        Map<String, List<Double>> doubleMap = new LinkedHashMap<String, List<Double>>();
        Map<String, Double[]> doubleMap = new LinkedHashMap<String, Double[]>();
        for (Map<String, Object> map : resultList) {
            String time = map.get("time").toString();
            time =time.substring(time.length() - 2);
            timeList.add(time);
            int index = timeList.indexOf(time);
            for (String sensor : sensors) {
                String[] split = sensor.split("-");
                String sensorKey = split[0];
                if (map.containsKey(sensorKey)) {
                    List<Double> doubles;
                    Double[] doubles;
                    if (doubleMap.containsKey(sensor)) {
                        doubles = doubleMap.get(sensor);
                    } else {
                        doubles = new ArrayList<Double>();
                        doubles = new Double[timeList.size()];
                    }
                    doubles.add((Double) map.get(sensorKey));
                    doubles[index] = (Double) map.get(sensorKey);
                    doubleMap.put(sensor, doubles);
                    
                }
                //dataList.add(map.get(sensorKey));
            }
        }
        dataList.add(doubleMap);
        String part = (String) parameters.get("part");
        resultMap.put("time" + part, timeList);
        String part = parameters.get("part").toString();
        resultMap.put("data" + part, dataList);
        return resultMap;
    }
@@ -319,7 +289,7 @@
        // 监控点平均值
        if (!parameters.containsKey("mac")) {
            resul = new ArrayList<Map<String,Object>>();
            Integer monitorPointId = Integer.valueOf((String) parameters.get("monitorPoint"));
            Integer monitorPointId = Integer.valueOf(parameters.get("monitorPoint").toString());
            List<Map<String, Object>> deviceVersions = deviceMapper.getDeviceVersionIdByMonitorPoint(monitorPointId);
            ExecutorService threadPool = Executors.newCachedThreadPool();
            CompletionService<List<Map<String, Object>>> cs = new ExecutorCompletionService<List<Map<String, Object>>>(threadPool);