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) { func ApiGetAccountRelationCount(ctx *gin.Context) {
req := ctx.MustGet("client_req").(*accountrelationproto.ApiCountReq) 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 { if ec != errcode.ErrCodeAccountRelationSrvOk {
logger.Error("ApiGetAccountRelationCount fail, req: %v, ec: %v", util.ToJson(req), ec) logger.Error("ApiGetAccountRelationCount fail, req: %v, ec: %v", util.ToJson(req), ec)
ReplyErrCodeMsg(ctx, ec) ReplyErrCodeMsg(ctx, ec)
@ -182,9 +182,8 @@ func ApiGetAccountRelationCount(ctx *gin.Context) {
} }
data := &accountrelationproto.ApiCountData{ data := &accountrelationproto.ApiCountData{
FollowCount: countMap[consts.Follow], FollowCount: followCount,
IsFollowedCount: countMap[consts.IsFollowed], IsFollowedCount: isfollowedCount,
FriendCount: countMap[consts.Friend],
} }
ReplyOk(ctx, data) ReplyOk(ctx, data)
} }

View File

@ -138,7 +138,7 @@ func OpGetAbleToAccessWeixinOfAccountRelationList(ctx *gin.Context) {
func OpGetAccountRelationCount(ctx *gin.Context) { func OpGetAccountRelationCount(ctx *gin.Context) {
req := ctx.MustGet("client_req").(*accountrelationproto.OpCountReq) 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 { if ec != errcode.ErrCodeAccountRelationSrvOk {
logger.Error("OpGetAccountRelationCount fail, req: %v, ec: %v", util.ToJson(req), ec) logger.Error("OpGetAccountRelationCount fail, req: %v, ec: %v", util.ToJson(req), ec)
ReplyErrCodeMsg(ctx, ec) ReplyErrCodeMsg(ctx, ec)
@ -146,9 +146,8 @@ func OpGetAccountRelationCount(ctx *gin.Context) {
} }
data := &accountrelationproto.OpCountData{ data := &accountrelationproto.OpCountData{
FollowCount: countMap[consts.Follow], FollowCount: followCount,
IsFollowedCount: countMap[consts.IsFollowed], IsFollowedCount: isfollowedCount,
FriendCount: countMap[consts.Friend],
} }
ReplyOk(ctx, data) ReplyOk(ctx, data)
} }

View File

@ -1997,80 +1997,29 @@ func (m *Mongo) GetFriendAccountRelationList(ctx *gin.Context, req *accountrelat
return 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() col := m.getColAccountRelation()
matchClause := bson.D{{ // 关注
Key: "$match", Value: bson.M{ filterFollow := qmgo.M{
"sub_mid": util.DerefInt64(req.Mid), "sub_mid": util.DerefInt64(req.Mid),
"predicate": bson.M{ "predicate": consts.Follow,
"$in": [2]int{consts.Follow, consts.IsFollowed}, "del_flag": 0,
},
"del_flag": 0,
},
}}
groupClause := bson.D{
{Key: "$group", Value: bson.D{
{Key: "_id", Value: "$obj_mid"},
{Key: "relation_count", Value: bson.M{
"$sum": 1,
}},
},
},
} }
havingClause := bson.D{{ filterIsFollowed := qmgo.M{
Key: "$match", Value: bson.M{ "sub_mid": util.DerefInt64(req.Mid),
"relation_count": 2, "predicate": consts.IsFollowed,
}, "del_flag": 0,
}}
secondOrderGroupClause := bson.D{
{Key: "$group", Value: bson.D{
{Key: "_id", Value: "$relation_count"},
{Key: "count", Value: bson.M{
"$sum": 1,
}},
}},
} }
pipeline := qmgo.Pipeline{matchClause, groupClause, havingClause, secondOrderGroupClause} followCount, err = col.Find(ctx, filterFollow).Count()
err = col.Aggregate(ctx, pipeline).All(&results)
if err != nil { if err != nil {
logger.Error("err : %v", err) logger.Error("err : %v", err)
return return
} }
return isfollowedCount, err = col.Find(ctx, filterIsFollowed).Count()
}
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)
if err != nil { if err != nil {
logger.Error("err : %v", err) logger.Error("err : %v", err)
return 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 ec = errcode.ErrCodeAccountRelationSrvOk
if ec = s.ApiGetAccountRelationCountBusinessValidate(ctx, req); ec != errcode.ErrCodeAccountRelationSrvOk { if ec = s.ApiGetAccountRelationCountBusinessValidate(ctx, req); ec != errcode.ErrCodeAccountRelationSrvOk {
return return
} }
countMap, err := _DefaultAccountRelation.OpCount(ctx, &accountrelationproto.OpCountReq{ followCount, isfollowedCount, err := _DefaultAccountRelation.OpCount(ctx, &accountrelationproto.OpCountReq{
Mid: req.Mid, Mid: req.Mid,
}) })
if err != nil { if err != nil {

View File

@ -137,38 +137,14 @@ func (p *AccountRelation) OpListFriend(ctx *gin.Context, req *accountrelationpro
return list, err return list, err
} }
func (p *AccountRelation) OpCount(ctx *gin.Context, req *accountrelationproto.OpCountReq) (map[int64]int64, error) { func (p *AccountRelation) OpCount(ctx *gin.Context, req *accountrelationproto.OpCountReq) (followCount, isfollowedCount int64, err error) {
countMap := make(map[int64]int64) followCount, isfollowedCount, err = p.store.GetAccountRelationCount(ctx, req)
mapList, err := p.store.GetAccountRelationCount(ctx, req)
if err != nil { if err != nil {
logger.Error("GetAccountRelationCount fail, err: %v", err) logger.Error("GetAccountRelationCount fail, err: %v", err)
return nil, err return
}
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 countMap, nil return
} }
func (p *AccountRelation) OpIsFollowedCount(ctx *gin.Context) ([]int64, []int64, error) { func (p *AccountRelation) OpIsFollowedCount(ctx *gin.Context) ([]int64, []int64, error) {

View File

@ -1592,14 +1592,14 @@ func (s *Service) OpGetAbleToAccessWeixinOfAccountRelationList(ctx *gin.Context,
return 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 ec = errcode.ErrCodeAccountRelationSrvOk
if ec = s.OpGetAccountRelationCountBusinessValidate(ctx, req); ec != errcode.ErrCodeAccountRelationSrvOk { if ec = s.OpGetAccountRelationCountBusinessValidate(ctx, req); ec != errcode.ErrCodeAccountRelationSrvOk {
return return
} }
countMap, err := _DefaultAccountRelation.OpCount(ctx, req) followCount, isfollowedCount, err := _DefaultAccountRelation.OpCount(ctx, req)
if err != nil { if err != nil {
logger.Error("OpCount fail, req: %v, err: %v", util.ToJson(req), err) logger.Error("OpCount fail, req: %v, err: %v", util.ToJson(req), err)
ec = errcode.ErrCodeAccountRelationSrvFail ec = errcode.ErrCodeAccountRelationSrvFail