has_like.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. pb "go-common/app/service/main/thumbup/api"
  6. "go-common/app/service/main/thumbup/dao"
  7. "go-common/app/service/main/thumbup/model"
  8. "go-common/library/log"
  9. "go-common/library/sync/errgroup"
  10. )
  11. // 用户点赞列表数据量少的话 全取比较好 点赞数量多的话 同时一次性取的少的话 zscore比较好
  12. var _rangeLimit = 20 //经验值
  13. // HasLike .
  14. func (s *Service) HasLike(c context.Context, business string, mid int64, messageIDs []int64) (res map[int64]int8, all map[int64]*pb.UserLikeState, err error) {
  15. if len(messageIDs) == 0 {
  16. return
  17. }
  18. var businessID int64
  19. if businessID, err = s.CheckBusiness(business); err != nil {
  20. return
  21. }
  22. if mid == 0 {
  23. return
  24. }
  25. var (
  26. likes = make(map[int64]*pb.UserLikeState)
  27. dislikes = make(map[int64]*pb.UserLikeState)
  28. messageIDMap = make(map[int64]bool)
  29. )
  30. for _, id := range messageIDs {
  31. messageIDMap[id] = true
  32. }
  33. group := errgroup.Group{}
  34. limit := s.dao.BusinessIDMap[businessID].UserLikesLimit
  35. if s.dao.BusinessIDMap[businessID].EnableUserLikeList() {
  36. group.Go(func() (err error) {
  37. exist, _ := s.dao.ExpireUserLikesCache(c, mid, businessID, model.StateLike)
  38. if exist && len(messageIDs) <= _rangeLimit {
  39. likes, err = s.dao.UserLikeExists(c, mid, businessID, messageIDs, model.StateLike)
  40. return
  41. }
  42. var items []*model.ItemLikeRecord
  43. if items, err = s.dao.UserLikeList(c, mid, businessID, model.StateLike, 0, limit); err != nil {
  44. return err
  45. }
  46. s.dbus.Do(c, func(c context.Context) {
  47. s.dao.PubUserMsg(c, business, mid, model.StateLike)
  48. })
  49. for _, item := range items {
  50. if messageIDMap[item.MessageID] {
  51. likes[item.MessageID] = &pb.UserLikeState{
  52. Mid: mid,
  53. Time: item.Time,
  54. State: pb.State_STATE_LIKE,
  55. }
  56. }
  57. }
  58. return
  59. })
  60. }
  61. if s.dao.BusinessIDMap[businessID].EnableUserDislikeList() {
  62. group.Go(func() (err error) {
  63. exist, _ := s.dao.ExpireUserLikesCache(c, mid, businessID, model.StateDislike)
  64. if exist && len(messageIDs) <= _rangeLimit {
  65. dislikes, err = s.dao.UserLikeExists(c, mid, businessID, messageIDs, model.StateDislike)
  66. return
  67. }
  68. var items []*model.ItemLikeRecord
  69. if items, err = s.dao.UserLikeList(c, mid, businessID, model.StateDislike, 0, limit); err != nil {
  70. return err
  71. }
  72. s.dbus.Do(c, func(c context.Context) {
  73. s.dao.PubUserMsg(c, business, mid, model.StateDislike)
  74. })
  75. for _, item := range items {
  76. if messageIDMap[item.MessageID] {
  77. dislikes[item.MessageID] = &pb.UserLikeState{
  78. Mid: mid,
  79. Time: item.Time,
  80. State: pb.State_STATE_DISLIKE,
  81. }
  82. }
  83. }
  84. return
  85. })
  86. }
  87. err = group.Wait()
  88. if err != nil {
  89. log.Errorv(c, log.KV("log", fmt.Sprintf("cache.Exists(%v) err: %+v", messageIDs, err)), log.KV("mid", mid), log.KV("business", business))
  90. dao.PromError("has_like:Exists")
  91. }
  92. res = make(map[int64]int8)
  93. all = make(map[int64]*pb.UserLikeState)
  94. for id, v := range likes {
  95. if v != nil {
  96. res[id] = model.StateLike
  97. all[id] = v
  98. }
  99. }
  100. for id, v := range dislikes {
  101. if v != nil {
  102. res[id] = model.StateDislike
  103. all[id] = v
  104. }
  105. }
  106. if len(messageIDs) == 1 {
  107. log.Info("has_like business: %s mid: %v message_id: %v, resp: %+v, err: %v", business, mid, messageIDs[0], res, err)
  108. }
  109. return
  110. }