xufenglei
2018-03-30 9b33e93fccddea356c3d686684fc58ceb176ff39
增加 省市区 查询条件
7 files modified
193 ■■■■■ changed files
src/main/java/com/moral/controller/MonitorPointController.java 11 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/mapper/SensorMapper.java 3 ●●●●● 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 115 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java 9 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/HistoryMinutelyMapper.xml 24 ●●●● patch | view | raw | blame | history
src/main/resources/mapper/SensorMapper.xml 29 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/controller/MonitorPointController.java
@@ -6,9 +6,13 @@
import com.moral.service.MonitorPointService;
import org.springframework.web.bind.annotation.*;
import static com.moral.common.util.WebUtils.getParametersStartingWith;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@RestController
@@ -53,4 +57,11 @@
        return new ResultBean<List<MonitorPoint>>(monitorPoints);
    }
    @GetMapping("list/region")
    public ResultBean<List<MonitorPoint>> getMonitorPointsByRegion(HttpServletRequest request) {
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        List<MonitorPoint> monitorPoints = monitorPointService.getMonitorPointsByRegion(parameters);
        return new ResultBean<List<MonitorPoint>>(monitorPoints);
    }
}
src/main/java/com/moral/mapper/SensorMapper.java
@@ -10,4 +10,7 @@
    List<Sensor> selectByVersionId(Integer deviceVersionId);
    List<Map<String, Object>> getSensorsByDeviceVersionId(Integer deviceVersionId);
    List<Sensor> getSensorsByCriteria(Map<String, Object> parameters);
}
src/main/java/com/moral/service/MonitorPointService.java
@@ -24,4 +24,6 @@
    List<Map<String,String>> queryMonitroPointsState(List<Integer> idList);
    List<MonitorPoint> getMonitorPointsByOrganizationId(Integer orgId);
    List<MonitorPoint> getMonitorPointsByRegion(Map<String, Object> parameters);
}
src/main/java/com/moral/service/impl/HistoryMinutelyServiceImpl.java
@@ -252,8 +252,6 @@
    
    @Override
    public List<Map<String, Object>> getMonitorPointOrDeviceAvgData(Map<String, Object> parameters) throws Exception {
        List<Map<String, Object>> resultList ;
        String type = (String) parameters.get("type");
        if (!parameters.containsKey("field")) {
            parameters.putAll(getElementByType(type));
@@ -273,123 +271,32 @@
        parameters.put("start", start);
        parameters.put("end", end);
        List<String> sensorKeys = new ArrayList<String>();
        Set<String> sensorsSet = new HashSet<String>();
        if (parameters.containsKey("sensorKeys")) {
            List<String> sensors;
            try {
                sensors = JSON.parseObject((String)parameters.get("sensors"), new TypeReference<List<String>>() {});
                parameters.put("sensors", new HashSet<String>(sensors));
                List<String> sensorKeys = new ArrayList<String>();
                for (String sensor : sensors) {
                    sensorKeys.add(sensor.split("-")[0]);
                }
                parameters.put("sensorKeys", sensorKeys);
            } catch (Exception e) {
                sensors = (List<String>) parameters.get("sensorKeys");
                sensorKeys = (List<String>)parameters.get("sensorKeys");
                sensors = sensorKeys;
            }
        }
        //sensorKeys.clear();
        // 监控点平均值
        if (!parameters.containsKey("mac")) {
            resultList = new ArrayList<Map<String,Object>>();
            Integer monitorPointId = Integer.valueOf(parameters.get("monitorPointId").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);
            List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
            for (Map<String, Object> map : deviceVersions) {
                cs.submit(new Callable<List<Map<String, Object>>>() {
                    @Override
                    public List<Map<String, Object>> call() throws Exception {
                        Map<String, Object> parameter = new HashMap<String, Object>(parameters);
                        Integer deviceVersionId = (Integer) map.get("deviceVersionId");
                        if (!parameter.containsKey("sensorKeys")) {
                            List<Map<String, Object>> sensors = sensorMapper.getSensorsByDeviceVersionId(deviceVersionId);
                            List<String> sensorKeys = getSensorKeyColumnBySensors(sensors, parameters);
                            parameter.put("sensorKeys", sensorKeys);
                        }
                        List<String> macs = deviceMapper.getDeviceMacByMonitorPointAndDeviceVersion(monitorPointId, deviceVersionId);
                        if (ObjectUtils.isEmpty(macs) || macs.contains("null") ) {
                            return new ArrayList<Map<String,Object>>();
            sensorsSet = new HashSet<String>(sensors);
                        }else {
                            parameter.put("macs", macs);
                            return historyMinutelyMapper.getMonitorPointOrDeviceAvgData(parameter);
            List<Sensor> sensors = sensorMapper.getSensorsByCriteria(parameters);
            for (Sensor sensor : sensors) {
                sensorKeys.add(sensor.getSensorKey());
                sensorsSet.add(sensor.getSensorKey() + "-" + sensor.getName() + "-" + sensor.getUnit());
                        }
                    }
                });
            }
            for (Map<String, Object> map : deviceVersions) {
                list.addAll(cs.take().get());
            }
            Map<String, Map<String, Number[]>> result = new LinkedHashMap<String, Map<String, Number[]>>();
            for (Map<String, Object> map : list) {
                time = (String) map.remove("time");
                Map<String, Number[]> times = new HashMap<String, Number[]>();
                if (result.containsKey(time)) {
                    times = (Map<String, Number[]>) result.get(time);
                }
                for (Map.Entry<String,Object> entry  : map.entrySet()) {
                    String sensorKey = entry.getKey();
                    Number[] doubles = new Number[3];
                    Double sum = (Double) entry.getValue();
                    Integer count = 1;
                    if (times.containsKey(sensorKey)) {
                        doubles = times.get(sensorKey);
                        sum = (Double) doubles[0] + sum;
                        count = (Integer) doubles[1] + 1;
                    }
                    doubles[0] = sum;
                    doubles[1] = count;
                    doubles[2] = new BigDecimal(sum / count).setScale(3, RoundingMode.HALF_UP).doubleValue();
                    times.put(sensorKey, doubles);
                }
                result.put(time, times);
            }
            Iterator<Entry<String, Map<String, Number[]>>> iterator = result.entrySet().iterator();
            while(iterator.hasNext()){
                Entry<String, Map<String, Number[]>> entry = iterator.next();
                String resultTime = entry.getKey();
                Map<String, Number[]> value = entry.getValue();
                Map<String, Object> resultMap = new HashMap<String, Object>();
                resultMap.put("time", resultTime);
                for (String sensorKey : value.keySet()) {
                    resultMap.put(sensorKey, value.get(sensorKey)[2]);
                }
                resultList.add(resultMap);
            }
        // 设备 平均值
        } else {
            // 所有传感器
            if (!parameters.containsKey("sensorKeys")) {
                String mac = (String) parameters.get("mac");
                Device device = new Device();
                device.setMac(mac);
                device = deviceMapper.selectOne(device);
                List<Map<String, Object>> sensors = sensorMapper.getSensorsByDeviceVersionId(device.getDeviceVersionId());
                List<String> sensorKeys = getSensorKeyColumnBySensors(sensors,parameters);
                parameters.put("sensorKeys", sensorKeys);
        parameters.put("sensors", sensorsSet);
                
            }
            resultList = historyMinutelyMapper.getMonitorPointOrDeviceAvgData(parameters);
        }
        return resultList;
    }
    private List<String> getSensorKeyColumnBySensors(List<Map<String, Object>> sensors,Map<String, Object> parameters) {
        Set<String> sensorSet = new HashSet<String>();
        List<String> sensorKeys = new ArrayList<String>();
        if (parameters.containsKey("sensors")) {
            sensorSet = (Set<String>) parameters.get("sensors");
        }
        for (Map<String, Object> sensor : sensors) {
            String sensorKey = (String) sensor.get("sensor_key");
            String name = (String) sensor.get("name");
            String unit = (String) sensor.get("unit");
            sensorSet.add(sensorKey + "-" + name + "-" + unit );
            sensorKeys.add(sensorKey);
        }
        parameters.put("sensors", sensorSet);
        return  sensorKeys;
        return historyMinutelyMapper.getMonitorPointOrDeviceAvgData(parameters);
    }
    @Override
src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java
@@ -204,4 +204,13 @@
        example.orderBy("name").asc();
        return monitorPointMapper.selectByExample(example);
    }
    @Override
    public List<MonitorPoint> getMonitorPointsByRegion(Map<String, Object> parameters) {
        Example example = new Example(MonitorPoint.class);
        Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete", Constants.IS_DELETE_FALSE);
        criteria.andEqualTo(parameters.get("name").toString(), parameters.get("value"));
        return monitorPointMapper.selectByExample(example);
    }
}
src/main/resources/mapper/HistoryMinutelyMapper.xml
@@ -34,10 +34,26 @@
        </if>
        <if test="mac == null">
        AND    h.mac IN
            <foreach  collection="macs" open="(" separator="," close=")" item="listItem">
                #{listItem}
            </foreach>
            (SELECT
                d.mac
            FROM
                device d,
                monitor_point mp
            WHERE
                d.monitor_point_id = mp.id
                <if test="provinceCode != null">
                AND mp.province_code = #{provinceCode}
                </if>
                <if test="cityCode != null">
                AND mp.city_code = #{cityCode}
                </if>
                <if test="areaCode != null">
                AND mp.area_code = #{areaCode}
                </if>
                <if test="monitorPointId != null">
                AND mp.id =    #{monitorPointId}
                </if>
            )
        </if>
        
        GROUP BY
src/main/resources/mapper/SensorMapper.xml
@@ -34,5 +34,34 @@
            dvs.sensor_id = s.id 
            AND dvs.device_version_id = #{deviceVersionId}        
    </select>
      <select id="getSensorsByCriteria" resultMap="BaseResultMap">
        SELECT
            DISTINCT s.*
        FROM
            sensor s,
            device_version_sensor dvs,
            device d,
            monitor_point mp
        WHERE
            s.id = dvs.sensor_id
            AND dvs.device_version_id = d.device_version_id
            AND d.monitor_point_id = mp.id
            <if test="provinceCode != null">
            AND mp.province_code = #{provinceCode}
            </if>
            <if test="cityCode != null">
            AND mp.city_code = #{cityCode}
            </if>
            <if test="areaCode != null">
            AND mp.area_code = #{areaCode}
            </if>
            <if test="monitorPointId != null">
            AND mp.id =    #{monitorPointId}
            </if>
            <if test="mac != null">
            AND d.mac =    #{mac}
            </if>
    </select>
  
</mapper>