Roc 2 anni fa
parent
commit
a58914b81a

+ 15 - 0
controller/community/question.go

@@ -52,6 +52,21 @@ func QuestionList(c *gin.Context) {
 		response.FailMsg("获取失败", "QuestionList ErrMsg:"+err.Error(), c)
 		return
 	}
+
+	// 点赞/吐槽数据
+	err = yb_community_question.HandleLikeOrRoastByCommunityQuestionItemList(userinfo.UserID, list)
+	if err != nil {
+		response.FailMsg("获取失败", "QuestionList ErrMsg:"+err.Error(), c)
+		return
+	}
+
+	// 评论数据
+	err = yb_community_question.HandleCommentByCommunityQuestionItemList(userinfo.UserID, list)
+	if err != nil {
+		response.FailMsg("获取失败", "QuestionList ErrMsg:"+err.Error(), c)
+		return
+	}
+
 	response.OkData("获取成功", list, c)
 }
 

+ 64 - 0
logic/yb_community_question/yb_community_question_comment.go

@@ -338,3 +338,67 @@ func SetYbCommentAnonymousUserTips(userId uint64) (err error) {
 	}
 	return
 }
+
+// HandleCommentByCommunityQuestionItemList 问答 评论 数据
+func HandleCommentByCommunityQuestionItemList(userId uint64, questionList []*response.CommunityQuestionItem) (err error) {
+	listLen := len(questionList)
+	if listLen == 0 {
+		return
+	}
+	idArr := make([]uint32, 0)
+	idMap := make(map[uint32]int)
+	for i := 0; i < listLen; i++ {
+		idArr = append(idArr, uint32(questionList[i].CommunityQuestionID))
+		idMap[uint32(questionList[i].CommunityQuestionID)] = 1
+	}
+
+	//精选评论数据、我的评论数据
+	ybCommunityQuestionCommentMap := make(map[uint32]*yb_community_question_comment.YbCommunityQuestionComment)
+
+	//获取精选评论数据
+	hotList, err := yb_community_question_comment.GetLastHotListByCommunityQuestionIds(idArr)
+	if err != nil {
+		return
+	}
+	for _, v := range hotList {
+		ybCommunityQuestionCommentMap[v.CommunityQuestionID] = v
+		delete(idMap, v.CommunityQuestionID)
+	}
+
+	myIdArr := make([]uint32, 0)
+	for id := range idMap {
+		myIdArr = append(myIdArr, id)
+	}
+
+	//获取我的评论数据
+	myList, err := yb_community_question_comment.GetLastMyListByCommunityQuestionIds(userId, idArr)
+	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)
+	if err != nil {
+		return
+	}
+	for _, v := range numCommentList {
+		numCommentMap[v.CommunityQuestionID] = v.Total
+	}
+
+	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
+		}
+	}
+	return
+}

+ 63 - 0
logic/yb_community_question/yb_community_question_like_roast.go

@@ -4,6 +4,7 @@ import (
 	"errors"
 	"fmt"
 	"hongze/hongze_yb/global"
+	"hongze/hongze_yb/models/response"
 	"hongze/hongze_yb/models/tables/yb_community_question_like_roast"
 	"time"
 )
@@ -80,3 +81,65 @@ func SetLikeOrRoast(userId uint64, communityQuestionId uint32, opType, enable, s
 	}
 	return
 }
+
+// HandleLikeOrRoastByCommunityQuestionItemList 问答 点赞/吐槽 数据
+func HandleLikeOrRoastByCommunityQuestionItemList(userId uint64, questionList []*response.CommunityQuestionItem) (err error) {
+	listLen := len(questionList)
+	if listLen == 0 {
+		return
+	}
+	idArr := make([]uint32, 0)
+	for i := 0; i < listLen; i++ {
+		idArr = append(idArr, uint32(questionList[i].CommunityQuestionID))
+	}
+
+	// 获取点赞和吐槽数据
+	ybCommunityQuestionLikeRoastMap := make(map[uint32]*yb_community_question_like_roast.YbCommunityQuestionLikeRoast)
+	ybCommunityQuestionLikeRoastList, err := yb_community_question_like_roast.GetByUserIdAndCommunityQuestionIds(userId, idArr)
+	if err != nil {
+		return
+	}
+	for _, v := range ybCommunityQuestionLikeRoastList {
+		ybCommunityQuestionLikeRoastMap[v.CommunityQuestionID] = v
+	}
+
+	// 获取点赞和吐槽汇总数
+	likeMap := make(map[uint32]int)
+	roastMap := make(map[uint32]int)
+
+	likeList, err := yb_community_question_like_roast.GetLikeNumCommentByCommunityQuestionIds(idArr)
+	if err != nil {
+		return
+	}
+	for _, v := range likeList {
+		likeMap[v.CommunityQuestionID] = v.Total
+	}
+
+	roastList, err := yb_community_question_like_roast.GetRoastNumCommentByCommunityQuestionIds(idArr)
+	if err != nil {
+		return
+	}
+	for _, v := range roastList {
+		roastMap[v.CommunityQuestionID] = v.Total
+	}
+
+	for _, v := range questionList {
+		if tmpTotal, ok := likeMap[uint32(v.CommunityQuestionID)]; ok {
+			v.LikeTotal = tmpTotal
+		}
+		if tmpTotal, ok := roastMap[uint32(v.CommunityQuestionID)]; ok {
+			v.RoastTotal = tmpTotal
+		}
+
+		if ybCommunityQuestionLikeRoast, ok := ybCommunityQuestionLikeRoastMap[uint32(v.CommunityQuestionID)]; ok {
+			switch ybCommunityQuestionLikeRoast.OpType {
+			//类型. 1-点赞 2-吐槽
+			case 1:
+				v.IsLike = true
+			case 2:
+				v.IsRoast = true
+			}
+		}
+	}
+	return
+}

