ソースを参照

fix:未读消息数走redis修改

duxin 1 年間 前
コミット
ccdd92d9cc
2 ファイル変更35 行追加32 行削除
  1. 7 11
      rpc/internal/common/messageService.go
  2. 28 21
      rpc/internal/common/sendMsg.go

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

@@ -60,19 +60,15 @@ func (service *MessageService) DeleteMessage(id []string, appId string) (int64,
 }
 
 // 未读消息合计
-func (service *MessageService) CountUnread(userId string, appId string) (int64, string, int) {
-	count := 0
-	types := entity.Mysql.Find("message_group", map[string]interface{}{}, `"msg_type"`, "", -1, -1)
-	if types != nil && len(*types) > 0 {
-		for _, v := range *types {
-			key := fmt.Sprintf(MsgCountKey, userId, qutil.IntAll(v["group_id"]))
-			if exists, _ := redis.Exists(redisModule, key); exists {
-				count += redis.GetInt(redisModule, key)
-			}
+func (service *MessageService) CountUnread(userId string) int64 {
+	var count int64
+	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))
 		}
 	}
-
-	return 1, "查询未读消息成功", count
+	return count
 }
 
 // 查询消息详情

+ 28 - 21
rpc/internal/common/sendMsg.go

@@ -100,6 +100,26 @@ func UserMsgList(this *message.UserMsgListReq) *message.UserMsgList {
 	var (
 		unread, count int64
 	)
+	data := new(message.UserMsgList)
+	if !this.IsMsgList && !this.IsColumnNewMsg && !this.IsColumn { //消息未读数统计
+		var isUnread int
+		for _, v := range entity.MessageColumn {
+			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 != len(entity.MessageColumn) {
+			m := &MessageService{}
+			unread = m.CountUnread(this.UserId)
+		}
+		if this.IsContainLetter { //私信统计
+			unread += unreadMsg(this)
+		}
+		data.Unread = unread
+		return data
+	}
 	cquery := map[string]interface{}{
 		"receive_userid": this.UserId,
 		"isdel":          1,
@@ -111,7 +131,7 @@ func UserMsgList(this *message.UserMsgListReq) *message.UserMsgList {
 	if this.Read != -1 {
 		cquery["isRead"] = this.Read
 	}
-	data := new(message.UserMsgList)
+
 	//获取栏目下的数据
 	sData := make(map[string][]*message.Messages)
 	t := time.Now()
@@ -151,24 +171,9 @@ func UserMsgList(this *message.UserMsgListReq) *message.UserMsgList {
 			}
 		}
 	}
-
 	//消息栏目下的最新消息
 	var columnData []*message.AllSortData
-	var msgTypes []string
-	for _, v := range entity.MessageColumn {
-		if common.IntAll(v["group_id"]) > 0 {
-			msgTypes = append(msgTypes, fmt.Sprintf(`"%s"`, common.InterfaceToStr(v["group_id"])))
-		}
-	}
 	if this.IsColumn {
-		columnUnread := make(map[string]int64)
-		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 appid=? GROUP BY group_id  ORDER BY FIELD(`group_id`,%s)", strings.Join(msgTypes, ",")), this.UserId, this.Appid)
-		log.Println("消息列表耗时2:", time.Since(t))
-		if query != nil && len(*query) > 0 {
-			for _, v := range *query {
-				columnUnread[common.InterfaceToStr(v["group_id"])] = common.Int64All(v["count"])
-			}
-		}
 		for _, v := range entity.MessageColumn {
 			var column message.AllSortData
 			column.Name = common.InterfaceToStr(v["name"])
@@ -179,7 +184,7 @@ func UserMsgList(this *message.UserMsgListReq) *message.UserMsgList {
 			} else {
 				//消息未读数
 				msgType := common.InterfaceToStr(v["group_id"])
-				column.UnreadMessages = columnUnread[msgType]
+				column.UnreadMessages = util.Int64All(redis.GetInt(redisModule, fmt.Sprintf(MsgCountKey, this.UserId, util.IntAll(msgType))))
 				column.Data = sData[msgType]
 			}
 			unread += column.UnreadMessages
@@ -271,7 +276,6 @@ func UserUnreadMsgList(this *message.UserUnreadMsgListReq) (int64, []*message.Me
 			"appid":          this.Appid,
 			"isRead":         0,
 		}
-		log.Println(query)
 		res := entity.Mysql.Find("message", query, "", "createtime desc", (int(this.OffSet)-1)*int(this.PageSize), int(this.PageSize))
 		if res != nil && len(*res) > 0 {
 			for _, val := range *res {
@@ -308,6 +312,9 @@ func UserUnreadMsgList(this *message.UserUnreadMsgListReq) (int64, []*message.Me
 }
 
 func MessageGetLast(this *message.UserMsgListReq) *message.Messages {
+	if !this.IsMsgList && !this.IsColumnNewMsg && !this.IsColumn {
+		return nil
+	}
 	query := map[string]interface{}{
 		"receive_userid": this.UserId,
 		"isdel":          1,
@@ -368,7 +375,7 @@ func MsgCountMinusOne(userId, appId string, msgType int64) bool {
 		Read:     0,
 	}, true)
 	if redis.GetInt(redisModule, keyString) <= 0 {
-		return redis.Put(redisModule, keyString, 0, -1)
+		return true
 	}
 	in := redis.Decrby(redisModule, keyString, 1)
 	return in > 0
@@ -440,7 +447,7 @@ func MultSave(this message.MultipleSaveMsgReq) (int64, string) {
 				})
 				logx.Info("执行事务是否成功:", ok)
 				if ok {
-					ok1 := MsgCountAdd(v, this.Appid, this.MsgType)
+					ok1 := MsgCountAdd(v, this.Appid, util.Int64All(group_id))
 					if !ok1 {
 						log.Println("存redis:", ok1, v)
 					}
@@ -449,7 +456,7 @@ func MultSave(this message.MultipleSaveMsgReq) (int64, string) {
 				in := entity.Mysql.InsertBySql(sql3, common.If(positionId != 0, positionId, nil))
 				logx.Info("插入消息返回 in1 id:", in)
 				if in > -1 {
-					ok := MsgCountAdd(v, this.Appid, this.MsgType)
+					ok := MsgCountAdd(v, this.Appid, util.Int64All(group_id))
 					if !ok {
 						log.Println("存redis:", ok, v)
 					}