完成获取消息数量的 Controller 方法 #29

Merged
baladiwei merged 1 commits from baladiwei/gitlink-notification-system:master into master 2021-09-08 17:15:02 +08:00
2 changed files with 107 additions and 18 deletions

View File

@ -0,0 +1,48 @@
package cn.org.gitlink.notification.model.dao.entity.vo;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable;
public class ReceiverNotificationCountVo implements Serializable {
private Integer receiver;
@JsonProperty("unread_notification")
private Integer unreadNotification;
private Integer type;
public ReceiverNotificationCountVo() {
}
public ReceiverNotificationCountVo(Integer receiver, Integer unreadNotification, Integer type) {
this.receiver = receiver;
this.unreadNotification = unreadNotification;
this.type = type;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public Integer getReceiver() {
return receiver;
}
public void setReceiver(Integer receiver) {
this.receiver = receiver;
}
public Integer getUnreadNotification() {
return unreadNotification;
}
public void setUnreadNotification(Integer unreadNotification) {
this.unreadNotification = unreadNotification;
}
}

View File

@ -3,7 +3,7 @@ package cn.org.gitlink.notification.reader.controller;
import cn.org.gitlink.notification.common.response.DataPacketUtil;
import cn.org.gitlink.notification.common.response.ResponseData;
import cn.org.gitlink.notification.common.utils.RedisUtil;
import cn.org.gitlink.notification.model.dao.entity.SysNotification;
import cn.org.gitlink.notification.model.dao.entity.vo.ReceiverNotificationCountVo;
import cn.org.gitlink.notification.model.service.notification.SysNotificationService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -11,14 +11,18 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping(value = "/gns/notification")
public class NotificationController {
//平台编号
private static final String PLATFORM_CODE_GITLINK = "gitlink";
//未读消息类型
private static final Integer NOTIFICATION_TYPE_ALL = -1; //全部未读消息
private static final Integer NOTIFICATION_TYPE_SYS = 1; //系统消息
private static final Integer NOTIFICATION_TYPE_ATME = 2; //@我的消息
@Autowired
private RedisUtil redisUtil;
@ -28,6 +32,15 @@ public class NotificationController {
private Logger logger = LogManager.getLogger(NotificationController.class);
/**
* 获取系统消息列表
*
* @param platform
* @param receiver
* @param pageNum
* @param pageSize
* @return
*/
@GetMapping(value = "/{platform}/list")
public ResponseData list(@PathVariable(name = "platform", required = true) String platform,
@RequestParam(name = "receiver", required = true) Integer receiver,
@ -35,39 +48,52 @@ public class NotificationController {
@RequestParam(name = "size", required = false, defaultValue = "20") Integer pageSize) {
//参数合法性验证
ResponseData jsonFailResult = validateParams(platform, receiver);
ResponseData jsonFailResult = validatePlatformCodeAndReceiver(platform, receiver);
if (jsonFailResult != null) return jsonFailResult;
//TODO: 获取约定的必要参数
//调用业务逻辑获取数据结果
List<SysNotification> foundList = notificationService.getUnreadNotificationByType(platform, receiver, pageSize);
//拼装约定的 json 格式并返回
return DataPacketUtil.jsonSuccessResult(foundList);
return null;
}//end of method
/**
* 获取消息数量
*
* @param platform 平台编号
* @param receiver 消息接收者
* @param type -1 全部消息 | 1 系统消息 | 2 @我
* @return
*/
@GetMapping(value = "/{platform}/count")
public ResponseData count(@PathVariable(name = "platform", required = true) String platform,
@RequestParam(name = "receiver", required = true) Integer receiver,
@RequestParam(name = "page", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(name = "size", required = false, defaultValue = "20") Integer pageSize) {
@RequestParam(name = "type", required = false, defaultValue = "-1") Integer type) {
//参数合法性验证
ResponseData jsonFailResult = validateParams(platform, receiver);
ResponseData jsonFailResult = validatePlatformCodeAndReceiver(platform, receiver);
if (jsonFailResult != null) return jsonFailResult;
//TODO: 获取必要的参数
jsonFailResult = validateNotificationType(type);
if (jsonFailResult != null) return jsonFailResult;
//调用业务逻辑获取数据结果
Integer foundCount = notificationService.getUnreadNotificationCount(platform, receiver);
int unreadNotificationCount = -1;
if (type == NOTIFICATION_TYPE_ALL) {
unreadNotificationCount = notificationService.getUnreadNotificationCount(platform, receiver);
} else {
unreadNotificationCount = notificationService.getUnreadNotificationByType(type, platform, receiver);
}//end of if-else
//拼装约定的 json 格式并返回
return DataPacketUtil.jsonSuccessResult(foundCount);
}
ReceiverNotificationCountVo countVo = new ReceiverNotificationCountVo();
countVo.setReceiver(receiver);
countVo.setType(type);
countVo.setUnreadNotification(unreadNotificationCount);
return DataPacketUtil.jsonSuccessResult(countVo);
}//end of method
//////////////////////////////// 私有方法 ////////////////////////////////
@ -79,7 +105,7 @@ public class NotificationController {
* @param receiver
* @return
*/
private ResponseData validateParams(String platform, Integer receiver) {
private ResponseData validatePlatformCodeAndReceiver(String platform, Integer receiver) {
//验证 {platform} 参数合法性以判断请求来源
if (!platform.trim().toLowerCase().equals(PLATFORM_CODE_GITLINK)) {
@ -96,4 +122,19 @@ public class NotificationController {
return null;
}//end of method
/**
* 验证消息类型合法性
*
* @param type
* @return
*/
private ResponseData validateNotificationType(Integer type) {
if (type == NOTIFICATION_TYPE_ALL || type == NOTIFICATION_TYPE_SYS || type == NOTIFICATION_TYPE_ATME) {
return null;
} else {
logger.debug("\t 输入参数 {type} 的值 {" + type + "} 超出约定范围");
return DataPacketUtil.jsonFailResult("{type} 参数非法");
}//end of if
}//end of method
}//end of class