diff --git a/src/main/java/com/rymcu/forest/mapper/NotificationMapper.java b/src/main/java/com/rymcu/forest/mapper/NotificationMapper.java index 2ae27ad..fee64a9 100644 --- a/src/main/java/com/rymcu/forest/mapper/NotificationMapper.java +++ b/src/main/java/com/rymcu/forest/mapper/NotificationMapper.java @@ -51,4 +51,11 @@ public interface NotificationMapper extends Mapper { * @return */ Integer readNotification(@Param("id") Integer id); + + /** + * 标记所有消息已读 + * @param idUser + * @return + */ + Integer readAllNotification(@Param("idUser") Integer idUser); } diff --git a/src/main/java/com/rymcu/forest/service/NotificationService.java b/src/main/java/com/rymcu/forest/service/NotificationService.java index 4cf634b..da7109b 100644 --- a/src/main/java/com/rymcu/forest/service/NotificationService.java +++ b/src/main/java/com/rymcu/forest/service/NotificationService.java @@ -3,6 +3,7 @@ package com.rymcu.forest.service; import com.rymcu.forest.core.service.Service; import com.rymcu.forest.dto.NotificationDTO; import com.rymcu.forest.entity.Notification; +import com.rymcu.forest.web.api.exception.BaseApiException; import java.util.List; @@ -47,6 +48,14 @@ public interface NotificationService extends Service { /** * 标记消息已读 * @param id + * @return */ Integer readNotification(Integer id); + + /** + * 标记所有消息已读 + * @return + * @throws BaseApiException + */ + Integer readAllNotification() throws BaseApiException; } diff --git a/src/main/java/com/rymcu/forest/service/impl/NotificationServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/NotificationServiceImpl.java index b571e6f..c409d8e 100644 --- a/src/main/java/com/rymcu/forest/service/impl/NotificationServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/NotificationServiceImpl.java @@ -7,6 +7,8 @@ import com.rymcu.forest.mapper.NotificationMapper; import com.rymcu.forest.service.NotificationService; import com.rymcu.forest.util.BeanCopierUtil; import com.rymcu.forest.util.NotificationUtils; +import com.rymcu.forest.util.UserUtils; +import com.rymcu.forest.web.api.exception.BaseApiException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -70,4 +72,9 @@ public class NotificationServiceImpl extends AbstractService imple public Integer readNotification(Integer id) { return notificationMapper.readNotification(id); } + + @Override + public Integer readAllNotification() throws BaseApiException { + return notificationMapper.readAllNotification(Objects.requireNonNull(UserUtils.getCurrentUserByToken()).getIdUser()); + } } diff --git a/src/main/java/com/rymcu/forest/web/api/notification/NotificationController.java b/src/main/java/com/rymcu/forest/web/api/notification/NotificationController.java index 42c5072..ac8e403 100644 --- a/src/main/java/com/rymcu/forest/web/api/notification/NotificationController.java +++ b/src/main/java/com/rymcu/forest/web/api/notification/NotificationController.java @@ -65,4 +65,13 @@ public class NotificationController { return GlobalResultGenerator.genSuccessResult("标记已读成功"); } + @PutMapping("/read-all") + public GlobalResult readAll() throws BaseApiException { + Integer result = notificationService.readAllNotification(); + if (result == 0) { + return GlobalResultGenerator.genErrorResult("标记已读失败"); + } + return GlobalResultGenerator.genSuccessResult("标记已读成功"); + } + } diff --git a/src/main/java/mapper/NotificationMapper.xml b/src/main/java/mapper/NotificationMapper.xml index 144d4ca..3e68965 100644 --- a/src/main/java/mapper/NotificationMapper.xml +++ b/src/main/java/mapper/NotificationMapper.xml @@ -25,6 +25,9 @@ update forest_notification set has_read = '1' where id = #{id} + + update forest_notification set has_read = '1' where id_user = #{idUser} and has_read = '0' +