diff --git a/kitex_server/initmicro.go b/kitex_server/initmicro.go index 945b3e7..7917ade 100755 --- a/kitex_server/initmicro.go +++ b/kitex_server/initmicro.go @@ -1,9 +1,11 @@ package kitex_server import ( - "github.com/ClubWeGo/favoritemicro/kitex_gen/favorite/favoriteservice" "log" + "github.com/ClubWeGo/favoritemicro/kitex_gen/favorite/favoriteservice" + + "github.com/ClubWeGo/commentmicro/kitex_gen/comment/commentservice" "github.com/ClubWeGo/relationmicro/kitex_gen/relation/combineservice" "github.com/ClubWeGo/usermicro/kitex_gen/usermicro/userservice" "github.com/ClubWeGo/videomicro/kitex_gen/videomicro/videoservice" @@ -15,6 +17,7 @@ var Userclient userservice.Client var Videoclient videoservice.Client var Relationclient combineservice.Client var FavoriteClient favoriteservice.Client +var CommentClient commentservice.Client func Init(r discovery.Resolver) { uc, err := userservice.NewClient("userservice", client.WithResolver(r)) @@ -40,4 +43,10 @@ func Init(r discovery.Resolver) { log.Fatal(err) } FavoriteClient = fc + + Cc, err := commentservice.NewClient("commentservice", client.WithResolver(r)) + if err != nil { + log.Fatal(err) + } + CommentClient = Cc } diff --git a/kitex_server/interaction.go b/kitex_server/interaction.go index 71a9dbb..f2a30cd 100644 --- a/kitex_server/interaction.go +++ b/kitex_server/interaction.go @@ -4,6 +4,7 @@ import ( "context" "sync" + "github.com/ClubWeGo/commentmicro/kitex_gen/comment" "github.com/ClubWeGo/douyin/pack" "github.com/ClubWeGo/usermicro/kitex_gen/usermicro" "github.com/ClubWeGo/videomicro/kitex_gen/videomicro" @@ -134,6 +135,7 @@ func CountUserFavorite(ctx context.Context, uid int64) (int64, int64, error) { return res.FavoriteCount, res.FavoritedCount, nil } +// 协程接口 // 传入userId切片,批量查询user对应的favorite, total_favorited // map[int64][]int64 [FavoriteCount FavoritedCount] func GetUsersFavoriteCountMap(idSet []int64, respUsersFavoriteCountMap chan map[int64][]int64, wg *sync.WaitGroup, errChan chan error) { @@ -184,3 +186,19 @@ func GetIsFavoriteMap(idSet []int64, currentUser int64, respIsFavoriteMap chan m respIsFavoriteMap <- res.IsFavoriteMap errChan <- nil } + +// 传入videoId切片,批量视频的评论数量 +func GetCommentCountMap(idSet []int64, respIsFavoriteMap chan map[int64]int64, wg *sync.WaitGroup, errChan chan error) { + defer wg.Done() + + res, err := CommentClient.VideosFavoriteCountMethod(context.Background(), &comment.VideosCommentCountReq{ + VideoIdList: idSet, + }) + if err != nil { + respIsFavoriteMap <- map[int64]int64{} + errChan <- err + return + } + respIsFavoriteMap <- res.CommentCountMap + errChan <- nil +} diff --git a/kitex_server/videoservice.go b/kitex_server/videoservice.go index 00dd8b0..6794383 100755 --- a/kitex_server/videoservice.go +++ b/kitex_server/videoservice.go @@ -59,9 +59,14 @@ func GetVideoLatestMap(idSet []int64, currentUser int64, respVideoMap chan map[i wgVideo.Add(1) go GetVideosFavoriteCountMap(idSet, respVideosFavoriteCountMap, wgVideo, respVideosFavoriteCountMapError) - // TODO: @weitao // 批量查询视频的评论数,传入视频id的切片,返回对应的评论数(需携带对应视频id),从comment服务 // 在此处类似上边的写法,写评论数的实现 + respCommentCountMap := make(chan map[int64]int64, 1) + defer close(respCommentCountMap) + respCommentCountMapError := make(chan error, 1) + defer close(respCommentCountMapError) + wgVideo.Add(1) + go GetCommentCountMap(idSet, respCommentCountMap, wgVideo, respCommentCountMapError) // 批量查询 is_favorite, 传入目标视频id切片和currentUser查is_favorite的切片(结果需要携带视频id,douyin里后续需要转成map):从favorite; respIsFavoriteMap := make(chan map[int64]bool, 1) @@ -87,7 +92,11 @@ func GetVideoLatestMap(idSet []int64, currentUser int64, respVideoMap chan map[i errSlice = append(errSlice, err) } - // @weitao 在此处类似上边的写法,把数据从你前面定义的数据chan里面拿到,把错误从对应的错误chan里面appen到errSlice里面 + VideosCommentCountMap := <-respCommentCountMap + err = <-respCommentCountMapError + if err != nil { + errSlice = append(errSlice, err) + } errChan <- errSlice // 记录错误的切片,至少应该返回一个空切片,否则chan会阻塞 @@ -96,7 +105,7 @@ func GetVideoLatestMap(idSet []int64, currentUser int64, respVideoMap chan map[i for _, id := range idSet { videoLatestMap[id] = core.Video{ // 视频id对应的Video存储查到的关键字段 FavoriteCount: VideosFavoriteCountMap[id], // - CommentCount: 0, // TODO: @weitao, 在此处从你返回的map里面读去对应id的评论数 + CommentCount: VideosCommentCountMap[id], // IsFavorite: IsFavoriteMap[id], // } }