kaiyu
2020-11-30 8bb0e02e8fd166f35782870983fd2140142df409
src/main/java/com/moral/controller/WebController.java
@@ -6,6 +6,7 @@
import com.moral.common.util.*;
import com.moral.common.webAnno.UserLoginToken;
import com.moral.entity.*;
import com.moral.mapper.MapPathMapper;
import com.moral.service.*;
import com.moral.util.DateUtil;
import com.moral.util.LatLngTransformation;
@@ -20,8 +21,7 @@
import java.util.*;
import java.util.stream.Collectors;
import org.apache.log4j.Logger;
import static com.moral.common.util.WebUtils.getParametersStartingWith;
@RestController
@RequestMapping("/web")
@@ -29,7 +29,6 @@
@SuppressWarnings({"rawtypes", "unchecked", "unused"})
public class WebController {
    private static Logger log = Logger.getLogger(WebController.class);
    private static Map<String, Sensor> sensors;
    @PostConstruct
@@ -45,6 +44,7 @@
    AccountService accountService;
    @Resource
    DictionaryDataService dictionaryDataService;
    @Resource
    OrganizationService organizationService;
    @Resource
    WebTokenService webTokenService;
@@ -62,18 +62,41 @@
    SensorService sensorService;
    @Resource
    OrganizationSensorsService organizationSensorsService;
    @Resource
    MapPathService mapPathService;
    @UserLoginToken
    @GetMapping("test")
    public String add() {
        return "test success!";
    public ResultBean<List<WebProvince>> add(HttpServletRequest request) {
        String token = request.getHeader("token");
        List<WebProvince> mapPath = mapPathService.getMapPath(token);
        if (ObjectUtils.isEmpty(mapPath))
            return new ResultBean<>("获取地图信息失败", ResultBean.FAIL);
        return new ResultBean<>(mapPath);
    }
    @GetMapping("redisTest")
    public ResultBean<List<WebProvince>> redisTest(HttpServletRequest request) {
        String token = request.getHeader("token");
        List<WebProvince> mapPath = mapPathService.getMapPath(token);
        if (ObjectUtils.isEmpty(mapPath))
            return new ResultBean<>("获取地图信息失败", ResultBean.FAIL);
        return new ResultBean<>(mapPath);
    }
    @GetMapping("sqlTest")
    public ResultBean<List<WebProvince>> sqlTest(HttpServletRequest request) {
        String token = request.getHeader("token");
        List<WebProvince> mapPath = mapPathService.getMapPathTest(token);
        if (ObjectUtils.isEmpty(mapPath))
            return new ResultBean<>("获取地图信息失败", ResultBean.FAIL);
        return new ResultBean<>(mapPath);
    }
    /**
     * @Description: 登陆接口
     * @Param: [parameters]
     * @return: java.util.Map<java.lang.String                                                                                                                               ,                                                                                                                               java.lang.Object>
     * @return: java.util.Map<java.lang.String                               ,                               java.lang.Object>
     * @Author: 下雨听风
     * @Date: 2020/10/19
     */
@@ -84,7 +107,7 @@
            resultMap.put("msg", "用户名及密码不允许为空!");
            resultMap.put("accountId", -1);
        } else {
            resultMap = accountService.webLogin(parameters);
            resultMap = accountService.bsWebLogin(parameters);
            String accountId = String.valueOf(resultMap.get("accountId"));
            if (!accountId.equals("-1")) {
                redisHashUtil.deleteMapVal("webToken", accountId);
@@ -97,7 +120,7 @@
    /**
     * @Description: 退出接口
     * @Param: [request]
     * @return: java.util.Map<java.lang.String                                                                                                                               ,                                                                                                                               java.lang.Object>
     * @return: java.util.Map<java.lang.String                               ,                               java.lang.Object>
     * @Author: 下雨听风
     * @Date: 2020/10/19
     */
@@ -112,13 +135,7 @@
        return resultMap;
    }
    /**
     * @Description:
     * @Param: [request]
     * @return: java.util.Map<java.lang.String                                                                                                                               ,                                                                                                                               java.lang.Object>
     * @Author: 下雨听风
     * @Date: 2020/10/19
     */
    @UserLoginToken
    @GetMapping("getAccountInfo")
    public Map<String, Object> getAccountInfo(HttpServletRequest request) {
@@ -130,48 +147,9 @@
            throw new WebAuthException("401,token无效");
        }
        Map<String, Object> resultMap = accountService.getAccountInfoById(id);
        Object orgId = resultMap.get("orgId");
        if (resultMap.get("orgId") != null && resultMap.get("orgId") instanceof Integer) {
            StringBuilder areaNamesBuilder = new StringBuilder("中国");
            //判断是否为本公司开发者
            if (!((Integer) orgId).equals(dictionaryDataService.querySupperOrgId())) {
                //不是本公司开发者则获取用户所属地区
                Organization organization = organizationService.getOrganizationById((Integer) orgId);
                if (organization.getAreaNames() != null) {
                    Map<String, String> areaNameMap = BeanUtils.beanToMap(organization.getAreaNames());
                    List<String> names = areaNameMap.entrySet().stream().filter(item -> {
                        return item.getValue() != null;
                    }).map(item -> {
                        return item.getValue();
                    }).collect(Collectors.toList());
                    AreaNames areaNames = organization.getAreaNames();
                    areaNamesBuilder.append("/");
                    areaNamesBuilder.append(String.join("/", names));
                }
                // 企业用户
                if (organization.getRank() != null && organization.getRank() == 0) {
                    resultMap.put("type", "enterprise");
                } else {
                    resultMap.put("type", "government");
                }
                Number mapAreaCode = null;
                if (organization.getVillageCode() != null) {
                    mapAreaCode = organization.getVillageCode();
                } else if (organization.getTownCode() != null) {
                    mapAreaCode = organization.getTownCode();
                } else if (organization.getAreaCode() != null) {
                    mapAreaCode = organization.getAreaCode();
                } else if (organization.getCityCode() != null) {
                    mapAreaCode = organization.getCityCode();
                } else if (organization.getProvinceCode() != null) {
                    mapAreaCode = organization.getProvinceCode();
                }
                resultMap.put("mapAreaCode", mapAreaCode.toString());
            }
            resultMap.put("mapPath", areaNamesBuilder.toString());
            String accountId = String.valueOf(resultMap.get("accountId"));
            resultMap.put("token", webTokenService.getToken(accountId));
        }
        String accountId = String.valueOf(resultMap.get("accountId"));
        resultMap.put("token", webTokenService.getToken(accountId));
        return resultMap;
    }
@@ -179,7 +157,7 @@
    /**
     * @Description: 获取传感器平均值
     * @Param: [request]
     * @return: com.moral.common.bean.ResultBean<java.util.List                                                                                                                               <                                                                                                                               java.util.Map                                                                                                                               <                                                                                                                               java.lang.String                                                                                                                               ,                                                                                                                               java.lang.Object>>>
     * @return: com.moral.common.bean.ResultBean<java.util.List                               <                               java.util.Map                               <                               java.lang.String                               ,                               java.lang.Object>>>
     * @Author: 下雨听风
     * @Date: 2020/10/19
     */
@@ -208,7 +186,7 @@
    /**
     * @Description: 获取某个站点设备信息
     * @Param: [request]
     * @return: com.moral.common.bean.ResultBean<java.util.List                                                                                                                               <                                                                                                                               com.moral.entity.MonitorPoint>>
     * @return: com.moral.common.bean.ResultBean<java.util.List                               <                               com.moral.entity.MonitorPoint>>
     * @Author: 下雨听风
     * @Date: 2020/10/19
     */
@@ -224,7 +202,7 @@
    /**
     * @Description: 获取坐标接口,前端用于建点
     * @Param: [request]
     * @return: com.moral.common.bean.ResultBean<java.util.List                                                                                                                               <                                                                                                                               com.moral.entity.Device>>
     * @return: com.moral.common.bean.ResultBean<java.util.List                               <                               com.moral.entity.Device>>
     * @Author: 下雨听风
     * @Date: 2020/10/19
     */
@@ -239,20 +217,19 @@
    /**
     * @Description: 根据monitorId获取该站点下每一台设备具体传感器的五分钟平均值
     * @Param: []
     * @return: com.moral.common.bean.ResultBean<java.util.Map                                                                                                                               <                                                                                                                               java.lang.String                                                                                                                               ,                                                                                                                               java.lang.Object>>
     * @return: com.moral.common.bean.ResultBean<java.util.Map                               <                               java.lang.String                               ,                               java.lang.Object>>
     * @Author: 下雨听风
     * @Date: 2020/10/19
     */
    @GetMapping("fiveMinuteAvgData")
    public ResultBean<List<Map<String, Object>>> getSensorFiveMinuteAvgData(HttpServletRequest request) {
    public ResultBean<Map<String, Object>> getSensorFiveMinuteAvgData(HttpServletRequest request) {
        //获取参数,传感器和monitorpointId
        Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
        if ((!parameters.containsKey("sensorKey")) || (!parameters.containsKey("monitorPointId")))
            return ResultBean.fail("参数为null");
        String sensorKey = (String) parameters.get("sensorKey");
        Integer monitorPointId = Integer.parseInt((String) parameters.get("monitorPointId"));
        //根据monitorpointId获取该站点下所有设备mac集合
        List<Device> devices = deviceService.getDevicesByMonitorPointId(monitorPointId);
@@ -278,22 +255,109 @@
            parameters.put("time", time);
            datas = historyFiveMinutelyService.getFiveMinutesDataByMacsAndTime(parameters);
        }
        datas = insertDeviceInfo(datas, devices);
        return new ResultBean<List<Map<String, Object>>>(datas);
        //根据monitorPointId获取国控站坐标
        List<Map<String, Object>> coordinate = new ArrayList<>();
        MonitorPoint monitorPoint = monitorPointService.queryMonitorPointById(monitorPointId);
        Integer orgId = monitorPoint.getOrganizationId();
        List<MonitorPoint> monitorPoints = monitorPointService.getMonitorPointsByOrganizationId(orgId);
        monitorPoints.forEach(value -> {
            if ("国控站".equals(value.getDescription())) {
                List list = LatLngTransformation.Convert_BD09_To_GCJ02(value.getLatitude(), value.getLongitude());
                Map<String, Object> controlStation = new HashMap<>();
                controlStation.put("name", value.getName());
                controlStation.put("longitude", list.get(0));
                controlStation.put("latitude", list.get(1));
                coordinate.add(controlStation);
            }
        });
        Map<String, Object> datasMap = new HashMap<>();
        datasMap.put("coordinate", coordinate);
        datasMap.put("device", datas);
        return new ResultBean<Map<String, Object>>(datasMap);
    }
    @GetMapping("getFiveMinuteAvgDataByMac")
    public ResultBean<Map<String, Object>> getFiveMinuteAvgDataByMac(HttpServletRequest request) {
        try{
    @GetMapping("cangzhouMIdGetAllDevice")
    public ResultBean<Map<String, Object>> cangzhouMIdGetAllDevice(HttpServletRequest request) {
        //获取参数,传感器和monitorpointId
        Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
        if ((!parameters.containsKey("sensorKey")) || (!parameters.containsKey("monitorPointId")))
            return ResultBean.fail("参数为null");
        String sensorKey = (String) parameters.get("sensorKey");
        Integer monitorPointId = Integer.parseInt((String) parameters.get("monitorPointId"));
        MonitorPoint mPoint = monitorPointService.queryMonitorPointById(monitorPointId);
        Integer code = mPoint.getCityCode();
        List<Device> devices = null;
        if (code == 130900) {
            devices = deviceService.getDeviceByCode();
        } else {
            devices = deviceService.getDevicesByMonitorPointId(monitorPointId);
        }
        //根据monitorpointId获取该站点下所有设备mac集合
        if (ObjectUtils.isEmpty(devices))
            return ResultBean.fail("站点下无设备或monitorPointId错误");
        List<String> macs = new ArrayList<>();
        devices.forEach(p -> {
            macs.add(p.getMac());
        });
        //根据时间和mac号集合以及传感器参数查询五分钟平均数据
        Map<String, Object> timeAndYearMonth = getTimeAndYearMonthForFiveMinuteData();
        String time = (String) timeAndYearMonth.get("time");
        String yearAndMonth = (String) timeAndYearMonth.get("yearAndMonth");
        parameters.put("time", time);
        parameters.put("yearAndMonth", yearAndMonth);
        parameters.put("macs", macs);
        List<Map<String, Object>> datas = historyFiveMinutelyService.getFiveMinutesDataByMacsAndTime(parameters);
        //如果当前五分钟数据还没有插入,则提取前五分钟数据返回
        if (ObjectUtils.isEmpty(datas)) {
            time = getFiveMinuteAgoTime(time);
            parameters.put("time", time);
            datas = historyFiveMinutelyService.getFiveMinutesDataByMacsAndTime(parameters);
        }
        datas = insertDeviceInfo(datas, devices);
        //根据monitorPointId获取国控站坐标
        List<Map<String, Object>> coordinate = new ArrayList<>();
        MonitorPoint monitorPoint = monitorPointService.queryMonitorPointById(monitorPointId);
        Integer orgId = monitorPoint.getOrganizationId();
        List<MonitorPoint> monitorPoints = monitorPointService.getMonitorPointsByOrganizationId(orgId);
        monitorPoints.forEach(value -> {
            if ("国控站".equals(value.getDescription())) {
                List list = LatLngTransformation.Convert_BD09_To_GCJ02(value.getLatitude(), value.getLongitude());
                Map<String, Object> controlStation = new HashMap<>();
                controlStation.put("name", value.getName());
                controlStation.put("longitude", list.get(0));
                controlStation.put("latitude", list.get(1));
                coordinate.add(controlStation);
            }
        });
        Map<String, Object> datasMap = new HashMap<>();
        datasMap.put("coordinate", coordinate);
        datasMap.put("device", datas);
        return new ResultBean<Map<String, Object>>(datasMap);
    }
    /**
     * @Description: 根据mac号获取单台设备信息,特殊客户只显示客户需要的传感器信息
     * @Param: [request]
     * @return: com.moral.common.bean.ResultBean<java.util.Map   <   java.lang.String   ,   java.lang.Object>>
     * @Author: 下雨听风
     * @Date: 2020/10/22
     */
    @GetMapping("fiveMinuteAvgDataByMac")
    public ResultBean<Map<String, Object>> fiveMinuteAvgDataByMac(HttpServletRequest request) {
        //获取参数,mac号
        Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
        if ((!parameters.containsKey("mac")))
            return ResultBean.fail("参数为null");
        String mac = (String) parameters.get("mac");
        log.info("获取到mac参数");
        //根据mac获取orgId
        String orgId = monitorPointService.getOrgIdByMac(mac);
@@ -301,33 +365,25 @@
        //判断是否特殊定制客户,如果为特殊定制客户则选取特定参数
        Map<String, Object> specialSensors = organizationSensorsService.getSensorsByOrgId(orgId);
        try {
            if (ObjectUtils.isEmpty(specialSensors)) {
                List<String> sensorKeys = sensorService.getSensorKeys();
                parameters.put("sensorKeys", sensorKeys);
            } else {
                String sensorKeys = (String) specialSensors.get("sensors");
                sensorKeys.trim();
                sensorKeys = sensorKeys.substring(1, sensorKeys.length() - 1);
                parameters.put("sensorKeys", Arrays.asList(sensorKeys.split(",")));
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        if (ObjectUtils.isEmpty(specialSensors)) {
            List<String> sensorKeys = sensorService.getSensorKeys();
            parameters.put("sensorKeys", sensorKeys);
        } else {
            String sensorKeys = (String) specialSensors.get("sensors");
            sensorKeys.trim();
            sensorKeys = sensorKeys.substring(1, sensorKeys.length() - 1);
            parameters.put("sensorKeys", Arrays.asList(sensorKeys.split(",")));
        }
        log.info("判断是否特殊客户");
        //设置查询参数
        Map<String, Object> timeAndYearMonth = getTimeAndYearMonthForFiveMinuteData();
        String time = (String) timeAndYearMonth.get("time");
        String yearAndMonth = (String) timeAndYearMonth.get("yearAndMonth");
        parameters.put("time", time);
        parameters.put("yearAndMonth", yearAndMonth);
        parameters.put("mac", mac);
        log.info("设置查询参数");
        Map<String, Object> datas = historyFiveMinutelyService.getFiveMinutesDataByMac(parameters);
        log.info("查询完毕");
        //如果当前五分钟数据还没有插入,则提取前五分钟数据返回
        if (ObjectUtils.isEmpty(datas)) {
@@ -335,20 +391,17 @@
            parameters.put("time", time);
            datas = historyFiveMinutelyService.getFiveMinutesDataByMac(parameters);
        }
        log.info("查询五分钟前数据");
        //去除无效参数
        datas.values().removeIf((value) -> {
            return ObjectUtils.isEmpty(value) || value.equals("[0, 0, 0]");
        });
        log.info("去除无效参数");
        //添加设备名称
        Map<String, Object> sortDatas = new LinkedHashMap<>();
        Map<String, Object> sortDatas = new LinkedHashMap<>();//排序后的数据,用于发送前端
        Device device = deviceService.getDeviceByMac(mac, true);
        sortDatas.put("名称", device.getName());
        log.info("添加设备名称");
        //参数转换中文
        datas.forEach((key, value) -> {
@@ -361,26 +414,39 @@
                sortDatas.put(sensor.getName(), Arrays.asList(str.split(",")).get(0) + unit);
            }
        });
        log.info("转换中文");
        sortDatas.put("时间", time);
        return new ResultBean<Map<String, Object>>(sortDatas);
        }catch (Exception e){
            log.error(e.getMessage());
            return null;
        }
    }
    /**
     * @Description: 根据token获取用户地图
     * @Param: [request]
     * @return: com.moral.common.bean.ResultBean<java.util.List   <   com.moral.entity.WebProvince>>
     * @Author: 下雨听风
     * @Date: 2020/11/27
     */
    @UserLoginToken
    @GetMapping("mapPath")
    public ResultBean<List<WebProvince>> mapPath(HttpServletRequest request) {
        String token = request.getHeader("token");
        List<WebProvince> mapPath = mapPathService.getMapPath(token);
        if (ObjectUtils.isEmpty(mapPath))
            return new ResultBean<>("获取地图信息失败", ResultBean.FAIL);
        return new ResultBean<>(mapPath);
    }
    /**
     * @Description: 返回结果添加设备经纬度以及state
     * @Param: [datas, devices]
     * @return: java.util.List<java.util.Map                                                                                                                               <                                                                                                                               java.lang.String                                                                                                                               ,                                                                                                                               java.lang.Object>>
     * @return: java.util.List<java.util.Map                               <                               java.lang.String                               ,                               java.lang.Object>>
     * @Author: 下雨听风
     * @Date: 2020/10/19
     */
    public List<Map<String, Object>> insertDeviceInfo(List<Map<String, Object>> datas, List<Device> devices) {
    private List<Map<String, Object>> insertDeviceInfo(List<Map<String, Object>> datas, List<Device> devices) {
        Map<String, Device> map = new HashMap<>();
        devices.forEach(p -> {
            map.put(p.getMac(), p);
@@ -439,7 +505,7 @@
     * @Author: 下雨听风
     * @Date: 2020/10/20
     */
    public String getFiveMinuteAgoTime(String time) {
    private String getFiveMinuteAgoTime(String time) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(time);