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