stat.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/job/main/reply-feed/model"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. )
  8. func (s *Service) updateStat(ctx context.Context, rpID int64, stat *model.ReplyStat) {
  9. s.statQ.Do(ctx, func(ctx context.Context) {
  10. s.dao.SetReplyStatMc(ctx, stat)
  11. })
  12. }
  13. // GetStatByID 从缓存获取单条评论stat获取不到则从DB获取
  14. func (s *Service) GetStatByID(ctx context.Context, oid int64, tp int, rpID int64) (stat *model.ReplyStat, err error) {
  15. stats, err := s.GetStatsByID(ctx, oid, tp, []int64{rpID})
  16. if err != nil {
  17. return
  18. }
  19. if len(stats) > 0 {
  20. stat = stats[0]
  21. } else {
  22. err = ecode.ReplyNotExist
  23. log.Error("reply not exists rpID %d", rpID)
  24. }
  25. return
  26. }
  27. // GetStatsByID 从缓存获取多条评论stat获取不到则从DB获取
  28. func (s *Service) GetStatsByID(ctx context.Context, oid int64, tp int, rpIDs []int64) (rs []*model.ReplyStat, err error) {
  29. var (
  30. rsMap map[int64]*model.ReplyStat
  31. missIDs []int64
  32. missed map[int64]*model.ReplyStat
  33. )
  34. if rsMap, missIDs, err = s.dao.ReplyStatsMc(ctx, rpIDs); err != nil {
  35. return
  36. }
  37. for _, stat := range rsMap {
  38. rs = append(rs, stat)
  39. }
  40. if len(missIDs) > 0 {
  41. // miss从DB查
  42. if missed, err = s.getStatsByIDDB(ctx, oid, tp, missIDs); err != nil {
  43. rs = nil
  44. return
  45. }
  46. for _, stat := range missed {
  47. stat := stat
  48. rs = append(rs, stat)
  49. s.statQ.Do(ctx, func(ctx context.Context) {
  50. s.dao.SetReplyStatMc(ctx, stat)
  51. })
  52. }
  53. }
  54. return
  55. }
  56. // getStatsByIDDB 从数据库获取热门评论stats,这里不需要一致性,所以跨表查再聚合
  57. func (s *Service) getStatsByIDDB(ctx context.Context, oid int64, tp int, rpIDs []int64) (rs map[int64]*model.ReplyStat, err error) {
  58. if len(rpIDs) == 0 {
  59. return
  60. }
  61. replyMap, err := s.dao.ReplyLHRCStatsByID(ctx, oid, rpIDs)
  62. if err != nil {
  63. return
  64. }
  65. reportMap, err := s.dao.ReportStatsByID(ctx, oid, rpIDs)
  66. if err != nil {
  67. return
  68. }
  69. ctime, err := s.dao.SubjectStats(ctx, oid, tp)
  70. if err != nil {
  71. return
  72. }
  73. for rpID := range replyMap {
  74. r, ok := reportMap[rpID]
  75. if ok && r != nil {
  76. replyMap[rpID].Report = r.Report
  77. }
  78. replyMap[rpID].SubjectTime = ctime
  79. }
  80. rs = replyMap
  81. return
  82. }
  83. // GetStatsDB 从数据库获取热门评论stats,这里不需要一致性,所以跨表查再聚合
  84. func (s *Service) GetStatsDB(ctx context.Context, oid int64, tp int) (rs []*model.ReplyStat, err error) {
  85. replyMap, err := s.dao.ReplyLHRCStats(ctx, oid, tp)
  86. if err != nil {
  87. return
  88. }
  89. var RpIDs []int64
  90. for rpID := range replyMap {
  91. RpIDs = append(RpIDs, rpID)
  92. }
  93. reportMap, err := s.dao.ReportStatsByID(ctx, oid, RpIDs)
  94. if err != nil {
  95. return
  96. }
  97. ctime, err := s.dao.SubjectStats(ctx, oid, tp)
  98. if err != nil {
  99. return
  100. }
  101. for rpID := range replyMap {
  102. r, ok := reportMap[rpID]
  103. if ok && r != nil {
  104. replyMap[rpID].Report = r.Report
  105. }
  106. replyMap[rpID].SubjectTime = ctime
  107. rs = append(rs, replyMap[rpID])
  108. }
  109. return
  110. }