From 660ff3e918de64b13769fc9270b257c7a5b9be1d Mon Sep 17 00:00:00 2001 From: DengMin <16679070171@qq.com> Date: Mon, 8 Aug 2022 15:52:04 +0800 Subject: [PATCH] 更新班级WebSocket消息通知 --- src/main/java/com/subsidy/mapper/ClassMemberMappingMapper.java | 3 +++ src/main/java/com/subsidy/service/ClassDictService.java | 1 + src/main/java/com/subsidy/service/impl/ClassDictServiceImpl.java | 27 +++++++++++++++++++++++++++ src/main/java/com/subsidy/service/impl/CompanyDictServiceImpl.java | 27 +++++++++++++++++++++++++++ src/main/java/com/subsidy/util/websocket/WebSocketUtil.java | 7 ++----- src/main/resources/mapper/ClassMemberMappingMapper.xml | 13 +++++++++++++ src/main/resources/mapper/VodDictMapper.xml | 2 +- 7 files changed, 74 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/subsidy/mapper/ClassMemberMappingMapper.java b/src/main/java/com/subsidy/mapper/ClassMemberMappingMapper.java index 36a6ca5..726c70c 100644 --- a/src/main/java/com/subsidy/mapper/ClassMemberMappingMapper.java +++ b/src/main/java/com/subsidy/mapper/ClassMemberMappingMapper.java @@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.subsidy.model.ClassDictDO; import com.subsidy.model.ClassMemberMappingDO; import com.subsidy.vo.administer.ClassDailyInfoVO; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import java.util.List; @@ -39,4 +40,6 @@ public interface ClassMemberMappingMapper extends BaseMapper getCLassMemberByCompanyId(@Param("companyId") Long companyId); } diff --git a/src/main/java/com/subsidy/service/ClassDictService.java b/src/main/java/com/subsidy/service/ClassDictService.java index 96ccd31..991cbf8 100644 --- a/src/main/java/com/subsidy/service/ClassDictService.java +++ b/src/main/java/com/subsidy/service/ClassDictService.java @@ -11,6 +11,7 @@ import com.subsidy.model.MemberDO; import com.subsidy.vo.classdict.GetAllClassesVO; import com.subsidy.vo.classdict.GetClassBaseInfoVO; +import java.io.IOException; import java.util.List; /** diff --git a/src/main/java/com/subsidy/service/impl/ClassDictServiceImpl.java b/src/main/java/com/subsidy/service/impl/ClassDictServiceImpl.java index f510627..ff0750c 100644 --- a/src/main/java/com/subsidy/service/impl/ClassDictServiceImpl.java +++ b/src/main/java/com/subsidy/service/impl/ClassDictServiceImpl.java @@ -1,7 +1,10 @@ package com.subsidy.service.impl; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.serializer.SerializerFeature; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.subsidy.common.ResponseData; import com.subsidy.common.constant.CourseNotification; import com.subsidy.common.exception.HttpException; import com.subsidy.dto.classDict.AddClassDTO; @@ -33,14 +36,20 @@ import com.subsidy.util.ConstantUtils; import com.subsidy.util.DateFormatUtil; import com.subsidy.util.QuartzUtil; import com.subsidy.util.SMSUtils; +import com.subsidy.util.websocket.WebSocketUtil; +import com.subsidy.vo.classdict.ClassSettingsVO; import com.subsidy.vo.classdict.GetAllClassesVO; import com.subsidy.vo.classdict.GetClassBaseInfoVO; +import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.socket.TextMessage; +import org.springframework.web.socket.WebSocketSession; +import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; @@ -153,6 +162,24 @@ public class ClassDictServiceImpl extends ServiceImpl list = classMemberMappingMapper.selectList(new QueryWrapper() + .lambda() + .eq(ClassMemberMappingDO::getClassId, classDictDO.getId())); + if(CollectionUtils.isNotEmpty(list)) { + for (ClassMemberMappingDO classMemberMappingDO : list) { + WebSocketSession webSocketSession = WebSocketUtil.webSocketMap.get(classMemberMappingDO.getMemberId()); + if(null != webSocketSession && webSocketSession.isOpen()) { + try { + List classSettings = this.baseMapper.getClassSettings(classMemberMappingDO.getMemberId()); + String data = JSONObject.toJSONString(ResponseData.generateCreatedResponse(0, classSettings), SerializerFeature.WriteMapNullValue); + webSocketSession.sendMessage(new TextMessage(data)); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } return ConstantUtils.SET_SUCCESS; } diff --git a/src/main/java/com/subsidy/service/impl/CompanyDictServiceImpl.java b/src/main/java/com/subsidy/service/impl/CompanyDictServiceImpl.java index f78bc04..866c3f6 100644 --- a/src/main/java/com/subsidy/service/impl/CompanyDictServiceImpl.java +++ b/src/main/java/com/subsidy/service/impl/CompanyDictServiceImpl.java @@ -1,9 +1,12 @@ package com.subsidy.service.impl; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.serializer.SerializerFeature; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.subsidy.common.ResponseData; import com.subsidy.common.configure.VODConfig; import com.subsidy.common.constant.VodConstant; import com.subsidy.common.exception.HttpException; @@ -33,7 +36,9 @@ import com.subsidy.util.ConstantUtils; import com.subsidy.util.DateFormatUtil; import com.subsidy.util.EhCacheUtil; import com.subsidy.util.VodUtil; +import com.subsidy.util.websocket.WebSocketUtil; import com.subsidy.vo.administer.OperatorsVO; +import com.subsidy.vo.classdict.ClassSettingsVO; import com.subsidy.vo.company.CDNStatDetailsVO; import com.subsidy.vo.company.DataOverviewVO; import com.subsidy.vo.company.GetAllCompanyVO; @@ -44,6 +49,9 @@ import com.subsidy.vo.company.MemberSummaryVO; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.web.socket.TextMessage; +import org.springframework.web.socket.WebSocketSession; + import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.ArrayList; @@ -105,6 +113,9 @@ public class CompanyDictServiceImpl extends ServiceImpl operators(OperatorsDTO operatorsDTO) { @@ -281,6 +292,22 @@ public class CompanyDictServiceImpl extends ServiceImpl list = classMemberMappingMapper.getCLassMemberByCompanyId(companyDictDO.getId()); + if(CollectionUtils.isNotEmpty(list)) { + for (ClassMemberMappingDO classMemberMappingDO : list) { + WebSocketSession webSocketSession = WebSocketUtil.webSocketMap.get(classMemberMappingDO.getMemberId()); + if(null != webSocketSession && webSocketSession.isOpen()) { + try { + List classSettings = classDictMapper.getClassSettings(classMemberMappingDO.getMemberId()); + String data = JSONObject.toJSONString(ResponseData.generateCreatedResponse(0, classSettings), SerializerFeature.WriteMapNullValue); + webSocketSession.sendMessage(new TextMessage(data)); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } return ConstantUtils.SET_SUCCESS; } diff --git a/src/main/java/com/subsidy/util/websocket/WebSocketUtil.java b/src/main/java/com/subsidy/util/websocket/WebSocketUtil.java index 8bbee2a..724ce45 100644 --- a/src/main/java/com/subsidy/util/websocket/WebSocketUtil.java +++ b/src/main/java/com/subsidy/util/websocket/WebSocketUtil.java @@ -1,6 +1,5 @@ package com.subsidy.util.websocket; -import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; @@ -20,8 +19,6 @@ import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.WebSocketMessage; import org.springframework.web.socket.WebSocketSession; - -import javax.websocket.OnMessage; import java.io.IOException; import java.util.Calendar; import java.util.Date; @@ -51,13 +48,13 @@ public class WebSocketUtil implements WebSocketHandler { private ClassDictMapper classDictMapper; private int heartbeatMin = 1; // 断连最小心跳次数 - private int heartbeatMax = 3; // 断连最大心跳次数 + private int heartbeatMax = 2; // 断连最大心跳次数 private int reconnectionSeconds = 60; //每次断连间隔重新连接秒 /** * 存放建立连接webSocket对象 */ - private ConcurrentHashMap webSocketMap = new ConcurrentHashMap(); + public static ConcurrentHashMap webSocketMap = new ConcurrentHashMap(); ConcurrentHashMap taskMap = new ConcurrentHashMap<>(1); diff --git a/src/main/resources/mapper/ClassMemberMappingMapper.xml b/src/main/resources/mapper/ClassMemberMappingMapper.xml index 74790ff..0345037 100644 --- a/src/main/resources/mapper/ClassMemberMappingMapper.xml +++ b/src/main/resources/mapper/ClassMemberMappingMapper.xml @@ -76,4 +76,17 @@ and t.member_id = #{memberId} + + diff --git a/src/main/resources/mapper/VodDictMapper.xml b/src/main/resources/mapper/VodDictMapper.xml index f0e9007..9268fac 100644 --- a/src/main/resources/mapper/VodDictMapper.xml +++ b/src/main/resources/mapper/VodDictMapper.xml @@ -39,7 +39,7 @@ t2.content, t.order_no, t.vod_code, - t3.vod_alias_name + IFNULL( t3.vod_alias_name, t.vod_name ) AS vod_alias_name FROM vod_dict t LEFT JOIN content_vod_mapping t3 ON t3.vod_id = t.id -- libgit2 0.25.0