Răsfoiți Sursa

完善站内信阅读接口

xrcoder 3 ani în urmă
părinte
comite
872107b319

+ 1 - 0
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错误");
 
 }

+ 6 - 3
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<NotifyMessageRespVO> 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<Boolean> batchUpdateNotifyMessageReadStatus(@RequestParam("ids") Collection<Long> 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<Boolean> batchUpdateAllNotifyMessageReadStatus(@RequestParam("ids") Collection<Long> ids) {
+    public CommonResult<Boolean> batchUpdateAllNotifyMessageReadStatus() {
+        notifyMessageService.batchUpdateAllNotifyMessageReadStatus(getLoginUserId(), UserTypeEnum.ADMIN.getValue());
         return success(Boolean.TRUE);
     }
 }

+ 1 - 1
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)

+ 1 - 1
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)

+ 2 - 2
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;
     /**
      * 阅读时间
      */

+ 7 - 0
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<NotifyMessageDO> {
                 .eq(NotifyMessageDO::getUserId, userId)
                 .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));
+    }
 }

+ 24 - 0
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<Long> ids, Long userId);
+
+    /**
+     * 批量修改用户所有未读消息标记已读
+     *
+     * @param userId 用户ID
+     * @param userType 用户类型
+     */
+    void batchUpdateAllNotifyMessageReadStatus(Long userId, Integer userType);
 }

+ 71 - 0
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<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);
+            }
+        }
+
+    }
 }