From 3bb603c96affbab6d02164fb2bbe96a1d72c14bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B7=B4=E6=8B=89=E8=BF=AA=E7=BB=B4?= Date: Tue, 14 Sep 2021 10:43:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0Notification=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1、调整web接口已支持客户端获取已读、未读的消息数、列表等操作 2、调整上述内容相关的 mybatis 配置 3、调整 Redis 缓存相关的 Key 生成策略及过程 --- .../dao/mapper/SysNotificationMapper.java | 13 +- .../notification/SysNotificationService.java | 33 +---- .../impl/SysNotificationServiceImpl.java | 61 ++++----- .../notification/SysNotificationMapper.xml | 15 ++- .../controller/NotificationController.java | 125 ++++++------------ 5 files changed, 96 insertions(+), 151 deletions(-) diff --git a/model/src/main/java/cn/org/gitlink/notification/model/dao/mapper/SysNotificationMapper.java b/model/src/main/java/cn/org/gitlink/notification/model/dao/mapper/SysNotificationMapper.java index 647063e..458bcc9 100644 --- a/model/src/main/java/cn/org/gitlink/notification/model/dao/mapper/SysNotificationMapper.java +++ b/model/src/main/java/cn/org/gitlink/notification/model/dao/mapper/SysNotificationMapper.java @@ -10,15 +10,15 @@ import java.util.List; public interface SysNotificationMapper extends BaseMapper { int deleteByPrimaryKey(@Param("platform") String platform, Integer id); - int insert(@Param("platform") String platform,@Param("record") SysNotification record); + int insert(@Param("platform") String platform, @Param("record") SysNotification record); - int insertSelective(@Param("platform") String platform,@Param("record") SysNotification record); + int insertSelective(@Param("platform") String platform, @Param("record") SysNotification record); SysNotification selectByPrimaryKey(@Param("platform") String platform, Integer id); int updateByPrimaryKeySelective(@Param("platform") String platform, SysNotification record); - int updateByPrimaryKey(@Param("platform") String platform,@Param("record") SysNotification record); + int updateByPrimaryKey(@Param("platform") String platform, @Param("record") SysNotification record); int updateStatusByNotificationId(@Param("platform") String platform, @Param("receiver") Integer receiver, @@ -26,7 +26,9 @@ public interface SysNotificationMapper extends BaseMapper { @Param("status") Integer status); int getNewSysNotificationNumber(@Param("platform") String platform, - @Param("receiver") Integer receiver); + @Param("receiver") Integer receiver, + @Param("type") Integer type, + @Param("status") Integer status); List getUnreadSysNotificationsList(@Param("platform") String platform, @Param("receiver") Integer receiver, @@ -37,7 +39,8 @@ public interface SysNotificationMapper extends BaseMapper { List getSysNotificationPageList(Page page, String orderBy, @Param("type") int type, @Param("platform") String platform, - @Param("receiver") Integer receiver); + @Param("receiver") Integer receiver, + @Param("status") Integer status); int getSysNotificationNumberByType(@Param("platform") String platform, @Param("receiver") Integer receiver, diff --git a/model/src/main/java/cn/org/gitlink/notification/model/service/notification/SysNotificationService.java b/model/src/main/java/cn/org/gitlink/notification/model/service/notification/SysNotificationService.java index d1f5dd7..3b08066 100644 --- a/model/src/main/java/cn/org/gitlink/notification/model/service/notification/SysNotificationService.java +++ b/model/src/main/java/cn/org/gitlink/notification/model/service/notification/SysNotificationService.java @@ -23,45 +23,26 @@ public interface SysNotificationService extends IService { int markNotificationAs(String platform, Integer receiver, String notificationIds, Integer status) throws Exception; /** - * 获取所有未读消息总数 + * 获取消息总数 * * @param platform 平台编号 * @param receiver 信息接收者 + * @param type 类型:-1 返回全部、1 系统消息、2 @我消息 + * @param status 状态 -1 全部、1 未读 2 已读 * @return */ - int getUnreadNotificationCount(String platform, Integer receiver) throws Exception; + int getNotificationCount(String platform, Integer receiver, Integer type, Integer status) throws Exception; /** - * 获取所有未读消息 - * - * @param platform 平台编号 - * @param receiver 消息接收者 - * @param size 页大小 - * @param type 1 系统消息 2 @我 - * @return - */ - List getUnreadNotificationByType(String platform, Integer receiver, Integer size, Integer type) throws Exception; - - /** - * 按分类获取未读消息总数 - * - * @param type 1 系统消息 2 @我 - * @param platform 平台编号 - * @param receiver 消息接收者 - * @return - */ - int getUnreadNotificationByType(Integer type, String platform, Integer receiver) throws Exception; - - /** - * 获取所有系统消息 + * 获取消息列表 * * @param type 类型 -1 全部 1 系统消息、2 @我 * @param page 页码 * @param size 页大小 - * @param orderBy 排序 * @param platform 平台编号 * @param receiver 消息接收者 + * @param status 状态 -1 全部、1 未读 2 已读 * @return */ - Page getNotification(int type, int page, int size, String orderBy, String platform, Integer receiver) throws Exception; + Page getNotification(String platform, Integer receiver, Integer status, Integer type, Integer page, Integer size) throws Exception; } diff --git a/model/src/main/java/cn/org/gitlink/notification/model/service/notification/impl/SysNotificationServiceImpl.java b/model/src/main/java/cn/org/gitlink/notification/model/service/notification/impl/SysNotificationServiceImpl.java index 3324ffa..2d25702 100644 --- a/model/src/main/java/cn/org/gitlink/notification/model/service/notification/impl/SysNotificationServiceImpl.java +++ b/model/src/main/java/cn/org/gitlink/notification/model/service/notification/impl/SysNotificationServiceImpl.java @@ -18,7 +18,7 @@ import java.util.List; @Service(value = "SysNotificationServiceImpl") public class SysNotificationServiceImpl extends ServiceImpl implements SysNotificationService { - private static final String CACHE_KEY_PREFFIX = "GNSCache#"; + private static final String CACHE_KEY_PREFIX = "GNSCPre"; @Autowired @@ -38,6 +38,7 @@ public class SysNotificationServiceImpl extends ServiceImpl list = Arrays.asList(newSysNotificationVo.getReceivers().split(",")); for (String receiver : list) { sysNotification.setReceiver(Integer.parseInt(receiver)); + //TODO: 需要批量插入的支持 baseMapper.insertSelective(newSysNotificationVo.getPlatform(), sysNotification); this.delUserCache(newSysNotificationVo.getPlatform(), Integer.parseInt(receiver)); } @@ -54,61 +55,49 @@ public class SysNotificationServiceImpl extends ServiceImpl getUnreadNotificationByType(String platform, Integer receiver, Integer size, Integer type) throws Exception { - String KEY_TYPE_LIST = CACHE_KEY_PREFFIX + platform.toUpperCase() + "#" + receiver + "#T" + type + "#LIST"; - Object foundResult = this.redisUtil.get(KEY_TYPE_LIST); - if (foundResult != null) { - return (List) foundResult; - } - List sysNotificationList = baseMapper.getUnreadSysNotificationsList(platform, receiver, 1, size, type); - this.redisUtil.set(KEY_TYPE_LIST, sysNotificationList); - return sysNotificationList; - } @Override - public int getUnreadNotificationByType(Integer type, String platform, Integer receiver) throws Exception { - String KEY_TYPE_COUNT = CACHE_KEY_PREFFIX + platform.toUpperCase() + "#" + receiver + "#T" + type + "#COUNT"; - Object foundResult = this.redisUtil.get(KEY_TYPE_COUNT); - if (foundResult != null) { - return (Integer) foundResult; - } - int count = baseMapper.getSysNotificationNumberByType(platform, receiver, type); - this.redisUtil.set(KEY_TYPE_COUNT, count); - return count; - } - - @Override - public Page getNotification(int type, int page, int size, String orderBy, String platform, Integer receiver) throws Exception { - String KEY_PAGE_LIST = CACHE_KEY_PREFFIX + platform.toUpperCase() + "#" + receiver + "P" + page + "S" + size + "T" + type + "#LIST"; - Object foundResult = this.redisUtil.get(KEY_PAGE_LIST); + public Page getNotification(String platform, Integer receiver, Integer status, Integer type, Integer page, Integer size) throws Exception { + String cacheKey = cacheKeyForPage(platform, receiver, type, status, page, size); + Object foundResult = this.redisUtil.get(cacheKey); if (foundResult != null) { return (Page) foundResult; } Page pageItem = new Page(page, size); List sysNotificationList = baseMapper.getSysNotificationPageList( - pageItem, orderBy, type, platform, receiver + pageItem, "", type, platform, receiver, status ); pageItem.setRecords(sysNotificationList); - this.redisUtil.set(KEY_PAGE_LIST, pageItem); + this.redisUtil.set(cacheKey, pageItem); return pageItem; } - //////////////////////////////// 私有方法 //////////////////////////////// private void delUserCache(String platform, Integer receiver) { - redisUtil.scanAndDel(CACHE_KEY_PREFFIX + platform.toUpperCase() + "#" + receiver + "*"); + redisUtil.scanAndDel(cachePrefixForPlatform(platform, receiver) + "*"); }//end of method + + private static String cacheKeyForCount(String platform, Integer receiver, Integer type, Integer status) { + return String.format("%s#T%s#S%s#Count", cachePrefixForPlatform(platform, receiver), type, status); + } + + private static String cacheKeyForPage(String platform, Integer receiver, Integer type, Integer status, Integer page, Integer size) { + return String.format("%s#T%s#S%s#P%s_S%s", cachePrefixForPlatform(platform, receiver), type, status, page, size); + } + + private static String cachePrefixForPlatform(String platform, Integer receiver) { + return String.format("%s#%s#R%s", CACHE_KEY_PREFIX, platform.trim().toUpperCase(), receiver); + } } diff --git a/model/src/main/resources/mapper/notification/SysNotificationMapper.xml b/model/src/main/resources/mapper/notification/SysNotificationMapper.xml index 95df095..104c05b 100644 --- a/model/src/main/resources/mapper/notification/SysNotificationMapper.xml +++ b/model/src/main/resources/mapper/notification/SysNotificationMapper.xml @@ -162,12 +162,20 @@ - update ${platform}_sys_notification set status = #{status} where receiver=#{receiver} and id in (${notificationIds}) + update ${platform}_sys_notification set status = #{status} where receiver=#{receiver} and id in + (${notificationIds}) \ No newline at end of file diff --git a/reader/src/main/java/cn/org/gitlink/notification/reader/controller/NotificationController.java b/reader/src/main/java/cn/org/gitlink/notification/reader/controller/NotificationController.java index 6abd003..385f8a3 100644 --- a/reader/src/main/java/cn/org/gitlink/notification/reader/controller/NotificationController.java +++ b/reader/src/main/java/cn/org/gitlink/notification/reader/controller/NotificationController.java @@ -19,14 +19,16 @@ import org.springframework.web.bind.annotation.*; @RestController @RequestMapping(value = "/gns/notification") public class NotificationController { + //未读消息类型 private static final Integer NOTIFICATION_TYPE_ALL = -1; //全部未读消息 private static final Integer NOTIFICATION_TYPE_SYS = 1; //系统消息 private static final Integer NOTIFICATION_TYPE_ATME = 2; //@我的消息 - //是否显示全部信息的判断 - private static final Integer SHOW_OPTION_UNREAD = 1; //只显示固定数目的未读消息 - private static final Integer SHOW_OPTION_ALL = 2; //显示所有消息 + //是否已读状态 + private static final Integer STATUS_OPTION_ALL = -1; //所有信息类型,包括未读、已读 + private static final Integer STATUS_OPTION_UNREAD = 1; //未读信息 + private static final Integer STATUS_OPTION_READ = 2; //已读信息 @Autowired private RedisUtil redisUtil; @@ -40,33 +42,31 @@ public class NotificationController { /** * 获取系统消息列表 * - * @param platform 平台 - * @param receiver 消息接收者 - * @param showOptions 显示选项:1 只获取未读消息、2 获取全部消息 - * @param type 消息类型:1 系统消息、2 @我消息 - * @param pageNum 页码(只在 show = 2 是生效) - * @param pageSize 页大小(只在 show = 2 是生效) + * @param platform 平台 + * @param receiver 消息接收者 + * @param status 信息状态:值未-1时,获取全部信息;值为1时,只获取未读消息;值为2时,获取已读消息 + * @param type 消息类型:1 系统消息、2 @我消息 * @return */ @ApiOperation("获取系统消息列表") @GetMapping(value = "/{platform}/list") public ResponseData list(@ApiParam(value = "平台编码", required = true) - @PathVariable(name = "platform", required = true) String platform, + @PathVariable(name = "platform", required = true) String platform, - @ApiParam(value = "消息接收者", required = true) - @RequestParam(name = "receiver", required = true) Integer receiver, + @ApiParam(value = "消息接收者", required = true) + @RequestParam(name = "receiver", required = true) Integer receiver, - @ApiParam(value = "显示选项:值为1时,只获取未读消息;值为2时,获取全部消息", required = false, defaultValue = "1") - @RequestParam(name = "show", required = false, defaultValue = "1") Integer showOptions, + @ApiParam(value = "信息状态:值未-1时,获取全部信息;值为1时,只获取未读消息;值为2时,获取已读消息", required = false, defaultValue = "1") + @RequestParam(name = "status", required = false, defaultValue = "1") Integer status, - @ApiParam(value = "消息类型:值为-1时,获取全部信息;值为1时,获取系统消息;值为2时,获取@我消息", defaultValue = "-1") - @RequestParam(name = "type", required = false, defaultValue = "-1") Integer type, + @ApiParam(value = "消息类型:值为-1时,获取全部信息;值为1时,获取系统消息;值为2时,获取@我消息", defaultValue = "-1") + @RequestParam(name = "type", required = false, defaultValue = "-1") Integer type, - @ApiParam(value = "页码 (只在 show = 2 获取宣布信息时生效)", required = false, defaultValue = "1") - @RequestParam(name = "page", required = false, defaultValue = "1") Integer pageNum, + @ApiParam(value = "页码:值为-1时(默认值),不开启分页;", required = false, defaultValue = "-1") + @RequestParam(name = "page", required = false, defaultValue = "-1") Integer page, - @ApiParam(value = "页大小页码 (只在 show = 2 获取宣布信息时生效)", required = false, defaultValue = "20") - @RequestParam(name = "size", required = false, defaultValue = "20") Integer pageSize) { + @ApiParam(value = "页大小页码", required = false, defaultValue = "20") + @RequestParam(name = "size", required = false, defaultValue = "20") Integer size) { //参数合法性验证 ResponseData jsonFailResult = validatePlatformCodeAndReceiver(platform, receiver); @@ -75,10 +75,7 @@ public class NotificationController { jsonFailResult = validateNotificationType(type); if (jsonFailResult != null) return jsonFailResult; - jsonFailResult = validatePageParams(pageNum, pageSize); - if (jsonFailResult != null) return jsonFailResult; - - jsonFailResult = validateShowOption(showOptions); + jsonFailResult = validateStatusParams(status); if (jsonFailResult != null) return jsonFailResult; ReceiverNotificationListVo notificationListVo = new ReceiverNotificationListVo(); @@ -87,33 +84,24 @@ public class NotificationController { try { //获取全部未读消息总数 - notificationListVo.setTotalUnreadNotifiationCount(notificationService.getUnreadNotificationCount(platform, receiver)); + notificationListVo.setTotalUnreadNotifiationCount(notificationService.getNotificationCount(platform, receiver, NOTIFICATION_TYPE_ALL, STATUS_OPTION_UNREAD)); //获取未读系统消息总数 - notificationListVo.setUnreadNotificationCount(notificationService.getUnreadNotificationByType(NOTIFICATION_TYPE_SYS, platform, receiver)); + notificationListVo.setUnreadNotificationCount(notificationService.getNotificationCount(platform, receiver, NOTIFICATION_TYPE_SYS, STATUS_OPTION_UNREAD)); //获取未读@我消息总数 - notificationListVo.setUnreadAtMeCount(notificationService.getUnreadNotificationByType(NOTIFICATION_TYPE_ATME, platform, receiver)); + notificationListVo.setUnreadAtMeCount(notificationService.getNotificationCount(platform, receiver, NOTIFICATION_TYPE_ATME, STATUS_OPTION_UNREAD)); - if (showOptions == SHOW_OPTION_ALL) { - Page foundPage = notificationService.getNotification(type, pageNum, pageSize, "", platform, receiver); - if (foundPage != null) { - notificationListVo.setPageNum(foundPage.getCurrent()); - notificationListVo.setPageSize(foundPage.getSize()); - notificationListVo.setRecordsCount(foundPage.getTotal()); - notificationListVo.setTotalPageCount(foundPage.getPages()); - notificationListVo.setRecords(foundPage.getRecords()); - } - return DataPacketUtil.jsonSuccessResult(notificationListVo); + if (page == -1) { + page = 1; } - if (type == NOTIFICATION_TYPE_ALL) { - Page foundPage = notificationService.getNotification(NOTIFICATION_TYPE_ALL, 1, pageSize, "", platform, receiver); - if (foundPage != null) { - notificationListVo.setRecords(foundPage.getRecords()); - } - } else if (type == NOTIFICATION_TYPE_SYS) { - notificationListVo.setRecords(notificationService.getUnreadNotificationByType(platform, receiver, pageSize, NOTIFICATION_TYPE_SYS)); - } else { - notificationListVo.setRecords(notificationService.getUnreadNotificationByType(platform, receiver, pageSize, NOTIFICATION_TYPE_ATME)); + //分页的数据 + Page foundPage = notificationService.getNotification(platform, receiver, status, type, page, size); + if (foundPage != null) { + notificationListVo.setPageNum(foundPage.getCurrent()); + notificationListVo.setPageSize(foundPage.getSize()); + notificationListVo.setRecordsCount(foundPage.getTotal()); + notificationListVo.setTotalPageCount(foundPage.getPages()); + notificationListVo.setRecords(foundPage.getRecords()); } return DataPacketUtil.jsonSuccessResult(notificationListVo); } catch (Exception e) { @@ -148,19 +136,11 @@ public class NotificationController { jsonFailResult = validateNotificationType(type); if (jsonFailResult != null) return jsonFailResult; - int unreadNotificationCount = -1; try { - if (type == NOTIFICATION_TYPE_ALL) { - unreadNotificationCount = notificationService.getUnreadNotificationCount(platform, receiver); - } else { - unreadNotificationCount = notificationService.getUnreadNotificationByType(type, platform, receiver); - }//end of if-else - ReceiverNotificationCountVo countVo = new ReceiverNotificationCountVo(); countVo.setReceiver(receiver); countVo.setType(type); - countVo.setUnreadNotification(unreadNotificationCount); - + countVo.setUnreadNotification(notificationService.getNotificationCount(platform, receiver, type, STATUS_OPTION_UNREAD)); return DataPacketUtil.jsonSuccessResult(countVo); } catch (Exception e) { logger.error(e); @@ -211,36 +191,17 @@ public class NotificationController { }//end of method /** - * 验证页码相关参数的合法性 + * stauts 参数合法性验证 * - * @param size 页大小 - * @param page 页码 + * @param status * @return */ - private ResponseData validatePageParams(Integer page, Integer size) { - if (page <= 0 || page > Integer.MAX_VALUE) { - logger.debug("\t 输入参数 {page} 的值 {" + page + "} 超出约定范围"); - return DataPacketUtil.jsonFailResult("{page} 参数非法"); - } else if (size <= 0 || size > Integer.MAX_VALUE) { - logger.debug("\t 输入参数 {size} 的值 {" + size + "} 超出约定范围"); - return DataPacketUtil.jsonFailResult("{size} 参数非法"); - }//end of if - return null; - }//end of method - - - /** - * 验证 showOption 参数 - * - * @param showOption - * @return - */ - private ResponseData validateShowOption(Integer showOption) { - if (showOption != SHOW_OPTION_UNREAD && showOption != SHOW_OPTION_ALL) { - logger.debug("\t 输入参数 {showOption} 的值 {" + showOption + "} 超出约定范围"); - return DataPacketUtil.jsonFailResult("{show} 参数非法"); + private ResponseData validateStatusParams(Integer status) { + if (status == STATUS_OPTION_ALL || status == STATUS_OPTION_READ || status == STATUS_OPTION_UNREAD) { + return null; + } else { + logger.debug("\t输入参数 {status} 的值 {" + status + "}超出约定范围"); + return DataPacketUtil.jsonFailResult("{status} 参数非法"); } - return null; }//end of method - }//end of class