From baa5125afccc592e610929886f9eca7caa0b38be Mon Sep 17 00:00:00 2001 From: Lansong <2095621390@qq.com> Date: Tue, 12 Mar 2024 10:11:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=AE=9E=E7=8E=B0=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- biz/adaptor/server.go | 7 -- biz/application/service/system.go | 63 ++--------------- .../mapper/notification/filter.go | 13 +++- .../mapper/notificationCount/mongo.go | 68 ------------------- go.mod | 6 +- go.sum | 8 +-- provider/provider.go | 2 - provider/wire_gen.go | 9 +-- 8 files changed, 27 insertions(+), 149 deletions(-) delete mode 100644 biz/infrastructure/mapper/notificationCount/mongo.go diff --git a/biz/adaptor/server.go b/biz/adaptor/server.go index e7a3a7f..3edd1ee 100644 --- a/biz/adaptor/server.go +++ b/biz/adaptor/server.go @@ -13,16 +13,9 @@ type SystemServerImpl struct { SystemService service.SystemService } -func (s *SystemServerImpl) InsertNotificationCount(ctx context.Context, req *system.InsertNotificationCountReq) (res *system.InsertNotificationCountResp, err error) { - return s.SystemService.InsertNotificationCount(ctx, req) -} - // func (s *SystemServerImpl) UpdateNotifications(ctx context.Context, req *system.UpdateNotificationsReq) (resp *system.UpdateNotificationsResp, err error) { // return s.SystemService.UpdateNotifications(ctx, req) // } -func (s *SystemServerImpl) ReadNotifications(ctx context.Context, req *system.ReadNotificationsReq) (resp *system.ReadNotificationsResp, err error) { - return s.SystemService.ReadNotifications(ctx, req) -} func (s *SystemServerImpl) GetSliders(ctx context.Context, req *system.GetSlidersReq) (resp *system.GetSlidersResp, err error) { return s.SystemService.GetSliders(ctx, req) diff --git a/biz/application/service/system.go b/biz/application/service/system.go index 9eda710..a3b585d 100644 --- a/biz/application/service/system.go +++ b/biz/application/service/system.go @@ -2,11 +2,9 @@ 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" @@ -15,7 +13,6 @@ import ( "github.com/samber/lo" "github.com/zeromicro/go-zero/core/stores/redis" "go.mongodb.org/mongo-driver/bson/primitive" - "strconv" ) type SystemService interface { @@ -26,38 +23,12 @@ 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) - ReadNotifications(ctx context.Context, req *gensystem.ReadNotificationsReq) (resp *gensystem.ReadNotificationsResp, err error) - InsertNotificationCount(ctx context.Context, req *gensystem.InsertNotificationCountReq) (resp *gensystem.InsertNotificationCountResp, err error) - //UpdateNotifications(ctx context.Context, req *gensystem.UpdateNotificationsReq) (resp *gensystem.UpdateNotificationsResp, err error) } type SystemServiceImpl struct { - NotificationMongoMapper notificationmapper.INotificationMongoMapper - NotificationCountMongoMapper notificationcountmapper.INotificationCountMongoMapper - SliderMongoMapper slidermapper.ISliderMongoMapper - Redis *redis.Redis -} - -func (s *SystemServiceImpl) InsertNotificationCount(ctx context.Context, req *gensystem.InsertNotificationCountReq) (resp *gensystem.InsertNotificationCountResp, err error) { - uid, _ := primitive.ObjectIDFromHex(req.UserId) - if err = s.NotificationCountMongoMapper.InsertCount(ctx, ¬ificationcountmapper.NotificationCount{ - ID: uid, - Sum: 0, - Read: 0, - }); err != nil { - return resp, err - } - return resp, nil -} -func (s *SystemServiceImpl) ReadNotifications(ctx context.Context, req *gensystem.ReadNotificationsReq) (resp *gensystem.ReadNotificationsResp, err error) { - uid, _ := primitive.ObjectIDFromHex(req.UserId) - if err = s.NotificationCountMongoMapper.AddCount(ctx, ¬ificationcountmapper.NotificationCount{ - ID: uid, - Read: req.ReadCount, - }); err != nil { - return resp, err - } - return resp, nil + NotificationMongoMapper notificationmapper.INotificationMongoMapper + SliderMongoMapper slidermapper.ISliderMongoMapper + Redis *redis.Redis } func (s *SystemServiceImpl) DeleteSlider(ctx context.Context, req *gensystem.DeleteSliderReq) (resp *gensystem.DeleteSliderResp, err error) { @@ -137,22 +108,15 @@ func (s *SystemServiceImpl) GetNotifications(ctx context.Context, req *gensystem } func (s *SystemServiceImpl) GetNotificationCount(ctx context.Context, req *gensystem.GetNotificationCountReq) (resp *gensystem.GetNotificationCountResp, err error) { - - userCount, err := s.NotificationCountMongoMapper.GetCount(ctx, req.UserId) + cnt, err := s.NotificationMongoMapper.Count(ctx, ¬ificationmapper.FilterOptions{ + OnlyUserIds: []string{consts.NotificationSystemKey, req.UserId}, + }) if err != nil { - fmt.Println(err) return resp, err } - system, err := s.Redis.GetCtx(ctx, fmt.Sprintf("%s:%s", consts.NotificationCount, consts.NotificationSystemKey)) - if err != nil { - return resp, err - } - - systemCount, _ := strconv.ParseInt(system, 10, 64) - return &gensystem.GetNotificationCountResp{ - Total: systemCount + userCount.Sum - userCount.Read, + Total: cnt, }, nil } @@ -168,19 +132,6 @@ func (s *SystemServiceImpl) CreateNotifications(ctx context.Context, req *gensys return resp, err } - if req.TargetUserId == consts.NotificationSystemKey { - if _, err = s.Redis.IncrCtx(ctx, fmt.Sprintf("%s:%s", consts.NotificationCount, consts.NotificationSystemKey)); err != nil { - return resp, err - } - } else { - uid, _ := primitive.ObjectIDFromHex(req.TargetUserId) - if err = s.NotificationCountMongoMapper.AddCount(ctx, ¬ificationcountmapper.NotificationCount{ - ID: uid, - Sum: 1, - }); err != nil { - return resp, err - } - } return resp, nil } diff --git a/biz/infrastructure/mapper/notification/filter.go b/biz/infrastructure/mapper/notification/filter.go index 7c522c2..33b96c8 100644 --- a/biz/infrastructure/mapper/notification/filter.go +++ b/biz/infrastructure/mapper/notification/filter.go @@ -6,8 +6,9 @@ import ( ) type FilterOptions struct { - OnlyUserId *string - OnlyType *int64 + OnlyUserId *string + OnlyUserIds []string + OnlyType *int64 } type MongoFilter struct { @@ -39,3 +40,11 @@ func (f *MongoFilter) CheckOnlyType() { f.m[consts.Type] = *f.OnlyType } } + +func (f *MongoFilter) CheckOnlyUserIds() { + if f.OnlyUserIds != nil { + f.m[consts.TargetUserId] = bson.M{ + "in": f.OnlyUserIds, + } + } +} diff --git a/biz/infrastructure/mapper/notificationCount/mongo.go b/biz/infrastructure/mapper/notificationCount/mongo.go deleted file mode 100644 index 0987207..0000000 --- a/biz/infrastructure/mapper/notificationCount/mongo.go +++ /dev/null @@ -1,68 +0,0 @@ -package notification - -import ( - "context" - "github.com/CloudStriver/cloudmind-system/biz/infrastructure/config" - "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" -) - -const ( - CollectionName = "notificationCount" - NotificationCountKey = "NotificationCount" -) - -var _ INotificationCountMongoMapper = (*MongoMapper)(nil) - -type ( - INotificationCountMongoMapper interface { - AddCount(ctx context.Context, notification *NotificationCount) error - GetCount(ctx context.Context, userId string) (*NotificationCount, error) - InsertCount(ctx context.Context, notification *NotificationCount) error - } - NotificationCount struct { - ID primitive.ObjectID `bson:"_id,omitempty" json:"_id,omitempty"` - Sum int64 `bson:"sum" json:"sum"` - Read int64 `bson:"read" json:"read"` - } - MongoMapper struct { - conn *monc.Model - } -) - -func (m MongoMapper) GetCount(ctx context.Context, userId string) (*NotificationCount, error) { - var data *NotificationCount - uid, _ := primitive.ObjectIDFromHex(userId) - key := NotificationCountKey + userId - if err := m.conn.FindOne(ctx, key, &data, bson.M{consts.ID: uid}); err != nil { - return nil, err - } - return data, nil -} - -func (m MongoMapper) AddCount(ctx context.Context, notification *NotificationCount) error { - key := NotificationCountKey + notification.ID.Hex() - if _, err := m.conn.UpdateOne(ctx, key, bson.M{consts.ID: notification.ID}, bson.M{"$inc": bson.M{ - consts.Sum: notification.Sum, - consts.Read: notification.Read, - }}); err != nil { - return err - } - return nil -} - -func (m MongoMapper) InsertCount(ctx context.Context, notification *NotificationCount) error { - key := NotificationCountKey + notification.ID.Hex() - if _, err := m.conn.InsertOne(ctx, key, notification); err != nil { - return err - } - return nil -} -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 b58315d..e6d10df 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-20240312013508-fed7c5ed3e2c + github.com/CloudStriver/service-idl-gen-go v0.0.0-20240312020955-691e500d8edb github.com/cloudwego/kitex v0.8.0 github.com/google/wire v0.5.0 github.com/kitex-contrib/obs-opentelemetry v0.2.5 @@ -14,9 +14,7 @@ require ( google.golang.org/grpc v1.61.0 ) -//replace ( -// github.com/CloudStriver/service-idl-gen-go => ../service-idl-gen-go -//) +//replace github.com/CloudStriver/service-idl-gen-go => ../service-idl-gen-go require ( github.com/apache/thrift v0.16.0 // indirect diff --git a/go.sum b/go.sum index b37b765..da48dc6 100644 --- a/go.sum +++ b/go.sum @@ -6,10 +6,10 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/CloudStriver/go-pkg v0.0.0-20240117111745-b4ba57a38f44 h1:7exnHSz2LCeghuVDVD8IS+JaPB/bxDJWFEL7xH5L2Tg= github.com/CloudStriver/go-pkg v0.0.0-20240117111745-b4ba57a38f44/go.mod h1:SsAxWs5EIcaDE/0e5buoFOWsM4lTvFZhySkV68+RT3g= -github.com/CloudStriver/service-idl-gen-go v0.0.0-20240312012247-c0e2c8143766 h1:rhL+eapoLlEERFy31AMPVK7mhoMCyHeaKT00Fob50u4= -github.com/CloudStriver/service-idl-gen-go v0.0.0-20240312012247-c0e2c8143766/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= -github.com/CloudStriver/service-idl-gen-go v0.0.0-20240312013508-fed7c5ed3e2c h1:xTv+HbCgCNjoXpPlPtYzQuzGXDaWcN/g/ncWmL2OuHU= -github.com/CloudStriver/service-idl-gen-go v0.0.0-20240312013508-fed7c5ed3e2c/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= +github.com/CloudStriver/service-idl-gen-go v0.0.0-20240312020023-d19062f3547f h1:pJDpY40HnUruaQcpSokQwHucExnzYUHTFxDRQXI56To= +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/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 d96b496..52b6ffd 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -1,7 +1,6 @@ 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" @@ -28,6 +27,5 @@ 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 722880d..aa5f8a9 100644 --- a/provider/wire_gen.go +++ b/provider/wire_gen.go @@ -11,7 +11,6 @@ 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" ) @@ -24,14 +23,12 @@ 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, - NotificationCountMongoMapper: iNotificationCountMongoMapper, - SliderMongoMapper: iSliderMongoMapper, - Redis: redisRedis, + NotificationMongoMapper: iNotificationMongoMapper, + SliderMongoMapper: iSliderMongoMapper, + Redis: redisRedis, } systemServerImpl := &adaptor.SystemServerImpl{ Config: configConfig,