Browse Source

fix:未读消息数走redis修改

duxin 1 year ago
parent
commit
ed14f47b70
2 changed files with 30 additions and 25 deletions
  1. 24 9
      rpc/internal/common/messageService.go
  2. 6 16
      rpc/internal/common/sendMsg.go

+ 24 - 9
rpc/internal/common/messageService.go

@@ -3,6 +3,7 @@ package common
 import (
 	"app.yhyue.com/moapp/MessageCenter/entity"
 	"app.yhyue.com/moapp/MessageCenter/rpc/type/message"
+	"app.yhyue.com/moapp/MessageCenter/util"
 	qutil "app.yhyue.com/moapp/jybase/common"
 	"app.yhyue.com/moapp/jybase/redis"
 	"errors"
@@ -60,27 +61,41 @@ func (service *MessageService) DeleteMessage(id []string, appId string) (int64,
 	return 1, "删除消息成功"
 }
 
-// 未读消息合计
-func (service *MessageService) CountUnread(userId string) int64 {
-	var count int64
-	var msgTypes []string
+// 未读消息合计 isRedis 是否需要初始化redis
+func (service *MessageService) CountUnread(userId string, isRedis bool) (map[string]int64, int64) {
+	var (
+		count    int64
+		msgTypes []string
+	)
+	data := make(map[string]int64)
 	for _, v := range entity.MessageColumn {
-		if qutil.IntAll(v["group_id"]) > 0 && qutil.IntAll(v["group_id"]) < 999 {
+		if util.IntAll(v["group_id"]) > 0 && util.IntAll(v["group_id"]) < 999 {
+			//去除全部与私信
 			msgTypes = append(msgTypes, fmt.Sprintf(`"%s"`, qutil.InterfaceToStr(v["group_id"])))
+			key := fmt.Sprintf(MsgCountKey, userId, util.IntAll(v["group_id"]))
+			if exists, _ := redis.Exists(redisModule, key); exists {
+				ct := util.Int64All(redis.GetInt(redisModule, key))
+				data[qutil.InterfaceToStr(v["group_id"])] = ct
+				count += ct
+			}
 		}
 	}
-	if len(msgTypes) > 0 {
+	if len(msgTypes) > 0 && len(msgTypes) != len(data) {
+		count = 0
 		query := entity.Mysql.SelectBySql(fmt.Sprintf("SELECT group_id,COUNT(CASE WHEN isRead=0 THEN 1 END) as count  FROM message where receive_userid=? and isdel=1 and group_id IS NOT NULL GROUP BY group_id  ORDER BY FIELD(`group_id`,%s)", strings.Join(msgTypes, ",")), userId)
 		if query != nil && len(*query) > 0 {
 			for _, v := range *query {
+				data[qutil.InterfaceToStr(v["group_id"])] = qutil.Int64All(v["count"])
 				count += qutil.Int64All(v["count"])
-				key := fmt.Sprintf(MsgCountKey, userId, qutil.IntAll(v["group_id"]))
-				redis.Put(redisModule, key, qutil.Int64All(v["count"]), -1)
+				if isRedis {
+					key := fmt.Sprintf(MsgCountKey, userId, qutil.IntAll(v["group_id"]))
+					redis.Put(redisModule, key, qutil.Int64All(v["count"]), -1)
+				}
 			}
 		}
 	}
 
-	return count
+	return data, count
 }
 
 // 查询消息详情

+ 6 - 16
rpc/internal/common/sendMsg.go

@@ -100,23 +100,11 @@ func UserMsgList(this *message.UserMsgListReq) *message.UserMsgList {
 	var (
 		unread, count int64
 	)
+	m := &MessageService{}
 	data := new(message.UserMsgList)
 	if !this.IsMsgList && !this.IsColumnNewMsg && !this.IsColumn { //消息未读数统计
-		var isUnread, isColumn int
-		for _, v := range entity.MessageColumn {
-			if util.IntAll(v["group_id"]) > 0 && util.IntAll(v["group_id"]) < 999 {
-				isColumn++ //去除全部与私信
-				key := fmt.Sprintf(MsgCountKey, this.UserId, util.IntAll(v["group_id"]))
-				if exists, _ := redis.Exists(redisModule, key); exists {
-					isUnread++
-					unread += util.Int64All(redis.GetInt(redisModule, key))
-				}
-			}
-		}
-		if isUnread != isColumn {
-			m := &MessageService{}
-			unread = m.CountUnread(this.UserId)
-		}
+		//获取总未读数 初始化
+		_, unread = m.CountUnread(this.UserId, true)
 		if this.IsContainLetter { //私信统计
 			unread += unreadMsg(this)
 		}
@@ -177,6 +165,8 @@ func UserMsgList(this *message.UserMsgListReq) *message.UserMsgList {
 	//消息栏目下的最新消息
 	var columnData []*message.AllSortData
 	if this.IsColumn {
+		//获取所有分类未读数 不初始化
+		sortUnread, _ := m.CountUnread(this.UserId, false)
 		for _, v := range entity.MessageColumn {
 			var column message.AllSortData
 			column.Name = common.InterfaceToStr(v["name"])
@@ -187,7 +177,7 @@ func UserMsgList(this *message.UserMsgListReq) *message.UserMsgList {
 			} else if common.IntAll(v["group_id"]) > 0 {
 				//消息未读数
 				msgType := common.InterfaceToStr(v["group_id"])
-				column.UnreadMessages = util.Int64All(redis.GetInt(redisModule, fmt.Sprintf(MsgCountKey, this.UserId, util.IntAll(msgType))))
+				column.UnreadMessages = sortUnread[msgType]
 				column.Data = sData[msgType]
 			}
 			unread += column.UnreadMessages