by Robin at 20241127

This commit is contained in:
Robin 2024-11-27 15:31:48 +08:00
parent f03238d753
commit 7be56f9775
10 changed files with 153 additions and 9 deletions

View File

@ -98,7 +98,7 @@ const (
const (
RedisStreamerPrefix = "streamer:" //streamer服务前缀
RedisMomentPrefix = "moment:" //moment服务前缀
RedisNotificationPrefix = "notification:" //notification服务前缀
RedisNotificationPrefix = "notif:" //notification服务前缀
RedisContactCustomerServicePrefix = "contact_customer_service:" //contact_customer_service服务前缀
)

View File

@ -354,8 +354,8 @@ const (
// 系统通知表的已读状态
const (
Notification_NotRead = 0 //未读
Notification_Read = 1 //已读
NotifReceive_NotRead = 0 //未读
NotifReceive_Read = 1 //已读
)
// 系统通知表的推送状态

View File

@ -36,3 +36,32 @@ type ApiReceiveResp struct {
base.BaseResponse
Data *ApiReceiveData `json:"data"`
}
// op 计数
type ApiCountUnreadReq struct {
base.BaseRequest
NType *int64 `json:"n_type"`
}
type ApiCountUnreadData struct {
}
type ApiCountUnreadResp struct {
base.BaseResponse
Data *ApiCountUnreadData `json:"data"`
}
// op 计数
type ApiReadReq struct {
base.BaseRequest
Ids []int64 `json:"ids"`
NType int64 `json:"n_type"`
}
type ApiReadData struct {
}
type ApiReadResp struct {
base.BaseResponse
Data *ApiReadData `json:"data"`
}

View File

@ -6576,6 +6576,24 @@ func (m *Mongo) CreateNotifReceives(ctx *gin.Context, notifReceives []*dbstruct.
return err
}
func (m *Mongo) ReadNotifReceiveByIds(ctx *gin.Context, req *notificationproto.ApiReadReq) (*qmgo.UpdateResult, error) {
col := m.getColNotifReceive()
update := qmgo.M{
"$set": qmgo.M{
"is_read": consts.NotifReceive_Read,
},
}
filter := qmgo.M{
"_id": qmgo.M{
"$in": req.Ids,
},
"is_read": consts.NotifReceive_NotRead,
"n_type": req.NType,
}
result, err := col.UpdateAll(ctx, filter, update)
return result, err
}
func (m *Mongo) DeleteNotifReceiveByIds(ctx *gin.Context, ids []int64) error {
col := m.getColNotifReceive()
update := qmgo.M{

View File

@ -4677,3 +4677,47 @@ func (s *Service) ApiReceiveAllBcstedNotifs(ctx *gin.Context, req *notificationp
return
}
func (s *Service) ApiReadNotification(ctx *gin.Context, req *notificationproto.ApiReadReq) (ec errcode.ErrCode) {
ec = errcode.ErrCodeNotificationSrvOk
// 标记已读
result, err := _DefaultNotifReceive.OpReadByIds(ctx, req)
if err != nil {
logger.Error("_DefaultNotifReceive OpReadByIds fail, req: %v, err: %v", util.ToJson(req), err)
ec = errcode.ErrCodeNotificationSrvFail
return
}
// 以真更新的条数decr
key := util.GetNotifUrcIdForRedis(req.BaseRequest.Mid, req.NType)
_, err = redis.GetRedisClient().DecrBy(key, result.ModifiedCount)
if err != nil {
logger.Error("Redis DecrBy fail, err: %v", err)
ec = errcode.ErrCodeNotificationSrvFail
return
}
return
}
func (s *Service) ApiGetNotificationUrcByMid(ctx *gin.Context, req *notificationproto.ApiCountUnreadReq) (mp map[int64]int64, ec errcode.ErrCode) {
ec = errcode.ErrCodeNotificationSrvOk
// 从redis中读取数据
mp = make(map[int64]int64)
if req.NType != nil {
nType := util.DerefInt64(req.NType)
total, _ := redis.GetRedisClient().GetInt64(util.GetNotifUrcIdForRedis(req.BaseRequest.Mid, nType))
mp[nType] = total
} else {
sysTotal, _ := redis.GetRedisClient().GetInt64(util.GetNotifUrcIdForRedis(req.BaseRequest.Mid, consts.Notif_System))
audTotal, _ := redis.GetRedisClient().GetInt64(util.GetNotifUrcIdForRedis(req.BaseRequest.Mid, consts.Notif_Audit))
vasTotal, _ := redis.GetRedisClient().GetInt64(util.GetNotifUrcIdForRedis(req.BaseRequest.Mid, consts.Notif_Vas))
mp[consts.Notif_System] = sysTotal
mp[consts.Notif_Audit] = audTotal
mp[consts.Notif_Vas] = vasTotal
}
return
}

View File

@ -10,6 +10,7 @@ import (
notificationproto "service/api/proto/notification/proto"
"github.com/gin-gonic/gin"
"github.com/qiniu/qmgo"
)
type NotifReceive struct {
@ -44,6 +45,15 @@ func (p *NotifReceive) OpCreateBatch(ctx *gin.Context, notifReceives []*dbstruct
return nil
}
func (p *NotifReceive) OpReadByIds(ctx *gin.Context, req *notificationproto.ApiReadReq) (*qmgo.UpdateResult, error) {
result, err := p.store.ReadNotifReceiveByIds(ctx, req)
if err != nil {
logger.Error("DeleteNotifBcstByIds fail, err: %v", err)
return nil, err
}
return result, nil
}
func (p *NotifReceive) OpDeleteByIds(ctx *gin.Context, ids []int64) error {
err := p.store.DeleteNotifReceiveByIds(ctx, ids)
if err != nil {

View File

@ -1,7 +1,6 @@
package logic
import (
"fmt"
"service/api/consts"
notificationproto "service/api/proto/notification/proto"
"service/app/mix/dao"
@ -107,7 +106,3 @@ func (p *Notification) OpUpdateByIds(ctx *gin.Context, notification *dbstruct.No
}
return nil
}
func GetNotificationCountIdForRedis(mid int64) string {
return fmt.Sprintf("%sunread_count_%d", consts.RedisNotificationPrefix, mid)
}

View File

@ -2,8 +2,10 @@ package service
import (
"service/api/consts"
"service/bizcommon/util"
"service/dbstruct"
"service/library/logger"
"service/library/redis"
"github.com/gin-gonic/gin"
goproto "google.golang.org/protobuf/proto"
@ -82,20 +84,48 @@ func (s *NotifBcstCenter) bcstNotifsToAll(ctx *gin.Context, nids []int64, objTyp
// 直接推送至用户
func (s *NotifBcstCenter) pushNotifsToMids(ctx *gin.Context, nids []int64, objMids []int64) error {
// 查询得到消息
notifMap := make(map[int64]*dbstruct.Notification)
nTypeTotalMap := make(map[int64]int64)
notifs, err := _DefaultNotification.GetListByIds(ctx, nids)
if err != nil {
logger.Error("GetNotificationListByIds fail, err: %v", err)
return err
}
for _, notif := range notifs {
notifMap[notif.GetId()] = notif
nTypeTotalMap[notif.GetNType()]++
}
notifReceives := make([]*dbstruct.NotifReceive, 0)
for _, mid := range objMids {
for _, nid := range nids {
notifReceives = append(notifReceives, &dbstruct.NotifReceive{
ObjMid: mid,
Nid: nid,
NType: notifMap[nid].GetNType(),
IsRead: consts.NotifReceive_NotRead,
})
}
}
err := _DefaultNotifReceive.OpCreateBatch(ctx, notifReceives)
err = _DefaultNotifReceive.OpCreateBatch(ctx, notifReceives)
if err != nil {
logger.Error("OpCreateBatch fail, err: %v", err)
return err
}
// 记录未读总数
for _, mid := range objMids {
for nType, total := range nTypeTotalMap {
_, err = redis.GetRedisClient().IncrBy(util.GetNotifUrcIdForRedis(mid, nType), total)
if err != nil {
logger.Error("Redis IncrBy fail, err: %v", err)
return err
}
}
}
return nil
}
@ -161,6 +191,7 @@ func (s *NotifBcstCenter) pullAllBcstedNotifs(ctx *gin.Context, vers, receiveVer
notifReceive := &dbstruct.NotifReceive{
ObjMid: objMid,
Nid: nid,
IsRead: consts.NotifReceive_NotRead,
}
notifReceives = append(notifReceives, notifReceive)
notifReceiveMap[nid] = notifReceive
@ -168,6 +199,7 @@ func (s *NotifBcstCenter) pullAllBcstedNotifs(ctx *gin.Context, vers, receiveVer
}
}
nTypeTotalMap := make(map[int64]int64)
notifs, err := _DefaultNotification.GetListByIds(ctx, nids)
if err != nil {
logger.Error("GetNotificationListByIds fail, err: %v", err)
@ -178,6 +210,7 @@ func (s *NotifBcstCenter) pullAllBcstedNotifs(ctx *gin.Context, vers, receiveVer
if ok {
ntf.NType = notif.GetNType()
}
nTypeTotalMap[notif.GetNType()]++
}
err = _DefaultNotifReceive.OpCreateBatch(ctx, notifReceives)
@ -185,5 +218,15 @@ func (s *NotifBcstCenter) pullAllBcstedNotifs(ctx *gin.Context, vers, receiveVer
logger.Error("OpCreateBatch fail, err: %v", err)
return err
}
// 记录未读总数
for nType, total := range nTypeTotalMap {
_, err = redis.GetRedisClient().IncrBy(util.GetNotifUrcIdForRedis(objMid, nType), total)
if err != nil {
logger.Error("Redis IncrBy fail, err: %v", err)
return err
}
}
return nil
}

View File

@ -451,3 +451,7 @@ func FormatTsAsNotifT(timestamp int64) string {
func GetNotifScene(key string, option int64) int64 {
return consts.AudNotifTempKeyMap[key][option]
}
func GetNotifUrcIdForRedis(mid, nType int64) string {
return fmt.Sprintf("%surc_%d_%d", consts.RedisNotificationPrefix, mid, nType)
}

View File

@ -81,6 +81,7 @@ type NotifReceive struct {
ObjMid int64 `json:"obj_mid" bson:"obj_mid"` // 通知接收人mid
Nid int64 `json:"nid" bson:"nid"` // 系统通知表id
NType int64 `json:"n_type" bson:"n_type"` // 消息类型
IsRead int64 `json:"is_read" bson:"is_read"` // 是否已读
Ct int64 `json:"ct" bson:"ct"` // 创建时间
Ut int64 `json:"ut" bson:"ut"` // 更新时间
DelFlag int64 `json:"del_flag" bson:"del_flag"` // 删除标记