userMessage添加分页查询,更新已读状态,批量操作全部已读

main
wangqing 1 year ago
parent 58d9c66939
commit 7801182926

@ -39,7 +39,7 @@ public class UserMessageDto implements Serializable {
/**
* 0 1
*/
private Integer havaRead;
private Integer haveRead;
/**

@ -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<UserMessage> pageList = userMessageService.findSearch(userId,page,size);
return Result.success(new PageResult<UserMessage>(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<UserMessage> userMessageList) {
userMessageService.batchHaveRead(userMessageList);
return Result.success();
}
}

@ -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<UserMessage,String> {
List<UserMessage> findAllByUserId(String userId);
Page<UserMessage> findByUserId(String userId, Pageable pageable);
}

@ -41,7 +41,7 @@ public class UserMessage implements Serializable {
/**
* 0 1
*/
private Integer havaRead;
private Integer haveRead;
/**

@ -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<UserMessage> 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<UserMessage> 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();
}
}

Loading…
Cancel
Save