rank.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package service
  2. import (
  3. "context"
  4. artmdl "go-common/app/interface/openplatform/article/model"
  5. "go-common/library/ecode"
  6. )
  7. // RankCategories rank categoires
  8. func (s *Service) RankCategories(c context.Context) (res []*artmdl.RankCategory) {
  9. res = s.c.RankCategories
  10. return
  11. }
  12. // Ranks get ranks
  13. func (s *Service) Ranks(c context.Context, cid int64, mid int64, ip string) (res []*artmdl.RankMeta, note string, err error) {
  14. var (
  15. exist bool
  16. addCache = true
  17. aids []int64
  18. rank artmdl.RankResp
  19. metas map[int64]*artmdl.Meta
  20. )
  21. if !s.ranksMap[cid] {
  22. err = ecode.RequestErr
  23. return
  24. }
  25. if exist, err = s.dao.ExpireRankCache(c, cid); err != nil {
  26. addCache = false
  27. err = nil
  28. }
  29. if exist {
  30. if rank, err = s.dao.RankCache(c, cid); err != nil {
  31. exist = false
  32. err = nil
  33. addCache = false
  34. }
  35. }
  36. if !exist {
  37. if rank, err = s.dao.Rank(c, cid, ip); err != nil {
  38. if rank, err = s.dao.RankCache(c, cid); err != nil {
  39. return
  40. }
  41. } else {
  42. if addCache && len(rank.List) > 0 {
  43. cache.Save(func() {
  44. s.dao.AddRankCache(context.TODO(), cid, rank)
  45. })
  46. }
  47. }
  48. }
  49. if len(rank.List) == 0 {
  50. return
  51. }
  52. for _, a := range rank.List {
  53. aids = append(aids, a.Aid)
  54. }
  55. if metas, err = s.ArticleMetas(c, aids); err != nil {
  56. return
  57. }
  58. var ups []int64
  59. for _, r := range rank.List {
  60. if metas[r.Aid] != nil {
  61. res = append(res, &artmdl.RankMeta{Meta: metas[r.Aid], Score: r.Score})
  62. ups = append(ups, metas[r.Aid].Author.Mid)
  63. }
  64. }
  65. if (len(ups) > 0) && (mid != 0) {
  66. if attentions, e := s.isAttentions(c, mid, ups); e == nil {
  67. for _, r := range res {
  68. r.Attention = attentions[r.Author.Mid]
  69. }
  70. }
  71. }
  72. if s.setting.ShowRankNote {
  73. note = rank.Note
  74. }
  75. return
  76. }
  77. func (s *Service) loadRanks() {
  78. for _, rank := range s.c.RankCategories {
  79. s.ranksMap[rank.ID] = true
  80. }
  81. }