From 9a8483c29564fb0915e3cacd09f29f617094b8a5 Mon Sep 17 00:00:00 2001 From: Robin <7434053+warrior_of_light_robin@user.noreply.gitee.com> Date: Tue, 3 Dec 2024 15:41:29 +0800 Subject: [PATCH] by Robin at 20241203 --- api/errcode/errcode.go | 2 ++ app/mix/service/apiservice.go | 33 +++++----------------------- app/mix/service/notif_bcst_center.go | 21 ++++++------------ app/mix/service/utilservice.go | 28 +++++++++++++++++++++++ 4 files changed, 43 insertions(+), 41 deletions(-) diff --git a/api/errcode/errcode.go b/api/errcode/errcode.go index bfe4af38..b87a0e5b 100644 --- a/api/errcode/errcode.go +++ b/api/errcode/errcode.go @@ -263,6 +263,7 @@ var ErrCodeMsgMap = map[ErrCode]string{ ErrCodeNotificationPushFail: "系统通知表推送失败", ErrCodeNotificationPullFail: "系统通知表拉取失败", ErrCodeNotificationWrongStatusForCancellation: "仅可撤销待推送的系统通知", + ErrCodeNotificationUrcUpdateFail: "未读数更新失败", ErrCodeRavenIQTestSrvFail: "瑞文智商测试表服务错误", ErrCodeRavenIQTestNotExist: "瑞文智商测试表不存在", @@ -631,6 +632,7 @@ const ( ErrCodeNotificationPushFail ErrCode = -46003 // 系统通知表推送失败 ErrCodeNotificationPullFail ErrCode = -46004 // 系统通知表拉取失败 ErrCodeNotificationWrongStatusForCancellation ErrCode = -46005 // 系统通知表状态错误 + ErrCodeNotificationUrcUpdateFail ErrCode = -46006 // 系统通知表未读数更新失败 // FrontendRoute: 47xxx ErrCodeFrontendRouteSrvOk ErrCode = ErrCodeOk diff --git a/app/mix/service/apiservice.go b/app/mix/service/apiservice.go index be9040bb..f1a85a02 100644 --- a/app/mix/service/apiservice.go +++ b/app/mix/service/apiservice.go @@ -4690,18 +4690,10 @@ func (s *Service) ApiReadNotification(ctx *gin.Context, req *notificationproto.A } // 以真更新的条数decr - key := util.GetNotifUrcIdForRedis(req.BaseRequest.Mid, req.NType) - totalKey := util.GetNotifUrcTotalIdForRedis(req.BaseRequest.Mid) - _, err = redis.GetRedisClient().DecrBy(key, result.ModifiedCount) + err = s.utilDecrUrc(req.BaseRequest.Mid, req.NType, result.ModifiedCount) if err != nil { - logger.Error("Redis DecrBy fail, err: %v", err) - ec = errcode.ErrCodeNotificationSrvFail - return - } - _, err = redis.GetRedisClient().DecrBy(totalKey, result.ModifiedCount) - if err != nil { - logger.Error("Redis DecrBy fail, err: %v", err) - ec = errcode.ErrCodeNotificationSrvFail + logger.Error("utilDecrUrc fail, req: %v, err: %v", util.ToJson(req), err) + ec = errcode.ErrCodeNotificationUrcUpdateFail return } @@ -4711,8 +4703,6 @@ func (s *Service) ApiReadNotification(ctx *gin.Context, req *notificationproto.A func (s *Service) ApiReadAllNotification(ctx *gin.Context, req *notificationproto.ApiReadAllReq) (ec errcode.ErrCode) { ec = errcode.ErrCodeNotificationSrvOk - totalDecr := int64(0) - for _, nType := range req.NTypes { // 标记已读 @@ -4724,23 +4714,12 @@ func (s *Service) ApiReadAllNotification(ctx *gin.Context, req *notificationprot } // 以真更新的条数decr - key := util.GetNotifUrcIdForRedis(req.BaseRequest.Mid, nType) - _, err = redis.GetRedisClient().DecrBy(key, result.ModifiedCount) + err = s.utilDecrUrc(req.BaseRequest.Mid, nType, result.ModifiedCount) if err != nil { - logger.Error("Redis DecrBy fail, err: %v", err) - ec = errcode.ErrCodeNotificationSrvFail + logger.Error("utilDecrUrc fail, req: %v, err: %v", util.ToJson(req), err) + ec = errcode.ErrCodeNotificationUrcUpdateFail return } - - totalDecr += result.ModifiedCount - } - - totalKey := util.GetNotifUrcTotalIdForRedis(req.BaseRequest.Mid) - _, err := redis.GetRedisClient().DecrBy(totalKey, totalDecr) - if err != nil { - logger.Error("Redis DecrBy fail, err: %v", err) - ec = errcode.ErrCodeNotificationSrvFail - return } return diff --git a/app/mix/service/notif_bcst_center.go b/app/mix/service/notif_bcst_center.go index d1b5b3b6..caf75a03 100644 --- a/app/mix/service/notif_bcst_center.go +++ b/app/mix/service/notif_bcst_center.go @@ -137,14 +137,10 @@ func (s *NotifBcstCenter) pushNotifsToMids(ctx *gin.Context, nids []int64, objMi // 记录未读总数 for _, mid := range objMids { for nType, total := range nTypeTotalMap { - _, err = redis.GetRedisClient().IncrBy(util.GetNotifUrcIdForRedis(mid, nType), total) + // 以真更新的条数incr + err = DefaultService.utilIncrUrc(mid, nType, total) if err != nil { - logger.Error("Redis IncrBy fail, err: %v", err) - return err - } - _, err = redis.GetRedisClient().IncrBy(util.GetNotifUrcTotalIdForRedis(mid), total) - if err != nil { - logger.Error("Redis IncrBy fail, err: %v", err) + logger.Error("utilIncrUrc fail, err: %v", err) return err } } @@ -245,14 +241,11 @@ func (s *NotifBcstCenter) pullAllBcstedNotifs(ctx *gin.Context, vers, receiveVer // 记录未读总数 for nType, total := range nTypeTotalMap { - _, err = redis.GetRedisClient().IncrBy(util.GetNotifUrcIdForRedis(objMid, nType), total) + + // 以真更新的条数incr + err = DefaultService.utilIncrUrc(objMid, nType, total) if err != nil { - logger.Error("Redis IncrBy fail, err: %v", err) - return err - } - _, err = redis.GetRedisClient().IncrBy(util.GetNotifUrcTotalIdForRedis(objMid), total) - if err != nil { - logger.Error("Redis IncrBy fail, err: %v", err) + logger.Error("utilIncrUrc fail, err: %v", err) return err } } diff --git a/app/mix/service/utilservice.go b/app/mix/service/utilservice.go index 59dd165c..32ff1f7d 100644 --- a/app/mix/service/utilservice.go +++ b/app/mix/service/utilservice.go @@ -2335,3 +2335,31 @@ func (s *Service) utilBuildInwardHyperLink(ctx *gin.Context, frontendRouteId int return hyperlinks, nil } + +func (s *Service) utilIncrUrc(mid, nType, incr int64) error { + _, err := redis.GetRedisClient().IncrBy(util.GetNotifUrcIdForRedis(mid, nType), incr) + if err != nil { + logger.Error("Redis IncrBy fail, err: %v", err) + return err + } + _, err = redis.GetRedisClient().IncrBy(util.GetNotifUrcTotalIdForRedis(mid), incr) + if err != nil { + logger.Error("Redis IncrBy fail, err: %v", err) + return err + } + return nil +} + +func (s *Service) utilDecrUrc(mid, nType, incr int64) error { + _, err := redis.GetRedisClient().DecrBy(util.GetNotifUrcIdForRedis(mid, nType), incr) + if err != nil { + logger.Error("Redis DecrBy fail, err: %v", err) + return err + } + _, err = redis.GetRedisClient().DecrBy(util.GetNotifUrcTotalIdForRedis(mid), incr) + if err != nil { + logger.Error("Redis DecrBy fail, err: %v", err) + return err + } + return nil +}