lizijie
2020-08-07 2edb89372b20625cedf80821dc90b7f7ae8c310c
根据时间段获取数据
13 files modified
167 ■■■■■ changed files
src/main/java/com/moral/controller/ScreenController.java 113 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/mapper/DeviceMapper.java 2 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/mapper/HistoryDailyMapper.java 3 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/mapper/HistoryHourlyMapper.java 1 ●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/DeviceService.java 2 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/HistoryDailyService.java 2 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/HistoryHourlyService.java 3 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/impl/DeviceServiceImpl.java 6 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/impl/HistoryDailyServiceImpl.java 7 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/impl/HistoryHourlyServiceImpl.java 6 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/DeviceMapper.xml 6 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/HistoryDailyMapper.xml 8 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/HistoryHourlyMapper.xml 8 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/controller/ScreenController.java
@@ -9,16 +9,7 @@
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.Resource;
@@ -1995,4 +1986,106 @@
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        weatherService.updateForecastWeather(parameters);
    }
    @GetMapping("dataByTimeSlot")
    @ApiOperation(value = "根据时间类型获取因子的值", notes = "根据时间类型获取因子的值")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "mac", value = "mac地址", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "起始时间(格式:2020-08-03/2020-08-03-13)", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间(格式:2020-08-04/2020-08-04-14)", required = true, paramType = "query", dataType = "String")})
    public ResultBean<List<Map<String, Object>>> getDataByTimeSlot(HttpServletRequest request) throws Exception {
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        String mac = parameters.get("mac").toString();
        String startTime = parameters.get("startTime").toString();
        String endTime = parameters.get("endTime").toString();
        //对时间进行操作的接口
        Calendar calendar = Calendar.getInstance();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        List<Map<String, Object>> list = new ArrayList();
        Map device = deviceService.getAllFieldByMac(parameters);
        Map monitorPointMap = monitorPointService.selectAllById(device.get("monitor_point_id").toString());
        Map<String, String> sensotMap = sensorService.getSensorsMap(parameters);
        if (startTime.length()==10&&endTime.length()==10){
            //获取当日时间
            Date dd = new Date();
            //格式化
            SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
            //获取系统当前时间
            String nowTime = sim.format(dd);
            int days = endTime.compareTo(nowTime);
            System.out.println(days);
            /*if (days<0){
                Date date=null;
                try {
                    date = new SimpleDateFormat("yy-MM-dd").parse(endTime);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                calendar.setTime(date);
                int day=calendar.get(Calendar.DATE);
                calendar.set(Calendar.DATE,day+1);
                endTime=new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
            }*/
            List<Map<String, Object>> sameDayHourlyList = null;
            if (days>=0){
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //获取系统当前时间
                String currentTime = df.format(dd);
                sameDayHourlyList = historyHourlyService.getDataByTimeSlot(mac,nowTime,currentTime);
            }
            List<Map<String, Object>> dataList = historyDailyService.getDataByTimeSlot(mac,startTime,endTime);
            Set<String> keys = sensotMap.keySet();
            for (String key:keys) {
                Map<String,Object> oneSensorMap = new LinkedHashMap<>();
                oneSensorMap.put("monitorPointName",monitorPointMap.get("name"));
                oneSensorMap.put("deviceName",device.get("name"));
                oneSensorMap.put("sersorKey",sensotMap.get(key));
                for (Map<String, Object> dataMap: dataList) {
                    JSONObject dataJson = JSONObject.parseObject(dataMap.get("json").toString());
                    oneSensorMap.put(dataMap.get("time").toString().substring(0,10),dataJson.getJSONArray(key).get(2));
                }
                Double avg = 0.0;
                if (sameDayHourlyList != null){
                    for (Map sameDayHourlyMap:sameDayHourlyList){
                        List<Double> arr = new ArrayList<>();
                        JSONObject hourlyJson = JSONObject.parseObject(sameDayHourlyMap.get("json").toString());
                        arr.add(Double.parseDouble(hourlyJson.getJSONArray(key).get(2).toString()));
                        Double sum = 0.0;
                        for (int i=0;i<arr.size();i++){
                            sum += arr.get(i);
                        }
                        avg = sum / arr.size();
                    }
                    oneSensorMap.put(nowTime,avg);
                }
                list.add(oneSensorMap);
            }
        }else if (startTime.length()==13&&endTime.length()==13){
            StringBuilder startTime_sb = new StringBuilder(startTime);
            startTime_sb.replace(10,11," ");
            startTime = startTime_sb.toString()+":00:00";
            StringBuilder endTime_sb = new StringBuilder(endTime);
            endTime_sb.replace(10,11," ");
            endTime = endTime_sb.toString()+":00:00";
            List<Map<String, Object>> hourlyList = historyHourlyService.getDataByTimeSlot(mac,startTime,endTime);
            Set<String> keys = sensotMap.keySet();
            for (String key:keys) {
                Map<String,Object> oneSensorMap = new LinkedHashMap<>();
                oneSensorMap.put("monitorPointName",monitorPointMap.get("name"));
                oneSensorMap.put("deviceName",device.get("name"));
                oneSensorMap.put("sersorKey",sensotMap.get(key));
                for (Map<String, Object> hourlyMap: hourlyList) {
                    JSONObject hourlyJson = JSONObject.parseObject(hourlyMap.get("json").toString());
                    oneSensorMap.put(hourlyMap.get("time").toString().substring(0,19),hourlyJson.getJSONArray(key).get(2));
                }
                list.add(oneSensorMap);
            }
        }else {
            return null;
        }
        return new ResultBean<List<Map<String, Object>>>(list);
    }
}
src/main/java/com/moral/mapper/DeviceMapper.java
@@ -56,4 +56,6 @@
    List<Device> selectDevicesAll(Map<String, Object> params);
    List<Device> getDeviceListByMonitorPointIds(List<Integer> id);
    Map<String, Object> selectAllFieldByMac(Map<String, Object> params);
}
src/main/java/com/moral/mapper/HistoryDailyMapper.java
@@ -1,5 +1,7 @@
package com.moral.mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
@@ -12,4 +14,5 @@
    // 根据选择参数返回检测因子的数值
    Map<String, Double> getTraceabilityData(Map<String, Object> parameters);
    List<Map<String, Object>> getDataByTimeSlot(@Param("mac") String mac, @Param("startTime") String startTime, @Param("endTime") String endTime);
}
src/main/java/com/moral/mapper/HistoryHourlyMapper.java
@@ -21,4 +21,5 @@
    List<Map<String,Object>> getBeamByMacs(Map<String,Object> params);
    List<Map<String, Object>> getDataByTimeSlot(@Param("mac") String mac, @Param("startTime") String startTime, @Param("endTime") String endTime);
}
src/main/java/com/moral/service/DeviceService.java
@@ -70,4 +70,6 @@
    List<Device> getDeviceById3(int id);
    List<Device> queryDevice(Map<String,Object> map);
    Map<String, Object> getAllFieldByMac(Map<String, Object> parameters);
}
src/main/java/com/moral/service/HistoryDailyService.java
@@ -9,4 +9,6 @@
    Map getOverproofData(Map<String, Object> parameters) throws Exception;
    Map<String, Double> getTraceabilityData(Map<String, Object> parameters) throws Exception;
    List<Map<String, Object>> getDataByTimeSlot(String mac, String startTime, String endTime) throws Exception;
}
src/main/java/com/moral/service/HistoryHourlyService.java
@@ -1,5 +1,6 @@
package com.moral.service;
import java.util.List;
import java.util.Map;
import com.moral.entity.Point;
@@ -21,4 +22,6 @@
    //获取historyHourly的小时数据
    Map<String,Object> getDataByMac(String mac,String time);
    List<Map<String, Object>> getDataByTimeSlot(String mac, String startTime, String endTime) throws Exception;
}
src/main/java/com/moral/service/impl/DeviceServiceImpl.java
@@ -697,4 +697,10 @@
        }
        return deviceListHasWind;
    }
    @Override
    public Map<String, Object> getAllFieldByMac(Map<String, Object> parameters) {
        Map<String, Object> deviceMap = deviceMapper.selectAllFieldByMac(parameters);
        return deviceMap;
    }
}
src/main/java/com/moral/service/impl/HistoryDailyServiceImpl.java
@@ -129,4 +129,11 @@
        return resultMap;
    }
    @Override
    public List<Map<String, Object>> getDataByTimeSlot(String mac, String startTime, String endTime) throws Exception {
        List<Map<String, Object>> resultMap = historyDailyMapper.getDataByTimeSlot(mac,startTime,endTime);
        //System.out.println(resultMap);
        return resultMap;
    }
}
src/main/java/com/moral/service/impl/HistoryHourlyServiceImpl.java
@@ -115,6 +115,12 @@
    }
    @Override
    public List<Map<String, Object>> getDataByTimeSlot(String mac, String startTime, String endTime) throws Exception {
        List<Map<String, Object>> resultMap = historyHourlyMapper.getDataByTimeSlot(mac,startTime,endTime);
        return resultMap;
    }
    @Override
    public Point getDirPoint(Map<String, Object> parameters) throws Exception {
        Map<String, Object> pollutionSourceData = getPollutionSourceDataByHour(parameters);
        if (MapUtils.isEmpty(pollutionSourceData)) {
src/main/resources/mapper/DeviceMapper.xml
@@ -485,4 +485,10 @@
        select * from device where monitor_point_id in
        <foreach  item="item" collection="list" index="index"  open="(" separator="," close=")">#{item}</foreach>
    </select>
    <select id="selectAllFieldByMac" resultType="java.util.Map">
            SELECT *
            FROM device
            WHERE mac = #{mac}
    </select>
</mapper>
src/main/resources/mapper/HistoryDailyMapper.xml
@@ -119,4 +119,12 @@
            </if>
        </where>
    </select>
    <!-- 根据设备mac地址,时间,污染因子查询一天的平均值 -->
    <select id="getDataByTimeSlot" resultType="java.util.Map">
        SELECT *
        FROM history_daily
        where mac=#{mac}
        AND time between #{startTime} and #{endTime}
    </select>
</mapper>
src/main/resources/mapper/HistoryHourlyMapper.xml
@@ -77,4 +77,12 @@
        and h.json->'$.e12[0]' is not null
        group by DATE_FORMAT(h.time,'%Y-%m-%d %H:%i:%s')
    </select>
    <!-- 根据设备mac地址,时间,污染因子查询一天的平均值 -->
    <select id="getDataByTimeSlot" resultType="java.util.Map">
        SELECT *
        FROM history_hourly
        where mac=#{mac}
        AND time between #{startTime} and #{endTime}
    </select>
</mapper>