|
|
@@ -13,6 +13,7 @@ import com.ozs.web.controller.websocket.WebSocketServer;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.scheduling.annotation.EnableAsync;
|
|
|
@@ -40,73 +41,77 @@ public class InsertUserStateUtil {
|
|
|
private BaseUserMapper baseUserMapper;
|
|
|
@Autowired
|
|
|
private UserStateStatisticsService userStateStatisticsService;
|
|
|
+ @Value("${base.isEnable:false}")
|
|
|
+ private String isEnable;
|
|
|
|
|
|
@Async
|
|
|
@Scheduled(cron = "0 */5 * * * ?")
|
|
|
@Transactional
|
|
|
public void tableCreationAndDelete() {
|
|
|
- log.info("开始执行 tableCreationAndDelete 任务");
|
|
|
- // 获取在线用户和工务段用户
|
|
|
- List<String> onlineUser = webSocketServer.getOnlineUser();
|
|
|
- List<String> userIdList = baseUserMapper.getUserIdsLists();
|
|
|
- //需要推送信息的用户
|
|
|
- List<String> userIdsAll = baseUserMapper.getUserIdsAll();
|
|
|
- //获取属于工段用户的用户ID
|
|
|
- List<String> userLists =baseUserMapper.getUserLists();
|
|
|
-
|
|
|
-
|
|
|
- // 使用Set提高查找性能
|
|
|
- Set<String> onlineSet = new HashSet<>(onlineUser);
|
|
|
- Set<String> userSet = new HashSet<>(userIdList);
|
|
|
- Set<String> userAll = new HashSet<>(userIdsAll);
|
|
|
-
|
|
|
- // 计算差集和交集
|
|
|
- Set<String> offlineUsers = userSet.stream()
|
|
|
- .filter(userId -> !onlineSet.contains(userId))
|
|
|
- .collect(Collectors.toSet());
|
|
|
- Set<String> offlineUsersAll = userAll.stream()
|
|
|
- .filter(userId -> !onlineSet.contains(userId))
|
|
|
- .collect(Collectors.toSet());
|
|
|
- Set<String> onlineUsers = onlineSet.stream()
|
|
|
- .filter(userSet::contains)
|
|
|
- .collect(Collectors.toSet());
|
|
|
-
|
|
|
- // 批量查询用户信息,减少数据库查询
|
|
|
- Map<String, BaseUser> userMap = baseUserMapper.getUsersByIds(
|
|
|
- Stream.concat(offlineUsers.stream(), onlineUsers.stream())
|
|
|
- .collect(Collectors.toList())
|
|
|
- ).stream().collect(Collectors.toMap(BaseUser::getUserId, user -> user));
|
|
|
-
|
|
|
- // 批量查询最新用户状态
|
|
|
- List<String> allUserIds = Stream.concat(offlineUsers.stream(), onlineUsers.stream())
|
|
|
- .collect(Collectors.toList());
|
|
|
- Map<String, UserStateStatistics> latestUserStateMap = getUserLatestStateMap(allUserIds);
|
|
|
- log.info("用户状态 {}", latestUserStateMap);
|
|
|
- // 处理离线用户
|
|
|
- processUserStates(offlineUsers, userMap, latestUserStateMap, 2, userStateStatisticsService);
|
|
|
-
|
|
|
- // 处理在线用户
|
|
|
- processUserStates(onlineUsers, userMap, latestUserStateMap, 1, userStateStatisticsService);
|
|
|
- // 推送信息
|
|
|
- String data = objStr(offlineUsersAll.size());
|
|
|
- log.info("准备推送数据: {}, 目标用户数: {}", data, userLists.size());
|
|
|
- // 先批量检查在线状态,减少重复调用
|
|
|
- List<String> onlineUserList = userLists.stream()
|
|
|
- .filter(webSocketServer::userOnline)
|
|
|
- .collect(Collectors.toList());
|
|
|
-
|
|
|
- log.info("在线用户数: {}", onlineUserList.size());
|
|
|
-
|
|
|
- // 批量推送
|
|
|
- onlineUserList.parallelStream().forEach(userId -> {
|
|
|
- try {
|
|
|
- webSocketServer.sendOneMessage(userId, data);
|
|
|
- log.info("推送成功 - 用户: {}", userId);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("推送消息给用户 {} 时发生异常: {}", userId, e.getMessage(), e);
|
|
|
- }
|
|
|
- });
|
|
|
- log.info("推送完成,成功推送数: {}", onlineUserList.size());
|
|
|
+ if ("true".equals(isEnable)) {
|
|
|
+ log.info("开始执行 tableCreationAndDelete 任务");
|
|
|
+ // 获取在线用户和工务段用户
|
|
|
+ List<String> onlineUser = webSocketServer.getOnlineUser();
|
|
|
+ List<String> userIdList = baseUserMapper.getUserIdsLists();
|
|
|
+ //需要推送信息的用户
|
|
|
+ List<String> userIdsAll = baseUserMapper.getUserIdsAll();
|
|
|
+ //获取属于工段用户的用户ID
|
|
|
+ List<String> userLists = baseUserMapper.getUserLists();
|
|
|
+
|
|
|
+
|
|
|
+ // 使用Set提高查找性能
|
|
|
+ Set<String> onlineSet = new HashSet<>(onlineUser);
|
|
|
+ Set<String> userSet = new HashSet<>(userIdList);
|
|
|
+ Set<String> userAll = new HashSet<>(userIdsAll);
|
|
|
+
|
|
|
+ // 计算差集和交集
|
|
|
+ Set<String> offlineUsers = userSet.stream()
|
|
|
+ .filter(userId -> !onlineSet.contains(userId))
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ Set<String> offlineUsersAll = userAll.stream()
|
|
|
+ .filter(userId -> !onlineSet.contains(userId))
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ Set<String> onlineUsers = onlineSet.stream()
|
|
|
+ .filter(userSet::contains)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+
|
|
|
+ // 批量查询用户信息,减少数据库查询
|
|
|
+ Map<String, BaseUser> userMap = baseUserMapper.getUsersByIds(
|
|
|
+ Stream.concat(offlineUsers.stream(), onlineUsers.stream())
|
|
|
+ .collect(Collectors.toList())
|
|
|
+ ).stream().collect(Collectors.toMap(BaseUser::getUserId, user -> user));
|
|
|
+
|
|
|
+ // 批量查询最新用户状态
|
|
|
+ List<String> allUserIds = Stream.concat(offlineUsers.stream(), onlineUsers.stream())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ Map<String, UserStateStatistics> latestUserStateMap = getUserLatestStateMap(allUserIds);
|
|
|
+ log.info("用户状态 {}", latestUserStateMap);
|
|
|
+ // 处理离线用户
|
|
|
+ processUserStates(offlineUsers, userMap, latestUserStateMap, 2, userStateStatisticsService);
|
|
|
+
|
|
|
+ // 处理在线用户
|
|
|
+ processUserStates(onlineUsers, userMap, latestUserStateMap, 1, userStateStatisticsService);
|
|
|
+ // 推送信息
|
|
|
+ String data = objStr(offlineUsersAll.size());
|
|
|
+ log.info("准备推送数据: {}, 目标用户数: {}", data, userLists.size());
|
|
|
+ // 先批量检查在线状态,减少重复调用
|
|
|
+ List<String> onlineUserList = userLists.stream()
|
|
|
+ .filter(webSocketServer::userOnline)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ log.info("在线用户数: {}", onlineUserList.size());
|
|
|
+
|
|
|
+ // 批量推送
|
|
|
+ onlineUserList.parallelStream().forEach(userId -> {
|
|
|
+ try {
|
|
|
+ webSocketServer.sendOneMessage(userId, data);
|
|
|
+ log.info("推送成功 - 用户: {}", userId);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("推送消息给用户 {} 时发生异常: {}", userId, e.getMessage(), e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ log.info("推送完成,成功推送数: {}", onlineUserList.size());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|