by Robin at 20240509 #416

Merged
chenhao merged 1 commits from BUG-RELATION-Robin into main 2024-05-09 00:06:14 +08:00
6 changed files with 26 additions and 103 deletions
Showing only changes of commit ed70c08149 - Show all commits

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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

View File

@ -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 {

View File

@ -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) {

View File

@ -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