zhangnaisong
2024-01-31 7367cf50bcceb23194aa071b696f126935dfd069
禁出电子围栏报警逻辑修改提交
已修改1个文件
141 ■■■■■ 文件已修改
ard-work/src/main/java/com/ruoyi/app/position/service/impl/ArdAppPositionServiceImpl.java 141 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ard-work/src/main/java/com/ruoyi/app/position/service/impl/ArdAppPositionServiceImpl.java
@@ -241,7 +241,7 @@
     * 刘苏义
     * 2023/8/31 8:54:06
     */
    public List<ArdAlarmWall> DetectionWallAlarm(ArdAppPosition ardAppPosition) {
    /*public List<ArdAlarmWall> DetectionWallAlarm(ArdAppPosition ardAppPosition) {
        List<ArdAlarmWall> ardAlarmWalls = new ArrayList<>();
        //获取当前用户的部门
        String userId = ardAppPosition.getUserId();
@@ -327,6 +327,145 @@
            }
        }
        return ardAlarmWalls;
    }*/
    public List<ArdAlarmWall> DetectionWallAlarm(ArdAppPosition ardAppPosition) {
        List<ArdAlarmWall> ardAlarmWalls = new ArrayList<>();
        //获取当前用户的部门
        String userId = ardAppPosition.getUserId();
        //获取用户关联的所有电子围栏
        List<ArdWall> ardWalls = ardWallMapper.selectArdWallListByUserId(userId);
        List<ArdWall> ardWallsNoIn = new ArrayList();//禁入电子围栏
        List<ArdWall> ardWallsNoOut = new ArrayList();//禁出电子围栏
        ardWallsNoIn = ardWalls.stream().filter(ardWall -> ardWall.getType().equals("1")).collect(Collectors.toList());
        ardWallsNoOut = ardWalls.stream().filter(ardWall -> ardWall.getType().equals("0")).collect(Collectors.toList());
        //当前用户的位置
        Point userPoint = new Point(ardAppPosition.getLongitude(), ardAppPosition.getLatitude());
        if(ardWallsNoIn.size() > 0){//判断禁入
            for(ArdWall wall : ardWallsNoIn){
                String wallId = wall.getId();//围栏ID
                String wallPoi = wall.getWallPoi();//围栏坐标集合
                //处理多边形的每个点的经纬度
                String[] parts = wallPoi.split(",");
                List<Point> pointList = new ArrayList<>();
                for (int i = 0; i < parts.length; i += 3) {
                    Point point = new Point();
                    point.setLongitude(Double.valueOf(parts[i]));
                    point.setLatitude(Double.valueOf(parts[i + 1]));
                    pointList.add(point);
                }
                boolean inPolygon = GisUtil.isInPolygon(userPoint, pointList);//判断是否在围栏内
                //创建围栏报警实体对象
                ArdAlarmWall ardAlarmWall = new ArdAlarmWall();
                ardAlarmWall.setWallId(wall.getId());
                ardAlarmWall.setWallName(wall.getWallName());
                ardAlarmWall.setUserId(userId);
                ardAlarmWall.setAlarmTime(DateUtils.getNowDate());
                ardAlarmWall.setAlarmType(wall.getType());
                ardAlarmWall.setLongitude(ardAppPosition.getLongitude());
                ardAlarmWall.setLatitude(ardAppPosition.getLatitude());
                ardAlarmWall.setAltitude(ardAppPosition.getAltitude());
                String key = userId + "_" + wallId;//最后一次报警缓存key
                if (inPolygon) {
                    //禁入围栏则若在围栏内,获取首次进入围栏报警ID
                    String lastInAlarmId = firstAlarmMap.get(key);
                    //判断当前用户是否首次进入围栏内
                    if (lastInAlarmId == null) {
                        //当前用户首次进入生成报警
                        String uuid = IdUtils.simpleUUID();
                        ardAlarmWall.setId(uuid);
                        ardAlarmWallService.insertArdAlarmWall(ardAlarmWall);
                        //记录首次报警id
                        firstAlarmMap.put(key, uuid);
                    } else {
                        //用户已进入更新最后报警
                        ardAlarmWall.setId(lastInAlarmId);
                        ardAlarmWallService.updateArdAlarmWall(ardAlarmWall);
                    }
                    ardAlarmWalls.add(ardAlarmWall);
                } else {
                    //禁入围栏则若在围栏外则移除报警缓存
                    firstAlarmMap.remove(key);
                }
            }
        }
        Boolean noOutFlag = true;
        if(ardWallsNoOut.size() > 0) {//判断禁出
            for(ArdWall wall : ardWallsNoOut){
                String wallPoi = wall.getWallPoi();//围栏坐标集合
                //处理多边形的每个点的经纬度
                String[] parts = wallPoi.split(",");
                List<Point> pointList = new ArrayList<>();
                for (int i = 0; i < parts.length; i += 3) {
                    Point point = new Point();
                    point.setLongitude(Double.valueOf(parts[i]));
                    point.setLatitude(Double.valueOf(parts[i + 1]));
                    pointList.add(point);
                }
                boolean inPolygon = GisUtil.isInPolygon(userPoint, pointList);//判断是否在围栏内
                if (!inPolygon) {
                    noOutFlag = true;
                } else {
                    noOutFlag = false;
                }
            }
        }
        if(noOutFlag){//全部禁出电子围栏都出去了
            for(ArdWall wall : ardWallsNoOut){
                String wallId = wall.getId();//围栏ID
                //String wallPoi = wall.getWallPoi();//围栏坐标集合
                //处理多边形的每个点的经纬度
                /*String[] parts = wallPoi.split(",");
                List<Point> pointList = new ArrayList<>();
                for (int i = 0; i < parts.length; i += 3) {
                    Point point = new Point();
                    point.setLongitude(Double.valueOf(parts[i]));
                    point.setLatitude(Double.valueOf(parts[i + 1]));
                    pointList.add(point);
                }
                boolean inPolygon = GisUtil.isInPolygon(userPoint, pointList);*///判断是否在围栏内
                //创建围栏报警实体对象
                ArdAlarmWall ardAlarmWall = new ArdAlarmWall();
                ardAlarmWall.setWallId(wall.getId());
                ardAlarmWall.setWallName(wall.getWallName());
                ardAlarmWall.setUserId(userId);
                ardAlarmWall.setAlarmTime(DateUtils.getNowDate());
                ardAlarmWall.setAlarmType(wall.getType());
                ardAlarmWall.setLongitude(ardAppPosition.getLongitude());
                ardAlarmWall.setLatitude(ardAppPosition.getLatitude());
                ardAlarmWall.setAltitude(ardAppPosition.getAltitude());
                String key = userId + "_" + wallId;//最后一次报警缓存key
                //if (!inPolygon) {
                    //禁出围栏则若在围栏外,获取首次走出围栏报警ID
                    String lastAlarmId = firstAlarmMap.get(key);
                    //判断当前用户是否首次在围栏外
                    if (lastAlarmId == null) {
                        String uuid = IdUtils.simpleUUID();
                        //当前用户首次出围栏生成报警
                        ardAlarmWall.setId(uuid);
                        ardAlarmWallService.insertArdAlarmWall(ardAlarmWall);
                        //记录首次报警id
                        firstAlarmMap.put(key, uuid);
                    } else {
                        //用户已经在围栏外更新报警
                        ardAlarmWall.setId(lastAlarmId);
                        ardAlarmWallService.updateArdAlarmWall(ardAlarmWall);
                    }
                    ardAlarmWalls.add(ardAlarmWall);
                /*} else {
                    //移除首次报警缓存
                    firstAlarmMap.remove(key);
                }*/
            }
        }else{
            for(ArdWall wall : ardWallsNoOut){
                String wallId = wall.getId();//围栏ID
                String key = userId + "_" + wallId;//最后一次报警缓存key
                firstAlarmMap.remove(key);
            }
        }
        return ardAlarmWalls;
    }
    @Override