Merge pull request '处理web接口需要的新需求、调整缓存策略' (#65) from baladiwei/gitlink-notification-system:master into master

This commit is contained in:
baladiwei 2021-09-14 10:47:41 +08:00
commit ca52fad393
5 changed files with 103 additions and 158 deletions

View File

@ -10,15 +10,15 @@ import java.util.List;
public interface SysNotificationMapper extends BaseMapper<SysNotification> {
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<SysNotification> {
@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<SysNotification> getUnreadSysNotificationsList(@Param("platform") String platform,
@Param("receiver") Integer receiver,
@ -37,7 +39,8 @@ public interface SysNotificationMapper extends BaseMapper<SysNotification> {
List<SysNotification> 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,

View File

@ -23,56 +23,37 @@ public interface SysNotificationService extends IService<SysNotification> {
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<SysNotification> 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<SysNotification> getNotification(int type, int page, int size, String orderBy, String platform, Integer receiver) throws Exception;
Page<SysNotification> getNotification(String platform, Integer receiver, Integer status, Integer type, Integer page, Integer size) throws Exception;
/**
* @Description: 批量删除系统消息
*
* @Param platform 平台编码
* @Param notificationIds 消息编号列表中英文逗号隔开eg221,2323,4,111,23123
* @return: int
* @Author: wanjia
* @Date: 2021/9/13
*/
* @Description: 批量删除系统消息
* @Param platform 平台编码
* @Param notificationIds 消息编号列表中英文逗号隔开eg221,2323,4,111,23123
* @return: int
* @Author: wanjia
* @Date: 2021/9/13
*/
int deleteNotifications(String platform, Integer receiver, String notificationIds) throws Exception;
}

View File

@ -18,7 +18,7 @@ import java.util.List;
@Service(value = "SysNotificationServiceImpl")
public class SysNotificationServiceImpl extends ServiceImpl<SysNotificationMapper, SysNotification> 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<SysNotificationMappe
List<String> 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,55 +55,32 @@ public class SysNotificationServiceImpl extends ServiceImpl<SysNotificationMappe
}
@Override
public int getUnreadNotificationCount(String platform, Integer receiver) throws Exception {
String KEY_ALL_COUNT = CACHE_KEY_PREFFIX + platform.toUpperCase() + "#" + receiver + "#COUNT";
Object foundResult = this.redisUtil.get(KEY_ALL_COUNT);
public int getNotificationCount(String platform, Integer receiver, Integer type, Integer status) throws Exception {
String cacheKey = cacheKeyForCount(platform, receiver, type, status);
Object foundResult = this.redisUtil.get(cacheKey);
if (foundResult != null) {
return (Integer) foundResult;
}
int count = baseMapper.getNewSysNotificationNumber(platform, receiver);
this.redisUtil.set(KEY_ALL_COUNT, count);
int count = baseMapper.getNewSysNotificationNumber(platform, receiver, type, status);
this.redisUtil.set(cacheKey, count);
return count;
}
@Override
public List<SysNotification> 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<SysNotification>) foundResult;
}
List<SysNotification> 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<SysNotification> 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<SysNotification> 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<SysNotification>) foundResult;
}
Page<SysNotification> pageItem = new Page<SysNotification>(page, size);
List<SysNotification> 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;
}
@ -117,6 +95,18 @@ public class SysNotificationServiceImpl extends ServiceImpl<SysNotificationMappe
//////////////////////////////// 私有方法 ////////////////////////////////
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);
}
}

View File

@ -170,7 +170,14 @@
<select id="getNewSysNotificationNumber" resultType="java.lang.Integer">
select count(`receiver`)
from ${platform}_sys_notification
where receiver = #{receiver} and status = 1 AND is_delete = -1
where receiver = #{receiver}
<if test="type != -1">
and type = #{type}
</if>
<if test="status != -1">
and status = #{status}
</if>
AND is_delete = -1
</select>
<select id="getSysNotificationNumberByType" resultType="java.lang.Integer">
select count(*)
@ -189,12 +196,15 @@
<if test="type != -1">
and type = #{type}
</if>
<if test="status != -1">
and status = #{status}
</if>
ORDER BY id DESC
</select>
<update id="deleteNotificationByIds">
update ${platform}_sys_notification set is_delete = 1 where receiver=#{receiver}
<if test="notificationIds != '-1'">
and id in (${notificationIds})
and id in (${notificationIds})
</if>
</update>
</mapper>

View File

@ -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