+ 7 - 1
models/response/community.go

@@ -19,7 +19,13 @@ 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"`
+	AudioList    []*CommunityQuestionAudioItem `json:"audio_list"`
+	IsLike       bool                          `json:"is_like" description:"是否已经点赞"`
+	IsRoast      bool                          `json:"is_roast" description:"是否已经吐槽"`
+	LikeTotal    int                           `json:"like_total" description:"点赞数"`
+	RoastTotal   int                           `json:"roast_total" description:"吐槽数"`
+	CommentTotal int                           `json:"comment_total" description:"总共评论数"`
+	Comment      string                        `json:"comment" description:"评论"`
 }
 
 type CommunityQuestionAudioItem struct {

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

@@ -110,3 +110,33 @@ func GetMyLatestComment(userId uint64) (item *YbCommunityQuestionComment, err er
 		Where("user_id = ? and type=1", userId).Order("comment_id desc").First(&item).Error
 	return
 }
+
+// NumCommentByCommunityQuestion 评论统计结构体
+type NumCommentByCommunityQuestion struct {
+	CommunityQuestionID uint32 `json:"community_question_id"`
+	Total               int    `json:"total"`
+}
+
+// GetNumCommentByCommunityQuestionIds 获取用户最新的留言
+func GetNumCommentByCommunityQuestionIds(communityQuestionIds []uint32) (item []*NumCommentByCommunityQuestion, err error) {
+	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
+		Select("community_question_id, count(1) total").
+		Where("community_question_id in (?) and type=1 and enabled = 1 ", communityQuestionIds).Scan(&item).Error
+	return
+}
+
+// GetLastHotListByCommunityQuestionIds 根据问答id列表获取最近精选的留言
+func GetLastHotListByCommunityQuestionIds(communityQuestionIds []uint32) (items []*YbCommunityQuestionComment, err error) {
+	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
+		Where("community_question_comment_id in (SELECT MAX(community_question_comment_id) AS community_question_comment_id FROM yb_community_question_comment WHERE community_question_id IN (?) AND enabled = 1 AND is_hot = 1 AND type = 1)", communityQuestionIds).
+		Find(&items).Error
+	return
+}
+
+// GetLastMyListByCommunityQuestionIds 根据问答id列表获取我最近的留言
+func GetLastMyListByCommunityQuestionIds(userId uint64, communityQuestionIds []uint32) (items []*YbCommunityQuestionComment, err error) {
+	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionComment{}).
+		Where("community_question_comment_id in (SELECT MAX(community_question_comment_id) AS community_question_comment_id FROM yb_community_question_comment WHERE user_id=? AND community_question_id IN (?) AND enabled = 1 AND is_hot = 1 AND type = 1)", userId, communityQuestionIds).
+		Find(&items).Error
+	return
+}

+ 40 - 1
models/tables/yb_community_question_like_roast/query.go

@@ -8,7 +8,7 @@ import (
 // GetByUserIdAndCommunityQuestionId 根据用户和问答id获取点赞/吐槽记录
 func GetByUserIdAndCommunityQuestionId(userId uint64, communityQuestionId uint32) (item *YbCommunityQuestionLikeRoast, err error) {
 	err = global.DEFAULT_MYSQL.
-		Where("user_id = ? and community_question_id = ?", userId, communityQuestionId).
+		Where("user_id = ? AND community_question_id = ?", userId, communityQuestionId).
 		First(&item).Error
 	if err == utils.ErrNoRow {
 		err = nil
@@ -31,3 +31,42 @@ func GetRoastNumByCommunityQuestionId(communityQuestionId uint32) (num int64, er
 		Count(&num).Error
 	return
 }
+
+// NumCommentByCommunityQuestionIds 评论统计结构体
+type NumCommentByCommunityQuestionIds struct {
+	CommunityQuestionID uint32 `json:"community_question_id"`
+	Total               int    `json:"total"`
+}
+
+// GetLikeNumCommentByCommunityQuestionIds 根据问答id列表获取所有问答的点赞数
+func GetLikeNumCommentByCommunityQuestionIds(communityQuestionIds []uint32) (items []*NumCommentByCommunityQuestionIds, err error) {
+	if len(communityQuestionIds) <= 0 {
+		return
+	}
+	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionLikeRoast{}).
+		Select("community_question_id, count(1) total").
+		Where("community_question_id in (?) AND enabled=1  AND op_type=1", communityQuestionIds).Scan(&items).Error
+	return
+}
+
+// GetRoastNumCommentByCommunityQuestionIds 根据问答id列表获取所有问答的吐槽数
+func GetRoastNumCommentByCommunityQuestionIds(communityQuestionIds []uint32) (items []*NumCommentByCommunityQuestionIds, err error) {
+	if len(communityQuestionIds) <= 0 {
+		return
+	}
+	err = global.DEFAULT_MYSQL.Model(YbCommunityQuestionLikeRoast{}).
+		Select("community_question_id, count(1) total").
+		Where("community_question_id in (?) AND enabled=1  AND op_type=2", communityQuestionIds).Scan(&items).Error
+	return
+}
+
+// GetByUserIdAndCommunityQuestionIds 根据用户id和问答id列表获取成功点赞/吐槽记录
+func GetByUserIdAndCommunityQuestionIds(userId uint64, communityQuestionIds []uint32) (items []*YbCommunityQuestionLikeRoast, err error) {
+	if len(communityQuestionIds) <= 0 {
+		return
+	}
+	err = global.DEFAULT_MYSQL.
+		Where("user_id = ? AND enabled=1  AND community_question_id in (?)", userId, communityQuestionIds).
+		Find(&items).Error
+	return
+}