From ed70c081490632a1d94777ef28447cd36530800b Mon Sep 17 00:00:00 2001 From: Leufolium Date: Thu, 9 May 2024 00:02:16 +0800 Subject: [PATCH] by Robin at 20240509 --- app/mix/controller/accountrelation_api.go | 7 +-- app/mix/controller/accountrelation_op.go | 7 +-- app/mix/dao/mongo.go | 75 ++++------------------- app/mix/service/apiservice.go | 4 +- app/mix/service/logic/accountrelation.go | 32 ++-------- app/mix/service/service.go | 4 +- 6 files changed, 26 insertions(+), 103 deletions(-) diff --git a/app/mix/controller/accountrelation_api.go b/app/mix/controller/accountrelation_api.go index 34877eeb..1af09e1e 100644 --- a/app/mix/controller/accountrelation_api.go +++ b/app/mix/controller/accountrelation_api.go @@ -174,7 +174,7 @@ func ApiGetAbleToAccessWeixinOfAccountRelationList(ctx *gin.Context) { func ApiGetAccountRelationCount(ctx *gin.Context) { req := ctx.MustGet("client_req").(*accountrelationproto.ApiCountReq) - countMap, ec := service.DefaultService.ApiGetAccountRelationCount(ctx, req) + followCount, isfollowedCount, ec := service.DefaultService.ApiGetAccountRelationCount(ctx, req) if ec != errcode.ErrCodeAccountRelationSrvOk { logger.Error("ApiGetAccountRelationCount fail, req: %v, ec: %v", util.ToJson(req), ec) ReplyErrCodeMsg(ctx, ec) @@ -182,9 +182,8 @@ func ApiGetAccountRelationCount(ctx *gin.Context) { } data := &accountrelationproto.ApiCountData{ - FollowCount: countMap[consts.Follow], - IsFollowedCount: countMap[consts.IsFollowed], - FriendCount: countMap[consts.Friend], + FollowCount: followCount, + IsFollowedCount: isfollowedCount, } ReplyOk(ctx, data) } diff --git a/app/mix/controller/accountrelation_op.go b/app/mix/controller/accountrelation_op.go index de4e2b8a..17ed346d 100644 --- a/app/mix/controller/accountrelation_op.go +++ b/app/mix/controller/accountrelation_op.go @@ -138,7 +138,7 @@ func OpGetAbleToAccessWeixinOfAccountRelationList(ctx *gin.Context) { func OpGetAccountRelationCount(ctx *gin.Context) { req := ctx.MustGet("client_req").(*accountrelationproto.OpCountReq) - countMap, ec := service.DefaultService.OpGetAccountRelationCount(ctx, req) + followCount, isfollowedCount, ec := service.DefaultService.OpGetAccountRelationCount(ctx, req) if ec != errcode.ErrCodeAccountRelationSrvOk { logger.Error("OpGetAccountRelationCount fail, req: %v, ec: %v", util.ToJson(req), ec) ReplyErrCodeMsg(ctx, ec) @@ -146,9 +146,8 @@ func OpGetAccountRelationCount(ctx *gin.Context) { } data := &accountrelationproto.OpCountData{ - FollowCount: countMap[consts.Follow], - IsFollowedCount: countMap[consts.IsFollowed], - FriendCount: countMap[consts.Friend], + FollowCount: followCount, + IsFollowedCount: isfollowedCount, } ReplyOk(ctx, data) } diff --git a/app/mix/dao/mongo.go b/app/mix/dao/mongo.go index 607dae7c..7960f4c2 100644 --- a/app/mix/dao/mongo.go +++ b/app/mix/dao/mongo.go @@ -1997,80 +1997,29 @@ func (m *Mongo) GetFriendAccountRelationList(ctx *gin.Context, req *accountrelat return } -func (m *Mongo) GetFriendAccountRelationCount(ctx *gin.Context, req *accountrelationproto.OpCountReq) (results []map[string]interface{}, err error) { +func (m *Mongo) GetAccountRelationCount(ctx *gin.Context, req *accountrelationproto.OpCountReq) (followCount, isfollowedCount int64, err error) { col := m.getColAccountRelation() - matchClause := bson.D{{ - Key: "$match", Value: bson.M{ - "sub_mid": util.DerefInt64(req.Mid), - "predicate": bson.M{ - "$in": [2]int{consts.Follow, consts.IsFollowed}, - }, - "del_flag": 0, - }, - }} - - groupClause := bson.D{ - {Key: "$group", Value: bson.D{ - {Key: "_id", Value: "$obj_mid"}, - {Key: "relation_count", Value: bson.M{ - "$sum": 1, - }}, - }, - }, + // 关注 + filterFollow := qmgo.M{ + "sub_mid": util.DerefInt64(req.Mid), + "predicate": consts.Follow, + "del_flag": 0, } - havingClause := bson.D{{ - Key: "$match", Value: bson.M{ - "relation_count": 2, - }, - }} - - secondOrderGroupClause := bson.D{ - {Key: "$group", Value: bson.D{ - {Key: "_id", Value: "$relation_count"}, - {Key: "count", Value: bson.M{ - "$sum": 1, - }}, - }}, + filterIsFollowed := qmgo.M{ + "sub_mid": util.DerefInt64(req.Mid), + "predicate": consts.IsFollowed, + "del_flag": 0, } - pipeline := qmgo.Pipeline{matchClause, groupClause, havingClause, secondOrderGroupClause} - - err = col.Aggregate(ctx, pipeline).All(&results) + followCount, err = col.Find(ctx, filterFollow).Count() if err != nil { logger.Error("err : %v", err) return } - return -} - -func (m *Mongo) GetAccountRelationCount(ctx *gin.Context, req *accountrelationproto.OpCountReq) (results []map[string]interface{}, err error) { - col := m.getColAccountRelation() - matchClause := bson.D{{ - Key: "$match", Value: bson.M{ - "sub_mid": util.DerefInt64(req.Mid), - "predicate": bson.M{ - "$in": [2]int{consts.Follow, consts.IsFollowed}, - }, - "del_flag": 0, - }, - }} - - groupClause := bson.D{ - {Key: "$group", Value: bson.D{ - {Key: "_id", Value: "$predicate"}, - {Key: "count", Value: bson.M{ - "$sum": 1, - }}, - }, - }, - } - - pipeline := qmgo.Pipeline{matchClause, groupClause} - - err = col.Aggregate(ctx, pipeline).All(&results) + isfollowedCount, err = col.Find(ctx, filterIsFollowed).Count() if err != nil { logger.Error("err : %v", err) return diff --git a/app/mix/service/apiservice.go b/app/mix/service/apiservice.go index 258baf5d..b7279b25 100644 --- a/app/mix/service/apiservice.go +++ b/app/mix/service/apiservice.go @@ -920,14 +920,14 @@ func (s *Service) ApiGetAbleToAccessWeixinOfAccountRelationList(ctx *gin.Context } // 查询关注、被关注数量 -func (s *Service) ApiGetAccountRelationCount(ctx *gin.Context, req *accountrelationproto.ApiCountReq) (countMap map[int64]int64, ec errcode.ErrCode) { +func (s *Service) ApiGetAccountRelationCount(ctx *gin.Context, req *accountrelationproto.ApiCountReq) (followCount, isfollowedCount int64, ec errcode.ErrCode) { ec = errcode.ErrCodeAccountRelationSrvOk if ec = s.ApiGetAccountRelationCountBusinessValidate(ctx, req); ec != errcode.ErrCodeAccountRelationSrvOk { return } - countMap, err := _DefaultAccountRelation.OpCount(ctx, &accountrelationproto.OpCountReq{ + followCount, isfollowedCount, err := _DefaultAccountRelation.OpCount(ctx, &accountrelationproto.OpCountReq{ Mid: req.Mid, }) if err != nil { diff --git a/app/mix/service/logic/accountrelation.go b/app/mix/service/logic/accountrelation.go index f4db4911..fd867215 100644 --- a/app/mix/service/logic/accountrelation.go +++ b/app/mix/service/logic/accountrelation.go @@ -137,38 +137,14 @@ func (p *AccountRelation) OpListFriend(ctx *gin.Context, req *accountrelationpro return list, err } -func (p *AccountRelation) OpCount(ctx *gin.Context, req *accountrelationproto.OpCountReq) (map[int64]int64, error) { - countMap := make(map[int64]int64) - mapList, err := p.store.GetAccountRelationCount(ctx, req) +func (p *AccountRelation) OpCount(ctx *gin.Context, req *accountrelationproto.OpCountReq) (followCount, isfollowedCount int64, err error) { + followCount, isfollowedCount, err = p.store.GetAccountRelationCount(ctx, req) if err != nil { logger.Error("GetAccountRelationCount fail, err: %v", err) - return nil, err - } - friendMapList, err := p.store.GetFriendAccountRelationCount(ctx, req) - if err != nil { - logger.Error("GetFriendAccountRelationCount fail, err: %v", err) - return nil, err - } - for _, _map := range mapList { - id, ok1 := _map["_id"].(int64) - count, ok2 := _map["count"].(int32) - if ok1 && ok2 { - countMap[id] = int64(count) - } else { - logger.Error("assertion err") - return nil, fmt.Errorf("assertion err") - } - } - if len(friendMapList) > 0 { - if count, ok := friendMapList[0]["count"].(int32); ok { - countMap[consts.Friend] = int64(count) - } else { - logger.Error("assertion err") - return nil, fmt.Errorf("assertion err") - } + return } - return countMap, nil + return } func (p *AccountRelation) OpIsFollowedCount(ctx *gin.Context) ([]int64, []int64, error) { diff --git a/app/mix/service/service.go b/app/mix/service/service.go index 6be4264e..e49115a0 100644 --- a/app/mix/service/service.go +++ b/app/mix/service/service.go @@ -1592,14 +1592,14 @@ func (s *Service) OpGetAbleToAccessWeixinOfAccountRelationList(ctx *gin.Context, return } -func (s *Service) OpGetAccountRelationCount(ctx *gin.Context, req *accountrelationproto.OpCountReq) (countMap map[int64]int64, ec errcode.ErrCode) { +func (s *Service) OpGetAccountRelationCount(ctx *gin.Context, req *accountrelationproto.OpCountReq) (followCount, isfollowedCount int64, ec errcode.ErrCode) { ec = errcode.ErrCodeAccountRelationSrvOk if ec = s.OpGetAccountRelationCountBusinessValidate(ctx, req); ec != errcode.ErrCodeAccountRelationSrvOk { return } - countMap, err := _DefaultAccountRelation.OpCount(ctx, req) + followCount, isfollowedCount, err := _DefaultAccountRelation.OpCount(ctx, req) if err != nil { logger.Error("OpCount fail, req: %v, err: %v", util.ToJson(req), err) ec = errcode.ErrCodeAccountRelationSrvFail