完善站内信阅读接口

This commit is contained in:
xrcoder 2022-08-06 11:30:12 +08:00
parent 7d6737479c
commit 872107b319
8 changed files with 113 additions and 7 deletions

View File

@ -149,5 +149,6 @@ public interface ErrorCodeConstants {
// ========== 站内信 1002024000 ========== // ========== 站内信 1002024000 ==========
ErrorCode NOTIFY_MESSAGE_NOT_EXISTS = new ErrorCode(1002024000, "站内信不存在"); ErrorCode NOTIFY_MESSAGE_NOT_EXISTS = new ErrorCode(1002024000, "站内信不存在");
ErrorCode NOTIFY_MESSAGE_ID_PARAM_ERROR = new ErrorCode(1002024001, "站内信ID错误");
} }

View File

@ -8,6 +8,7 @@ import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyM
import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessageRespVO; import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessageRespVO;
import cn.iocoder.yudao.module.system.convert.notify.NotifyMessageConvert; import cn.iocoder.yudao.module.system.convert.notify.NotifyMessageConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyMessageDO; import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyMessageDO;
import cn.iocoder.yudao.module.system.enums.notify.NotifyReadStatusEnum;
import cn.iocoder.yudao.module.system.service.notify.NotifyMessageService; import cn.iocoder.yudao.module.system.service.notify.NotifyMessageService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
@ -77,7 +78,8 @@ public class UserNotifyMessageController {
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class) @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
public CommonResult<NotifyMessageRespVO> readNotifyMessage(@RequestParam("id") Long id) { public CommonResult<NotifyMessageRespVO> readNotifyMessage(@RequestParam("id") Long id) {
NotifyMessageDO notifyMessage = notifyMessageService.getNotifyMessage(id); NotifyMessageDO notifyMessage = notifyMessageService.getNotifyMessage(id);
// TODO 记录消息已读 // 记录消息已读
notifyMessageService.updateNotifyMessageReadStatus(id, NotifyReadStatusEnum.READ.getStatus());
return success(NotifyMessageConvert.INSTANCE.convert(notifyMessage)); return success(NotifyMessageConvert.INSTANCE.convert(notifyMessage));
} }
@ -85,13 +87,14 @@ public class UserNotifyMessageController {
@ApiOperation("批量标记已读") @ApiOperation("批量标记已读")
@ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class) @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
public CommonResult<Boolean> batchUpdateNotifyMessageReadStatus(@RequestParam("ids") Collection<Long> ids) { public CommonResult<Boolean> batchUpdateNotifyMessageReadStatus(@RequestParam("ids") Collection<Long> ids) {
notifyMessageService.batchUpdateNotifyMessageReadStatus(ids, getLoginUserId());
return success(Boolean.TRUE); return success(Boolean.TRUE);
} }
@GetMapping("/read/all") @GetMapping("/read/all")
@ApiOperation("所有未读消息标记已读") @ApiOperation("所有未读消息标记已读")
@ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class) public CommonResult<Boolean> batchUpdateAllNotifyMessageReadStatus() {
public CommonResult<Boolean> batchUpdateAllNotifyMessageReadStatus(@RequestParam("ids") Collection<Long> ids) { notifyMessageService.batchUpdateAllNotifyMessageReadStatus(getLoginUserId(), UserTypeEnum.ADMIN.getValue());
return success(Boolean.TRUE); return success(Boolean.TRUE);
} }
} }

View File

@ -35,7 +35,7 @@ public class NotifyMessageBaseVO {
private String content; private String content;
@ApiModelProperty(value = "是否已读 0-未读 1-已读") @ApiModelProperty(value = "是否已读 0-未读 1-已读")
private Boolean readStatus; private Integer readStatus;
@ApiModelProperty(value = "阅读时间") @ApiModelProperty(value = "阅读时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)

View File

@ -18,7 +18,7 @@ public class NotifyMessagePageReqVO extends PageParam {
private String title; private String title;
@ApiModelProperty(value = "是否已读 0-未读 1-已读") @ApiModelProperty(value = "是否已读 0-未读 1-已读")
private Boolean readStatus; private Integer readStatus;
@ApiModelProperty(value = "创建时间") @ApiModelProperty(value = "创建时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)

View File

@ -48,9 +48,9 @@ public class NotifyMessageDO extends BaseDO {
/** /**
* 是否已读 0-未读 1-已读 * 是否已读 0-未读 1-已读
* *
* 枚举 {@link TODO system_notify_message_read_status 对应的类} * 枚举 {@link cn.iocoder.yudao.module.system.enums.notify.NotifyReadStatusEnum}
*/ */
private Boolean readStatus; private Integer readStatus;
/** /**
* 阅读时间 * 阅读时间
*/ */

View File

@ -34,4 +34,11 @@ public interface NotifyMessageMapper extends BaseMapperX<NotifyMessageDO> {
.eq(NotifyMessageDO::getUserId, userId) .eq(NotifyMessageDO::getUserId, userId)
.eq(NotifyMessageDO::getUserType, userType)); .eq(NotifyMessageDO::getUserType, userType));
} }
default List<NotifyMessageDO> selectUnreadListByUserIdAndUserType(Long userId, Integer userType) {
return selectList(new LambdaQueryWrapperX<NotifyMessageDO>()
.eq(NotifyMessageDO::getReadStatus, NotifyReadStatusEnum.UNREAD.getStatus())
.eq(NotifyMessageDO::getUserId, userId)
.eq(NotifyMessageDO::getUserType, userType));
}
} }

