@@ -43,19 +43,23 @@ func (s *commentService) GetCommentList(articleID int64, cursorTime int64) (*mod
4343 return nil , errors .New ("查询评论信息出错" )
4444 }
4545
46+ commentList , minCursorTime := buildCommentList (comtList )
4647 resp .ArticleID = articleID
4748 resp .TotalNum = len (comtList )
48- resp .Cursor = cursorTime
49- buildCommentList ( comtList )
49+ resp .Cursor = minCursorTime
50+ resp . CommentList = commentList
5051 return resp , nil
5152}
5253
53- func buildCommentList (comtList []model.Comment ) []* model.CommentInfo {
54+ func buildCommentList (comtList []model.Comment ) ([]* model.CommentInfo , int64 ) {
55+ var minCursorTime int64 = model .MAXCursorTime
56+
5457 sortComments (comtList , func (p , q * model.Comment ) bool {
5558 return p .ID < q .ID
5659 })
5760 detailedCommentList := make ([]* model.CommentInfo , len (comtList ))
5861 for i := range comtList {
62+ minCursorTime = util .MinInt64 (minCursorTime , comtList [i ].CreateTime )
5963 userInfo , err := repository .UserRepository .GetUserByUserID (util .DB (), comtList [i ].UserID )
6064 if err != nil {
6165 logs .Logger .Errorf ("查询作者信息出错" )
@@ -70,20 +74,20 @@ func buildCommentList(comtList []model.Comment) []*model.CommentInfo {
7074 LikeCount : comtList [i ].LikeCount ,
7175 CreateTime : comtList [i ].CreateTime ,
7276 }
73- detailedCommentList [i ].ParentComment = findParentComment (i , detailedCommentList [i ].CommentID , detailedCommentList )
77+ detailedCommentList [i ].ParentComment = findParentComment (i , comtList [i ].ParentID , detailedCommentList )
7478 }
75- return detailedCommentList
79+ return detailedCommentList , minCursorTime
7680}
7781
78- func findParentComment (len int , commentID int64 , detailedCommentList []* model.CommentInfo ) * model.CommentInfo {
82+ func findParentComment (len int , parentID int64 , detailedCommentList []* model.CommentInfo ) * model.CommentInfo {
7983 var l , r int = 0 , len
8084 var mid int
8185 for l <= r {
8286 mid = (l + r ) >> 1
83- if detailedCommentList [mid ].CommentID == commentID {
87+ if detailedCommentList [mid ].CommentID == parentID {
8488 return detailedCommentList [mid ]
8589 }
86- if detailedCommentList [mid ].CommentID > commentID {
90+ if detailedCommentList [mid ].CommentID > parentID {
8791 r = mid - 1
8892 } else {
8993 l = mid + 1
@@ -110,7 +114,7 @@ func (pw commentWrapper) Less(i, j int) bool { // rewrite Less()
110114 return pw .by (& pw .comments [i ], & pw .comments [j ])
111115}
112116
113- // 封装成 SortPerson 方法
117+ // sortComments
114118func sortComments (comments []model.Comment , by sortBy ) {
115119 sort .Sort (commentWrapper {comments , by })
116120}
0 commit comments