fengxiang
2018-03-07 501ebd43da6372ee753d422290327e339b04abb5
Merge remote-tracking branch 'origin/master'
5 files modified
303 ■■■■■ changed files
src/main/java/com/moral/controller/ScreenController.java 70 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/MonitorPointService.java 2 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/impl/HistoryMinutelyServiceImpl.java 217 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java 10 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/HistoryMinutelyMapper.xml 4 ●●●● patch | view | raw | blame | history
src/main/java/com/moral/controller/ScreenController.java
@@ -1,32 +1,22 @@
package com.moral.controller;
import static com.moral.common.util.RedisUtil.get;
import static com.moral.common.util.RedisUtil.hasKey;
import static com.moral.common.util.ResourceUtil.getValue;
import static com.moral.common.util.WebUtils.getParametersStartingWith;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.*;
import com.moral.common.bean.JsonData;
import com.moral.common.bean.PageResult;
import com.moral.common.xml.Version;
import com.moral.entity.Account;
import com.moral.entity.Device;
import com.moral.entity.MapBounds;
import com.moral.entity.MonitorPoint;
import com.moral.entity.alarm.AlarmConfigValue;
import com.moral.entity.alarm.AlarmSensorLevel;
import com.moral.service.*;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.log4j.Logger;
import org.dom4j.Document;
@@ -35,13 +25,40 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.moral.common.bean.ResultBean;
import com.moral.common.util.ValidateUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.xml.sax.InputSource;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.moral.common.bean.JsonData;
import com.moral.common.bean.PageResult;
import com.moral.common.bean.ResultBean;
import com.moral.common.util.ValidateUtil;
import com.moral.common.xml.Version;
import com.moral.entity.Account;
import com.moral.entity.Device;
import com.moral.entity.MapBounds;
import com.moral.entity.MonitorPoint;
import com.moral.entity.alarm.AlarmConfigValue;
import com.moral.entity.alarm.AlarmSensorLevel;
import com.moral.service.AccountService;
import com.moral.service.AlarmConfigService;
import com.moral.service.AreaService;
import com.moral.service.DeviceService;
import com.moral.service.HistoryMinutelyService;
import com.moral.service.HistoryService;
import com.moral.service.MachineActivateService;
import com.moral.service.MonitorPointService;
import com.moral.service.SensorService;
/**
 * The Class ScreenController.大屏接口
@@ -400,4 +417,11 @@
        jsonData.setExtData(returnMap);
        return jsonData;
    }
    @GetMapping("monitor_points")
    public ResultBean<List<MonitorPoint>> getMonitorPointsByOrganizationId(Integer orgId) {
        //Map<String, Object> parameters = getParametersStartingWith(request, null);
        List<MonitorPoint> monitorPoints = monitorPointService.getMonitorPointsByOrganizationId(orgId);
        return new ResultBean<List<MonitorPoint>>(monitorPoints);
    }
}
src/main/java/com/moral/service/MonitorPointService.java
@@ -22,4 +22,6 @@
    
    List<MonitorPoint> getMonitorPointsByName(String name);
    List<Map<String,String>> queryMonitroPointsState(List<Integer> idList);
    List<MonitorPoint> getMonitorPointsByOrganizationId(Integer orgId);
}
src/main/java/com/moral/service/impl/HistoryMinutelyServiceImpl.java
@@ -8,6 +8,8 @@
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
@@ -19,6 +21,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
@@ -31,16 +34,20 @@
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;
import com.moral.entity.Device;
import com.moral.entity.Sensor;
import com.moral.mapper.DeviceMapper;
import com.moral.mapper.HistoryMinutelyMapper;
import com.moral.mapper.SensorMapper;
import com.moral.service.HistoryMinutelyService;
@Service
@SuppressWarnings({ "unchecked", "rawtypes", "unused" })
public class HistoryMinutelyServiceImpl implements HistoryMinutelyService {
    @Resource
@@ -52,7 +59,7 @@
    @Resource
    private SensorMapper sensorMapper;
    private Set<String> sensorKeys = new HashSet<String>();
    // volatile
    @Override
    public Map<String, Object> getDayAQIByDevice(Map<String, Object> parameters) {
@@ -128,109 +135,132 @@
        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> dataList = new ArrayList<Object>();
        String sensorKey = (String) parameters.get("sensorKey");
        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));
        }
        String part = (String) parameters.get("part");
        resultMap.put("time" + part, timeList);
        resultMap.put("data" + part, dataList);
        return resultMap;
    }
    @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);
        for (Map<String, Object> map : list) {
        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()));
        Map[] maps = new HashMap[list.size()];
        Set<String> sensors = new TreeSet<String>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return Integer.compare(Integer.valueOf(o1.split("-")[0].replace("e", "")), Integer.valueOf(o2.split("-")[0].replace("e", "")));
            }
        });
        for (Object object : dataList) {
            Map<String, Object> map = (Map<String, Object>)object;
            for (String key : map.keySet()) {
                if (key.startsWith("data")) {
                    int index = Integer.valueOf(key.replace("data", ""));
                    maps[index] = map;
                }
            });
            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("-");
                if (key.startsWith("sensors")) {
                    sensors.addAll((List<String>) map.get(key));
                }
            }
            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("times", timeList);
        resultMap.put("datas", Arrays.asList(maps));
        resultMap.put("sensors", new ArrayList<Object>(sensors));
        return resultMap;
    }
    public Map<String, List<Object>> getMonitorPointOrDeviceAvgData4Compare(Map<String, Object> parameters) throws Exception {
        Map<String, List<Object>> resultMap = new HashMap<String, List<Object>>();
        List<Map<String, Object>> resultList = getMonitorPointOrDeviceAvgData(parameters);
        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");
            Sensor sensor = new Sensor();
            sensor.setSensorKey(sensorKey);
            sensor = sensorMapper.selectOne(sensor );
            sensors = new HashSet<String>();
            sensors.add(sensorKey + "-" + sensor.getName() + "-" + sensor.getUnit());
        } else {
            sensors = new TreeSet<String>(new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    return Integer.compare(Integer.valueOf(o1.split("-")[0].replace("e", "")), Integer.valueOf(o2.split("-")[0].replace("e", "")));
                }
            });
            sensors.addAll((Set<String>)parameters.get("sensors"));
        }
        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);
            int index = timeList.indexOf(time);
            for (String sensor : sensors) {
                String[] split = sensor.split("-");
                String sensorKey = split[0];
                if (map.containsKey(sensorKey)) {
                    Double[] doubles;
                    if (doubleMap.containsKey(sensor)) {
                        doubles = doubleMap.get(sensor);
                    } else {
                        doubles = new Double[timeList.size()];
                    }
                    doubles[index] = (Double) map.get(sensorKey);
                    doubleMap.put(sensor, doubles);
                }
            }
        }
        dataList.add(doubleMap);
        String part = parameters.get("part").toString();
        resultMap.put("data" + part, dataList);
        resultMap.put("sensors" + part, new ArrayList<Object>(sensors));
        return resultMap;
    }
    
    @Override
    public List<Map<String, Object>> getMonitorPointOrDeviceAvgData(Map<String, Object> parameters) throws Exception {
@@ -268,11 +298,11 @@
            parameters.put("sensorKeyColumn", sensorKeyColumn);
        }
        sensorKeys.clear();
        //sensorKeys.clear();
        // 监控点平均值
        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);
@@ -285,9 +315,8 @@
                        Integer deviceVersionId = (Integer) map.get("deviceVersionId");
                        if (!parameter.containsKey("sensorKeyColumn")) {
                            List<Map<String, Object>> sensors = sensorMapper.getSensorsByDeviceVersionId(deviceVersionId);
                            String sensorKeyColumn = getSensorKeyColumnBySensors(sensors);
                            String sensorKeyColumn = getSensorKeyColumnBySensors(sensors, parameters);
                            parameter.put("sensorKeyColumn", sensorKeyColumn);
                            parameters.put("sensors", sensorKeys);
                        }
                        List<String> macs = deviceMapper.getDeviceMacByMonitorPointAndDeviceVersion(monitorPointId, deviceVersionId);
                        if (ObjectUtils.isEmpty(macs) || macs.contains("null") ) {
@@ -348,9 +377,8 @@
                device.setMac(mac);
                device = deviceMapper.selectOne(device);
                List<Map<String, Object>> sensors = sensorMapper.getSensorsByDeviceVersionId(device.getDeviceVersionId());
                String sensorKeyColumn = getSensorKeyColumnBySensors(sensors);
                String sensorKeyColumn = getSensorKeyColumnBySensors(sensors,parameters);
                parameters.put("sensorKeyColumn", sensorKeyColumn);
                parameters.put("sensors", sensorKeys);
                
            }
            resul = historyMinutelyMapper.getMonitorPointOrDeviceAvgData(parameters);
@@ -358,17 +386,22 @@
        return resul;
    }
    private String getSensorKeyColumnBySensors(List<Map<String, Object>> sensors) {
    private String getSensorKeyColumnBySensors(List<Map<String, Object>> sensors,Map<String, Object> parameters) {
        String sensorKeyColumn = "";
        Set<String> sensorKeys = new HashSet<String>();
        if (parameters.containsKey("sensors")) {
            sensorKeys = (Set<String>) parameters.get("sensors");
        }
        for (int i = 0; i < sensors.size(); i++) {
            String sensorKey = (String) sensors.get(i).get("sensor_key");
            String name = (String) sensors.get(i).get("name");
            String unit = (String) sensors.get(i).get("unit");
            sensorKeyColumn += "AVG(json -> '$." + sensorKey + "[0]') " + sensorKey;
            if (i != sensors.size() - 1) {
                sensorKeyColumn += " ,";
            } 
            sensorKeys.add(sensorKey + "-" + name);
            sensorKeys.add(sensorKey + "-" + name + "-" + unit );
            parameters.put("sensors", sensorKeys);
        }
        return sensorKeyColumn;
    }
src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java
@@ -171,4 +171,14 @@
        }).collect(Collectors.toList());
        return list;
    }
    @Override
    public List<MonitorPoint> getMonitorPointsByOrganizationId(Integer orgId) {
        MonitorPoint monitorPoint = new MonitorPoint();
        monitorPoint.setIsDelete(Constants.IS_DELETE_FALSE);
        if (Constants.isNotSpecialOrgId(orgId)) {
            monitorPoint.setOrganizationId(orgId);
        }
        return monitorPointMapper.select(monitorPoint);
    }
}
src/main/resources/mapper/HistoryMinutelyMapper.xml
@@ -2,7 +2,7 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.moral.mapper.HistoryMinutelyMapper">
    <select id="getSersionAvgByDevice" resultType="map">
    <select id="getSersionAvgByDevice" resultType="java.util.LinkedHashMap">
        SELECT
            ${sensorKeyColumn}
        FROM
@@ -13,7 +13,7 @@
        AND time &lt; #{end}
    </select>
    
    <select id="getMonitorPointOrDeviceAvgData" resultType="map">
    <select id="getMonitorPointOrDeviceAvgData" resultType="java.util.LinkedHashMap">
        SELECT
            DATE_FORMAT(time, #{typeFormat}) time,
            ${sensorKeyColumn}