dangbei.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package thirdp
  2. import (
  3. "math"
  4. "go-common/app/interface/main/tv/dao/thirdp"
  5. "go-common/app/interface/main/tv/model"
  6. tpMdl "go-common/app/interface/main/tv/model/thirdp"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "github.com/pkg/errors"
  10. )
  11. func (s *Service) buildPager(req *tpMdl.ReqDBeiPages) (pager *model.IdxPager, err error) {
  12. var (
  13. addCache bool
  14. count int
  15. )
  16. // pick up the count from redis, otherwise pick it from DB
  17. if count, err = s.dao.GetThirdpCnt(ctx, req.TypeC); err != nil {
  18. if count, err = s.dao.ThirdpCnt(ctx, req.TypeC); err != nil {
  19. return // if db count still error, fatal error
  20. }
  21. log.Error("PickDBeiPage - Can't Get Count, Pass by DB, Page %d", req.Page)
  22. addCache = true
  23. }
  24. pager = &model.IdxPager{
  25. CurrentPage: int(req.Page),
  26. TotalItems: count,
  27. TotalPages: int(math.Ceil(float64(count) / float64(req.Ps))),
  28. PageSize: int(req.Ps),
  29. }
  30. if req.Page > int64(pager.TotalPages) {
  31. err = ecode.TvDangbeiPageNotExist
  32. return
  33. }
  34. // async Reset the DB data: Count & CurrentPage ID in MC for next time
  35. if addCache {
  36. cache.Save(func() {
  37. s.dao.SetThirdpCnt(ctx, count, req.TypeC)
  38. log.Info("PickDBeiPage Set Count %d Into Cache", count)
  39. })
  40. }
  41. return
  42. }
  43. // PickDBeiPage picks the dangbei's page
  44. func (s *Service) PickDBeiPage(page int64, typeC string) (data *tpMdl.DBeiPage, err error) {
  45. var (
  46. cPageID int64
  47. sids []int64 // this page's season ids
  48. sns []*model.SeasonCMS
  49. arcs []*model.ArcCMS
  50. dbeiSns []*tpMdl.DBeiSeason
  51. pager *model.IdxPager
  52. )
  53. req := &tpMdl.ReqDBeiPages{
  54. Ps: s.conf.Cfg.Dangbei.Pagesize,
  55. TypeC: typeC,
  56. Page: page,
  57. }
  58. if pager, err = s.buildPager(req); err != nil {
  59. return
  60. }
  61. if req.LastID, err = s.dao.LoadPageID(ctx, req); err != nil {
  62. log.Error("MangoPage getPageID LastPage %d Miss, Pass by offset", page-1)
  63. return
  64. }
  65. if sids, cPageID, err = s.dao.DBeiPages(ctx, req); err != nil {
  66. return
  67. }
  68. if len(sids) == 0 {
  69. err = errors.Wrapf(ecode.NothingFound, "Type_C [%s], Page [%d], Offset Result Empty", typeC, page)
  70. return
  71. }
  72. // load data from cache and transform data to Dangbei structure
  73. if typeC == thirdp.DBeiPGC { // pgc - seasonCMS
  74. if sns, _, err = s.cmsDao.LoadSnsCMS(ctx, sids); err != nil {
  75. log.Error("PickDBeiPage - PGC - LoadSnsCMS - Sids %v, Error %v", sids, err)
  76. return
  77. }
  78. for _, v := range sns { // transform the object to DbeiSeason
  79. dbeiSns = append(dbeiSns, tpMdl.DBeiSn(v))
  80. }
  81. } else if typeC == thirdp.DBeiUGC { // ugc - arcCMS
  82. if arcs, err = s.cmsDao.LoadArcsMedia(ctx, sids); err != nil {
  83. log.Error("PickDBeiPage - UGC - LoadArcsMedia - Sids %v, Error %v", sids, err)
  84. return
  85. }
  86. for _, v := range arcs {
  87. first, second := s.arcDao.GetPTypeName(int32(v.TypeID))
  88. dbeiSns = append(dbeiSns, tpMdl.DbeiArc(v, first, second))
  89. }
  90. }
  91. // async Reset the DB data: CurrentPage ID in MC for next time
  92. cache.Save(func() {
  93. s.dao.SetPageID(ctx, &tpMdl.ReqPageID{Page: page, ID: cPageID, TypeC: typeC})
  94. })
  95. data = &tpMdl.DBeiPage{
  96. List: dbeiSns,
  97. Pager: pager,
  98. }
  99. return
  100. }