Selaa lähdekoodia

fix:未读消息数走redis修改

duxin 1 vuosi sitten
vanhempi
commit
5b8f7bce28
2 muutettua tiedostoa jossa 41 lisäystä ja 25 poistoa
  1. 11 3
      rpc/internal/common/messageService.go
  2. 30 22
      rpc/internal/common/sendMsg.go

+ 11 - 3
rpc/internal/common/messageService.go

@@ -8,6 +8,7 @@ import (
 	"errors"
 	"fmt"
 	"log"
+	"strings"
 	"time"
 )
 
@@ -62,12 +63,19 @@ func (service *MessageService) DeleteMessage(id []string, appId string) (int64,
 // 未读消息合计
 func (service *MessageService) CountUnread(userId string) int64 {
 	var count int64
+	var msgTypes []string
 	for _, v := range entity.MessageColumn {
-		key := fmt.Sprintf(MsgCountKey, userId, qutil.IntAll(v["group_id"]))
-		if exists, _ := redis.Exists(redisModule, key); exists {
-			count += qutil.Int64All(redis.GetInt(redisModule, key))
+		msgTypes = append(msgTypes, fmt.Sprintf(`"%s"`, qutil.InterfaceToStr(v["group_id"])))
+	}
+	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 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)
 		}
 	}
+
 	return count
 }
 

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

@@ -351,34 +351,42 @@ func MessageGetLast(this *message.UserMsgListReq) *message.Messages {
 // MsgCountAdd 消息未读数量加1
 func MsgCountAdd(userId, appId string, msgType int64) bool {
 	keyString := fmt.Sprintf(MsgCountKey, userId, msgType)
-	in := redis.Incr(redisModule, keyString)
-	FindUserMsg(message.FindUserMsgReq{
-		UserId:   userId,
-		Appid:    appId,
-		OffSet:   1,
-		PageSize: 5,
-		MsgType:  -1,
-		Read:     0,
-	}, true)
-	return in > 0
+	exists, _ := redis.Exists(redisModule, keyString)
+	if exists {
+		in := redis.Incr(redisModule, keyString)
+		FindUserMsg(message.FindUserMsgReq{
+			UserId:   userId,
+			Appid:    appId,
+			OffSet:   1,
+			PageSize: 5,
+			MsgType:  -1,
+			Read:     0,
+		}, true)
+		return in > 0
+	}
+	return true
 }
 
 // MsgCountMinusOne 根据消息类型未读消息数量减1
 func MsgCountMinusOne(userId, appId string, msgType int64) bool {
 	keyString := fmt.Sprintf(MsgCountKey, userId, msgType)
-	FindUserMsg(message.FindUserMsgReq{
-		UserId:   userId,
-		Appid:    appId,
-		OffSet:   1,
-		PageSize: 5,
-		MsgType:  -1,
-		Read:     0,
-	}, true)
-	if redis.GetInt(redisModule, keyString) <= 0 {
-		return true
+	exists, _ := redis.Exists(redisModule, keyString)
+	if exists {
+		FindUserMsg(message.FindUserMsgReq{
+			UserId:   userId,
+			Appid:    appId,
+			OffSet:   1,
+			PageSize: 5,
+			MsgType:  -1,
+			Read:     0,
+		}, true)
+		if redis.GetInt(redisModule, keyString) <= 0 {
+			return true
+		}
+		in := redis.Decrby(redisModule, keyString, 1)
+		return in > 0
 	}
-	in := redis.Decrby(redisModule, keyString, 1)
-	return in > 0
+	return true
 }
 
 // MsgCountZero 把该消息类型未读数量置0