|  |  | 
 |  |  | package com.ruoyi.app.position.service.impl; | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson2.JSON; | 
 |  |  | import com.ruoyi.alarm.wall.domain.ArdAlarmWall; | 
 |  |  | import com.ruoyi.alarm.wall.mapper.ArdAlarmWallMapper; | 
 |  |  | import com.ruoyi.alarm.wall.service.IArdAlarmWallService; | 
 |  |  | import com.ruoyi.alarmpoints.wall.domain.ArdWall; | 
 |  |  | import com.ruoyi.alarmpoints.wall.mapper.ArdWallMapper; | 
 |  |  | import com.ruoyi.app.position.appcontainer.AppContainer; | 
 |  |  | import com.ruoyi.app.position.domain.ArdAppPosition; | 
 |  |  | import com.ruoyi.app.position.mapper.ArdAppPositionMapper; | 
 |  |  | import com.ruoyi.app.position.service.IArdAppPositionService; | 
 |  |  | 
 |  |  | import com.ruoyi.common.utils.SecurityUtils; | 
 |  |  | import com.ruoyi.common.utils.uuid.IdUtils; | 
 |  |  | import com.ruoyi.scheduling.domian.SchedulingParam; | 
 |  |  | import com.ruoyi.system.mapper.SysDeptMapper; | 
 |  |  | import com.ruoyi.system.mapper.SysUserMapper; | 
 |  |  | import com.ruoyi.system.service.ISysUserService; | 
 |  |  | import com.ruoyi.utils.gis.GisUtil; | 
 |  |  | 
 |  |  | import org.springframework.stereotype.Service; | 
 |  |  |  | 
 |  |  | import javax.annotation.Resource; | 
 |  |  | import java.text.SimpleDateFormat; | 
 |  |  | import java.util.*; | 
 |  |  | import java.util.stream.Collectors; | 
 |  |  |  | 
 |  |  | 
 |  |  |     IArdAlarmWallService ardAlarmWallService; | 
 |  |  |     @Resource | 
 |  |  |     private SysUserMapper sysUserMapper; | 
 |  |  |     @Resource | 
 |  |  |     private SysDeptMapper sysDeptMapper; | 
 |  |  |  | 
 |  |  |     Map<String, String> firstAlarmMap = new HashMap<>();//首次报警缓存key:用户id_围栏id value: 报警id | 
 |  |  |     /** | 
 |  |  | 
 |  |  |         ardAppPosition.setId(IdUtils.simpleUUID()); | 
 |  |  |         ardAppPosition.setCreateTime(DateUtils.getNowDate()); | 
 |  |  |         ardAppPositionMapper.insertArdAppPosition(ardAppPosition); | 
 |  |  |         return DetectionWallAlarm(ardAppPosition); | 
 |  |  |         try{ | 
 |  |  |             return DetectionWallAlarm(ardAppPosition); | 
 |  |  |         }finally { | 
 |  |  |             AppContainer.getArdAppPositionMap().put(ardAppPosition.getUserId(),ardAppPosition);//实时位置存入容器 | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  | 
 |  |  |      * 刘苏义 | 
 |  |  |      * 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(); | 
 |  |  | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         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("2")).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; | 
 |  |  |                     break; | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         if(noOutFlag){//全部禁出电子围栏都出去了 | 
 |  |  |             for(ArdWall wall : ardWallsNoOut){ | 
 |  |  |                 String wallId = wall.getId();//围栏ID | 
 |  |  |                 //创建围栏报警实体对象 | 
 |  |  |                 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 | 
 |  |  |                 //禁出围栏则若在围栏外,获取首次走出围栏报警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{ | 
 |  |  |             for(ArdWall wall : ardWallsNoOut){ | 
 |  |  |                 String wallId = wall.getId();//围栏ID | 
 |  |  |                 String key = userId + "_" + wallId;//最后一次报警缓存key | 
 |  |  |                 firstAlarmMap.remove(key); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return ardAlarmWalls; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void sendCheckCommandersPosition(String usersId,Map<String, Object> para) { | 
 |  |  |         String reason = (String) para.get("reason"); | 
 |  |  |         List<String> usersIdList = (List<String>) para.get("usersIdList"); | 
 |  |  |         for(String commanderId : usersIdList){ | 
 |  |  |             //设置key为checkCommanderPosition_指挥端主键_单兵端主键 | 
 |  |  |             String key = "checkCommanderPosition_" + commanderId + "_" + usersId; | 
 |  |  |             Map<String,Object> value = new HashMap(); | 
 |  |  |             value.put("reason",reason); | 
 |  |  |             value.put("state","0"); | 
 |  |  |             value.put("begin",""); | 
 |  |  |             redisCache.setCacheObject(key, JSON.toJSONString(value)); | 
 |  |  |         } | 
 |  |  |     public List<Map<String,Object>> getOnlineCommanderPosition(String soilderId) { | 
 |  |  |         List<Map<String,Object>> result = ardAppPositionMapper.getOnlineCommanderPosition(soilderId); | 
 |  |  |         return result; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void sendCheckSYCarsPosition(String usersId, Map<String, Object> para) { | 
 |  |  |         String reason = (String) para.get("reason"); | 
 |  |  |         List<String> usersIdList = (List<String>) para.get("usersIdList"); | 
 |  |  |         for(String commanderId : usersIdList){ | 
 |  |  |             //设置key为checkCommanderPosition_指挥端主键/PC端主键_单兵端主键 | 
 |  |  |             String key = "checkSYCarsPosition_" + commanderId + "_" + usersId; | 
 |  |  |             Map<String,Object> value = new HashMap(); | 
 |  |  |             value.put("reason",reason); | 
 |  |  |             value.put("state","0"); | 
 |  |  |             value.put("begin",""); | 
 |  |  |             redisCache.setCacheObject(key, JSON.toJSONString(value)); | 
 |  |  |         } | 
 |  |  |     public List<ArdAppPosition> getAPPPositionByUserIdAndTime(Map<String, String> para) { | 
 |  |  |         String userId = para.get("userId"); | 
 |  |  |         String beginTime = para.get("beginTime"); | 
 |  |  |         String endTime = para.get("endTime"); | 
 |  |  |         List<ArdAppPosition> result = ardAppPositionMapper.getAPPPositionByUserIdAndTime(userId,beginTime,endTime); | 
 |  |  |         return result; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public List<Map<String,Object>> getOnlineAPPSoilderByCommanderId(String userId) { | 
 |  |  |         SysUser sysUser = sysUserMapper.selectUserById(userId);//获取系统用户 | 
 |  |  |         List<Long> deptIdList = new ArrayList(); | 
 |  |  |         deptIdList.add(sysUser.getDeptId()); | 
 |  |  |         deptIdList = getOwnAndChildrenDeptIdList(deptIdList,new ArrayList());//递归查询下属部门主键 | 
 |  |  |         List<SysUser> result = sysUserMapper.getOwnAndChildrenOnlineSoilderList(deptIdList); | 
 |  |  |         List<Map<String,Object>> resultList = new ArrayList(); | 
 |  |  |         for(SysUser sysUserIn : result){ | 
 |  |  |             Map<String,Object> map = new HashMap(); | 
 |  |  |             map.put("userId",sysUserIn.getUserId()); | 
 |  |  |             map.put("deptId",sysUserIn.getDeptId()); | 
 |  |  |             map.put("userName",sysUserIn.getUserName()); | 
 |  |  |             map.put("nickName",sysUserIn.getNickName()); | 
 |  |  |             try{ | 
 |  |  |                 map.put("longitude",AppContainer.getArdAppPositionMap().get(sysUserIn.getUserId()).getLongitude()); | 
 |  |  |                 map.put("latitude",AppContainer.getArdAppPositionMap().get(sysUserIn.getUserId()).getLatitude()); | 
 |  |  |                 map.put("altitude",AppContainer.getArdAppPositionMap().get(sysUserIn.getUserId()).getAltitude()); | 
 |  |  |             }catch(Exception e){ | 
 |  |  |                 ArdAppPosition ardAppPosition = ardAppPositionMapper.getLastAPPPositionByUserId(sysUserIn.getUserId()); | 
 |  |  |                 map.put("longitude",ardAppPosition.getLongitude()); | 
 |  |  |                 map.put("latitude",ardAppPosition.getLatitude()); | 
 |  |  |                 map.put("altitude",ardAppPosition.getAltitude()); | 
 |  |  |             } | 
 |  |  |             resultList.add(map); | 
 |  |  |         } | 
 |  |  |         return resultList; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public List<Long> getOwnAndChildrenDeptIdList(List<Long> deptIdList,List<Long> deptIdListr){ | 
 |  |  |         deptIdListr.addAll(deptIdList); | 
 |  |  |         List<Long> result = sysDeptMapper.getChildrenDeptIdList(deptIdList); | 
 |  |  |         if(result.size() != 0){ | 
 |  |  |             deptIdListr.addAll(result); | 
 |  |  |             result = getOwnAndChildrenDeptIdList(result,deptIdListr); | 
 |  |  |         } | 
 |  |  |         deptIdListr = deptIdListr.stream().distinct().collect(Collectors.toList()); | 
 |  |  |         return deptIdListr; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |