service.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package favorite
  2. import (
  3. "context"
  4. "go-common/app/interface/main/tv/conf"
  5. "go-common/app/interface/main/tv/dao/archive"
  6. "go-common/app/interface/main/tv/dao/favorite"
  7. "go-common/app/interface/main/tv/model"
  8. arcwar "go-common/app/service/main/archive/api"
  9. favmdl "go-common/app/service/main/favorite/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. )
  13. // Service .
  14. type Service struct {
  15. conf *conf.Config
  16. dao *favorite.Dao
  17. arcDao *archive.Dao
  18. }
  19. // New .
  20. func New(c *conf.Config) *Service {
  21. srv := &Service{
  22. conf: c,
  23. dao: favorite.New(c),
  24. arcDao: archive.New(c),
  25. }
  26. return srv
  27. }
  28. const (
  29. _ActAdd = 1
  30. _ActDel = 2
  31. )
  32. // Favorites picks one page of the member's favorites
  33. func (s *Service) Favorites(ctx context.Context, req *model.ReqFav) (resM *model.FavMList, err error) {
  34. var (
  35. res *favmdl.Favorites
  36. arcs map[int64]*arcwar.Arc
  37. aids []int64
  38. pageNum int
  39. )
  40. resM = &model.FavMList{}
  41. resM.Page.Size = s.conf.Cfg.FavPs
  42. if res, err = s.dao.FavoriteV3(ctx, req.MID, req.Pn); err != nil { // pick favorite original data
  43. log.Error("FavoriteV3 Mid %d, Pn %d, GetFav Err %v", req.MID, req.Pn, err)
  44. return
  45. }
  46. if len(res.List) == 0 {
  47. return
  48. }
  49. resM.Page = res.Page
  50. // temp logic because client misuses the count as the number of pages
  51. if resM.Page.Count%resM.Page.Size == 0 {
  52. pageNum = resM.Page.Count / resM.Page.Size
  53. } else {
  54. pageNum = resM.Page.Count/resM.Page.Size + 1
  55. }
  56. resM.Page.Count = pageNum
  57. // temp logic
  58. for _, v := range res.List { // combine aids and get the archive info
  59. aids = append(aids, v.Oid)
  60. }
  61. if arcs, err = s.arcDao.Archives(ctx, aids); err != nil {
  62. log.Error("FavoriteV3 Mid %d, Pn %d, GetArc Err #%v", req.MID, req.Pn, err)
  63. return
  64. }
  65. for _, v := range res.List { // arrange the final result
  66. if arc, ok := arcs[v.Oid]; ok {
  67. resM.List = append(resM.List, arc)
  68. } else {
  69. log.Warn("FavoriteV3 Mid %d, Pn %d, Miss Arc Info %d", req.MID, req.Pn, v.Oid)
  70. }
  71. }
  72. return
  73. }
  74. // FavAct is favorite action, add or delete
  75. func (s *Service) FavAct(ctx context.Context, req *model.ReqFavAct) (err error) {
  76. if req.Action == _ActAdd {
  77. return s.dao.FavAdd(ctx, req.MID, req.AID)
  78. } else if req.Action == _ActDel {
  79. return s.dao.FavDel(ctx, req.MID, req.AID)
  80. }
  81. return ecode.RequestErr
  82. }