diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 35410ca..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# 默认忽略的文件 -/shelf/ -/workspace.xml -# 基于编辑器的 HTTP 客户端请求 -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/cloudmind-system.iml b/.idea/cloudmind-system.iml deleted file mode 100644 index 5e764c4..0000000 --- a/.idea/cloudmind-system.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 108cae1..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/biz/adaptor/server.go b/biz/adaptor/server.go index c68f6a8..260c80b 100644 --- a/biz/adaptor/server.go +++ b/biz/adaptor/server.go @@ -17,10 +17,6 @@ func (s *SystemServerImpl) UpdateNotifications(ctx context.Context, req *system. return s.SystemService.UpdateNotifications(ctx, req) } -func (s *SystemServerImpl) DeleteNotifications(ctx context.Context, req *system.DeleteNotificationsReq) (resp *system.DeleteNotificationsResp, err error) { - return s.SystemService.DeleteNotifications(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 6a88879..d9bcda6 100644 --- a/biz/application/service/system.go +++ b/biz/application/service/system.go @@ -14,14 +14,13 @@ import ( ) 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) DeleteSlider(ctx context.Context, req *gensystem.DeleteSliderReq) (resp *gensystem.DeleteSliderResp, err error) UpdateSlider(ctx context.Context, req *gensystem.UpdateSliderReq) (resp *gensystem.UpdateSliderResp, err error) CreateSlider(ctx context.Context, req *gensystem.CreateSliderReq) (resp *gensystem.CreateSliderResp, err error) GetSliders(ctx context.Context, req *gensystem.GetSlidersReq) (resp *gensystem.GetSlidersResp, err error) - DeleteNotifications(ctx context.Context, req *gensystem.DeleteNotificationsReq) (resp *gensystem.DeleteNotificationsResp, err error) + 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) UpdateNotifications(ctx context.Context, req *gensystem.UpdateNotificationsReq) (resp *gensystem.UpdateNotificationsResp, err error) } @@ -30,25 +29,12 @@ type SystemServiceImpl struct { SliderMongoMapper slidermapper.ISliderMongoMapper } -func (s *SystemServiceImpl) DeleteNotifications(ctx context.Context, req *gensystem.DeleteNotificationsReq) (resp *gensystem.DeleteNotificationsResp, err error) { - if err = s.NotificationMongoMapper.DeleteNotifications(ctx, ¬ificationmapper.FilterOptions{ - OnlyUserId: req.OnlyUserId, - OnlyType: req.OnlyType, - OnlyIsRead: req.OnlyIsRead, - OnlyNotificationIds: req.OnlyNotificationIds, - }); err != nil { - return resp, err - } - return resp, nil -} - func (s *SystemServiceImpl) UpdateNotifications(ctx context.Context, req *gensystem.UpdateNotificationsReq) (resp *gensystem.UpdateNotificationsResp, err error) { if err = s.NotificationMongoMapper.UpdateNotifications(ctx, ¬ificationmapper.FilterOptions{ - OnlyUserId: req.OnlyUserId, - OnlyType: req.OnlyType, - OnlyIsRead: req.OnlyIsRead, - OnlyNotificationIds: req.OnlyNotificationIds, - }, req.IsRead); err != nil { + OnlyUserId: lo.ToPtr(req.UserId), + OnlyType: req.OnlyType, + OnlyStatus: lo.ToPtr(int64(gensystem.NotificationStatus_NotRead)), + }); err != nil { return resp, err } return resp, nil @@ -113,10 +99,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, total, err := s.NotificationMongoMapper.GetNotificationsAndCount(ctx, ¬ificationmapper.FilterOptions{ - OnlyUserId: req.OnlyUserId, + notifications, err := s.NotificationMongoMapper.GetNotifications(ctx, ¬ificationmapper.FilterOptions{ + OnlyUserId: lo.ToPtr(req.UserId), OnlyType: req.OnlyType, - OnlyIsRead: req.OnlyIsRead, }, p, mongop.IdCursorType) if err != nil { return resp, err @@ -128,16 +113,15 @@ func (s *SystemServiceImpl) GetNotifications(ctx context.Context, req *gensystem if p.LastToken != nil { resp.Token = *p.LastToken } - resp.Total = total return resp, nil } func (s *SystemServiceImpl) GetNotificationCount(ctx context.Context, req *gensystem.GetNotificationCountReq) (resp *gensystem.GetNotificationCountResp, err error) { resp = new(gensystem.GetNotificationCountResp) if resp.Total, err = s.NotificationMongoMapper.Count(ctx, ¬ificationmapper.FilterOptions{ - OnlyUserId: req.OnlyUserId, + OnlyUserId: lo.ToPtr(req.UserId), OnlyType: req.OnlyType, - OnlyIsRead: req.OnlyIsRead, + OnlyStatus: lo.ToPtr(int64(gensystem.NotificationStatus_NotRead)), }); err != nil { return resp, err } @@ -145,10 +129,15 @@ func (s *SystemServiceImpl) GetNotificationCount(ctx context.Context, req *gensy } func (s *SystemServiceImpl) CreateNotifications(ctx context.Context, req *gensystem.CreateNotificationsReq) (resp *gensystem.CreateNotificationsResp, err error) { - notifications := lo.Map[*gensystem.NotificationInfo, *notificationmapper.Notification](req.Notifications, func(item *gensystem.NotificationInfo, _ int) *notificationmapper.Notification { - return convertor.NotificationInfoToNotificationMapper(item) - }) - if err = s.NotificationMongoMapper.InsertMany(ctx, notifications); err != nil { + if err = s.NotificationMongoMapper.InsertOne(ctx, ¬ificationmapper.Notification{ + TargetUserId: req.TargetUserId, + SourceUserId: req.SourceUserId, + SourceContentId: req.SourceContentId, + Type: req.Type, + TargetType: req.TargetType, + Text: req.Text, + Status: int64(gensystem.NotificationStatus_NotRead), + }); err != nil { return resp, err } return resp, nil diff --git a/biz/infrastructure/consts/field.go b/biz/infrastructure/consts/field.go index 9199a36..f154e2c 100644 --- a/biz/infrastructure/consts/field.go +++ b/biz/infrastructure/consts/field.go @@ -11,4 +11,5 @@ const ( ImageUrl = "imageUrl" LinkUrl = "linkUrl" IsPublic = "isPublic" + Status = "status" ) diff --git a/biz/infrastructure/convertor/convertor.go b/biz/infrastructure/convertor/convertor.go index 40e56b2..a6c3e31 100644 --- a/biz/infrastructure/convertor/convertor.go +++ b/biz/infrastructure/convertor/convertor.go @@ -6,18 +6,6 @@ import ( gensystem "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/system" ) -func NotificationInfoToNotificationMapper(in *gensystem.NotificationInfo) *notificationmapper.Notification { - return ¬ificationmapper.Notification{ - TargetUserId: in.TargetUserId, - SourceUserId: in.SourceUserId, - SourceContentId: in.SourceContentId, - Type: in.Type, - TargetType: in.TargetType, - Text: in.Text, - IsRead: in.IsRead, - } -} - func NotificationMapperToNotification(in *notificationmapper.Notification) *gensystem.Notification { return &gensystem.Notification{ NotificationId: in.ID.Hex(), @@ -28,7 +16,7 @@ func NotificationMapperToNotification(in *notificationmapper.Notification) *gens Type: in.Type, Text: in.Text, CreateTime: in.CreateAt.UnixMilli(), - IsRead: in.IsRead, + Status: in.Status, } } diff --git a/biz/infrastructure/mapper/notification/filter.go b/biz/infrastructure/mapper/notification/filter.go index 1195f65..6678de1 100644 --- a/biz/infrastructure/mapper/notification/filter.go +++ b/biz/infrastructure/mapper/notification/filter.go @@ -2,16 +2,13 @@ package notification import ( "github.com/CloudStriver/cloudmind-system/biz/infrastructure/consts" - "github.com/samber/lo" "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/primitive" ) type FilterOptions struct { - OnlyUserId *string - OnlyType *int64 - OnlyIsRead *bool - OnlyNotificationIds []string + OnlyUserId *string + OnlyType *int64 + OnlyStatus *int64 } type MongoFilter struct { @@ -30,20 +27,9 @@ func (f *MongoFilter) toBson() bson.M { f.CheckOnlyUserId() f.CheckOnlyType() f.CheckOnlyIsRead() - f.CheckOnlyNotificationIds() return f.m } -func (f *MongoFilter) CheckOnlyNotificationIds() { - if f.OnlyNotificationIds != nil { - f.m[consts.ID] = bson.M{"$in": lo.Map[string, primitive.ObjectID](f.OnlyNotificationIds, func(item string, index int) primitive.ObjectID { - oid, _ := primitive.ObjectIDFromHex(item) - return oid - }), - } - } -} - func (f *MongoFilter) CheckOnlyUserId() { if f.OnlyUserId != nil { f.m[consts.TargetUserId] = *f.OnlyUserId @@ -57,7 +43,7 @@ func (f *MongoFilter) CheckOnlyType() { } func (f *MongoFilter) CheckOnlyIsRead() { - if f.OnlyIsRead != nil { - f.m[consts.IsRead] = bson.M{"$exists": *f.OnlyIsRead} + if f.OnlyStatus != nil { + f.m[consts.Status] = *f.OnlyStatus } } diff --git a/biz/infrastructure/mapper/notification/mongo.go b/biz/infrastructure/mapper/notification/mongo.go index 19c7cbb..be6b82f 100644 --- a/biz/infrastructure/mapper/notification/mongo.go +++ b/biz/infrastructure/mapper/notification/mongo.go @@ -4,6 +4,7 @@ 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" @@ -25,11 +26,11 @@ var _ INotificationMongoMapper = (*MongoMapper)(nil) type ( INotificationMongoMapper interface { - GetNotifications(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Notification, int64, error) + 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, isRead bool) error + UpdateNotifications(ctx context.Context, fopts *FilterOptions) error DeleteNotifications(ctx context.Context, fopts *FilterOptions) error - InsertMany(ctx context.Context, data []*Notification) error + InsertOne(ctx context.Context, data *Notification) error GetNotificationsAndCount(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Notification, int64, error) } Notification struct { @@ -42,7 +43,7 @@ type ( Text string `bson:"text,omitempty" json:"text,omitempty"` CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"` UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"` - IsRead bool `bson:"isRead,omitempty" json:"isRead,omitempty"` + Status int64 `bson:"status,omitempty" json:"status,omitempty"` } MongoMapper struct { conn *monc.Model @@ -61,38 +62,17 @@ func (m *MongoMapper) GetNotificationsAndCount(ctx context.Context, fopts *Filte count int64 err1, err2 error ) - p := mongop.NewMongoPaginator(pagination.NewRawStore(sorter), popts) - - filter := MakeBsonFilter(fopts) - sort, err := p.MakeSortOptions(ctx, filter) - if err != nil { - return nil, 0, err - } - - if err = mr.Finish(func() error { - count, err1 = m.conn.CountDocuments(ctx, filter) + if err := mr.Finish(func() error { + count, err1 = m.Count(ctx, fopts) if err1 != nil { return err1 } return nil }, func() error { - if err2 = m.conn.Find(ctx, &data, filter, &options.FindOptions{ - Sort: sort, - Limit: popts.Limit, - Skip: popts.Offset, - }); err2 != nil { + data, err2 = m.GetNotifications(ctx, fopts, popts, sorter) + if err2 != nil { return err2 } - // 如果是反向查询,反转数据 - if *popts.Backward { - lo.Reverse(data) - } - if len(data) > 0 { - err2 = p.StoreCursor(ctx, data[0], data[len(data)-1]) - if err2 != nil { - return err2 - } - } return nil }); err != nil { return nil, 0, err @@ -100,26 +80,23 @@ func (m *MongoMapper) GetNotificationsAndCount(ctx context.Context, fopts *Filte return data, count, nil } -func (m *MongoMapper) InsertMany(ctx context.Context, datas []*Notification) error { - lo.ForEach(datas, func(data *Notification, _ int) { - if data.ID.IsZero() { - data.ID = primitive.NewObjectID() - } - data.CreateAt = time.Now() - data.UpdateAt = time.Now() - }) +func (m *MongoMapper) InsertOne(ctx context.Context, data *Notification) error { + if data.ID.IsZero() { + data.ID = primitive.NewObjectID() + } + data.CreateAt = time.Now() + data.UpdateAt = time.Now() - _, err := m.conn.InsertMany(ctx, lo.ToAnySlice(datas)) + _, err := m.conn.InsertOneNoCache(ctx, data) return err } -func (m *MongoMapper) GetNotifications(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Notification, int64, error) { +func (m *MongoMapper) GetNotifications(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Notification, error) { var data []*Notification p := mongop.NewMongoPaginator(pagination.NewRawStore(sorter), popts) - filter := MakeBsonFilter(fopts) sort, err := p.MakeSortOptions(ctx, filter) if err != nil { - return nil, 0, err + return nil, err } if err = m.conn.Find(ctx, &data, filter, &options.FindOptions{ @@ -127,33 +104,25 @@ func (m *MongoMapper) GetNotifications(ctx context.Context, fopts *FilterOptions Limit: popts.Limit, Skip: popts.Offset, }); err != nil { - return nil, 0, err + return nil, err } - // 如果是反向查询,反转数据 if *popts.Backward { - for i := 0; i < len(data)/2; i++ { - data[i], data[len(data)-i-1] = data[len(data)-i-1], data[i] - } + lo.Reverse(data) } if len(data) > 0 { err = p.StoreCursor(ctx, data[0], data[len(data)-1]) if err != nil { - return nil, 0, err + return nil, err } } - count, err := m.conn.CountDocuments(ctx, filter) - if err != nil { - return nil, 0, err - } - - return data, count, nil + return data, nil } -func (m *MongoMapper) UpdateNotifications(ctx context.Context, fopts *FilterOptions, isRead bool) error { +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.IsRead: isRead, consts.UpdateAt: time.Now()}}); err != nil { + 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 diff --git a/go.mod b/go.mod index 1209f49..220f6e0 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-20240207103956-4877398ec51e + github.com/CloudStriver/service-idl-gen-go v0.0.0-20240213134058-123dd3968487 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 8e7d20f..9f48534 100644 --- a/go.sum +++ b/go.sum @@ -14,6 +14,8 @@ github.com/CloudStriver/service-idl-gen-go v0.0.0-20240207015802-678257e52c14 h1 github.com/CloudStriver/service-idl-gen-go v0.0.0-20240207015802-678257e52c14/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= github.com/CloudStriver/service-idl-gen-go v0.0.0-20240207103956-4877398ec51e h1:nO3+GrijkLpFAdk+tt+n/R4nJzkeRYoMuXXEBk2LjV0= github.com/CloudStriver/service-idl-gen-go v0.0.0-20240207103956-4877398ec51e/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= +github.com/CloudStriver/service-idl-gen-go v0.0.0-20240213134058-123dd3968487 h1:aP0RpwK4Rh85H4IAs8AGeOqLePzGTr11QVlIjc6C14k= +github.com/CloudStriver/service-idl-gen-go v0.0.0-20240213134058-123dd3968487/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=