|  |  | 
 |  |  | import com.ruoyi.app.position.domain.ArdAppPosition; | 
 |  |  | import com.ruoyi.app.position.service.IArdAppPositionService; | 
 |  |  | import com.ruoyi.common.core.domain.entity.SysUser; | 
 |  |  | import com.ruoyi.common.utils.StringUtils; | 
 |  |  | import com.ruoyi.common.utils.spring.SpringUtils; | 
 |  |  | import com.ruoyi.utils.websocket.util.WebSocketUtils; | 
 |  |  | import com.ruoyi.system.service.ISysUserService; | 
 |  |  | import org.springframework.stereotype.Component; | 
 |  |  |  | 
 |  |  | import javax.annotation.PostConstruct; | 
 |  |  | import javax.annotation.Resource; | 
 |  |  | import javax.websocket.Session; | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.HashMap; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Map; | 
 |  |  | 
 |  |  | import java.util.concurrent.ScheduledExecutorService; | 
 |  |  | import java.util.concurrent.ScheduledFuture; | 
 |  |  | import java.util.concurrent.TimeUnit; | 
 |  |  | import java.util.stream.Collectors; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * @Description: app位置推送 | 
 |  |  |  * @Description: app位置推送(弃用) | 
 |  |  |  * @ClassName: AppPositionPush | 
 |  |  |  * @Author: 刘苏义 | 
 |  |  |  * @Date: 2023年07月25日16:07:04 | 
 |  |  | 
 |  |  |     //app位置上传 | 
 |  |  |     public static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(20); | 
 |  |  |     public static final Map<Session, ScheduledFuture<?>> taskMap = new HashMap<>(); | 
 |  |  |     public static final Map<Session, Map> defaultDataMap = new HashMap<>();//默认会话发送数据map | 
 |  |  |     public static final Map<String, List<ArdAppPosition>> initDataMap = new HashMap<>();//每个会话初始发送数据map | 
 |  |  |  | 
 |  |  |     // 初始定时任务,根据用户设置的频率定期推送实时位置信息 | 
 |  |  |     public static void initPushTask(Session session, int pushFrequency) { | 
 |  |  |     public static void initPushTask(String userId, Session session, int pushFrequency) { | 
 |  |  |         initDataMap.put(userId, getAppPositionList()); | 
 |  |  |         List<ArdAppPosition> appPositionList = initDataMap.get(userId); | 
 |  |  |         if (appPositionList.size() == 0) { | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |         // 启动新的定时任务 | 
 |  |  |         if (pushFrequency > 0) { | 
 |  |  |             scheduler.scheduleAtFixedRate(() -> { | 
 |  |  |                 Map map = defaultDataMap.get(session); | 
 |  |  |                 WebSocketUtils.sendMessage(session, map); | 
 |  |  |                 if (appPositionList.size() > 0) { | 
 |  |  |                     Map newMap = new HashMap<>(); | 
 |  |  |                     newMap.put("50000", appPositionList); | 
 |  |  |                     WebSocketUtils.sendMessage(session, newMap); | 
 |  |  |                 } | 
 |  |  |             }, 0, pushFrequency, TimeUnit.MILLISECONDS); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     // 新的定时任务,根据用户设置的频率定期推送实时位置信息 | 
 |  |  |     public static void startLocationPushTask(String userId, Session session, int pushFrequency) { | 
 |  |  |     public static void startLocationPushTask(String userId, Session session, String guideUserId, int pushFrequency) { | 
 |  |  |         // 取消之前设置的定时任务 | 
 |  |  |         stopLocationPushTask(session); | 
 |  |  |  | 
 |  |  |         // 启动新的定时任务 | 
 |  |  |         if (pushFrequency > 0) { | 
 |  |  |             ScheduledFuture<?> task = ScheduledFutureTask(session, userId, pushFrequency); | 
 |  |  |             List<ArdAppPosition> appPositionList = getAppPositionList().stream() | 
 |  |  |                     .filter(obj -> obj.getUserId().equals(guideUserId)) | 
 |  |  |                     .collect(Collectors.toList()); | 
 |  |  |             if (appPositionList.size() == 0) { | 
 |  |  |                 return; | 
 |  |  |             } | 
 |  |  |             ScheduledFuture<?> task = scheduler.scheduleAtFixedRate(() -> { | 
 |  |  |                 Map newMap = new HashMap<>(); | 
 |  |  |                 newMap.put("50000", appPositionList); | 
 |  |  |                 WebSocketUtils.sendMessage(session, newMap); | 
 |  |  |             }, 0, pushFrequency, TimeUnit.MILLISECONDS); | 
 |  |  |             taskMap.put(session, task); // 存储新的定时任务 | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |     public static ScheduledFuture<?> ScheduledFutureTask(Session session, String userId, Integer pushFrequency) { | 
 |  |  |         return scheduler.scheduleAtFixedRate(() -> { | 
 |  |  |             ArdAppPosition ardAppPosition = getAppPositionList().get(userId); | 
 |  |  |             Map<String, ArdAppPosition> DataMap = new HashMap<>(); | 
 |  |  |             DataMap.put(userId,ardAppPosition); | 
 |  |  |             WebSocketUtils.sendMessage(session, DataMap); | 
 |  |  |         }, 0, pushFrequency, TimeUnit.MILLISECONDS); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //取消定时任务 | 
 |  |  |     public static void stopLocationPushTask(Session session) { | 
 |  |  |         ScheduledFuture<?> scheduledTask = taskMap.get(session); | 
 |  |  | 
 |  |  |             scheduledTask.cancel(false); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //查询所有app用户的位置信息 | 
 |  |  |     public static Map<String,ArdAppPosition> getAppPositionList() | 
 |  |  |     { | 
 |  |  |         Map<String,ArdAppPosition> userMap=new HashMap<>(); | 
 |  |  |     public static List<ArdAppPosition> getAppPositionList() { | 
 |  |  |         List<ArdAppPosition> ArdAppPositions = new ArrayList<>(); | 
 |  |  |         ISysUserService sysUserService = SpringUtils.getBean(ISysUserService.class); | 
 |  |  |         //获取所有app用户 | 
 |  |  |         List<SysUser> list = sysUserService.selectAppUserListNoDataScope(new SysUser()); | 
 |  |  |         for(SysUser sysUser:list) | 
 |  |  |         { | 
 |  |  |         List<SysUser> list = sysUserService.selectAllAppUserList(new SysUser()); | 
 |  |  |         for (SysUser sysUser : list) { | 
 |  |  |             if(StringUtils.isNull(sysUser.getAppOnlineState())) | 
 |  |  |             {continue;} | 
 |  |  |             if(sysUser.getAppOnlineState().equals("0")) | 
 |  |  |             {continue; } | 
 |  |  |             IArdAppPositionService ardAppPositionService = SpringUtils.getBean(IArdAppPositionService.class); | 
 |  |  |             ArdAppPosition ardAppPosition = ardAppPositionService.selectLastArdAppPositionByUserId(sysUser.getUserId()); | 
 |  |  |             userMap.put(sysUser.getUserId(),ardAppPosition); | 
 |  |  |             ArdAppPositions.add(ardAppPosition); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         return userMap; | 
 |  |  |         return ArdAppPositions; | 
 |  |  |     } | 
 |  |  | } | 
 |  |  |  |