Commit 872107b319973655e11c716a2cf922348d3febb9

Authored by xrcoder
1 parent 7d673747

完善站内信阅读接口

yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java
... ... @@ -149,5 +149,6 @@ public interface ErrorCodeConstants {
149 149  
150 150 // ========== 站内信 1002024000 ==========
151 151 ErrorCode NOTIFY_MESSAGE_NOT_EXISTS = new ErrorCode(1002024000, "站内信不存在");
  152 + ErrorCode NOTIFY_MESSAGE_ID_PARAM_ERROR = new ErrorCode(1002024001, "站内信ID错误");
152 153  
153 154 }
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/notify/UserNotifyMessageController.java
... ... @@ -8,6 +8,7 @@ import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyM
8 8 import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessageRespVO;
9 9 import cn.iocoder.yudao.module.system.convert.notify.NotifyMessageConvert;
10 10 import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyMessageDO;
  11 +import cn.iocoder.yudao.module.system.enums.notify.NotifyReadStatusEnum;
11 12 import cn.iocoder.yudao.module.system.service.notify.NotifyMessageService;
12 13 import io.swagger.annotations.Api;
13 14 import io.swagger.annotations.ApiImplicitParam;
... ... @@ -77,7 +78,8 @@ public class UserNotifyMessageController {
77 78 @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
78 79 public CommonResult<NotifyMessageRespVO> readNotifyMessage(@RequestParam("id") Long id) {
79 80 NotifyMessageDO notifyMessage = notifyMessageService.getNotifyMessage(id);
80   - // TODO 记录消息已读。
  81 + // 记录消息已读。
  82 + notifyMessageService.updateNotifyMessageReadStatus(id, NotifyReadStatusEnum.READ.getStatus());
81 83 return success(NotifyMessageConvert.INSTANCE.convert(notifyMessage));
82 84 }
83 85  
... ... @@ -85,13 +87,14 @@ public class UserNotifyMessageController {
85 87 @ApiOperation("批量标记已读")
86 88 @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
87 89 public CommonResult<Boolean> batchUpdateNotifyMessageReadStatus(@RequestParam("ids") Collection<Long> ids) {
  90 + notifyMessageService.batchUpdateNotifyMessageReadStatus(ids, getLoginUserId());
88 91 return success(Boolean.TRUE);
89 92 }
90 93  
91 94 @GetMapping("/read/all")
92 95 @ApiOperation("所有未读消息标记已读")
93   - @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
94   - public CommonResult<Boolean> batchUpdateAllNotifyMessageReadStatus(@RequestParam("ids") Collection<Long> ids) {
  96 + public CommonResult<Boolean> batchUpdateAllNotifyMessageReadStatus() {
  97 + notifyMessageService.batchUpdateAllNotifyMessageReadStatus(getLoginUserId(), UserTypeEnum.ADMIN.getValue());
95 98 return success(Boolean.TRUE);
96 99 }
97 100 }
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/notify/vo/message/NotifyMessageBaseVO.java
... ... @@ -35,7 +35,7 @@ public class NotifyMessageBaseVO {
35 35 private String content;
36 36  
37 37 @ApiModelProperty(value = "是否已读 0-未读 1-已读")
38   - private Boolean readStatus;
  38 + private Integer readStatus;
39 39  
40 40 @ApiModelProperty(value = "阅读时间")
41 41 @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/notify/vo/message/NotifyMessagePageReqVO.java
... ... @@ -18,7 +18,7 @@ public class NotifyMessagePageReqVO extends PageParam {
18 18 private String title;
19 19  
20 20 @ApiModelProperty(value = "是否已读 0-未读 1-已读")
21   - private Boolean readStatus;
  21 + private Integer readStatus;
22 22  
23 23 @ApiModelProperty(value = "创建时间")
24 24 @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/notify/NotifyMessageDO.java
... ... @@ -48,9 +48,9 @@ public class NotifyMessageDO extends BaseDO {
48 48 /**
49 49 * 是否已读 0-未读 1-已读
50 50 *
51   - * 枚举 {@link TODO system_notify_message_read_status 对应的类}
  51 + * 枚举 {@link cn.iocoder.yudao.module.system.enums.notify.NotifyReadStatusEnum}
52 52 */
53   - private Boolean readStatus;
  53 + private Integer readStatus;
54 54 /**
55 55 * 阅读时间
56 56 */
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/notify/NotifyMessageMapper.java
... ... @@ -34,4 +34,11 @@ public interface NotifyMessageMapper extends BaseMapperX&lt;NotifyMessageDO&gt; {
34 34 .eq(NotifyMessageDO::getUserId, userId)
35 35 .eq(NotifyMessageDO::getUserType, userType));
36 36 }
  37 +
  38 + default List<NotifyMessageDO> selectUnreadListByUserIdAndUserType(Long userId, Integer userType) {
  39 + return selectList(new LambdaQueryWrapperX<NotifyMessageDO>()
  40 + .eq(NotifyMessageDO::getReadStatus, NotifyReadStatusEnum.UNREAD.getStatus())
  41 + .eq(NotifyMessageDO::getUserId, userId)
  42 + .eq(NotifyMessageDO::getUserType, userType));
  43 + }
37 44 }
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifyMessageService.java
... ... @@ -71,4 +71,28 @@ public interface NotifyMessageService {
71 71 * @return 返回未读站内信条数
72 72 */
73 73 Long getUnreadNotifyMessageCount(Long userId, Integer userType);
  74 +
  75 + /**
  76 + * 修改站内信阅读状态
  77 + *
  78 + * @param id 站内信编号
  79 + * @param status 状态
  80 + */
  81 + void updateNotifyMessageReadStatus(Long id, Integer status);
  82 +
  83 + /**
  84 + * 批量修改站内信阅读状态
  85 + *
  86 + * @param ids 站内信编号集合
  87 + * @param userId 用户ID
  88 + */
  89 + void batchUpdateNotifyMessageReadStatus(Collection<Long> ids, Long userId);
  90 +
  91 + /**
  92 + * 批量修改用户所有未读消息标记已读
  93 + *
  94 + * @param userId 用户ID
  95 + * @param userType 用户类型
  96 + */
  97 + void batchUpdateAllNotifyMessageReadStatus(Long userId, Integer userType);
74 98 }
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifyMessageServiceImpl.java
1 1 package cn.iocoder.yudao.module.system.service.notify;
2 2  
  3 +import cn.hutool.core.collection.CollUtil;
  4 +import cn.hutool.core.util.NumberUtil;
3 5 import cn.iocoder.yudao.framework.common.core.KeyValue;
4 6 import cn.iocoder.yudao.framework.common.pojo.PageResult;
  7 +import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
5 8 import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessageCreateReqVO;
6 9 import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessagePageReqVO;
7 10 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;
9 12 import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyMessageDO;
10 13 import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyTemplateDO;
11 14 import cn.iocoder.yudao.module.system.dal.mysql.notify.NotifyMessageMapper;
  15 +import cn.iocoder.yudao.module.system.enums.notify.NotifyReadStatusEnum;
12 16 import com.google.common.annotations.VisibleForTesting;
13 17 import org.springframework.stereotype.Service;
14 18 import org.springframework.validation.annotation.Validated;
... ... @@ -125,4 +129,71 @@ public class NotifyMessageServiceImpl implements NotifyMessageService {
125 129 public Long getUnreadNotifyMessageCount(Long userId, Integer userType) {
126 130 return notifyMessageMapper.selectUnreadCountByUserIdAndUserType(userId, userType);
127 131 }
  132 +
  133 + /**
  134 + * 修改站内信阅读状态
  135 + *
  136 + * @param id 站内信编号
  137 + * @param status 状态
  138 + */
  139 + @Override
  140 + public void updateNotifyMessageReadStatus(Long id, Integer status) {
  141 + // 校验消息是否存在
  142 + this.validateNotifyMessageExists(id);
  143 + // 更新状态
  144 + batchUpdateReadStatus(CollectionUtils.singleton(id));
  145 + }
  146 +
  147 + /**
  148 + * 批量修改站内信阅读状态
  149 + *
  150 + * @param ids 站内信编号集合
  151 + * @param userId 用户ID
  152 + */
  153 + @Override
  154 + public void batchUpdateNotifyMessageReadStatus(Collection<Long> ids, Long userId) {
  155 + List<NotifyMessageDO> list = getNotifyMessageList(ids);
  156 + if (CollUtil.isEmpty(list)) {
  157 + throw exception(NOTIFY_MESSAGE_NOT_EXISTS);
  158 + }
  159 + // 验证站内信是否是属于用户
  160 + for (NotifyMessageDO messageDO : list) {
  161 + checkNotifyMessageIdValid(messageDO, userId);
  162 + }
  163 + batchUpdateReadStatus(ids);
  164 + }
  165 +
  166 + @VisibleForTesting
  167 + public void checkNotifyMessageIdValid(NotifyMessageDO notifyMessageDO, Long userId) {
  168 + if (!NumberUtil.equals(notifyMessageDO.getUserId(), userId)) {
  169 + throw exception(NOTIFY_MESSAGE_ID_PARAM_ERROR);
  170 + }
  171 + }
  172 +
  173 + /**
  174 + * 批量修改用户所有未读消息标记已读
  175 + *
  176 + * @param userId 用户ID
  177 + * @param userType 用户类型
  178 + */
  179 + @Override
  180 + public void batchUpdateAllNotifyMessageReadStatus(Long userId, Integer userType) {
  181 + List<NotifyMessageDO> list = notifyMessageMapper.selectUnreadListByUserIdAndUserType(userId, userType);
  182 + if (CollUtil.isNotEmpty(list)) {
  183 + batchUpdateReadStatus(CollectionUtils.convertList(list, NotifyMessageDO::getId));
  184 +
  185 + }
  186 + }
  187 +
  188 + private void batchUpdateReadStatus(Collection<Long> ids) {
  189 + if (CollUtil.isNotEmpty(ids)) {
  190 + for (Long id : ids) {
  191 + NotifyMessageDO updateObj = new NotifyMessageDO();
  192 + updateObj.setId(id);
  193 + updateObj.setReadStatus(NotifyReadStatusEnum.READ.getStatus());
  194 + notifyMessageMapper.updateById(updateObj);
  195 + }
  196 + }
  197 +
  198 + }
128 199 }
... ...