Browse Source

未读消息数量修改

renjiaojiao 2 years ago
parent
commit
62b260c0d6

+ 28 - 0
api/internal/handler/getmsgtypehandler.go

@@ -0,0 +1,28 @@
+package handler
+
+import (
+	"net/http"
+
+	"app.yhyue.com/moapp/MessageCenter/api/internal/logic"
+	"app.yhyue.com/moapp/MessageCenter/api/internal/svc"
+	"app.yhyue.com/moapp/MessageCenter/api/internal/types"
+	"github.com/zeromicro/go-zero/rest/httpx"
+)
+
+func GetMsgTypeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.GetMsgTypeReq
+		if err := httpx.Parse(r, &req); err != nil {
+			httpx.Error(w, err)
+			return
+		}
+
+		l := logic.NewGetMsgTypeLogic(r.Context(), svcCtx)
+		resp, err := l.GetMsgType(&req)
+		if err != nil {
+			httpx.Error(w, err)
+		} else {
+			httpx.OkJson(w, resp)
+		}
+	}
+}

+ 49 - 0
api/internal/logic/getmsgtypelogic.go

@@ -0,0 +1,49 @@
+package logic
+
+import (
+	"app.yhyue.com/moapp/MessageCenter/rpc/messageclient"
+	"context"
+
+	"app.yhyue.com/moapp/MessageCenter/api/internal/svc"
+	"app.yhyue.com/moapp/MessageCenter/api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type GetMsgTypeLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewGetMsgTypeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMsgTypeLogic {
+	return &GetMsgTypeLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx,
+	}
+}
+
+func (l *GetMsgTypeLogic) GetMsgType(req *types.GetMsgTypeReq) (resp *types.GetMsgTypeRes, err error) {
+	result := &types.GetMsgTypeRes{}
+	lsi := l.svcCtx.MessageCenter
+	res, err := lsi.GetMsgType(l.ctx, &messageclient.GetMsgTypeReq{
+		Appid:  req.AppId,
+		UserId: req.UserId,
+	})
+	if err != nil {
+		return nil, err
+	}
+	for _, val := range res.Data {
+		result.Data = append(result.Data, map[string]interface{}{
+			"msgType":         val.MsgType,
+			"name":            val.Name,
+			"code":            val.Code,
+			"displayPlatform": val.DisplayPlatform,
+			"img":             val.Img,
+		})
+	}
+	result.Code = res.Code
+	result.Message = res.Message
+	return result, nil
+}

+ 16 - 13
rpc/internal/common/messageService.go

@@ -5,14 +5,14 @@ import (
 	"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"
-	"github.com/zeromicro/go-zero/core/logx"
+	"fmt"
 	"log"
 	"strconv"
 )
 
-type MessageService struct {
-}
+type MessageService struct{}
 
 // 修改消息阅读状态
 func (service *MessageService) ChangeReadStatus(data *message.ChangeReadStatusReq) (int64, string) {
@@ -25,7 +25,7 @@ func (service *MessageService) ChangeReadStatus(data *message.ChangeReadStatusRe
 	if !b {
 		return 0, "修改消息阅读状态失败"
 	}
-	MsgCountMinusOne(qutil.ObjToString((*msg)["receive_userid"]), strconv.Itoa(int((*msg)["msg_type"].(int64))), data.Appid)
+	MsgCountMinusOne(qutil.ObjToString((*msg)["receive_userid"]), data.Appid, qutil.Int64All((*msg)["msg_type"]))
 	return 1, "修改消息阅读状态成功"
 }
 
@@ -61,15 +61,18 @@ func (service *MessageService) DeleteMessage(id []string, appId string) (int64,
 }
 
 // 未读消息合计
-func (service *MessageService) CountUnread(userId string, appId string) (int64, string, int64) {
-	query := map[string]interface{}{
-		"receive_userid": userId,
-		"isRead":         0,
-		"isdel":          1,
-		"appid":          appId,
+func (service *MessageService) CountUnread(userId string, appId string) (int64, string, int) {
+	count := 0
+	types := entity.Mysql.Find("message_column", 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["msg_type"]))
+			if exists, _ := redis.Exists(redisModule, key); exists {
+				count += redis.GetInt(redisModule, key)
+			}
+		}
 	}
-	count := entity.Mysql.Count("message", query)
-	logx.Info("未读消息数量:", count)
+
 	return 1, "查询未读消息成功", count
 }
 
