一、创建MessageDao类

image.png
在 com.project.emos.wx.db.dao 包中创建 MessageDao.java 类

  1. package com.project.emos.wx.db.dao;
  2. import cn.hutool.core.date.DateField;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.json.JSONObject;
  5. import com.project.emos.wx.entity.MessageEntity;
  6. import com.project.emos.wx.entity.MessageRefEntity;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.data.domain.Sort;
  9. import org.springframework.data.mongodb.core.MongoTemplate;
  10. import org.springframework.data.mongodb.core.aggregation.Aggregation;
  11. import org.springframework.data.mongodb.core.aggregation.AggregationResults;
  12. import org.springframework.data.mongodb.core.query.Criteria;
  13. import org.springframework.stereotype.Repository;
  14. import java.util.Date;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. /**
  18. * @author JianqingWeng
  19. * @Description
  20. * @create 2021-06-21 20:31
  21. */
  22. @Repository
  23. public class MessageDao {
  24. @Autowired
  25. private MongoTemplate mongoTemplate;
  26. public String insert(MessageEntity entity) {
  27. Date sendTime = entity.getSendTime();
  28. // 北京时间转换为格林威治时间
  29. DateUtil.offset(sendTime, DateField.HOUR, 8);
  30. entity.setSendTime(sendTime);
  31. entity = mongoTemplate.save(entity);
  32. return entity.get_id();
  33. }
  34. public List<HashMap> searchMessageByPage(int userId, long start, int length) {
  35. JSONObject json = new JSONObject();
  36. JSONObject set = json.set("$toString", "$_id");
  37. Aggregation aggregation = Aggregation.newAggregation(
  38. Aggregation.addFields().addField("id").withValue(json).build(),
  39. Aggregation.lookup("message_ref", "id", "messageId", "ref"),
  40. Aggregation.match(Criteria.where("ref.receiverId").is(userId)),
  41. Aggregation.sort(Sort.by(Sort.Direction.DESC, "sendTime")),
  42. Aggregation.skip(start),
  43. Aggregation.limit(length)
  44. );
  45. AggregationResults<HashMap> results = mongoTemplate.aggregate
  46. (aggregation, "message", HashMap.class);
  47. List<HashMap> list = results.getMappedResults();
  48. list.forEach(one -> {
  49. List<MessageRefEntity> refList = (List<MessageRefEntity>) one.get("ref");
  50. MessageRefEntity entity = refList.get(0);
  51. Boolean readFlag = entity.getReadFlag();
  52. String refId = entity.get_id();
  53. one.put("readFlag", readFlag);
  54. one.put("refId", refId);
  55. one.remove("ref");
  56. one.remove("_id");
  57. //把格林尼治时间转换成北京时间
  58. Date sendTime = (Date) one.get("sendTime");
  59. sendTime = DateUtil.offset(sendTime, DateField.HOUR, -8);
  60. String today = DateUtil.today();
  61. //如果是今天的消息,只显示发送时间,不需要显示日期
  62. if (today.equals(DateUtil.date(sendTime).toDateStr())) {
  63. one.put("sendTime", DateUtil.format(sendTime, "HH:mm"));
  64. }
  65. //如果是以往的消息,只显示日期,不显示发送时间
  66. else {
  67. one.put("sendTime", DateUtil.format(sendTime, "yyyy/MM/dd"));
  68. }
  69. });
  70. return list;
  71. }
  72. public HashMap searchMessageById(String id) {
  73. HashMap map = mongoTemplate.findById(id, HashMap.class, "message");
  74. Date sendTime = (Date) map.get("sendTime");
  75. //把格林威治时间转换成北京时间
  76. sendTime = DateUtil.offset(sendTime, DateField.HOUR, -8);
  77. map.replace("sendTime", DateUtil.format(sendTime, "yyyy-MM-dd HH:mm"));
  78. return map;
  79. }
  80. }

二、创建MessageRefDao类

image.png

  1. package com.project.emos.wx.db.dao;
  2. import com.mongodb.client.result.DeleteResult;
  3. import com.mongodb.client.result.UpdateResult;
  4. import com.project.emos.wx.entity.MessageRefEntity;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.data.mongodb.core.MongoTemplate;
  7. import org.springframework.data.mongodb.core.query.Criteria;
  8. import org.springframework.data.mongodb.core.query.Query;
  9. import org.springframework.data.mongodb.core.query.Update;
  10. import org.springframework.stereotype.Repository;
  11. /**
  12. * @author JianqingWeng
  13. * @Description
  14. * @create 2021-06-21 20:54
  15. */
  16. @Repository
  17. public class MessageRefDao {
  18. @Autowired
  19. private MongoTemplate mongoTemplate;
  20. public String insert(MessageRefEntity entity) {
  21. entity = mongoTemplate.save(entity);
  22. return entity.get_id();
  23. }
  24. /**
  25. * 查询未读消息数量
  26. * @param userId
  27. * @return
  28. */
  29. public long searchUnreadCount(int userId) {
  30. Query query = new Query();
  31. query.addCriteria(Criteria.where("readFlag").
  32. is(false).
  33. and("receiverId").
  34. is(userId));
  35. long count = mongoTemplate.count(query, MessageRefEntity.class);
  36. return count;
  37. }
  38. /**
  39. * 查询新接收消息数量
  40. * @param userId
  41. * @return
  42. */
  43. public long searchLastCount(int userId) {
  44. Query query = new Query();
  45. query.addCriteria(Criteria.where("lastFlag").is(true).and("receiverId").is(userId));
  46. Update update = new Update();
  47. update.set("lastFlag", false);
  48. UpdateResult result = mongoTemplate.updateMulti(query, update, "message_ref");
  49. long rows = result.getModifiedCount();
  50. return rows;
  51. }
  52. /**
  53. * 把未读消息变更为已读消息
  54. * @param id
  55. * @return
  56. */
  57. public long updateUnreadMessage(String id) {
  58. Query query = new Query();
  59. query.addCriteria(Criteria.where("_id").is(id));
  60. Update update = new Update();
  61. update.set("readFlag", true);
  62. UpdateResult result = mongoTemplate.updateFirst(query,
  63. update, "message_ref");
  64. long rows = result.getModifiedCount();
  65. return rows;
  66. }
  67. /**
  68. * 根据ID删除ref消息
  69. * @param id
  70. * @return
  71. */
  72. public long deleteMessageRefById(String id) {
  73. Query query = new Query();
  74. query.addCriteria(Criteria.where("_id").is(id));
  75. DeleteResult result = mongoTemplate.remove(query, "message_ref");
  76. long rows = result.getDeletedCount();
  77. return rows;
  78. }
  79. /**
  80. * 删除某个用户全部消息
  81. * @param userId
  82. * @return
  83. */
  84. public long deleteUserMessageRef(int userId) {
  85. Query query = new Query();
  86. query.addCriteria(Criteria.where("receiverId").is(userId));
  87. DeleteResult result = mongoTemplate.remove(query, "message_ref");
  88. long rows = result.getDeletedCount();
  89. return rows;
  90. }
  91. }