diff --git a/biz/adaptor/server.go b/biz/adaptor/server.go index 3edd1ee..25d4e81 100644 --- a/biz/adaptor/server.go +++ b/biz/adaptor/server.go @@ -13,6 +13,10 @@ type SystemServerImpl struct { SystemService service.SystemService } +func (s *SystemServerImpl) CreateNotificationCount(ctx context.Context, req *system.CreateNotificationCountReq) (res *system.CreateNotificationCountResp, err error) { + return s.SystemService.CreateNotificationCount(ctx, req) +} + // func (s *SystemServerImpl) UpdateNotifications(ctx context.Context, req *system.UpdateNotificationsReq) (resp *system.UpdateNotificationsResp, err error) { // return s.SystemService.UpdateNotifications(ctx, req) // } diff --git a/biz/application/service/system.go b/biz/application/service/system.go index a3b585d..1e8c404 100644 --- a/biz/application/service/system.go +++ b/biz/application/service/system.go @@ -2,9 +2,11 @@ package service import ( "context" + "fmt" "github.com/CloudStriver/cloudmind-system/biz/infrastructure/consts" "github.com/CloudStriver/cloudmind-system/biz/infrastructure/convertor" notificationmapper "github.com/CloudStriver/cloudmind-system/biz/infrastructure/mapper/notification" + notificationcountmapper "github.com/CloudStriver/cloudmind-system/biz/infrastructure/mapper/notificationCount" slidermapper "github.com/CloudStriver/cloudmind-system/biz/infrastructure/mapper/slider" "github.com/CloudStriver/go-pkg/utils/pagination/mongop" "github.com/CloudStriver/go-pkg/utils/pconvertor" @@ -23,12 +25,23 @@ type SystemService interface { GetNotifications(ctx context.Context, req *gensystem.GetNotificationsReq) (resp *gensystem.GetNotificationsResp, err error) GetNotificationCount(ctx context.Context, req *gensystem.GetNotificationCountReq) (resp *gensystem.GetNotificationCountResp, err error) CreateNotifications(ctx context.Context, req *gensystem.CreateNotificationsReq) (resp *gensystem.CreateNotificationsResp, err error) + CreateNotificationCount(ctx context.Context, req *gensystem.CreateNotificationCountReq) (resp *gensystem.CreateNotificationCountResp, err error) } type SystemServiceImpl struct { - NotificationMongoMapper notificationmapper.INotificationMongoMapper - SliderMongoMapper slidermapper.ISliderMongoMapper - Redis *redis.Redis + NotificationMongoMapper notificationmapper.INotificationMongoMapper + NotificationCountMongoMapper notificationcountmapper.INotificationCountMongoMapper + SliderMongoMapper slidermapper.ISliderMongoMapper + Redis *redis.Redis +} + +func (s *SystemServiceImpl) CreateNotificationCount(ctx context.Context, req *gensystem.CreateNotificationCountReq) (resp *gensystem.CreateNotificationCountResp, err error) { + uid, _ := primitive.ObjectIDFromHex(req.UserId) + err = s.NotificationCountMongoMapper.CreateNotificationCount(ctx, ¬ificationcountmapper.NotificationCount{ + ID: uid, + Read: 0, + }) + return resp, err } func (s *SystemServiceImpl) DeleteSlider(ctx context.Context, req *gensystem.DeleteSliderReq) (resp *gensystem.DeleteSliderResp, err error) { @@ -90,9 +103,9 @@ func (s *SystemServiceImpl) GetSliders(ctx context.Context, req *gensystem.GetSl func (s *SystemServiceImpl) GetNotifications(ctx context.Context, req *gensystem.GetNotificationsReq) (resp *gensystem.GetNotificationsResp, err error) { resp = new(gensystem.GetNotificationsResp) p := pconvertor.PaginationOptionsToModelPaginationOptions(req.PaginationOptions) - notifications, err := s.NotificationMongoMapper.GetNotifications(ctx, ¬ificationmapper.FilterOptions{ - OnlyUserId: lo.ToPtr(req.UserId), - OnlyType: req.OnlyType, + notifications, cnt, err := s.NotificationMongoMapper.GetNotificationsAndCount(ctx, ¬ificationmapper.FilterOptions{ + OnlyUserIds: []string{req.UserId, consts.NotificationSystemKey}, + OnlyType: req.OnlyType, }, p, mongop.IdCursorType) if err != nil { return resp, err @@ -104,6 +117,15 @@ func (s *SystemServiceImpl) GetNotifications(ctx context.Context, req *gensystem if p.LastToken != nil { resp.Token = *p.LastToken } + + uid, _ := primitive.ObjectIDFromHex(req.UserId) + if err = s.NotificationCountMongoMapper.UpdateNotificationCount(ctx, ¬ificationcountmapper.NotificationCount{ + ID: uid, + Read: cnt, + }); err != nil { + return resp, err + } + return resp, nil } @@ -115,8 +137,14 @@ func (s *SystemServiceImpl) GetNotificationCount(ctx context.Context, req *gensy return resp, err } + read, err := s.NotificationCountMongoMapper.GetNotificationCount(ctx, req.UserId) + if err != nil { + fmt.Println(err) + + return resp, err + } return &gensystem.GetNotificationCountResp{ - Total: cnt, + Total: cnt - read, }, nil } diff --git a/biz/infrastructure/consts/field.go b/biz/infrastructure/consts/field.go index 3c677ff..47222c7 100644 --- a/biz/infrastructure/consts/field.go +++ b/biz/infrastructure/consts/field.go @@ -1,20 +1,18 @@ package consts const ( - ID = "_id" - CreateAt = "createAt" - TargetUserId = "targetUserId" - IsRead = "isRead" - UpdateAt = "updateAt" - Type = "type" - TargetType = "targetType" - ImageUrl = "imageUrl" - Sum = "sum" - Read = "read" - IsPublic = "isPublic" - Status = "status" - //NotificationReadCountKey = "NotificationReadCount" - NotificationCount = "NotificationCount" + ID = "_id" + CreateAt = "createAt" + TargetUserId = "targetUserId" + IsRead = "isRead" + UpdateAt = "updateAt" + Type = "type" + TargetType = "targetType" + ImageUrl = "imageUrl" + Sum = "sum" + Read = "read" + IsPublic = "isPublic" + Status = "status" NotificationSystemKey = "system" //NotificationAll = "all" ) diff --git a/biz/infrastructure/mapper/notification/filter.go b/biz/infrastructure/mapper/notification/filter.go index 33b96c8..f9c7375 100644 --- a/biz/infrastructure/mapper/notification/filter.go +++ b/biz/infrastructure/mapper/notification/filter.go @@ -26,6 +26,7 @@ func MakeBsonFilter(options *FilterOptions) bson.M { func (f *MongoFilter) toBson() bson.M { f.CheckOnlyUserId() f.CheckOnlyType() + f.CheckOnlyUserIds() return f.m } @@ -44,7 +45,7 @@ func (f *MongoFilter) CheckOnlyType() { func (f *MongoFilter) CheckOnlyUserIds() { if f.OnlyUserIds != nil { f.m[consts.TargetUserId] = bson.M{ - "in": f.OnlyUserIds, + "$in": f.OnlyUserIds, } } } diff --git a/biz/infrastructure/mapper/notification/mongo.go b/biz/infrastructure/mapper/notification/mongo.go index 165c00a..47af161 100644 --- a/biz/infrastructure/mapper/notification/mongo.go +++ b/biz/infrastructure/mapper/notification/mongo.go @@ -4,10 +4,8 @@ import ( "context" "github.com/CloudStriver/go-pkg/utils/pagination" "github.com/CloudStriver/go-pkg/utils/pagination/mongop" - gensystem "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/system" "github.com/samber/lo" "github.com/zeromicro/go-zero/core/mr" - "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo/options" "time" @@ -15,7 +13,6 @@ import ( "github.com/zeromicro/go-zero/core/stores/monc" "github.com/CloudStriver/cloudmind-system/biz/infrastructure/config" - "github.com/CloudStriver/cloudmind-system/biz/infrastructure/consts" ) const ( @@ -28,7 +25,6 @@ type ( INotificationMongoMapper interface { GetNotifications(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Notification, error) Count(ctx context.Context, fopts *FilterOptions) (int64, error) - UpdateNotifications(ctx context.Context, fopts *FilterOptions) error DeleteNotifications(ctx context.Context, fopts *FilterOptions) error InsertOne(ctx context.Context, data *Notification) error GetNotificationsAndCount(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Notification, int64, error) @@ -119,14 +115,6 @@ func (m *MongoMapper) GetNotifications(ctx context.Context, fopts *FilterOptions return data, nil } -func (m *MongoMapper) UpdateNotifications(ctx context.Context, fopts *FilterOptions) error { - filter := MakeBsonFilter(fopts) - if _, err := m.conn.UpdateManyNoCache(ctx, filter, bson.M{"$set": bson.M{consts.Status: int64(gensystem.NotificationStatus_Read), consts.UpdateAt: time.Now()}}); err != nil { - return err - } - return nil -} - func (m *MongoMapper) Count(ctx context.Context, fopts *FilterOptions) (int64, error) { f := MakeBsonFilter(fopts) return m.conn.CountDocuments(ctx, f) diff --git a/biz/infrastructure/mapper/notificationCount/mongo.go b/biz/infrastructure/mapper/notificationCount/mongo.go new file mode 100644 index 0000000..a67f3dc --- /dev/null +++ b/biz/infrastructure/mapper/notificationCount/mongo.go @@ -0,0 +1,68 @@ +package notification + +import ( + "context" + "errors" + "github.com/CloudStriver/cloudmind-system/biz/infrastructure/consts" + "github.com/zeromicro/go-zero/core/stores/monc" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" + + "github.com/CloudStriver/cloudmind-system/biz/infrastructure/config" +) + +const ( + CollectionName = "notificationCount" + NotificationCountKey = "cache:NotificationCount:" +) + +var _ INotificationCountMongoMapper = (*MongoMapper)(nil) + +type ( + INotificationCountMongoMapper interface { + GetNotificationCount(ctx context.Context, userId string) (int64, error) + UpdateNotificationCount(ctx context.Context, data *NotificationCount) error + CreateNotificationCount(ctx context.Context, data *NotificationCount) error + } + NotificationCount struct { + ID primitive.ObjectID `bson:"_id,omitempty" json:"_id,omitempty"` + Read int64 `bson:"read,omitempty" json:"read,omitempty"` + } + MongoMapper struct { + conn *monc.Model + } +) + +func (m MongoMapper) CreateNotificationCount(ctx context.Context, data *NotificationCount) error { + key := NotificationCountKey + data.ID.Hex() + _, err := m.conn.InsertOne(ctx, key, data) + return err +} + +func (m MongoMapper) GetNotificationCount(ctx context.Context, userId string) (int64, error) { + key := NotificationCountKey + userId + var data *NotificationCount + uid, _ := primitive.ObjectIDFromHex(userId) + err := m.conn.FindOne(ctx, key, &data, bson.M{"_id": uid}) + switch { + case errors.Is(err, monc.ErrNotFound): + return 0, consts.ErrNotFound + case err == nil: + return data.Read, err + default: + return 0, err + } +} + +func (m MongoMapper) UpdateNotificationCount(ctx context.Context, data *NotificationCount) error { + key := NotificationCountKey + data.ID.Hex() + _, err := m.conn.UpdateOne(ctx, key, bson.M{consts.ID: data.ID}, bson.M{"$set": data}) + return err +} + +func NewNotificationCountModel(config *config.Config) INotificationCountMongoMapper { + conn := monc.MustNewModel(config.Mongo.URL, config.Mongo.DB, CollectionName, config.CacheConf) + return &MongoMapper{ + conn: conn, + } +} diff --git a/go.mod b/go.mod index e6d10df..a9bb341 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.20 require ( github.com/CloudStriver/go-pkg v0.0.0-20240117111745-b4ba57a38f44 - github.com/CloudStriver/service-idl-gen-go v0.0.0-20240312020955-691e500d8edb + github.com/CloudStriver/service-idl-gen-go v0.0.0-20240316123600-5ec7ab682e84 github.com/cloudwego/kitex v0.8.0 github.com/google/wire v0.5.0 github.com/kitex-contrib/obs-opentelemetry v0.2.5 diff --git a/go.sum b/go.sum index da48dc6..60f24ee 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,7 @@ github.com/CloudStriver/service-idl-gen-go v0.0.0-20240312020023-d19062f3547f h1 github.com/CloudStriver/service-idl-gen-go v0.0.0-20240312020023-d19062f3547f/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= github.com/CloudStriver/service-idl-gen-go v0.0.0-20240312020955-691e500d8edb h1:YRJbAIyNLuWVDqCa9/4z4Qf5lfEA413o4B66DgHaIA4= github.com/CloudStriver/service-idl-gen-go v0.0.0-20240312020955-691e500d8edb/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= +github.com/CloudStriver/service-idl-gen-go v0.0.0-20240316123600-5ec7ab682e84/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= github.com/ajstarks/deck v0.0.0-20200831202436-30c9fc6549a9/go.mod h1:JynElWSGnm/4RlzPXRlREEwqTHAN3T56Bv2ITsFT3gY= github.com/ajstarks/deck/generate v0.0.0-20210309230005-c3f852c02e19/go.mod h1:T13YZdzov6OU0A1+RfKZiZN9ca6VeKdBdyDV+BY97Tk= github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw= diff --git a/provider/provider.go b/provider/provider.go index 52b6ffd..d96b496 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -1,6 +1,7 @@ package provider import ( + notificationcountmapper "github.com/CloudStriver/cloudmind-system/biz/infrastructure/mapper/notificationCount" slidermapper "github.com/CloudStriver/cloudmind-system/biz/infrastructure/mapper/slider" "github.com/CloudStriver/cloudmind-system/biz/infrastructure/store/redis" "github.com/google/wire" @@ -27,5 +28,6 @@ var InfrastructureSet = wire.NewSet( var MapperSet = wire.NewSet( notificationmapper.NewNotificationModel, + notificationcountmapper.NewNotificationCountModel, slidermapper.NewSliderModel, ) diff --git a/provider/wire_gen.go b/provider/wire_gen.go index aa5f8a9..722880d 100644 --- a/provider/wire_gen.go +++ b/provider/wire_gen.go @@ -11,6 +11,7 @@ import ( "github.com/CloudStriver/cloudmind-system/biz/application/service" "github.com/CloudStriver/cloudmind-system/biz/infrastructure/config" "github.com/CloudStriver/cloudmind-system/biz/infrastructure/mapper/notification" + notification2 "github.com/CloudStriver/cloudmind-system/biz/infrastructure/mapper/notificationCount" "github.com/CloudStriver/cloudmind-system/biz/infrastructure/mapper/slider" "github.com/CloudStriver/cloudmind-system/biz/infrastructure/store/redis" ) @@ -23,12 +24,14 @@ func NewSystemServerImpl() (*adaptor.SystemServerImpl, error) { return nil, err } iNotificationMongoMapper := notification.NewNotificationModel(configConfig) + iNotificationCountMongoMapper := notification2.NewNotificationCountModel(configConfig) iSliderMongoMapper := slider.NewSliderModel(configConfig) redisRedis := redis.NewRedis(configConfig) systemServiceImpl := &service.SystemServiceImpl{ - NotificationMongoMapper: iNotificationMongoMapper, - SliderMongoMapper: iSliderMongoMapper, - Redis: redisRedis, + NotificationMongoMapper: iNotificationMongoMapper, + NotificationCountMongoMapper: iNotificationCountMongoMapper, + SliderMongoMapper: iSliderMongoMapper, + Redis: redisRedis, } systemServerImpl := &adaptor.SystemServerImpl{ Config: configConfig,