@@ -170,6 +173,6 @@ func (service *MessageService) UpdateMessageReadStatus(msgType int, receiveUseri
 	if !b {
 		return 0, errors.New("修改消息已读出错")
 	}
-	MsgCountZero(receiveUserid, strconv.Itoa(msgType), appId)
+	MsgCountZero(receiveUserid, appId, qutil.Int64All(msgType))
 	return 1, nil
 }

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

@@ -18,7 +18,7 @@ import (
 
 // 类型的顺序
 const order = "1,4"
-const MsgCountKey = "count_%s_%s" //redis 消息未读数量 Count.用户id.消息类型=数量
+const MsgCountKey = "count_%s_%d" //redis 消息未读数量 Count.用户id.消息类型=数量
 const redisModule = "msgCount"
 
 func FindUserMsg(this message.FindUserMsgReq, isClean bool) message.FindUserMsgRes {
@@ -343,7 +343,7 @@ func ClassCountUnread(msgType int, userId string, appId string) (int64, string,
 }
 
 // MsgCountAdd 消息未读数量加1
-func MsgCountAdd(userId, msgType, appId string) bool {
+func MsgCountAdd(userId, appId string, msgType int64) bool {
 	keyString := fmt.Sprintf(MsgCountKey, userId, msgType)
 	in := redis.Incr(redisModule, keyString)
 	FindUserMsg(message.FindUserMsgReq{
@@ -358,7 +358,7 @@ func MsgCountAdd(userId, msgType, appId string) bool {
 }
 
 // MsgCountMinusOne 根据消息类型未读消息数量减1
-func MsgCountMinusOne(userId, msgType, appId string) bool {
+func MsgCountMinusOne(userId, appId string, msgType int64) bool {
 	keyString := fmt.Sprintf(MsgCountKey, userId, msgType)
 	FindUserMsg(message.FindUserMsgReq{
 		UserId:   userId,
@@ -376,7 +376,7 @@ func MsgCountMinusOne(userId, msgType, appId string) bool {
 }
 
 // MsgCountZero 把该消息类型未读数量置0
-func MsgCountZero(userId, msgType, appId string) bool {
+func MsgCountZero(userId, appId string, msgType int64) bool {
 	keyString := fmt.Sprintf(MsgCountKey, userId, msgType)
 	fool := redis.Put(redisModule, keyString, 0, -1)
 	FindUserMsg(message.FindUserMsgReq{
@@ -435,7 +435,7 @@ func MultSave(this message.MultipleSaveMsgReq) (int64, string) {
 				})
 				logx.Info("执行事务是否成功:", ok)
 				if ok {
-					ok1 := MsgCountAdd(v, strconv.Itoa(int(this.MsgType)), this.Appid)
+					ok1 := MsgCountAdd(v, this.Appid, this.MsgType)
 					if !ok1 {
 						log.Println("存redis:", ok1, v)
 					}
@@ -444,7 +444,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, strconv.Itoa(int(this.MsgType)), this.Appid)
+					ok := MsgCountAdd(v, this.Appid, this.MsgType)
 					if !ok {
 						log.Println("存redis:", ok, v)
 					}

+ 2 - 1
rpc/internal/logic/getunreadcountlogic.go

@@ -3,6 +3,7 @@ package logic
 import (
 	service "app.yhyue.com/moapp/MessageCenter/rpc/internal/common"
 	"app.yhyue.com/moapp/MessageCenter/rpc/messageclient"
+	qutil "app.yhyue.com/moapp/jybase/common"
 	"context"
 
 	"app.yhyue.com/moapp/MessageCenter/rpc/internal/svc"
@@ -31,6 +32,6 @@ func (l *GetUnreadCountLogic) GetUnreadCount(in *messageclient.GetUnreadCountReq
 	return &messageclient.GetUnreadCountResponse{
 		Code:    code,
 		Message: msg,
-		Count:   count,
+		Count:   qutil.Int64All(count),
 	}, nil
 }