jinpengyong
2021-08-02 dce0021131e955bac968cb2a7e24ce3673eb1a3b
监测因子趋势图
7 files modified
212 ■■■■ changed files
screen-api/src/main/java/com/moral/api/controller/WebController.java 37 ●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/pojo/form/device/MonitorPointQueryForm.java 2 ●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/DeviceService.java 4 ●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java 134 ●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/MonitorPointServiceImpl.java 23 ●●●●● patch | view | raw | blame | history
screen-api/src/main/resources/mapper/DeviceMapper.xml 7 ●●●● patch | view | raw | blame | history
screen-common/src/main/java/com/moral/constant/Constants.java 5 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/controller/WebController.java
@@ -10,6 +10,8 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@@ -26,9 +28,10 @@
/**
 * web端接口
 * @author  moral
 * @version  v1.0
 * */
 *
 * @author moral
 * @version v1.0
 */
@Slf4j
@Api(tags = {"web端接口"})
@RestController
@@ -53,7 +56,7 @@
    /**
     * @param request 请求信息
     * @return 返回请求成功后的对象信息
     * */
     */
    @GetMapping("getHourlyAqi")
    @ApiOperation(value = "获取一小时AQI", notes = "获取一小时AQI")
    @ApiImplicitParams(value = {
@@ -72,7 +75,7 @@
    /**
     * @param request 请求信息
     * @return 返回请求成功后的对象信息
     * */
     */
    @GetMapping("getMonthAvg")
    @ApiOperation(value = "获取某设备某因子本月均值", notes = "获取某设备某因子本月均值")
    @ApiImplicitParams(value = {
@@ -92,7 +95,7 @@
    /**
     * @param request 请求信息
     * @return 返回请求成功后的对象信息
     * */
     */
    @GetMapping("getWindData")
    @ApiOperation(value = "获取风场数据", notes = "获取风场数据")
    @ApiImplicitParams(value = {
@@ -113,38 +116,38 @@
    /**
     * @param request 请求信息
     * @return 返回请求成功后的对象信息
     * */
     */
    @GetMapping("getMacSensors")
    @ApiOperation(value = "根据mac获取对应所有因子", notes = "根据mac获取对应所有因子")
    @ApiOperation(value = "根据多个mac获取公有因子", notes = "根据多个mac获取公有因子")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "mac", value = "设备mac", required = true, paramType = "query", dataType = "String")
            @ApiImplicitParam(name = "macs", value = "设备mac,多个逗号隔开", required = true, paramType = "query", dataType = "String")
    })
    public ResultMessage getMacSensors(HttpServletRequest request) {
        Map<String, Object> params = WebUtils.getParametersStartingWith(request, null);
        if (!params.containsKey("mac")) {
        if (!params.containsKey("macs")) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        Map<String, Object> response = deviceService.getSensorsByMac(params.get("mac").toString());
        Map<String, Object> response = deviceService.getSensorsByMac(params);
        return ResultMessage.ok(response);
    }
    /**
     * @param request 请求信息
     * @return 返回请求成功后的对象信息
     * */
     */
    @GetMapping("getTrendChartData")
    @ApiOperation(value = "获取监测因子趋势图数据", notes = "获取监测因子趋势图数据")
    @ApiOperation(value = "多设备单监测因子趋势图", notes = "多设备单监测因子趋势图.")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "mac", value = "设备mac", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "macs", value = "设备mac,多个逗号隔开", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sensorCode", value = "因子code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "数据类型,日(day),月(month),年(year)", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "time", value = "数据时间", required = true, paramType = "query", dataType = "String")
            @ApiImplicitParam(name = "times", value = "数据时间,日(2021-07-29),月(2021-07),年(2021),多个逗号隔开", required = true, paramType = "query", dataType = "String")
    })
    public ResultMessage getTrendChartData(HttpServletRequest request) {
        Map<String, Object> params = WebUtils.getParametersStartingWith(request, null);
        if (!params.containsKey("mac") || !params.containsKey("sensorCode") || !params.containsKey("type") || !params.containsKey("time")) {
        if (!params.containsKey("macs") || !params.containsKey("sensorCode") || !params.containsKey("type") || !params.containsKey("times")) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        List<Map<String, Object>> response = deviceService.getTrendChartData(params);
@@ -154,7 +157,7 @@
    /**
     * @param request 请求信息
     * @return 返回请求成功后的对象信息
     * */
     */
    @GetMapping("getMapPath")
    @ApiOperation(value = "获取当前用户地图权限", notes = "获取当前用户地图权限")
    @ApiImplicitParams(value = {
screen-api/src/main/java/com/moral/api/pojo/form/device/MonitorPointQueryForm.java
@@ -25,7 +25,7 @@
    Integer regionCode;
    public boolean valid(){
        if(ObjectUtils.isEmpty(organizationId)||ObjectUtils.isEmpty(regionCode))
        if(ObjectUtils.isEmpty(organizationId))
            return false;
        return true;
    }
screen-api/src/main/java/com/moral/api/service/DeviceService.java
@@ -19,8 +19,8 @@
    //根据站点id获取设备列表
    List<Device> getDevicesByMonitorPointId(Integer monitorPointId);
    //根据mac获取因子信息
    Map<String, Object> getSensorsByMac(String mac);
    //根据macs获取公有因子信息
    Map<String, Object> getSensorsByMac(Map<String,Object> params);
    //获取监测因子趋势图数据
    List<Map<String, Object>> getTrendChartData(Map<String,Object> params);
screen-api/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java
@@ -1,12 +1,13 @@
package com.moral.api.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.Device;
import com.moral.api.entity.Sensor;
import com.moral.api.mapper.DeviceMapper;
import com.moral.api.service.DeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.service.HistoryHourlyService;
import com.moral.constant.Constants;
import com.moral.constant.RedisConstants;
import com.moral.util.DateUtils;
@@ -16,9 +17,18 @@
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;
/**
 * <p>
@@ -42,49 +52,107 @@
        QueryWrapper<Device> wrapper = new QueryWrapper();
        wrapper.eq("monitor_point_id",monitorPointId);
        wrapper.eq("is_delete", Constants.NOT_DELETE);
        List<Device> devices = deviceMapper.selectList(wrapper);
        return devices;
        return deviceMapper.selectList(wrapper);
    }
    @Override
    public Map<String, Object> getSensorsByMac(String mac) {
        //从redis中获取设备因子信息
        Device device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, mac);
        List<Sensor> sensors = device.getVersion().getSensors();
        Map<String, Object> result = new HashMap<>();
        for (Sensor sensor : sensors) {
            String sensorCode = sensor.getCode();
            String name = sensor.getName();
            result.put(sensorCode, name);
    public Map<String, Object> getSensorsByMac(Map<String, Object> params) {
        String[] macs = params.get("macs").toString().split(",");
        List<Map<String, Object>> elementLists = new ArrayList<>();
        for (String mac : macs) {
            //从redis中获取设备因子信息
            Device device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, mac);
            List<Sensor> sensors = device.getVersion().getSensors();
            Map<String, Object> map = new HashMap<>();
            for (Sensor sensor : sensors) {
                String sensorCode = sensor.getCode();
                String name = sensor.getName();
                map.put(sensorCode, name);
            }
            elementLists.add(map);
        }
        return result;
        Map<String, Object> map = elementLists.parallelStream()
                .filter(elementList -> elementList.size() != 0)
                .reduce((a, b) -> {
                    a.keySet().retainAll(b.keySet());
                    return a;
                }).orElse(new HashMap<>());
        return map;
    }
    @Override
    public List<Map<String, Object>> getTrendChartData(Map<String, Object> params) {
        Object type = params.get("type");
        String start = params.remove("time").toString();
        //设备mac
        String[] macs = params.remove("macs").toString().split(",");
        //所选时间
        String[] times = params.remove("times").toString().split(",");
        //因子code
        String sensorCode = params.get("sensorCode").toString();
        String end;
        String timeUnits;
        String dateFormat;
        if ("day".equals(type)) {
            end = DateUtils.getDateAddDay(start, 1);
            timeUnits = "hourly";
            dateFormat = "%H";
        } else if ("month".equals(type)) {
            end = DateUtils.getDateAddMonth(start, 1);
            timeUnits = "daily";
            dateFormat = "%d";
        } else {
            end = DateUtils.getDateAddYear(start, 1);
            timeUnits = "monthly";
            dateFormat = "%m";
        }
        params.put("dateFormat", dateFormat);
        params.put("timeUnits", timeUnits);
        params.put("start", start);
        params.put("end", end);
        return deviceMapper.getTrendChartData(params);
    }
        //返回结果集,time=data
        List<Map<String, Object>> result = new ArrayList<>();
        for (String start : times) {
            Map<String, Object> resultMap = new HashMap<>();
            if ("day".equals(type)) {
                end = DateUtils.getDateAddDay(start, 1);
                timeUnits = "hourly";
                dateFormat = "%k";
            } else if ("month".equals(type)) {
                end = DateUtils.getDateAddMonth(start, 1);
                timeUnits = "daily";
                dateFormat = "%e";
            } else {
                end = DateUtils.getDateAddYear(start, 1);
                timeUnits = "monthly";
                dateFormat = "%c";
            }
            params.put("timeUnits", timeUnits);
            params.put("dateFormat", dateFormat);
            params.put("start", start);
            params.put("end", end);
            params.put("macs", macs);
            //获取多设备指定因子数据
            List<Map<String, Object>> list = deviceMapper.getTrendChartData(params);
            //按time分组
            Map<String, List<Map<String, Object>>> data = list.parallelStream()
                    .collect(Collectors.groupingBy(o -> o.get("time").toString()));
            //TreeMap<String, List<Map<String, Object>>> data = new TreeMap<>(listMap);
            for (Map.Entry<String, List<Map<String, Object>>> entry : data.entrySet()) {
                List<Object> values = new ArrayList<>();
                String time = entry.getKey();
                List<Map<String, Object>> value = entry.getValue();
                if (value.isEmpty()) {
                    continue;
                }
                for (String mac : macs) {
                    boolean flag = false;
                    for (Map<String, Object> map : value) {
                        if (mac.equals(map.get("mac"))) {
                            Object o = map.get(sensorCode);
                            values.add(o);
                            flag = true;
                            break;
                        }
                    }
                    if (!flag) {
                        values.add("");
                    }
                }
                resultMap.put(time, values);
            }
            result.add(resultMap);
        }
        return result;
    }
}
screen-api/src/main/java/com/moral/api/service/impl/MonitorPointServiceImpl.java
@@ -12,6 +12,7 @@
import com.moral.api.service.OrganizationService;
import com.moral.constant.Constants;
import com.moral.util.RegionCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -20,7 +21,7 @@
/**
 * <p>
 *  服务实现类
 * 服务实现类
 * </p>
 *
 * @author moral
@@ -41,7 +42,11 @@
        //取参
        Integer organizationId = form.getOrganizationId();
        Integer regionCode = form.getRegionCode();
        String region = RegionCodeUtils.regionCodeConvertToName(regionCode);
        String region = null;
        if (regionCode != null) {
            region = RegionCodeUtils.regionCodeConvertToName(regionCode);
        }
        //查询子组织
        List<Organization> childrenOrganization = organizationService.getChildrenOrganizationsById(organizationId);
        List<Integer> organizationIds = new ArrayList<>();
@@ -51,8 +56,12 @@
        organizationIds.add(organizationId);
        //查询站点
        QueryWrapper<MonitorPoint> queryMonitorPointsWrapper = new QueryWrapper<>();
        queryMonitorPointsWrapper.eq(region,regionCode);
        queryMonitorPointsWrapper.in("organization_id",organizationIds);
        //如果region不为空,就查询当前组织,所选城市下所有站点及设备信息
        if (region != null){
            queryMonitorPointsWrapper.eq(region, regionCode);
        }
        queryMonitorPointsWrapper.in("organization_id", organizationIds);
        queryMonitorPointsWrapper.eq("is_delete", Constants.NOT_DELETE);
        List<MonitorPoint> monitorPoints = monitorPointMapper.selectList(queryMonitorPointsWrapper);
        //查询站点对应的设备
@@ -69,9 +78,9 @@
        Organization stateControlStationOrganization = organizationService.getStateControlStation();
        //获取国控站组织下的所有站点
        QueryWrapper<MonitorPoint> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",Constants.NOT_DELETE);
        queryWrapper.eq("organization_id",stateControlStationOrganization.getId());
        queryWrapper.eq(RegionCodeUtils.regionCodeConvertToName(regionCode),regionCode);
        queryWrapper.eq("is_delete", Constants.NOT_DELETE);
        queryWrapper.eq("organization_id", stateControlStationOrganization.getId());
        queryWrapper.eq(RegionCodeUtils.regionCodeConvertToName(regionCode), regionCode);
        return monitorPointMapper.selectList(queryWrapper);
    }
}
screen-api/src/main/resources/mapper/DeviceMapper.xml
@@ -28,11 +28,14 @@
    <!--监测因子趋势图数据-->
    <select id="getTrendChartData" resultType="java.util.Map">
        SELECT
        SELECT mac,
        DATE_FORMAT(`time`,#{dateFormat}) AS `time`,
        `value`->'$.${sensorCode}' AS '${sensorCode}'
        FROM history_${timeUnits}
        WHERE mac = #{mac}
        WHERE mac IN
            <foreach collection="macs" item="mac" index="index" open="(" close=")" separator=",">
                #{mac}
            </foreach>
        AND `time` <![CDATA[>=]]> #{start}
        AND `time` <![CDATA[<]]> #{end}
        ORDER BY `time`
screen-common/src/main/java/com/moral/constant/Constants.java
@@ -120,6 +120,11 @@
    public static final String UN_ADJUST = "unadjust";
    /*
     * 中间表后准
     * */
    public static final String TRANSITION = "transition";
    /*
     * 离线设备状态码
     * */
    public static final String DEVICE_STATE_OFFLINE = "0";