From 7801182926864ea147f56511b76807a94ada6fe9 Mon Sep 17 00:00:00 2001 From: wangqing <408564126@qq.com> Date: Tue, 23 Jan 2024 15:12:55 +0800 Subject: [PATCH] =?UTF-8?q?userMessage=E6=B7=BB=E5=8A=A0=E5=88=86=E9=A1=B5?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=EF=BC=8C=E6=9B=B4=E6=96=B0=E5=B7=B2=E8=AF=BB?= =?UTF-8?q?=E7=8A=B6=E6=80=81=EF=BC=8C=E6=89=B9=E9=87=8F=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E5=85=A8=E9=83=A8=E5=B7=B2=E8=AF=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/dto/UserMessageDto.java | 2 +- .../controller/UserMessageController.java | 24 +++++++-- .../com/luoo/user/dao/UserMessageDao.java | 8 +++ .../java/com/luoo/user/pojo/UserMessage.java | 2 +- .../luoo/user/service/UserMessageService.java | 53 ++++++++++++++++++- 5 files changed, 82 insertions(+), 7 deletions(-) diff --git a/luoo_common/src/main/java/dto/UserMessageDto.java b/luoo_common/src/main/java/dto/UserMessageDto.java index 43d383d..d0c9b9f 100644 --- a/luoo_common/src/main/java/dto/UserMessageDto.java +++ b/luoo_common/src/main/java/dto/UserMessageDto.java @@ -39,7 +39,7 @@ public class UserMessageDto implements Serializable { /** * 是否已读 0为未读 1为已读 */ - private Integer havaRead; + private Integer haveRead; /** diff --git a/luoo_user/src/main/java/com/luoo/user/controller/UserMessageController.java b/luoo_user/src/main/java/com/luoo/user/controller/UserMessageController.java index 73d067f..6cf7a4c 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/UserMessageController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/UserMessageController.java @@ -1,11 +1,13 @@ package com.luoo.user.controller; +import api.PageResult; import api.Result; import com.luoo.user.pojo.UserMessage; import com.luoo.user.service.UserMessageService; import dto.UserMessageDto; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -36,10 +38,24 @@ public class UserMessageController { * 根据登录的用户ID获取消息 * @return */ - @GetMapping("/list/{userId}") - public Result list(@PathVariable String userId){ - List list = userMessageService.findByUserId(userId); - return Result.success(list); + @GetMapping("/list/{userId}/{page}/{size}") + public Result list(@PathVariable String userId,@PathVariable int page,@PathVariable int size){ +// List list = userMessageService.findByUserId(userId); + + Page pageList = userMessageService.findSearch(userId,page,size); + return Result.success(new PageResult(pageList.getTotalElements(),pageList.getContent())); + } + + @PutMapping("/haveRead/{messageId}") + public Result haveRead(@PathVariable String messageId) { + userMessageService.haveRead(messageId); + return Result.success(); + } + + @PutMapping("/batchHaveRead") + public Result haveRead(@RequestBody List userMessageList) { + userMessageService.batchHaveRead(userMessageList); + return Result.success(); } } diff --git a/luoo_user/src/main/java/com/luoo/user/dao/UserMessageDao.java b/luoo_user/src/main/java/com/luoo/user/dao/UserMessageDao.java index 144ef15..bdc5112 100644 --- a/luoo_user/src/main/java/com/luoo/user/dao/UserMessageDao.java +++ b/luoo_user/src/main/java/com/luoo/user/dao/UserMessageDao.java @@ -1,6 +1,10 @@ package com.luoo.user.dao; import com.luoo.user.pojo.UserMessage; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.mongodb.repository.MongoRepository; import java.util.List; @@ -8,4 +12,8 @@ import java.util.List; public interface UserMessageDao extends MongoRepository { List findAllByUserId(String userId); + + Page findByUserId(String userId, Pageable pageable); + + } diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/UserMessage.java b/luoo_user/src/main/java/com/luoo/user/pojo/UserMessage.java index b209cbb..12102e8 100644 --- a/luoo_user/src/main/java/com/luoo/user/pojo/UserMessage.java +++ b/luoo_user/src/main/java/com/luoo/user/pojo/UserMessage.java @@ -41,7 +41,7 @@ public class UserMessage implements Serializable { /** * 是否已读 0为未读 1为已读 */ - private Integer havaRead; + private Integer haveRead; /** diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserMessageService.java b/luoo_user/src/main/java/com/luoo/user/service/UserMessageService.java index eb12109..533c1d9 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserMessageService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserMessageService.java @@ -2,9 +2,17 @@ package com.luoo.user.service; import com.luoo.user.dao.UserMessageDao; import com.luoo.user.pojo.UserMessage; +import com.mongodb.bulk.BulkWriteResult; import dto.UserMessageDto; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.mongodb.core.BulkOperations; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.data.mongodb.core.query.Update; import org.springframework.stereotype.Service; import util.IdWorker; @@ -21,10 +29,14 @@ public class UserMessageService { private UserMessageDao userMessageDao; @Autowired private IdWorker idWorker; + + @Autowired + private MongoTemplate mongoTemplate; + public void sendUserMessage(UserMessageDto userMessageDto) { userMessageDto.setMessageId(idWorker.nextId()+""); - userMessageDto.setHavaRead(0); + userMessageDto.setHaveRead(0); userMessageDto.setSendTime(new Date()); rabbitTemplate.convertAndSend("userMessage",userMessageDto); } @@ -39,4 +51,43 @@ public class UserMessageService { return userMessageDao.findAllByUserId(userId); } + + public Page findSearch(String userId,int page,int size) { + + + PageRequest pageRequest = PageRequest.of(page-1,size); + return userMessageDao.findByUserId(userId,pageRequest); + } + + + public void haveRead(String messageId) { + + Query query = new Query(); + query.addCriteria(Criteria.where("_id").is(messageId)); + Update update = new Update(); + update.set("haveRead",1); //已读为1 + mongoTemplate.updateFirst(query,update,UserMessage.class); + + } + + + + public void batchHaveRead(List userMessageList) { + + BulkOperations bulkOps = mongoTemplate.bulkOps(BulkOperations.BulkMode.ORDERED, UserMessage.class); + + for (UserMessage userMessage: userMessageList) { + Query query = new Query(); + query.addCriteria(Criteria.where("_id").is(userMessage.getMessageId())); + Update update = new Update(); + update.set("haveRead",1); //已读为1 + // 添加更新操作 + bulkOps.updateOne(query,update); + + } + + //执行批量更新操作 + + BulkWriteResult result = bulkOps.execute(); + } }