From bc9c9685672806554a4b3ad4cc4643b80a049608 Mon Sep 17 00:00:00 2001 From: Love-YeLin <1807209079@qq.com> Date: Sat, 13 Apr 2024 16:44:04 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=96=B0=E5=A2=9Epaltform=E8=AF=84?= =?UTF-8?q?=E8=AE=BA=E5=8C=BA=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 008ddd1..e5cbd0f 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.20 require ( github.com/CloudStriver/cloudmind-mq v1.0.12-0.20240406130428-3a00c4159388 github.com/CloudStriver/go-pkg v0.0.0-20240206060942-84060a3dd273 - github.com/CloudStriver/service-idl-gen-go v0.0.0-20240413082126-0c956fdadb0e + github.com/CloudStriver/service-idl-gen-go v0.0.0-20240413084243-a8d7e75059be github.com/bytedance/sonic v1.10.2 github.com/cloudwego/kitex v0.8.0 github.com/elastic/go-elasticsearch/v8 v8.11.1 diff --git a/go.sum b/go.sum index 801b2b4..97dfbef 100644 --- a/go.sum +++ b/go.sum @@ -389,6 +389,8 @@ github.com/CloudStriver/service-idl-gen-go v0.0.0-20240412074550-8ac73424809b h1 github.com/CloudStriver/service-idl-gen-go v0.0.0-20240412074550-8ac73424809b/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= github.com/CloudStriver/service-idl-gen-go v0.0.0-20240413082126-0c956fdadb0e h1:omzlXEIZrTLH+awCjmBpyn/Y0ZDa077zov9GQNBq8gA= github.com/CloudStriver/service-idl-gen-go v0.0.0-20240413082126-0c956fdadb0e/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= +github.com/CloudStriver/service-idl-gen-go v0.0.0-20240413084243-a8d7e75059be h1:HddeenNcxzNp1CCpxmFRxJlZjKEy8M//Clb7T38gef0= +github.com/CloudStriver/service-idl-gen-go v0.0.0-20240413084243-a8d7e75059be/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= From 54bc5a2ad11950f2fe0dbc7ab477c569f58c8ddf Mon Sep 17 00:00:00 2001 From: Love-YeLin <1807209079@qq.com> Date: Sun, 14 Apr 2024 13:57:10 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=96=B0=E5=A2=9Epaltform=E8=AF=84?= =?UTF-8?q?=E8=AE=BA=E5=8C=BA=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- biz/adaptor/server.go | 81 +++++++++++++++++++-- biz/application/service/comment.go | 25 +++---- biz/application/service/subject.go | 17 +---- biz/infrastructure/mapper/comment/filter.go | 2 +- biz/infrastructure/mapper/comment/mongo.go | 2 +- biz/infrastructure/mapper/subject/mongo.go | 6 +- go.mod | 2 +- 7 files changed, 94 insertions(+), 41 deletions(-) diff --git a/biz/adaptor/server.go b/biz/adaptor/server.go index c7edf14..3030fd4 100644 --- a/biz/adaptor/server.go +++ b/biz/adaptor/server.go @@ -4,7 +4,6 @@ import ( "context" "github.com/CloudStriver/platform/biz/application/service" "github.com/CloudStriver/platform/biz/infrastructure/config" - "github.com/CloudStriver/platform/biz/infrastructure/consts" "github.com/CloudStriver/service-idl-gen-go/kitex_gen/platform" "github.com/zeromicro/go-zero/core/mr" ) @@ -69,11 +68,36 @@ func (c *PlatformServerImpl) CreateComment(ctx context.Context, req *platform.Cr if res, err = c.CommentService.CreateComment(ctx, req); err != nil { return res, err } + + var getSubjectResp *platform.GetCommentSubjectResp + if getSubjectResp, err = c.SubjectService.GetCommentSubject(ctx, &platform.GetCommentSubjectReq{SubjectId: req.SubjectId}); err != nil { + return res, err + } + _ = mr.Finish(func() error { - c.CommentService.UpdateCount(ctx, req.RootId, req.SubjectId, req.FatherId, consts.Increment) + if req.RootId != req.SubjectId { + if req.FatherId != req.SubjectId { + var getRootComment *platform.GetCommentResp + if getRootComment, err = c.CommentService.GetComment(ctx, &platform.GetCommentReq{CommentId: req.RootId}); err != nil { + return err + } + // 二级评论 + 三级评论 + c.CommentService.UpdateCount(ctx, req.RootId, getRootComment.Count-1) + } + } return nil }, func() error { - c.SubjectService.UpdateCount(ctx, req.RootId, req.SubjectId, req.FatherId, consts.Increment) + if req.RootId == req.SubjectId { + // 一级评论 + if req.FatherId == req.SubjectId { + c.SubjectService.UpdateCount(ctx, req.SubjectId, getSubjectResp.RootCount+1, getSubjectResp.AllCount+1) + } + } else { + // 二级评论 + 三级评论 + if req.FatherId != req.SubjectId { + c.SubjectService.UpdateCount(ctx, req.SubjectId, getSubjectResp.RootCount, getSubjectResp.AllCount+1) + } + } return nil }) return res, nil @@ -84,18 +108,59 @@ func (c *PlatformServerImpl) UpdateComment(ctx context.Context, req *platform.Up } func (c *PlatformServerImpl) DeleteComment(ctx context.Context, req *platform.DeleteCommentReq) (res *platform.DeleteCommentResp, err error) { - var data *platform.GetCommentResp - if data, err = c.CommentService.GetComment(ctx, &platform.GetCommentReq{CommentId: req.CommentId}); err != nil { + var ( + getCommentResp *platform.GetCommentResp + getSubjectResp *platform.GetCommentSubjectResp + ) + + if getCommentResp, err = c.CommentService.GetComment(ctx, &platform.GetCommentReq{CommentId: req.CommentId}); err != nil { return res, err } - if res, err = c.CommentService.DeleteComment(ctx, req); err != nil { + + if getSubjectResp, err = c.SubjectService.GetCommentSubject(ctx, &platform.GetCommentSubjectReq{SubjectId: getCommentResp.SubjectId}); err != nil { return res, err } + + if getCommentResp.RootId == getCommentResp.SubjectId { + // 一级评论 + if getCommentResp.FatherId == getCommentResp.SubjectId { + if res, err = c.CommentService.DeleteComment(ctx, req.CommentId, true); err != nil { + return res, err + } + } + } else { + // 二级评论 + 三级评论 + if getCommentResp.FatherId != getCommentResp.SubjectId { + if res, err = c.CommentService.DeleteComment(ctx, req.CommentId, false); err != nil { + return res, err + } + } + } + _ = mr.Finish(func() error { - c.CommentService.UpdateCount(ctx, data.RootId, data.SubjectId, data.FatherId, consts.Decrement) + if getCommentResp.RootId != getCommentResp.SubjectId { + if getCommentResp.FatherId != getCommentResp.SubjectId { + var getRootComment *platform.GetCommentResp + if getRootComment, err = c.CommentService.GetComment(ctx, &platform.GetCommentReq{CommentId: getCommentResp.RootId}); err != nil { + return err + } + // 二级评论 + 三级评论 + c.CommentService.UpdateCount(ctx, getCommentResp.RootId, getRootComment.Count-1) + } + } return nil }, func() error { - c.SubjectService.UpdateCount(ctx, data.RootId, data.SubjectId, data.FatherId, consts.Decrement) + if getCommentResp.RootId == getCommentResp.SubjectId { + // 一级评论 + if getCommentResp.FatherId == getCommentResp.SubjectId { + c.SubjectService.UpdateCount(ctx, getCommentResp.SubjectId, getSubjectResp.RootCount-1, getSubjectResp.AllCount-getCommentResp.Count-1) + } + } else { + // 二级评论 + 三级评论 + if getCommentResp.FatherId != getCommentResp.SubjectId { + c.SubjectService.UpdateCount(ctx, getCommentResp.SubjectId, getSubjectResp.RootCount, getSubjectResp.AllCount-1) + } + } return nil }) return res, nil diff --git a/biz/application/service/comment.go b/biz/application/service/comment.go index b21af79..8ff56f7 100644 --- a/biz/application/service/comment.go +++ b/biz/application/service/comment.go @@ -17,13 +17,13 @@ import ( ) type ICommentService interface { - UpdateCount(ctx context.Context, rootId, subjectId, fatherId string, count int64) + UpdateCount(ctx context.Context, rootId string, count int64) GetComment(ctx context.Context, req *platform.GetCommentReq) (resp *platform.GetCommentResp, err error) GetCommentList(ctx context.Context, req *platform.GetCommentListReq) (resp *platform.GetCommentListResp, err error) GetCommentBlocks(ctx context.Context, req *platform.GetCommentBlocksReq) (resp *platform.GetCommentBlocksResp, err error) CreateComment(ctx context.Context, req *platform.CreateCommentReq) (resp *platform.CreateCommentResp, err error) UpdateComment(ctx context.Context, req *platform.UpdateCommentReq) (resp *platform.UpdateCommentResp, err error) - DeleteComment(ctx context.Context, req *platform.DeleteCommentReq) (resp *platform.DeleteCommentResp, err error) + DeleteComment(ctx context.Context, commentId string, level bool) (resp *platform.DeleteCommentResp, err error) DeleteCommentByIds(ctx context.Context, req *platform.DeleteCommentByIdsReq) (resp *platform.DeleteCommentByIdsResp, err error) SetCommentAttrs(ctx context.Context, req *platform.SetCommentAttrsReq, res *platform.GetCommentSubjectResp) (resp *platform.SetCommentAttrsResp, err error) } @@ -181,13 +181,8 @@ func (s *CommentService) CreateComment(ctx context.Context, req *platform.Create return resp, nil } -func (s *CommentService) UpdateCount(ctx context.Context, rootId, subjectId, fatherId string, count int64) { - if rootId != subjectId { - if fatherId != subjectId { - // 二级评论 + 三级评论 - s.CommentMongoMapper.UpdateCount(ctx, rootId, count) - } - } +func (s *CommentService) UpdateCount(ctx context.Context, rootId string, count int64) { + s.CommentMongoMapper.UpdateCount(ctx, rootId, count) } func (s *CommentService) UpdateComment(ctx context.Context, req *platform.UpdateCommentReq) (resp *platform.UpdateCommentResp, err error) { @@ -208,11 +203,15 @@ func (s *CommentService) UpdateComment(ctx context.Context, req *platform.Update return resp, nil } -func (s *CommentService) DeleteComment(ctx context.Context, req *platform.DeleteCommentReq) (resp *platform.DeleteCommentResp, err error) { +func (s *CommentService) DeleteComment(ctx context.Context, commentId string, level bool) (resp *platform.DeleteCommentResp, err error) { resp = new(platform.DeleteCommentResp) - if _, err = s.CommentMongoMapper.Delete(ctx, req.CommentId); err != nil { - log.CtxError(ctx, "删除评论 失败[%v]\n", err) - return resp, err + if level { + + } else { + if _, err = s.CommentMongoMapper.Delete(ctx, commentId); err != nil { + log.CtxError(ctx, "删除评论 失败[%v]\n", err) + return resp, err + } } return resp, nil } diff --git a/biz/application/service/subject.go b/biz/application/service/subject.go index a96f647..8bbed4f 100644 --- a/biz/application/service/subject.go +++ b/biz/application/service/subject.go @@ -5,7 +5,6 @@ import ( "github.com/CloudStriver/cloudmind-mq/app/util/message" "github.com/CloudStriver/go-pkg/utils/pconvertor" "github.com/CloudStriver/go-pkg/utils/util/log" - "github.com/CloudStriver/platform/biz/infrastructure/consts" "github.com/CloudStriver/platform/biz/infrastructure/kq" subjectMapper "github.com/CloudStriver/platform/biz/infrastructure/mapper/subject" gencontent "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/content" @@ -17,7 +16,7 @@ import ( ) type ISubjectService interface { - UpdateCount(ctx context.Context, rootId, subjectId, fatherId string, count int64) + UpdateCount(ctx context.Context, subjectId string, rootCount, allCount int64) GetCommentSubject(ctx context.Context, req *platform.GetCommentSubjectReq) (resp *platform.GetCommentSubjectResp, err error) CreateCommentSubject(ctx context.Context, req *platform.CreateCommentSubjectReq) (resp *platform.CreateCommentSubjectResp, err error) UpdateCommentSubject(ctx context.Context, req *platform.UpdateCommentSubjectReq) (resp *platform.UpdateCommentSubjectResp, err error) @@ -74,18 +73,8 @@ func (s *SubjectService) CreateCommentSubject(ctx context.Context, req *platform return resp, nil } -func (s *SubjectService) UpdateCount(ctx context.Context, rootId, subjectId, fatherId string, count int64) { - if rootId == subjectId { - // 一级评论 - if fatherId == subjectId { - s.SubjectMongoMapper.UpdateCount(ctx, subjectId, count, count) - } - } else { - // 二级评论 + 三级评论 - if fatherId != subjectId { - s.SubjectMongoMapper.UpdateCount(ctx, subjectId, count, consts.InitNumber) - } - } +func (s *SubjectService) UpdateCount(ctx context.Context, subjectId string, rootCount, allCount int64) { + s.SubjectMongoMapper.UpdateCount(ctx, subjectId, rootCount, allCount) } func (s *SubjectService) UpdateCommentSubject(ctx context.Context, req *platform.UpdateCommentSubjectReq) (resp *platform.UpdateCommentSubjectResp, err error) { diff --git a/biz/infrastructure/mapper/comment/filter.go b/biz/infrastructure/mapper/comment/filter.go index 9d3c392..792e9be 100644 --- a/biz/infrastructure/mapper/comment/filter.go +++ b/biz/infrastructure/mapper/comment/filter.go @@ -56,7 +56,7 @@ func (f *MongoFilter) CheckOnlyUserId() { func (f *MongoFilter) CheckOnlyRootId() { if f.OnlyRootId != nil { - f.m[consts.FatherId] = *f.OnlyRootId + f.m[consts.RootId] = *f.OnlyRootId } } diff --git a/biz/infrastructure/mapper/comment/mongo.go b/biz/infrastructure/mapper/comment/mongo.go index 28b1ab2..b0ac4fb 100644 --- a/biz/infrastructure/mapper/comment/mongo.go +++ b/biz/infrastructure/mapper/comment/mongo.go @@ -128,7 +128,7 @@ func (m *MongoMapper) UpdateCount(ctx context.Context, id string, count int64) { oid, _ := primitive.ObjectIDFromHex(id) key := prefixCommentCacheKey + id - _, _ = m.conn.UpdateOne(ctx, key, bson.M{consts.ID: oid}, bson.M{"$inc": bson.M{consts.Count: count}}) + _, _ = m.conn.UpdateOne(ctx, key, bson.M{consts.ID: oid}, bson.M{"$set": bson.M{consts.Count: lo.ToPtr(count)}}) } func (m *MongoMapper) Delete(ctx context.Context, id string) (int64, error) { diff --git a/biz/infrastructure/mapper/subject/mongo.go b/biz/infrastructure/mapper/subject/mongo.go index fd78f2e..ecee0a1 100644 --- a/biz/infrastructure/mapper/subject/mongo.go +++ b/biz/infrastructure/mapper/subject/mongo.go @@ -25,7 +25,7 @@ type ( Insert(ctx context.Context, data *Subject) (string, error) FindOne(ctx context.Context, id string) (*Subject, error) Update(ctx context.Context, data *Subject) (*mongo.UpdateResult, error) - UpdateCount(ctx context.Context, id string, allCount, rootCount int64) + UpdateCount(ctx context.Context, id string, rootCount, allCount int64) Delete(ctx context.Context, id string) (int64, error) GetConn() *monc.Model StartClient() *mongo.Client @@ -105,14 +105,14 @@ func (m *MongoMapper) Update(ctx context.Context, data *Subject) (*mongo.UpdateR return res, err } -func (m *MongoMapper) UpdateCount(ctx context.Context, id string, allCount, rootCount int64) { +func (m *MongoMapper) UpdateCount(ctx context.Context, id string, rootCount, allCount int64) { tracer := otel.GetTracerProvider().Tracer(trace.TraceName) _, span := tracer.Start(ctx, "mongo.UpdateCount", oteltrace.WithSpanKind(oteltrace.SpanKindConsumer)) defer span.End() oid, _ := primitive.ObjectIDFromHex(id) key := prefixSubjectCacheKey + id - _, _ = m.conn.UpdateOne(ctx, key, bson.M{consts.ID: oid}, bson.M{"$inc": bson.M{consts.RootCount: rootCount, consts.AllCount: allCount}, "$set": bson.M{consts.UpdateAt: time.Now()}}) + _, _ = m.conn.UpdateOne(ctx, key, bson.M{consts.ID: oid}, bson.M{"$set": bson.M{consts.RootCount: rootCount, consts.AllCount: allCount, consts.UpdateAt: time.Now()}}) } func (m *MongoMapper) Delete(ctx context.Context, id string) (int64, error) { diff --git a/go.mod b/go.mod index e5cbd0f..9e7a7ae 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,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 //replace github.com/CloudStriver/cloudmind-mq => ../cloudmind-mq require ( From e06345f1853926040ba6d705bdfb0efe1793bdc0 Mon Sep 17 00:00:00 2001 From: Love-YeLin <1807209079@qq.com> Date: Sun, 14 Apr 2024 16:01:55 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=96=B0=E5=A2=9Epaltform=E8=AF=84?= =?UTF-8?q?=E8=AE=BA=E5=8C=BA=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- biz/adaptor/server.go | 6 +- biz/application/service/comment.go | 78 +++++++++++++++++++-- biz/application/service/relation.go | 6 +- biz/application/service/subject.go | 30 ++++---- biz/infrastructure/convertor/convertor.go | 1 + biz/infrastructure/mapper/comment/filter.go | 3 +- biz/infrastructure/mapper/comment/mongo.go | 1 + biz/infrastructure/mapper/subject/mongo.go | 1 + provider/wire_gen.go | 11 +-- 9 files changed, 109 insertions(+), 28 deletions(-) diff --git a/biz/adaptor/server.go b/biz/adaptor/server.go index 3030fd4..2246d22 100644 --- a/biz/adaptor/server.go +++ b/biz/adaptor/server.go @@ -82,7 +82,7 @@ func (c *PlatformServerImpl) CreateComment(ctx context.Context, req *platform.Cr return err } // 二级评论 + 三级评论 - c.CommentService.UpdateCount(ctx, req.RootId, getRootComment.Count-1) + c.CommentService.UpdateCount(ctx, req.RootId, getRootComment.Count+1) } } return nil @@ -124,14 +124,14 @@ func (c *PlatformServerImpl) DeleteComment(ctx context.Context, req *platform.De if getCommentResp.RootId == getCommentResp.SubjectId { // 一级评论 if getCommentResp.FatherId == getCommentResp.SubjectId { - if res, err = c.CommentService.DeleteComment(ctx, req.CommentId, true); err != nil { + if res, err = c.CommentService.DeleteComment(ctx, req.CommentId, getCommentResp.Type, true); err != nil { return res, err } } } else { // 二级评论 + 三级评论 if getCommentResp.FatherId != getCommentResp.SubjectId { - if res, err = c.CommentService.DeleteComment(ctx, req.CommentId, false); err != nil { + if res, err = c.CommentService.DeleteComment(ctx, req.CommentId, getCommentResp.Type, false); err != nil { return res, err } } diff --git a/biz/application/service/comment.go b/biz/application/service/comment.go index 8ff56f7..b08bfc4 100644 --- a/biz/application/service/comment.go +++ b/biz/application/service/comment.go @@ -2,15 +2,21 @@ package service import ( "context" + "github.com/CloudStriver/cloudmind-mq/app/util/message" "github.com/CloudStriver/go-pkg/utils/pagination" + "github.com/CloudStriver/go-pkg/utils/pconvertor" "github.com/CloudStriver/go-pkg/utils/util/log" + "github.com/CloudStriver/platform/biz/infrastructure/consts" "github.com/CloudStriver/platform/biz/infrastructure/convertor" + "github.com/CloudStriver/platform/biz/infrastructure/kq" commentMapper "github.com/CloudStriver/platform/biz/infrastructure/mapper/comment" subjectMapper "github.com/CloudStriver/platform/biz/infrastructure/mapper/subject" "github.com/CloudStriver/platform/biz/infrastructure/sort" "github.com/CloudStriver/service-idl-gen-go/kitex_gen/platform" + "github.com/bytedance/sonic" "github.com/google/wire" "github.com/samber/lo" + "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "math" @@ -23,14 +29,15 @@ type ICommentService interface { GetCommentBlocks(ctx context.Context, req *platform.GetCommentBlocksReq) (resp *platform.GetCommentBlocksResp, err error) CreateComment(ctx context.Context, req *platform.CreateCommentReq) (resp *platform.CreateCommentResp, err error) UpdateComment(ctx context.Context, req *platform.UpdateCommentReq) (resp *platform.UpdateCommentResp, err error) - DeleteComment(ctx context.Context, commentId string, level bool) (resp *platform.DeleteCommentResp, err error) + DeleteComment(ctx context.Context, commentId string, commentType int64, level bool) (resp *platform.DeleteCommentResp, err error) DeleteCommentByIds(ctx context.Context, req *platform.DeleteCommentByIdsReq) (resp *platform.DeleteCommentByIdsResp, err error) SetCommentAttrs(ctx context.Context, req *platform.SetCommentAttrsReq, res *platform.GetCommentSubjectResp) (resp *platform.SetCommentAttrsResp, err error) } type CommentService struct { - CommentMongoMapper commentMapper.IMongoMapper - SubjectMongoMapper subjectMapper.IMongoMapper + CommentMongoMapper commentMapper.IMongoMapper + SubjectMongoMapper subjectMapper.IMongoMapper + DeleteCommentRelationKq *kq.DeleteCommentRelationKq } var CommentSet = wire.NewSet( @@ -67,6 +74,7 @@ func (s *CommentService) GetComment(ctx context.Context, req *platform.GetCommen AtUserId: data.AtUserId, Content: data.Content, Meta: data.Meta, + Type: data.Type, CreateTime: data.CreateAt.UnixMilli(), } return resp, nil @@ -174,6 +182,7 @@ func (s *CommentService) CreateComment(ctx context.Context, req *platform.Create Count: lo.ToPtr(int64(0)), State: int64(platform.State_Normal), Attrs: int64(platform.Attrs_None), + Type: req.Type, }); err != nil { log.CtxError(ctx, "创建评论 失败[%v]\n", err) return resp, err @@ -203,9 +212,61 @@ func (s *CommentService) UpdateComment(ctx context.Context, req *platform.Update return resp, nil } -func (s *CommentService) DeleteComment(ctx context.Context, commentId string, level bool) (resp *platform.DeleteCommentResp, err error) { +func (s *CommentService) DeleteComment(ctx context.Context, commentId string, commentType int64, level bool) (resp *platform.DeleteCommentResp, err error) { resp = new(platform.DeleteCommentResp) + if level { + var ( + ids []string + comments []*commentMapper.Comment + ) + if err = s.CommentMongoMapper.GetConn().Find(ctx, &comments, bson.M{consts.RootId: commentId}); err != nil { + return resp, err + } + + ids = lo.Map(comments, func(comment *commentMapper.Comment, _ int) string { + return comment.ID.Hex() + }) + + tx := s.CommentMongoMapper.StartClient() + if err = tx.UseSession(ctx, func(sessionContext mongo.SessionContext) error { + var err1 error + if err1 = sessionContext.StartTransaction(); err1 != nil { + return err1 + } + if _, err1 = s.CommentMongoMapper.Delete(sessionContext, commentId); err1 != nil { + if rbErr := sessionContext.AbortTransaction(sessionContext); rbErr != nil { + log.CtxError(sessionContext, "删除评论: 产生错误[%v]: 回滚异常[%v]\n", err1, rbErr) + } + return err1 + } + + if _, err1 = s.CommentMongoMapper.DeleteMany(sessionContext, ids); err1 != nil { + if rbErr := sessionContext.AbortTransaction(sessionContext); rbErr != nil { + log.CtxError(sessionContext, "删除子评论 产生错误[%v]: 回滚异常[%v]\n", err1, rbErr) + } + return err1 + } + + if err1 = sessionContext.CommitTransaction(sessionContext); err1 != nil { + log.CtxError(sessionContext, "删除评论: 提交事务异常[%v]\n", err1) + return err1 + } + + return nil + }); err != nil { + return resp, err + } + + for _, v := range comments { + data, _ := sonic.Marshal(&message.DeleteCommentRelationsMessage{ + FromType: v.Type, + FromId: v.ID.Hex(), + }) + if err2 := s.DeleteCommentRelationKq.Push(pconvertor.Bytes2String(data)); err2 != nil { + return resp, err2 + } + } } else { if _, err = s.CommentMongoMapper.Delete(ctx, commentId); err != nil { @@ -213,6 +274,15 @@ func (s *CommentService) DeleteComment(ctx context.Context, commentId string, le return resp, err } } + + data, _ := sonic.Marshal(&message.DeleteCommentRelationsMessage{ + FromType: commentType, + FromId: commentId, + }) + if err2 := s.DeleteCommentRelationKq.Push(pconvertor.Bytes2String(data)); err2 != nil { + return resp, err2 + } + return resp, nil } diff --git a/biz/application/service/relation.go b/biz/application/service/relation.go index 0923b36..71dffce 100644 --- a/biz/application/service/relation.go +++ b/biz/application/service/relation.go @@ -71,7 +71,7 @@ func (s *RelationServiceImpl) DeleteNode(ctx context.Context, req *platform.Dele OnlyFromId: lo.ToPtr(req.NodeId), }); err1 != nil { if rbErr := sessionContext.AbortTransaction(sessionContext); rbErr != nil { - log.CtxError(sessionContext, "保存文件中产生错误[%v]: 回滚异常[%v]\n", err1, rbErr) + log.CtxError(sessionContext, "删除关系节点:产生错误[%v]: 回滚异常[%v]\n", err1, rbErr) } return err1 } @@ -81,13 +81,13 @@ func (s *RelationServiceImpl) DeleteNode(ctx context.Context, req *platform.Dele OnlyToId: lo.ToPtr(req.NodeId), }); err1 != nil { if rbErr := sessionContext.AbortTransaction(sessionContext); rbErr != nil { - log.CtxError(sessionContext, "保存文件中产生错误[%v]: 回滚异常[%v]\n", err1, rbErr) + log.CtxError(sessionContext, "删除关系节点:产生错误[%v]: 回滚异常[%v]\n", err1, rbErr) } return err1 } if err1 = sessionContext.CommitTransaction(sessionContext); err1 != nil { - log.CtxError(sessionContext, "保存文件: 提交事务异常[%v]\n", err1) + log.CtxError(sessionContext, "删除关系节点: 提交事务异常[%v]\n", err1) return err1 } return nil diff --git a/biz/application/service/subject.go b/biz/application/service/subject.go index 8bbed4f..126ab1d 100644 --- a/biz/application/service/subject.go +++ b/biz/application/service/subject.go @@ -7,7 +7,6 @@ import ( "github.com/CloudStriver/go-pkg/utils/util/log" "github.com/CloudStriver/platform/biz/infrastructure/kq" subjectMapper "github.com/CloudStriver/platform/biz/infrastructure/mapper/subject" - gencontent "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/content" "github.com/CloudStriver/service-idl-gen-go/kitex_gen/platform" "github.com/bytedance/sonic" "github.com/google/wire" @@ -24,8 +23,8 @@ type ISubjectService interface { } type SubjectService struct { - SubjectMongoMapper subjectMapper.IMongoMapper - DeleteFileRelationKq *kq.DeleteCommentRelationKq + SubjectMongoMapper subjectMapper.IMongoMapper + DeleteSubjectRelationKq *kq.DeleteCommentRelationKq } var SubjectSet = wire.NewSet( @@ -48,6 +47,7 @@ func (s *SubjectService) GetCommentSubject(ctx context.Context, req *platform.Ge AllCount: *data.AllCount, State: data.State, Attrs: data.Attrs, + Type: data.Type, } return resp, nil } @@ -66,6 +66,7 @@ func (s *SubjectService) CreateCommentSubject(ctx context.Context, req *platform AllCount: lo.ToPtr(int64(0)), State: int64(platform.State_Normal), Attrs: int64(platform.Attrs_None), + Type: req.Type, }); err != nil { log.CtxError(ctx, "创建评论区 失败[%v]\n", err) return resp, err @@ -84,12 +85,11 @@ func (s *SubjectService) UpdateCommentSubject(ctx context.Context, req *platform return resp, err } if _, err = s.SubjectMongoMapper.Update(ctx, &subjectMapper.Subject{ - ID: oid, - TopCommentId: nil, - RootCount: req.RootCount, - AllCount: req.AllCount, - State: req.State, - Attrs: req.Attrs, + ID: oid, + RootCount: req.RootCount, + AllCount: req.AllCount, + State: req.State, + Attrs: req.Attrs, }); err != nil { log.CtxError(ctx, "修改评论区信息 失败[%v]\n", err) return resp, err @@ -99,16 +99,22 @@ func (s *SubjectService) UpdateCommentSubject(ctx context.Context, req *platform func (s *SubjectService) DeleteCommentSubject(ctx context.Context, req *platform.DeleteCommentSubjectReq) (resp *platform.DeleteCommentSubjectResp, err error) { resp = new(platform.DeleteCommentSubjectResp) + var subject *subjectMapper.Subject + if subject, err = s.SubjectMongoMapper.FindOne(ctx, req.SubjectId); err != nil { + log.CtxError(ctx, "删除评论区 失败[%v]\n", err) + return resp, err + } + if _, err = s.SubjectMongoMapper.Delete(ctx, req.SubjectId); err != nil { log.CtxError(ctx, "删除评论区 失败[%v]\n", err) return resp, err } // 发送删除评论区关联文件的消息 data, _ := sonic.Marshal(&message.DeleteCommentRelationsMessage{ - FromType: int64(gencontent.TargetType_UserType), - FromId: req.UserId, + FromType: subject.Type, + FromId: req.SubjectId, }) - if err2 := s.DeleteFileRelationKq.Push(pconvertor.Bytes2String(data)); err2 != nil { + if err2 := s.DeleteSubjectRelationKq.Push(pconvertor.Bytes2String(data)); err2 != nil { return resp, err2 } return resp, nil diff --git a/biz/infrastructure/convertor/convertor.go b/biz/infrastructure/convertor/convertor.go index 2e96532..06e3d2a 100644 --- a/biz/infrastructure/convertor/convertor.go +++ b/biz/infrastructure/convertor/convertor.go @@ -25,6 +25,7 @@ func CommentMapperToComment(data *comment.Comment) *platform.Comment { AtUserId: data.AtUserId, Content: data.Content, Meta: data.Meta, + Type: data.Type, CreateTime: data.CreateAt.UnixMilli(), } } diff --git a/biz/infrastructure/mapper/comment/filter.go b/biz/infrastructure/mapper/comment/filter.go index 792e9be..04bc289 100644 --- a/biz/infrastructure/mapper/comment/filter.go +++ b/biz/infrastructure/mapper/comment/filter.go @@ -31,13 +31,14 @@ func makeMongoFilter(opts *FilterOptions) bson.M { func (f *MongoFilter) toBson() bson.M { f.CheckOnlyUserId() f.CheckOnlyAtUserId() + f.CheckOnlyCommentIds() f.CheckOnlyRootId() f.CheckOnlyState() f.CheckOnlyAttrs() return f.m } -func (f *MongoFilter) CheckOnlyFileIds() { +func (f *MongoFilter) CheckOnlyCommentIds() { if f.OnlyCommentIds != nil { f.m[consts.ID] = bson.M{ "$in": lo.Map[string, primitive.ObjectID](f.OnlyCommentIds, func(s string, _ int) primitive.ObjectID { diff --git a/biz/infrastructure/mapper/comment/mongo.go b/biz/infrastructure/mapper/comment/mongo.go index b0ac4fb..603ede5 100644 --- a/biz/infrastructure/mapper/comment/mongo.go +++ b/biz/infrastructure/mapper/comment/mongo.go @@ -43,6 +43,7 @@ type ( Comment struct { ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` + Type int64 `bson:"type,omitempty" json:"type,omitempty"` UserId string `bson:"userId,omitempty" json:"userId,omitempty"` AtUserId string `bson:"atUserId,omitempty" json:"atUserId,omitempty"` SubjectId string `bson:"subjectId,omitempty" json:"subjectId,omitempty"` diff --git a/biz/infrastructure/mapper/subject/mongo.go b/biz/infrastructure/mapper/subject/mongo.go index ecee0a1..702ebcf 100644 --- a/biz/infrastructure/mapper/subject/mongo.go +++ b/biz/infrastructure/mapper/subject/mongo.go @@ -33,6 +33,7 @@ type ( Subject struct { ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` + Type int64 `bson:"type,omitempty" json:"type,omitempty"` UserId string `bson:"userId,omitempty" json:"userId,omitempty"` TopCommentId *string `bson:"topCommentId,omitempty" json:"topCommentId,omitempty"` RootCount *int64 `bson:"rootCount,omitempty" json:"rootCount,omitempty"` diff --git a/provider/wire_gen.go b/provider/wire_gen.go index a0097cc..03346f4 100644 --- a/provider/wire_gen.go +++ b/provider/wire_gen.go @@ -27,9 +27,11 @@ func NewPlatformServerImpl() (*adaptor.PlatformServerImpl, error) { } iMongoMapper := comment.NewMongoMapper(configConfig) subjectIMongoMapper := subject.NewMongoMapper(configConfig) + deleteCommentRelationKq := kq.NewDeleteCommentRelationKq(configConfig) commentService := &service.CommentService{ - CommentMongoMapper: iMongoMapper, - SubjectMongoMapper: subjectIMongoMapper, + CommentMongoMapper: iMongoMapper, + SubjectMongoMapper: subjectIMongoMapper, + DeleteCommentRelationKq: deleteCommentRelationKq, } iEsMapper := label.NewEsMapper(configConfig) labelIMongoMapper := label.NewMongoMapper(configConfig) @@ -37,10 +39,9 @@ func NewPlatformServerImpl() (*adaptor.PlatformServerImpl, error) { LabelEsMapper: iEsMapper, LabelMongoMapper: labelIMongoMapper, } - deleteCommentRelationKq := kq.NewDeleteCommentRelationKq(configConfig) subjectService := &service.SubjectService{ - SubjectMongoMapper: subjectIMongoMapper, - DeleteFileRelationKq: deleteCommentRelationKq, + SubjectMongoMapper: subjectIMongoMapper, + DeleteSubjectRelationKq: deleteCommentRelationKq, } redisRedis := redis.NewRedis(configConfig) relationNeo4jMapper := relation.NewNeo4jMapper(configConfig) From e27d714c86544faaccaddf32f48e85e272eac0b0 Mon Sep 17 00:00:00 2001 From: Love-YeLin <1807209079@qq.com> Date: Sun, 14 Apr 2024 16:29:06 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9commnet=E7=9A=84=E5=88=A0?= =?UTF-8?q?=E9=99=A4bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 4 ++-- go.sum | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 9e7a7ae..35cd5be 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.20 require ( github.com/CloudStriver/cloudmind-mq v1.0.12-0.20240406130428-3a00c4159388 github.com/CloudStriver/go-pkg v0.0.0-20240206060942-84060a3dd273 - github.com/CloudStriver/service-idl-gen-go v0.0.0-20240413084243-a8d7e75059be + github.com/CloudStriver/service-idl-gen-go v0.0.0-20240414082236-a2f4391d68a2 github.com/bytedance/sonic v1.10.2 github.com/cloudwego/kitex v0.8.0 github.com/elastic/go-elasticsearch/v8 v8.11.1 @@ -22,7 +22,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 //replace github.com/CloudStriver/cloudmind-mq => ../cloudmind-mq require ( diff --git a/go.sum b/go.sum index 97dfbef..7134096 100644 --- a/go.sum +++ b/go.sum @@ -391,6 +391,8 @@ github.com/CloudStriver/service-idl-gen-go v0.0.0-20240413082126-0c956fdadb0e h1 github.com/CloudStriver/service-idl-gen-go v0.0.0-20240413082126-0c956fdadb0e/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= github.com/CloudStriver/service-idl-gen-go v0.0.0-20240413084243-a8d7e75059be h1:HddeenNcxzNp1CCpxmFRxJlZjKEy8M//Clb7T38gef0= github.com/CloudStriver/service-idl-gen-go v0.0.0-20240413084243-a8d7e75059be/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= +github.com/CloudStriver/service-idl-gen-go v0.0.0-20240414082236-a2f4391d68a2 h1:ZlZ3HnW1/rdTPwuoB2ynmj9rnAEmHBouHzguxMnN4pc= +github.com/CloudStriver/service-idl-gen-go v0.0.0-20240414082236-a2f4391d68a2/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= From ba61b3502a7745c5ce527231c4d09a5482d51b7b Mon Sep 17 00:00:00 2001 From: Love-YeLin <1807209079@qq.com> Date: Sun, 14 Apr 2024 21:26:19 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E8=AF=84=E8=AE=BA=E5=88=97=E8=A1=A8=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E8=AF=84=E8=AE=BA=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- biz/application/service/relation.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/biz/application/service/relation.go b/biz/application/service/relation.go index 71dffce..fa10ff5 100644 --- a/biz/application/service/relation.go +++ b/biz/application/service/relation.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "github.com/CloudStriver/go-pkg/utils/pagination" + "github.com/CloudStriver/go-pkg/utils/pagination/mongop" "github.com/CloudStriver/go-pkg/utils/pconvertor" "github.com/CloudStriver/go-pkg/utils/util/log" "github.com/CloudStriver/platform/biz/infrastructure/config" @@ -161,7 +162,7 @@ func (s *RelationServiceImpl) GetRelations(ctx context.Context, req *platform.Ge OnlyFromId: lo.ToPtr(o.FromFilterOptions.FromId), OnlyToType: lo.ToPtr(o.FromFilterOptions.ToType), OnlyRelationType: lo.ToPtr(req.RelationType), - }, p, sort.TimeCursorType); err != nil { + }, p, mongop.IdCursorType); err != nil { return resp, err } @@ -178,7 +179,7 @@ func (s *RelationServiceImpl) GetRelations(ctx context.Context, req *platform.Ge OnlyToId: lo.ToPtr(o.ToFilterOptions.ToId), OnlyToType: lo.ToPtr(o.ToFilterOptions.ToType), OnlyRelationType: lo.ToPtr(req.RelationType), - }, p, sort.TimeCursorType); err != nil { + }, p, mongop.IdCursorType); err != nil { return resp, err }