From 872107b319973655e11c716a2cf922348d3febb9 Mon Sep 17 00:00:00 2001 From: xrcoder <53924337@qq.com> Date: Sat, 6 Aug 2022 11:30:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E7=AB=99=E5=86=85=E4=BF=A1?= =?UTF-8?q?=E9=98=85=E8=AF=BB=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/enums/ErrorCodeConstants.java | 1 + .../notify/UserNotifyMessageController.java | 9 ++- .../vo/message/NotifyMessageBaseVO.java | 2 +- .../vo/message/NotifyMessagePageReqVO.java | 2 +- .../dataobject/notify/NotifyMessageDO.java | 4 +- .../dal/mysql/notify/NotifyMessageMapper.java | 7 ++ .../service/notify/NotifyMessageService.java | 24 +++++++ .../notify/NotifyMessageServiceImpl.java | 71 +++++++++++++++++++ 8 files changed, 113 insertions(+), 7 deletions(-) diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java index a126aee6e..91dae4dfe 100644 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java +++ b/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 { // ========== 站内信 1002024000 ========== ErrorCode NOTIFY_MESSAGE_NOT_EXISTS = new ErrorCode(1002024000, "站内信不存在"); + ErrorCode NOTIFY_MESSAGE_ID_PARAM_ERROR = new ErrorCode(1002024001, "站内信ID错误"); } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/notify/UserNotifyMessageController.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/notify/UserNotifyMessageController.java index eaae42e81..4104acdc1 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/notify/UserNotifyMessageController.java +++ b/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 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.dal.dataobject.notify.NotifyMessageDO; +import cn.iocoder.yudao.module.system.enums.notify.NotifyReadStatusEnum; import cn.iocoder.yudao.module.system.service.notify.NotifyMessageService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; @@ -77,7 +78,8 @@ public class UserNotifyMessageController { @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class) public CommonResult readNotifyMessage(@RequestParam("id") Long id) { NotifyMessageDO notifyMessage = notifyMessageService.getNotifyMessage(id); - // TODO 记录消息已读。 + // 记录消息已读。 + notifyMessageService.updateNotifyMessageReadStatus(id, NotifyReadStatusEnum.READ.getStatus()); return success(NotifyMessageConvert.INSTANCE.convert(notifyMessage)); } @@ -85,13 +87,14 @@ public class UserNotifyMessageController { @ApiOperation("批量标记已读") @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class) public CommonResult batchUpdateNotifyMessageReadStatus(@RequestParam("ids") Collection ids) { + notifyMessageService.batchUpdateNotifyMessageReadStatus(ids, getLoginUserId()); return success(Boolean.TRUE); } @GetMapping("/read/all") @ApiOperation("所有未读消息标记已读") - @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class) - public CommonResult batchUpdateAllNotifyMessageReadStatus(@RequestParam("ids") Collection ids) { + public CommonResult batchUpdateAllNotifyMessageReadStatus() { + notifyMessageService.batchUpdateAllNotifyMessageReadStatus(getLoginUserId(), UserTypeEnum.ADMIN.getValue()); return success(Boolean.TRUE); } } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/notify/vo/message/NotifyMessageBaseVO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/notify/vo/message/NotifyMessageBaseVO.java index ca77eaccb..e7f7439c2 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/notify/vo/message/NotifyMessageBaseVO.java +++ b/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 { private String content; @ApiModelProperty(value = "是否已读 0-未读 1-已读") - private Boolean readStatus; + private Integer readStatus; @ApiModelProperty(value = "阅读时间") @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/notify/vo/message/NotifyMessagePageReqVO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/notify/vo/message/NotifyMessagePageReqVO.java index 6db082d2c..7f24c61c0 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/notify/vo/message/NotifyMessagePageReqVO.java +++ b/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 { private String title; @ApiModelProperty(value = "是否已读 0-未读 1-已读") - private Boolean readStatus; + private Integer readStatus; @ApiModelProperty(value = "创建时间") @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/notify/NotifyMessageDO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/notify/NotifyMessageDO.java index c826c9d10..714ec4498 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/notify/NotifyMessageDO.java +++ b/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 { /** * 是否已读 0-未读 1-已读 * - * 枚举 {@link TODO system_notify_message_read_status 对应的类} + * 枚举 {@link cn.iocoder.yudao.module.system.enums.notify.NotifyReadStatusEnum} */ - private Boolean readStatus; + private Integer readStatus; /** * 阅读时间 */ diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/notify/NotifyMessageMapper.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/notify/NotifyMessageMapper.java index 899208b8b..2c9137f11 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/notify/NotifyMessageMapper.java +++ b/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 { .eq(NotifyMessageDO::getUserId, userId) .eq(NotifyMessageDO::getUserType, userType)); } + + default List selectUnreadListByUserIdAndUserType(Long userId, Integer userType) { + return selectList(new LambdaQueryWrapperX() + .eq(NotifyMessageDO::getReadStatus, NotifyReadStatusEnum.UNREAD.getStatus()) + .eq(NotifyMessageDO::getUserId, userId) + .eq(NotifyMessageDO::getUserType, userType)); + } } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifyMessageService.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifyMessageService.java index 847ab9a4b..f74a394b7 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifyMessageService.java +++ b/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 { * @return 返回未读站内信条数 */ Long getUnreadNotifyMessageCount(Long userId, Integer userType); + + /** + * 修改站内信阅读状态 + * + * @param id 站内信编号 + * @param status 状态 + */ + void updateNotifyMessageReadStatus(Long id, Integer status); + + /** + * 批量修改站内信阅读状态 + * + * @param ids 站内信编号集合 + * @param userId 用户ID + */ + void batchUpdateNotifyMessageReadStatus(Collection ids, Long userId); + + /** + * 批量修改用户所有未读消息标记已读 + * + * @param userId 用户ID + * @param userType 用户类型 + */ + void batchUpdateAllNotifyMessageReadStatus(Long userId, Integer userType); } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifyMessageServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifyMessageServiceImpl.java index c37ada4fd..4c385ee48 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifyMessageServiceImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifyMessageServiceImpl.java @@ -1,7 +1,10 @@ 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.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.NotifyMessagePageReqVO; 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.NotifyTemplateDO; 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 org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; @@ -125,4 +129,71 @@ public class NotifyMessageServiceImpl implements NotifyMessageService { public Long getUnreadNotifyMessageCount(Long userId, Integer 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 ids, Long userId) { + List 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 list = notifyMessageMapper.selectUnreadListByUserIdAndUserType(userId, userType); + if (CollUtil.isNotEmpty(list)) { + batchUpdateReadStatus(CollectionUtils.convertList(list, NotifyMessageDO::getId)); + + } + } + + private void batchUpdateReadStatus(Collection ids) { + if (CollUtil.isNotEmpty(ids)) { + for (Long id : ids) { + NotifyMessageDO updateObj = new NotifyMessageDO(); + updateObj.setId(id); + updateObj.setReadStatus(NotifyReadStatusEnum.READ.getStatus()); + notifyMessageMapper.updateById(updateObj); + } + } + + } }