article.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package data
  2. import (
  3. "context"
  4. "sort"
  5. "time"
  6. "go-common/app/interface/main/creative/model/data"
  7. artmdl "go-common/app/interface/openplatform/article/model"
  8. "go-common/library/log"
  9. )
  10. // ThirtyDayArticle for 30 days.
  11. func (s *Service) ThirtyDayArticle(c context.Context, mid int64, ip string) (res []*artmdl.ThirtyDayArticle, err error) {
  12. if res, err = s.data.ThirtyDayArticleCache(c, mid); err != nil && len(res) != 0 {
  13. return
  14. }
  15. if res, err = s.art.ThirtyDayArticle(c, mid, ip); err != nil && res != nil {
  16. s.data.AddCache(func() {
  17. s.data.AddThirtyDayArticleCache(context.Background(), mid, res)
  18. })
  19. }
  20. return
  21. }
  22. //ArtThirtyDay for article.
  23. func (s *Service) ArtThirtyDay(c context.Context, mid int64, ty byte) (res []*data.ArtTrend, err error) {
  24. if res, err = s.data.ArtThirtyDay(c, mid, ty); err != nil {
  25. log.Error("s.data.ArtThirtyDay mid(%d)|err(%v)", mid, err)
  26. }
  27. return
  28. }
  29. //ArtRank for article rank.
  30. func (s *Service) ArtRank(c context.Context, mid int64, ty byte) (res *data.ArtRankList, err error) {
  31. daytime := time.Now().AddDate(0, 0, -1).Add(-12 * time.Hour)
  32. dt := daytime.Format("20060102")
  33. var ar *data.ArtRankMap
  34. if ar, err = s.data.ArtRank(c, mid, ty, dt); err != nil {
  35. log.Error("s.data.ArtRank mid(%d)|err(%v)", mid, err)
  36. return
  37. }
  38. if ar == nil {
  39. log.Info("s.data.ArtRank mid(%d) article rank(%+v)", mid, ar)
  40. return
  41. }
  42. sortK := make([]int, 0, len(ar.AIDs))
  43. aids := make([]int64, 0, len(ar.AIDs))
  44. for k, v := range ar.AIDs {
  45. aids = append(aids, v)
  46. sortK = append(sortK, k)
  47. }
  48. artMap, _ := s.art.ArticleMetas(c, aids, "")
  49. if len(artMap) == 0 {
  50. return
  51. }
  52. arts := make([]*data.ArtMeta, 0, len(artMap))
  53. sort.Ints(sortK)
  54. for _, k := range sortK {
  55. if aid, ok := ar.AIDs[k]; ok {
  56. if a, ok := artMap[aid]; ok {
  57. art := &data.ArtMeta{}
  58. art.AID = a.ID
  59. art.Title = a.Title
  60. art.PTime = a.PublishTime
  61. if v, ok := ar.Incrs[k]; ok {
  62. art.Incr = v
  63. }
  64. arts = append(arts, art)
  65. }
  66. }
  67. }
  68. res = &data.ArtRankList{Arts: arts}
  69. return
  70. }
  71. //ArtReadAnalysis for article.
  72. func (s *Service) ArtReadAnalysis(c context.Context, mid int64) (res *data.ArtRead, err error) {
  73. if res, err = s.data.ReadAnalysis(c, mid); err != nil {
  74. log.Error("s.data.ReadAnalysis mid(%d)|err(%v)", mid, err)
  75. }
  76. return
  77. }