yb_community_question_like_tease.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package yb_community_question
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_yb/global"
  6. "hongze/hongze_yb/models/response"
  7. "hongze/hongze_yb/models/tables/yb_community_question_like_tease"
  8. "time"
  9. )
  10. // SetLikeOrTease 用户对问答进行(取消)点赞或(取消)吐槽
  11. func SetLikeOrTease(userId uint64, communityQuestionId uint32, opType, enable, sourceAgent int8) (ybCommunityQuestionLikeTease *yb_community_question_like_tease.YbCommunityQuestionLikeTease, likeNum, teaseNum int64, err error, errMsg string) {
  12. //user.UserID
  13. defer func() {
  14. if err != nil {
  15. global.LOG.Critical(fmt.Sprintf("SetLike: userId=%d, err:%s, errMsg:%s", userId, err.Error(), errMsg))
  16. }
  17. }()
  18. if sourceAgent == 0 {
  19. err = errors.New("请输入来源")
  20. return
  21. }
  22. if communityQuestionId <= 0 {
  23. err = errors.New("请输入问答ID")
  24. return
  25. }
  26. // 判断是否有点赞权限(不需要权限校验,先保留,有需要后续再说)
  27. //err, errMsg = services.CheckSimpleCompanyProduct(user)
  28. //if err != nil {
  29. // return
  30. //}
  31. //查询用户对问答的点赞/吐槽状态
  32. ybCommunityQuestionLikeTease, err = yb_community_question_like_tease.GetByUserIdAndCommunityQuestionId(userId, communityQuestionId)
  33. if err != nil {
  34. errMsg = "查询点赞/吐槽记录出错"
  35. return
  36. }
  37. //如果未点赞则新增点赞记录
  38. now := time.Now()
  39. if ybCommunityQuestionLikeTease.CommunityQuestionLikeTeaseID <= 0 {
  40. ybCommunityQuestionLikeTease = &yb_community_question_like_tease.YbCommunityQuestionLikeTease{
  41. //CommunityQuestionLikeTeaseID: 0,
  42. UserID: userId,
  43. OpType: opType,
  44. Enabled: 1,
  45. CreateTime: now,
  46. ModifyTime: now,
  47. CommunityQuestionID: communityQuestionId,
  48. SourceAgent: sourceAgent,
  49. }
  50. err = ybCommunityQuestionLikeTease.Create()
  51. if err != nil {
  52. errMsg = err.Error()
  53. err = errors.New("新增点赞/吐槽记录出错")
  54. return
  55. }
  56. } else {
  57. ybCommunityQuestionLikeTease.OpType = opType
  58. ybCommunityQuestionLikeTease.Enabled = enable
  59. ybCommunityQuestionLikeTease.ModifyTime = now
  60. updates := []string{"op_type", "enabled", "modify_time"}
  61. err = ybCommunityQuestionLikeTease.Update(updates)
  62. if err != nil {
  63. errMsg = "更新点赞记录出错"
  64. return
  65. }
  66. }
  67. //查询总的点赞数
  68. likeNum, err = yb_community_question_like_tease.GetLikeNumByCommunityQuestionId(communityQuestionId)
  69. if err != nil {
  70. errMsg = err.Error()
  71. err = errors.New("查询点赞数出错")
  72. }
  73. teaseNum, err = yb_community_question_like_tease.GetTeaseNumByCommunityQuestionId(communityQuestionId)
  74. if err != nil {
  75. errMsg = err.Error()
  76. err = errors.New("查询吐槽数出错")
  77. }
  78. return
  79. }
  80. // HandleLikeOrTeaseByCommunityQuestionItemList 问答 点赞/吐槽 数据
  81. func HandleLikeOrTeaseByCommunityQuestionItemList(userId uint64, questionList []*response.CommunityQuestionItem) (err error) {
  82. listLen := len(questionList)
  83. if listLen == 0 {
  84. return
  85. }
  86. idArr := make([]uint32, 0)
  87. for i := 0; i < listLen; i++ {
  88. idArr = append(idArr, uint32(questionList[i].CommunityQuestionID))
  89. }
  90. // 获取点赞和吐槽数据
  91. ybCommunityQuestionLikeTeaseMap := make(map[uint32]*yb_community_question_like_tease.YbCommunityQuestionLikeTease)
  92. ybCommunityQuestionLikeTeaseList, err := yb_community_question_like_tease.GetByUserIdAndCommunityQuestionIds(userId, idArr)
  93. if err != nil {
  94. return
  95. }
  96. for _, v := range ybCommunityQuestionLikeTeaseList {
  97. ybCommunityQuestionLikeTeaseMap[v.CommunityQuestionID] = v
  98. }
  99. // 获取点赞和吐槽汇总数
  100. likeMap := make(map[uint32]int)
  101. teaseMap := make(map[uint32]int)
  102. likeList, err := yb_community_question_like_tease.GetLikeNumCommentByCommunityQuestionIds(idArr)
  103. if err != nil {
  104. return
  105. }
  106. for _, v := range likeList {
  107. likeMap[v.CommunityQuestionID] = v.Total
  108. }
  109. teaseList, err := yb_community_question_like_tease.GetTeaseNumCommentByCommunityQuestionIds(idArr)
  110. if err != nil {
  111. return
  112. }
  113. for _, v := range teaseList {
  114. teaseMap[v.CommunityQuestionID] = v.Total
  115. }
  116. for _, v := range questionList {
  117. if tmpTotal, ok := likeMap[uint32(v.CommunityQuestionID)]; ok {
  118. v.LikeTotal = tmpTotal
  119. }
  120. if tmpTotal, ok := teaseMap[uint32(v.CommunityQuestionID)]; ok {
  121. v.TeaseTotal = tmpTotal
  122. }
  123. if ybCommunityQuestionLikeTease, ok := ybCommunityQuestionLikeTeaseMap[uint32(v.CommunityQuestionID)]; ok {
  124. switch ybCommunityQuestionLikeTease.OpType {
  125. //类型. 1-点赞 2-吐槽
  126. case 1:
  127. v.IsLike = true
  128. case 2:
  129. v.IsTease = true
  130. }
  131. }
  132. }
  133. return
  134. }