ard-work/src/main/java/com/ruoyi/app/position/service/impl/ArdAppPositionServiceImpl.java
@@ -1,29 +1,28 @@
package com.ruoyi.app.position.service.impl;
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.domain.ArdAppPosition;
import com.ruoyi.app.position.mapper.ArdAppPositionMapper;
import com.ruoyi.app.position.service.IArdAppPositionService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.scheduling.domian.SchedulingParam;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.utils.tools.GisTool;
import com.ruoyi.utils.tools.Point;
import com.ruoyi.utils.gis.GisUtil;
import com.ruoyi.utils.gis.Point;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
@@ -40,7 +39,14 @@
    private ArdAppPositionMapper ardAppPositionMapper;
    @Resource
    private ISysUserService iSysUserService;
    @Resource
    RedisCache redisCache;
    @Resource
    ArdWallMapper ardWallMapper;
    @Resource
    IArdAlarmWallService ardAlarmWallService;
    Map<String, String> firstAlarmMap = new HashMap<>();//首次报警缓存key:用户id_围栏id value: 报警id
    /**
     * 查询app位置
     *
@@ -81,10 +87,11 @@
     * @return 结果
     */
    @Override
    public int insertArdAppPosition(ArdAppPosition ardAppPosition) {
    public List<ArdAlarmWall> insertArdAppPosition(ArdAppPosition ardAppPosition) {
        ardAppPosition.setId(IdUtils.simpleUUID());
        ardAppPosition.setCreateTime(DateUtils.getNowDate());
        return ardAppPositionMapper.insertArdAppPosition(ardAppPosition);
        ardAppPositionMapper.insertArdAppPosition(ardAppPosition);
        return DetectionWallAlarm(ardAppPosition);
    }
    /**
@@ -146,9 +153,10 @@
            SysUser user = new SysUser();
            user.setDeptId(deptId);
            List<SysUser> appUserList = iSysUserService.selectAllAppUserList(user);
            //过滤在线
            //过滤在线和单兵端
            List<SysUser> onLineList = appUserList.stream()
                    .filter(sysUser -> (sysUser.getAppOnlineState().equals("1")))
                    .filter(sysUser -> (sysUser.getAppUserType().equals("1")))
                    .collect(Collectors.toList());
            //过滤范围
            for (SysUser sysUser : onLineList) {
@@ -159,7 +167,7 @@
                    if (longitude == null || latitude == null) {
                        continue;
                    }
                    double distance = GisTool.getDistance(new double[]{longitude, latitude}, new double[]{lon, lat});
                    double distance = GisUtil.getDistance(new double[]{longitude, latitude}, new double[]{lon, lat});
                    if (distance <= radius) {
                        Map<String, Object> params = new HashMap<>();
                        params.put("longitude", lon);
@@ -174,6 +182,7 @@
        }
        return filteredList;
    }
    /**
     * 获取封控圈内所有在线app用户(多边形)
     * 刘苏义
@@ -192,6 +201,7 @@
            //过滤在线
            List<SysUser> onLineList = appUserList.stream()
                    .filter(sysUser -> (sysUser.getAppOnlineState().equals("1")))
                    .filter(sysUser -> (sysUser.getAppUserType().equals("1")))
                    .collect(Collectors.toList());
            //过滤范围
            for (SysUser sysUser : onLineList) {
@@ -202,8 +212,8 @@
                    if (lon == null || lat == null) {
                        continue;
                    }
                    Point point2D=new Point(lon,lat);
                    boolean inPolygon = GisTool.isInPolygon(point2D, partitionLocation);
                    Point point2D = new Point(lon, lat);
                    boolean inPolygon = GisUtil.isInPolygon(point2D, partitionLocation);
                    if (inPolygon) {
                        Map<String, Object> params = new HashMap<>();
                        params.put("longitude", lon);
@@ -218,4 +228,97 @@
        }
        return filteredList;
    }
    /**
     * 实时位置检测围栏报警
     * 刘苏义
     * 2023/8/31 8:54:06
     */
    public List<ArdAlarmWall> DetectionWallAlarm(ArdAppPosition ardAppPosition) {
        List<ArdAlarmWall> ardAlarmWalls = new ArrayList<>();
        //获取当前用户的部门
        String userId = ardAppPosition.getUserId();
        //获取用户关联的所有电子围栏
        List<ArdWall> ardWalls = ardWallMapper.selectArdWallListByUserId(userId);
        if (ardWalls.size() > 0) {
            //遍历所有电子围栏
            for (ArdWall wall : ardWalls) {
                String wallId = wall.getId();//围栏ID
                String wallType = wall.getType();//围栏类型
                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);
                }
                //当前用户的位置
                Point userPoint = new Point(ardAppPosition.getLongitude(), ardAppPosition.getLatitude());
                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 ("1".equals(wallType)) {
                    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);
                    }
                } else//禁出
                {
                    //判断当前用户位置是否在围栏外
                    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);
                    }
                }
            }
        }
        return ardAlarmWalls;
    }
}