一、创建MessageDao类
在 com.example.emos.wx.db.dao 包中创建 MessageDao.java 类
@Repository
public class MessageDao {
@Autowired
private MongoTemplate mongoTemplate;
public String insert(MessageEntity entity){
Date sendTime=entity.getSendTime();
sendTime=DateUtil.offset(sendTime, DateField.HOUR,8);
entity.setSendTime(sendTime);
entity=mongoTemplate.save(entity);
return entity.get_id();
}
public List<HashMap> searchMessageByPage(int userId,long start,int length){
JSONObject json=new JSONObject();
json.set("$toString","$_id");
Aggregation aggregation=Aggregation.newAggregation(
Aggregation.addFields().addField("id").withValue(json).build(),
Aggregation.lookup("message_ref","id","messageId","ref"),
Aggregation.match(Criteria.where("ref.receiverId").is(userId)),
Aggregation.sort(Sort.by(Sort.Direction.DESC,"sendTime")),
Aggregation.skip(start),
Aggregation.limit(length)
);
AggregationResults<HashMap> results=mongoTemplate.aggregate(aggregation,"message",HashMap.class);
List<HashMap> list=results.getMappedResults();
list.forEach(one->{
List<MessageRefEntity> refList= (List<MessageRefEntity>) one.get("ref");
MessageRefEntity entity=refList.get(0);
boolean readFlag=entity.getReadFlag();
String refId=entity.get_id();
one.put("readFlag",readFlag);
one.put("refId",refId);
one.remove("ref");
one.remove("_id");
Date sendTime= (Date) one.get("sendTime");
sendTime=DateUtil.offset(sendTime,DateField.HOUR,-8);
String today=DateUtil.today();
if(today.equals(DateUtil.date(sendTime).toDateStr())){
one.put("sendTime",DateUtil.format(sendTime,"HH:mm"));
}
else{
one.put("sendTime",DateUtil.format(sendTime,"yyyy/MM/dd"));
}
});
return list;
}
public HashMap searchMessageById(String id){
HashMap map=mongoTemplate.findById(id,HashMap.class,"message");
Date sendTime= (Date) map.get("sendTime");
sendTime=DateUtil.offset(sendTime,DateField.HOUR,-8);
map.replace("sendTime",DateUtil.format(sendTime,"yyyy-MM-dd HH:mm"));
return map;
}
}
二、创建MessageRefDao类
在 com.example.emos.wx.db.dao 包中创建 MessageRefDao.java 类
@Repository
public class MessageRefDao {
@Autowired
private MongoTemplate mongoTemplate;
public String insert(MessageRefEntity entity) {
entity = mongoTemplate.save(entity);
return entity.get_id();
}
public long searchUnreadCount(int userId) {
Query query = new Query();
query.addCriteria(Criteria.where("readFlag").is(false).and("receiverId").is(userId));
long count = mongoTemplate.count(query, MessageRefEntity.class);
return count;
}
public long searchLastCount(int userId) {
Query query = new Query();
query.addCriteria(Criteria.where("lastFlag").is(true).and("receiverId").is(userId));
Update update = new Update();
update.set("lastFlag", false);
UpdateResult result = mongoTemplate.updateMulti(query, update, "message_ref");
long rows = result.getModifiedCount();
return rows;
}
public long updateUnreadMessage(String id){
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(id));
Update update = new Update();
update.set("readFlag", true);
UpdateResult result = mongoTemplate.updateFirst(query, update, "message_ref");
long rows = result.getModifiedCount();
return rows;
}
public long deleteMessageRefById(String id){
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(id));
DeleteResult result=mongoTemplate.remove(query,"message_ref");
long rows=result.getDeletedCount();
return rows;
}
public long deleteUserMessageRef(int userId){
Query query = new Query();
query.addCriteria(Criteria.where("receiverId").is(userId));
DeleteResult result=mongoTemplate.remove(query,"message_ref");
long rows=result.getDeletedCount();
return rows;
}
}