View File

@ -71,4 +71,28 @@ public interface NotifyMessageService {
* @return 返回未读站内信条数 * @return 返回未读站内信条数
*/ */
Long getUnreadNotifyMessageCount(Long userId, Integer userType); Long getUnreadNotifyMessageCount(Long userId, Integer userType);
/**
* 修改站内信阅读状态
*
* @param id 站内信编号
* @param status 状态
*/
void updateNotifyMessageReadStatus(Long id, Integer status);
/**
* 批量修改站内信阅读状态
*
* @param ids 站内信编号集合
* @param userId 用户ID
*/
void batchUpdateNotifyMessageReadStatus(Collection<Long> ids, Long userId);
/**
* 批量修改用户所有未读消息标记已读
*
* @param userId 用户ID
* @param userType 用户类型
*/
void batchUpdateAllNotifyMessageReadStatus(Long userId, Integer userType);
} }

View File

@ -1,7 +1,10 @@
package cn.iocoder.yudao.module.system.service.notify; package cn.iocoder.yudao.module.system.service.notify;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.iocoder.yudao.framework.common.core.KeyValue; import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessageCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessageCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessagePageReqVO; import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessagePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessageUpdateReqVO; import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessageUpdateReqVO;
@ -9,6 +12,7 @@ import cn.iocoder.yudao.module.system.convert.notify.NotifyMessageConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyMessageDO; import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyMessageDO;
import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyTemplateDO; import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyTemplateDO;
import cn.iocoder.yudao.module.system.dal.mysql.notify.NotifyMessageMapper; import cn.iocoder.yudao.module.system.dal.mysql.notify.NotifyMessageMapper;
import cn.iocoder.yudao.module.system.enums.notify.NotifyReadStatusEnum;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -125,4 +129,71 @@ public class NotifyMessageServiceImpl implements NotifyMessageService {
public Long getUnreadNotifyMessageCount(Long userId, Integer userType) { public Long getUnreadNotifyMessageCount(Long userId, Integer userType) {
return notifyMessageMapper.selectUnreadCountByUserIdAndUserType(userId, userType); return notifyMessageMapper.selectUnreadCountByUserIdAndUserType(userId, userType);
} }
/**
* 修改站内信阅读状态
*
* @param id 站内信编号
* @param status 状态
*/
@Override
public void updateNotifyMessageReadStatus(Long id, Integer status) {
// 校验消息是否存在
this.validateNotifyMessageExists(id);
// 更新状态
batchUpdateReadStatus(CollectionUtils.singleton(id));
}
/**
* 批量修改站内信阅读状态
*
* @param ids 站内信编号集合
* @param userId 用户ID
*/
@Override
public void batchUpdateNotifyMessageReadStatus(Collection<Long> ids, Long userId) {
List<NotifyMessageDO> list = getNotifyMessageList(ids);
if (CollUtil.isEmpty(list)) {
throw exception(NOTIFY_MESSAGE_NOT_EXISTS);
}
// 验证站内信是否是属于用户
for (NotifyMessageDO messageDO : list) {
checkNotifyMessageIdValid(messageDO, userId);
}
batchUpdateReadStatus(ids);
}
@VisibleForTesting
public void checkNotifyMessageIdValid(NotifyMessageDO notifyMessageDO, Long userId) {
if (!NumberUtil.equals(notifyMessageDO.getUserId(), userId)) {
throw exception(NOTIFY_MESSAGE_ID_PARAM_ERROR);
}
}
/**
* 批量修改用户所有未读消息标记已读
*
* @param userId 用户ID
* @param userType 用户类型
*/
@Override
public void batchUpdateAllNotifyMessageReadStatus(Long userId, Integer userType) {
List<NotifyMessageDO> list = notifyMessageMapper.selectUnreadListByUserIdAndUserType(userId, userType);
if (CollUtil.isNotEmpty(list)) {
batchUpdateReadStatus(CollectionUtils.convertList(list, NotifyMessageDO::getId));
}
}
private void batchUpdateReadStatus(Collection<Long> ids) {
if (CollUtil.isNotEmpty(ids)) {
for (Long id : ids) {
NotifyMessageDO updateObj = new NotifyMessageDO();
updateObj.setId(id);
updateObj.setReadStatus(NotifyReadStatusEnum.READ.getStatus());
notifyMessageMapper.updateById(updateObj);
}
}
}
} }