yb_community_question_like_tease.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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, source 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, source)
  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. Source: source,
  50. }
  51. err = ybCommunityQuestionLikeTease.Create()
  52. if err != nil {
  53. errMsg = err.Error()
  54. err = errors.New("新增点赞/吐槽记录出错")
  55. return
  56. }
  57. } else {
  58. ybCommunityQuestionLikeTease.OpType = opType
  59. ybCommunityQuestionLikeTease.Enabled = enable
  60. ybCommunityQuestionLikeTease.SourceAgent = sourceAgent
  61. ybCommunityQuestionLikeTease.ModifyTime = now
  62. updates := []string{"op_type", "enabled", "source_agent", "modify_time"}
  63. err = ybCommunityQuestionLikeTease.Update(updates)
  64. if err != nil {
  65. errMsg = "更新点赞记录出错"
  66. return
  67. }
  68. }
  69. //查询总的点赞数
  70. likeNum, err = yb_community_question_like_tease.GetLikeNumByCommunityQuestionId(communityQuestionId, int(source))
  71. if err != nil {
  72. errMsg = err.Error()
  73. err = errors.New("查询点赞数出错")
  74. }
  75. teaseNum, err = yb_community_question_like_tease.GetTeaseNumByCommunityQuestionId(communityQuestionId, int(source))
  76. if err != nil {
  77. errMsg = err.Error()
  78. err = errors.New("查询吐槽数出错")
  79. }
  80. return
  81. }
  82. // HandleLikeOrTeaseByCommunityQuestionItemList 问答 点赞/吐槽 数据
  83. func HandleLikeOrTeaseByCommunityQuestionItemList(userId uint64, questionList []*response.CommunityQuestionItem) (err error) {
  84. listLen := len(questionList)
  85. if listLen == 0 {
  86. return
  87. }
  88. idArr := make([]uint32, 0)
  89. for i := 0; i < listLen; i++ {
  90. idArr = append(idArr, uint32(questionList[i].CommunityQuestionID))
  91. }
  92. // 获取点赞和吐槽数据
  93. ybCommunityQuestionLikeTeaseMap := make(map[uint32]*yb_community_question_like_tease.YbCommunityQuestionLikeTease)
  94. ybCommunityQuestionLikeTeaseList, err := yb_community_question_like_tease.GetByUserIdAndCommunityQuestionIds(userId, idArr, yb_community_question_like_tease.SourceQuestion)
  95. if err != nil {
  96. return
  97. }
  98. for _, v := range ybCommunityQuestionLikeTeaseList {
  99. ybCommunityQuestionLikeTeaseMap[v.CommunityQuestionID] = v
  100. }
  101. // 获取点赞和吐槽汇总数
  102. likeMap := make(map[uint32]int)
  103. teaseMap := make(map[uint32]int)
  104. likeList, err := yb_community_question_like_tease.GetLikeNumCommentByCommunityQuestionIds(idArr, yb_community_question_like_tease.SourceQuestion)
  105. if err != nil {
  106. return
  107. }
  108. for _, v := range likeList {
  109. likeMap[v.CommunityQuestionID] = v.Total
  110. }
  111. teaseList, err := yb_community_question_like_tease.GetTeaseNumCommentByCommunityQuestionIds(idArr, yb_community_question_like_tease.SourceQuestion)
  112. if err != nil {
  113. return
  114. }
  115. for _, v := range teaseList {
  116. teaseMap[v.CommunityQuestionID] = v.Total
  117. }
  118. for _, v := range questionList {
  119. if tmpTotal, ok := likeMap[uint32(v.CommunityQuestionID)]; ok {
  120. v.LikeTotal = tmpTotal
  121. }
  122. if tmpTotal, ok := teaseMap[uint32(v.CommunityQuestionID)]; ok {
  123. v.TeaseTotal = tmpTotal
  124. }
  125. if ybCommunityQuestionLikeTease, ok := ybCommunityQuestionLikeTeaseMap[uint32(v.CommunityQuestionID)]; ok {
  126. //类型. 1-点赞 2-吐槽
  127. v.OpType = ybCommunityQuestionLikeTease.OpType
  128. }
  129. }
  130. return
  131. }