diff --git a/cloud-common-websocket/src/main/java/com/cm/websocket/WebSocket.java b/cloud-common-websocket/src/main/java/com/cm/websocket/WebSocket.java index 33b0cd8..2914c9d 100644 --- a/cloud-common-websocket/src/main/java/com/cm/websocket/WebSocket.java +++ b/cloud-common-websocket/src/main/java/com/cm/websocket/WebSocket.java @@ -97,6 +97,8 @@ public class WebSocket { sendTextMessage(session, appSocketMessage); } else if (AppSocketTypeEnum.GROUP_MESSAGE.getValue() == appSocketMessage.getType()) { AppSocketSessionManager.getInstance().getMessageService().sendGroupTextMessage(session, appSocketMessage); + } else if (AppSocketTypeEnum.UPDATE_SERVICE_HANDLE_STATUS.getValue() == appSocketMessage.getType()) { + updateServiceHandleStatus(session, appSocketMessage); } else if (AppSocketTypeEnum.SEARCH_ONLINE_USER.getValue() == appSocketMessage.getType()) { listOnlineUser(session, appSocketMessage); } else if (AppSocketTypeEnum.SEARCH_ONLINE_USER_FRIEND.getValue() == appSocketMessage.getType()) { @@ -125,6 +127,19 @@ public class WebSocket { } } + /** + * 更新业务处理状态并返回总数 + */ + private void updateServiceHandleStatus(Session session, AppSocketMessage appSocketMessage) { + String userId = appSocketMessage.getFrom(); + appSocketMessage.setTo(userId); + AppSocketMessage.CountNeedToDealWithBody countNeedToDealWithBody = JSONObject.parseObject(appSocketMessage.getBody(), AppSocketMessage.CountNeedToDealWithBody.class); + INoticeService noticeService = serviceContext.getBean(INoticeService.class); + noticeService.updateHandleStatus(userId, countNeedToDealWithBody); + countNeedToDealWithBody.setServiceId(null); + countNeedToDealWith(session, appSocketMessage, noticeService, countNeedToDealWithBody); + } + /** * 待办总数 * @@ -136,8 +151,21 @@ public class WebSocket { appSocketMessage.setTo(userId); AppSocketMessage.CountNeedToDealWithBody countNeedToDealWithBody = JSONObject.parseObject(appSocketMessage.getBody(), AppSocketMessage.CountNeedToDealWithBody.class); INoticeService noticeService = serviceContext.getBean(INoticeService.class); - List counts = noticeService.listNoticeCount(userId, countNeedToDealWithBody); + countNeedToDealWith(session, appSocketMessage, noticeService, countNeedToDealWithBody); + } + /** + * 待办总数 + * + * @param session + * @param appSocketMessage + * @param noticeService + * @param countNeedToDealWithBody + */ + private void countNeedToDealWith(Session session, AppSocketMessage appSocketMessage, INoticeService noticeService, AppSocketMessage.CountNeedToDealWithBody countNeedToDealWithBody) { + appSocketMessage.setType(AppSocketTypeEnum.SEARCH_COUNT_NEED_TO_DEALT_WITH.getValue()); + List counts = noticeService.listNoticeCount(appSocketMessage.getFrom(), countNeedToDealWithBody); + appSocketMessage.setBody(JSONArray.toJSONString(counts)); send(session, appSocketMessage); } diff --git a/cloud-common-websocket/src/main/java/com/cm/websocket/controller/apis/message/MessageController.java b/cloud-common-websocket/src/main/java/com/cm/websocket/controller/apis/message/MessageController.java index ef3b941..a5d95a3 100644 --- a/cloud-common-websocket/src/main/java/com/cm/websocket/controller/apis/message/MessageController.java +++ b/cloud-common-websocket/src/main/java/com/cm/websocket/controller/apis/message/MessageController.java @@ -54,7 +54,15 @@ public class MessageController { @PostMapping("noticewithtarget") @CheckRequestBodyAnnotation public SuccessResult saveNoticeWithTarget(@RequestBody NoticeVO noticeVO) throws Exception { - return messageService.saveNoticeWithTarget(noticeVO); + return messageService.saveNoticeWithTarget(noticeVO, false); + } + + @ApiOperation(value = "通知消息", notes = "通知消息接口") + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @PostMapping("noticewithtarget/save") + @CheckRequestBodyAnnotation + public SuccessResult saveNoticeWithTargetAndSave(@RequestBody NoticeVO noticeVO) throws Exception { + return messageService.saveNoticeWithTarget(noticeVO, true); } } diff --git a/cloud-common-websocket/src/main/java/com/cm/websocket/controller/resource/message/MessageResourceController.java b/cloud-common-websocket/src/main/java/com/cm/websocket/controller/resource/message/MessageResourceController.java index 114854d..04b5c39 100644 --- a/cloud-common-websocket/src/main/java/com/cm/websocket/controller/resource/message/MessageResourceController.java +++ b/cloud-common-websocket/src/main/java/com/cm/websocket/controller/resource/message/MessageResourceController.java @@ -31,7 +31,7 @@ public class MessageResourceController { @Autowired private IMessageService messageService; - @ApiOperation(value = "通知消息", notes = "通知消息接口") + @ApiOperation(value = "通知消息(不保存记录)", notes = "通知消息(不保存记录)接口") @ApiImplicitParams({ @ApiImplicitParam(name = "access_token", value = "access_token", paramType = "query") }) @@ -39,7 +39,18 @@ public class MessageResourceController { @PostMapping("noticewithtarget") @CheckRequestBodyAnnotation public SuccessResult saveNoticeWithTarget(@RequestBody NoticeVO noticeVO) throws Exception { - return messageService.saveNoticeWithTarget(noticeVO); + return messageService.saveNoticeWithTarget(noticeVO, false); + } + + @ApiOperation(value = "通知消息(保存记录)", notes = "通知消息(保存记录)接口") + @ApiImplicitParams({ + @ApiImplicitParam(name = "access_token", value = "access_token", paramType = "query") + }) + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @PostMapping("noticewithtarget/save") + @CheckRequestBodyAnnotation + public SuccessResult saveNoticeWithTargetAndSave(@RequestBody NoticeVO noticeVO) throws Exception { + return messageService.saveNoticeWithTarget(noticeVO, true); } } diff --git a/cloud-common-websocket/src/main/java/com/cm/websocket/enums/AppSocketTypeEnum.java b/cloud-common-websocket/src/main/java/com/cm/websocket/enums/AppSocketTypeEnum.java index 58c8491..18b6523 100644 --- a/cloud-common-websocket/src/main/java/com/cm/websocket/enums/AppSocketTypeEnum.java +++ b/cloud-common-websocket/src/main/java/com/cm/websocket/enums/AppSocketTypeEnum.java @@ -68,6 +68,10 @@ public enum AppSocketTypeEnum { * 目标通知,用于APP打开特定页面 */ NOTICE_TARGET_MESSAGE(108), + /** + * 更新业务的处理状态 + */ + UPDATE_SERVICE_HANDLE_STATUS(501), /** * 查询全部在线用户,body 为查询用户的 userId */ diff --git a/cloud-common-websocket/src/main/java/com/cm/websocket/pojo/AppSocketMessage.java b/cloud-common-websocket/src/main/java/com/cm/websocket/pojo/AppSocketMessage.java index 88b0fe3..f2ff815 100644 --- a/cloud-common-websocket/src/main/java/com/cm/websocket/pojo/AppSocketMessage.java +++ b/cloud-common-websocket/src/main/java/com/cm/websocket/pojo/AppSocketMessage.java @@ -263,6 +263,7 @@ public class AppSocketMessage { private String system; private String module; private String menu; + private String serviceId; private Integer count; public String getSystem() { @@ -289,6 +290,14 @@ public class AppSocketMessage { this.menu = menu; } + public String getServiceId() { + return serviceId == null ? "" : serviceId; + } + + public void setServiceId(String serviceId) { + this.serviceId = serviceId; + } + public Integer getCount() { return count == null ? 0 : count; } diff --git a/cloud-common-websocket/src/main/java/com/cm/websocket/service/IMessageService.java b/cloud-common-websocket/src/main/java/com/cm/websocket/service/IMessageService.java index 696706d..b1e12ee 100644 --- a/cloud-common-websocket/src/main/java/com/cm/websocket/service/IMessageService.java +++ b/cloud-common-websocket/src/main/java/com/cm/websocket/service/IMessageService.java @@ -40,10 +40,11 @@ public interface IMessageService { * 通知消息 * * @param noticeVO + * @param save * @return * @throws SearchException */ - SuccessResult saveNoticeWithTarget(NoticeVO noticeVO) throws Exception; + SuccessResult saveNoticeWithTarget(NoticeVO noticeVO, boolean save) throws Exception; /** * 群发文本消息 diff --git a/cloud-common-websocket/src/main/java/com/cm/websocket/service/INoticeService.java b/cloud-common-websocket/src/main/java/com/cm/websocket/service/INoticeService.java index b3d435a..ae6a163 100644 --- a/cloud-common-websocket/src/main/java/com/cm/websocket/service/INoticeService.java +++ b/cloud-common-websocket/src/main/java/com/cm/websocket/service/INoticeService.java @@ -26,6 +26,15 @@ public interface INoticeService { */ void save(String userId, NoticeVO.Notice notice) throws Exception; + + /** + * 更新已办状态 + * + * @param userId + * @param countNeedToDealWithBody + */ + void updateHandleStatus(String userId, AppSocketMessage.CountNeedToDealWithBody countNeedToDealWithBody); + /** * 通知数量 * @@ -42,4 +51,5 @@ public interface INoticeService { * @return */ Integer count(Map params); + } diff --git a/cloud-common-websocket/src/main/java/com/cm/websocket/service/impl/MessageServiceImpl.java b/cloud-common-websocket/src/main/java/com/cm/websocket/service/impl/MessageServiceImpl.java index 82a5738..cec13ab 100644 --- a/cloud-common-websocket/src/main/java/com/cm/websocket/service/impl/MessageServiceImpl.java +++ b/cloud-common-websocket/src/main/java/com/cm/websocket/service/impl/MessageServiceImpl.java @@ -1,5 +1,6 @@ package com.cm.websocket.service.impl; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.cm.common.base.AbstractService; import com.cm.common.exception.SaveException; @@ -23,6 +24,9 @@ import org.springframework.stereotype.Service; import javax.websocket.Session; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; /** * When you feel like quitting. Think about why you started @@ -41,6 +45,7 @@ public class MessageServiceImpl extends AbstractService implements IMessageServi private IUserImBaseService userImBaseService; @Autowired private INoticeService noticeService; + private ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1); @Override public SuccessResult sendByAppId(String appId) { @@ -81,7 +86,7 @@ public class MessageServiceImpl extends AbstractService implements IMessageServi } @Override - public SuccessResult saveNoticeWithTarget(NoticeVO noticeVO) throws Exception { + public SuccessResult saveNoticeWithTarget(NoticeVO noticeVO, boolean save) throws Exception { for (NoticeVO.Notice notice : noticeVO.getNoticies()) { if (notice.getUserIds() == null || notice.getUserIds().isEmpty()) { continue; @@ -93,20 +98,42 @@ public class MessageServiceImpl extends AbstractService implements IMessageServi notice.setMsg("您有一条系统消息"); } List appSocketSessions = AppSocketSessionManager.getInstance().listSessionByUserIds(notice.getUserIds()); - for (AppSocketSession appSocketSession : appSocketSessions) { - Session session = appSocketSession.getSession(); - AppSocketMessage.NoticeBody noticeBody = new AppSocketMessage.NoticeBody(notice.getTitle(), notice.getMsg(), notice.getTarget()); - AppSocketMessage appSocketMessage = new AppSocketMessage(AppSocketTypeEnum.NOTICE_TARGET_MESSAGE.getValue(), - true, - AppSocketSessionManager.FORM_SYSTEM, - appSocketSession.getUserId(), - JSONObject.toJSONString(noticeBody)); - WebSocket.send(session, appSocketMessage); - } - // 保存通知记录 - List userIds = notice.getUserIds(); - for (String userId : userIds) { - noticeService.save(userId, notice); + // 推送3s后通知 + scheduledExecutorService.schedule(() -> { + for (AppSocketSession appSocketSession : appSocketSessions) { + Session session = appSocketSession.getSession(); + AppSocketMessage.NoticeBody noticeBody = new AppSocketMessage.NoticeBody(notice.getTitle(), notice.getMsg(), notice.getTarget()); + AppSocketMessage appSocketMessage = new AppSocketMessage(AppSocketTypeEnum.NOTICE_TARGET_MESSAGE.getValue(), + true, + AppSocketSessionManager.FORM_SYSTEM, + appSocketSession.getUserId(), + JSONObject.toJSONString(noticeBody)); + WebSocket.send(session, appSocketMessage); + } + }, 3, TimeUnit.SECONDS); + if (save) { + // 保存通知记录 + List userIds = notice.getUserIds(); + for (String userId : userIds) { + noticeService.save(userId, notice); + } + // 3s后通知,保证事务提交 + scheduledExecutorService.schedule(() -> { + // 更新状态待办 + for (AppSocketSession appSocketSession : appSocketSessions) { + Session session = appSocketSession.getSession(); + AppSocketMessage.CountNeedToDealWithBody countNeedToDealWithBody = new AppSocketMessage.CountNeedToDealWithBody(); + countNeedToDealWithBody.setSystem(notice.getSystem()); + // 查询待办状态 + List counts = noticeService.listNoticeCount(appSocketSession.getUserId(), countNeedToDealWithBody); + AppSocketMessage appSocketMessage = new AppSocketMessage(AppSocketTypeEnum.SEARCH_COUNT_NEED_TO_DEALT_WITH.getValue(), + true, + AppSocketSessionManager.FORM_SYSTEM, + appSocketSession.getUserId(), + JSONArray.toJSONString(counts)); + WebSocket.send(session, appSocketMessage); + } + }, 3, TimeUnit.SECONDS); } } return new SuccessResult(); diff --git a/cloud-common-websocket/src/main/java/com/cm/websocket/service/impl/NoticeServiceImpl.java b/cloud-common-websocket/src/main/java/com/cm/websocket/service/impl/NoticeServiceImpl.java index 036ba24..54f5dc9 100644 --- a/cloud-common-websocket/src/main/java/com/cm/websocket/service/impl/NoticeServiceImpl.java +++ b/cloud-common-websocket/src/main/java/com/cm/websocket/service/impl/NoticeServiceImpl.java @@ -47,6 +47,22 @@ public class NoticeServiceImpl extends AbstractService implements INoticeService noticeDao.save(params); } + @Override + public void updateHandleStatus(String userId, AppSocketMessage.CountNeedToDealWithBody countNeedToDealWithBody) { + if (StringUtils.isBlank(countNeedToDealWithBody.getSystem())) { + return; + } + if (StringUtils.isBlank(countNeedToDealWithBody.getServiceId())) { + return; + } + Map params = getHashMap(6); + params.put("isHandle", 1); + params.put("noticeSystem", countNeedToDealWithBody.getSystem()); + params.put("noticeServiceId", countNeedToDealWithBody.getServiceId()); + setUpdateInfoByUserId(params, userId); + noticeDao.updateHandle(params); + } + @Override public List listNoticeCount(String userId, AppSocketMessage.CountNeedToDealWithBody countNeedToDealWithBody) { List noticeCounts = new ArrayList<>(); @@ -70,7 +86,7 @@ public class NoticeServiceImpl extends AbstractService implements INoticeService for (String module : modules) { AppSocketMessage.CountNeedToDealWithBody systemCount = new AppSocketMessage.CountNeedToDealWithBody(); systemCount.setSystem(countNeedToDealWithBody.getSystem()); - systemCount.setModule(countNeedToDealWithBody.getModule()); + systemCount.setModule(module); params.put("noticeModule", module); systemCount.setCount(count(params)); noticeCounts.add(systemCount); @@ -84,6 +100,7 @@ public class NoticeServiceImpl extends AbstractService implements INoticeService AppSocketMessage.CountNeedToDealWithBody systemCount = new AppSocketMessage.CountNeedToDealWithBody(); systemCount.setSystem(countNeedToDealWithBody.getSystem()); systemCount.setModule(countNeedToDealWithBody.getModule()); + systemCount.setMenu(menu); params.put("noticeMenu", menu); systemCount.setCount(count(params)); noticeCounts.add(systemCount); diff --git a/cloud-common-websocket/src/main/resources/mybatis/mapper/notice-mapper.xml b/cloud-common-websocket/src/main/resources/mybatis/mapper/notice-mapper.xml index 02ab228..16d3502 100644 --- a/cloud-common-websocket/src/main/resources/mybatis/mapper/notice-mapper.xml +++ b/cloud-common-websocket/src/main/resources/mybatis/mapper/notice-mapper.xml @@ -67,6 +67,8 @@ gmt_modified = #{gmtModified}, modifier = #{modifier} WHERE + notice_system = #{noticeSystem} + AND notice_service_id = #{noticeServiceId}