Browse Source

问答列表展示所有精选

hsun 2 years ago
parent
commit
90b0a85ac7

+ 13 - 66
logic/yb_community_question/yb_community_question_comment.go

@@ -319,86 +319,33 @@ func HandleCommentByCommunityQuestionItemList(userId uint64, questionList []*res
 		return
 	}
 	idArr := make([]uint32, 0)
-	idMap := make(map[uint32]int)
 
-	userIds := []uint64{userId} //所有的评论人
+	// 问题ID-精选评论列表
+	questionIdCommentsMap := make(map[uint32][]*response.CommunityQuestionCommentListItem, 0)
 
 	for i := 0; i < listLen; i++ {
 		idArr = append(idArr, uint32(questionList[i].CommunityQuestionID))
-		idMap[uint32(questionList[i].CommunityQuestionID)] = 1
+		questionIdCommentsMap[uint32(questionList[i].CommunityQuestionID)] = make([]*response.CommunityQuestionCommentListItem, 0)
 	}
 
-	//精选评论数据、我的评论数据
-	ybCommunityQuestionCommentMap := make(map[uint32]*yb_community_question_comment.YbCommunityQuestionComment)
-
-	//获取精选评论数据
-	hotList, err := yb_community_question_comment.GetLastHotListByCommunityQuestionIds(idArr)
+	// 精选评论数据
+	hotList, err := yb_community_question_comment.GetHotListByCommunityQuestionIds(idArr)
 	if err != nil {
 		return
 	}
 	for _, v := range hotList {
-		ybCommunityQuestionCommentMap[v.CommunityQuestionID] = v
-		delete(idMap, v.CommunityQuestionID)
-
-		//评论人id
-		userIds = append(userIds, v.UserID)
-	}
-
-	//需要查询的我的问答的评论
-	myIdArr := make([]uint32, 0)
-	for id := range idMap {
-		myIdArr = append(myIdArr, id)
-	}
-
-	//获取我的评论数据
-	myList, err := yb_community_question_comment.GetLastMyListByCommunityQuestionIds(userId, myIdArr)
-	if err != nil {
-		return
-	}
-	for _, v := range myList {
-		ybCommunityQuestionCommentMap[v.CommunityQuestionID] = v
-	}
-
-	// 获取留言汇总数
-	numCommentMap := make(map[uint32]int)
-	numCommentList, err := yb_community_question_comment.GetNumCommentByCommunityQuestionIds(idArr, userId)
-	if err != nil {
-		return
-	}
-	for _, v := range numCommentList {
-		numCommentMap[v.CommunityQuestionID] = v.Total
-	}
-
-	var userOthers []*wx_user.WxUser
-	if len(userIds) > 0 {
-		userOthers, err = wx_user.GetByUserIds(userIds)
-		if err != nil {
-			err = errors.New("查询精选留言用户出错")
-			return
-		}
-	}
-	usersMap := make(map[uint64]*wx_user.WxUser)
-	for _, v := range userOthers {
-		usersMap[v.UserID] = v
+		questionIdCommentsMap[v.CommunityQuestionID] = append(questionIdCommentsMap[v.CommunityQuestionID], &response.CommunityQuestionCommentListItem{
+			QaAvatarUrl: v.QaAvatarUrl,
+			Comment: v.Content,
+		})
 	}
 
 	for _, v := range questionList {
-		//评论汇总数
-		if tmpTotal, ok := numCommentMap[uint32(v.CommunityQuestionID)]; ok {
-			v.CommentTotal = tmpTotal
-		}
-
-		//最近一条 精选/我的 评论
-		if ybCommunityQuestionCommentInfo, ok := ybCommunityQuestionCommentMap[uint32(v.CommunityQuestionID)]; ok {
-			v.Comment = ybCommunityQuestionCommentInfo.Content
-			v.QaAvatarUrl = ybCommunityQuestionCommentInfo.QaAvatarUrl
-			v.CommentUserName = "匿名用户" + strconv.Itoa(int(3333+ybCommunityQuestionCommentInfo.UserID))
-			if info, ok := usersMap[ybCommunityQuestionCommentInfo.UserID]; ok && ybCommunityQuestionCommentInfo.IsShowName == 1 {
-				v.CommentUserName = info.NickName
-				//v.UserImgUrl = info.Headimgurl
-			}
-		}
+		comments := questionIdCommentsMap[uint32(v.CommunityQuestionID)]
+		v.CommentTotal = len(comments)
+		v.CommentList = comments
 	}
+
 	return
 }
 

+ 16 - 8
models/response/community.go

@@ -22,14 +22,15 @@ type CommunityQuestionItem struct {
 	ReplyStatus             int    `json:"reply_status" description:"回复状态 1-待分配 2-待回答 3-已回答"`
 	//AuthOk                  bool                          `json:"auth_ok" description:"是否有权限"`
 	//PermissionInfo          PermissionCheckInfo           `json:"permission_info"`
-	AudioList       []*CommunityQuestionAudioItem `json:"audio_list"`
-	OpType          int8                          `description:"类型. 1-点赞 2-吐槽" json:"op_type"`
-	LikeTotal       int                           `json:"like_total" description:"点赞数"`
-	TeaseTotal      int                           `json:"tease_total" description:"吐槽数"`
-	CommentTotal    int                           `json:"comment_total" description:"总共评论数"`
-	Comment         string                        `json:"comment" description:"评论"`
-	CommentUserName string                        `json:"comment_user_name" description:"评论人"`
-	QaAvatarUrl     string                        `description:"用户头像" json:"qa_avatar_url"`
+	AudioList    []*CommunityQuestionAudioItem `json:"audio_list"`
+	OpType       int8                          `description:"类型. 1-点赞 2-吐槽" json:"op_type"`
+	LikeTotal    int                           `json:"like_total" description:"点赞数"`
+	TeaseTotal   int                           `json:"tease_total" description:"吐槽数"`
+	CommentTotal int                           `json:"comment_total" description:"总共评论数"`
+	//Comment         string                        `json:"comment" description:"评论"`
+	//CommentUserName string                        `json:"comment_user_name" description:"评论人"`
+	//QaAvatarUrl     string                        `description:"用户头像" json:"qa_avatar_url"`
+	CommentList []*CommunityQuestionCommentListItem `json:"comment_list"`
 }
 
 type CommunityQuestionAudioItem struct {
@@ -123,3 +124,10 @@ type RespCommunityQuestionCommentList struct {
 	HotTotal int64                               `json:"hot_total"`
 	MyTotal  int64                               `json:"my_total"`
 }
+
+// CommunityQuestionCommentListItem 问答列表评论列表
+type CommunityQuestionCommentListItem struct {
+	QaAvatarUrl     string `description:"用户头像" json:"qa_avatar_url"`
+	Comment         string `description:"评论" json:"comment"`
+	//CommentUserName string `description:"评论人" json:"comment_user_name"`
+}

+ 9 - 0
models/tables/yb_community_question_comment/query.go

@@ -127,3 +127,12 @@ func GetUserAvatarRandom() (item *UserAvatarRandom, err error) {
 	err = global.DEFAULT_MYSQL.Raw(sql).Scan(&item).Error
 	return
 }
+
+// GetHotListByCommunityQuestionIds 根据问答id列表获取精选留言
+func GetHotListByCommunityQuestionIds(communityQuestionIds []uint32) (items []*YbCommunityQuestionComment, err error) {
+	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
+		Where("community_question_id IN (?) AND enabled = 1 AND is_hot = 1 AND type = 1", communityQuestionIds).
+		Order("community_question_id DESC").
+		Find(&items).Error
+	return
+}