Bläddra i källkod

Merge branch 'feature/v1.2.7' into dev/v1.2.7_rjj

renjiaojiao 1 år sedan
förälder
incheckning
f890dbebe3
2 ändrade filer med 51 tillägg och 39 borttagningar
  1. 29 10
      rpc/internal/common/messageService.go
  2. 22 29
      rpc/internal/common/sendMsg.go

+ 29 - 10
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,45 @@ 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, groupIds []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"]))
+			groupIds = append(groupIds, qutil.InterfaceToStr(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 {
-				count += qutil.Int64All(v["count"])
-				key := fmt.Sprintf(MsgCountKey, userId, qutil.IntAll(v["group_id"]))
-				redis.Put(redisModule, key, qutil.Int64All(v["count"]), -1)
+				unread := qutil.Int64All(v["count"])
+				data[qutil.InterfaceToStr(v["group_id"])] = unread
+				count += unread
+			}
+		}
+		if isRedis { //初始化未读数
+			for _, v1 := range groupIds {
+				key := fmt.Sprintf(MsgCountKey, userId, qutil.IntAll(v1))
+				redis.Put(redisModule, key, data[v1], -1)
 			}
 		}
 	}
 
-	return count
+	return data, count
 }
 
 // 查询消息详情

+ 22 - 29
rpc/internal/common/sendMsg.go

@@ -99,23 +99,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)
 		}
@@ -176,6 +164,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"])
@@ -186,10 +176,10 @@ 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]
+				unread += sortUnread[msgType]
 				column.Data = sData[msgType]
 			}
-			unread += column.UnreadMessages
 			columnData = append(columnData, &column)
 		}
 	}
@@ -227,7 +217,7 @@ func UserMsgList(this *message.UserMsgListReq) *message.UserMsgList {
 		}
 	}
 	data.Count = count
-	if !this.IsColumn && this.Read == 0 {
+	if this.Read == 0 {
 		unread = count
 		if this.IsContainLetter { //是否需要统计私信未读数
 			unread += unreadMsg(this)
@@ -393,17 +383,20 @@ func MsgCountMinusOne(userId, appId string, msgType int64) bool {
 
 // MsgCountZero 把该消息类型未读数量置0
 func MsgCountZero(userId, appId string, msgType int64) bool {
-	keyString := fmt.Sprintf(MsgCountKey, userId, msgType)
-	fool := redis.Put(redisModule, keyString, 0, -1)
-	FindUserMsg(message.FindUserMsgReq{
-		UserId:   userId,
-		Appid:    appId,
-		OffSet:   1,
-		PageSize: 5,
-		MsgType:  -1,
-		Read:     0,
-	}, true)
-	return fool
+	if msgType > 0 && msgType < 999 { //全部私信不统计
+		keyString := fmt.Sprintf(MsgCountKey, userId, msgType)
+		fool := redis.Put(redisModule, keyString, 0, -1)
+		FindUserMsg(message.FindUserMsgReq{
+			UserId:   userId,
+			Appid:    appId,
+			OffSet:   1,
+			PageSize: 5,
+			MsgType:  -1,
+			Read:     0,
+		}, true)
+		return fool
+	}
+	return true
 }
 
 func MultSave(this message.MultipleSaveMsgReq) (int64, string) {