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 36f7fd04..23b3fe44 100644 --- a/app/mix/dao/mongo.go +++ b/app/mix/dao/mongo.go @@ -1999,80 +1999,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 b9981edf..4370b775 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 64ca3627..fc1b50b5 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