favorite.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/interface/openplatform/article/dao"
  5. artmdl "go-common/app/interface/openplatform/article/model"
  6. favmdl "go-common/app/service/main/favorite/model"
  7. "go-common/library/log"
  8. )
  9. // AddFavorite add article favorite .
  10. func (s *Service) AddFavorite(c context.Context, mid, aid, fid int64, ip string) (err error) {
  11. if err = s.checkArticle(c, aid); err != nil {
  12. return
  13. }
  14. arg := &favmdl.ArgAdd{Type: favmdl.Article, Mid: mid, Oid: aid, Fid: fid}
  15. if err = s.favRPC.Add(c, arg); err != nil {
  16. dao.PromError("rpc:添加收藏")
  17. log.Error("s.favRPC.Add(%+v) error(%+v)", arg, err)
  18. }
  19. return
  20. }
  21. // DelFavorite del article favorite .
  22. func (s *Service) DelFavorite(c context.Context, mid, aid, fid int64, ip string) (err error) {
  23. arg := &favmdl.ArgDel{Type: favmdl.Article, Mid: mid, Oid: aid, Fid: fid}
  24. if err = s.favRPC.Del(c, arg); err != nil {
  25. dao.PromError("rpc:删除收藏")
  26. log.Error("s.favRPC.Del(%+v) error(%+v)", arg, err)
  27. }
  28. return
  29. }
  30. // Favorites article favorites.
  31. func (s *Service) Favorites(c context.Context, mid, fid int64, pn, ps int, ip string) (favs *favmdl.Favorites, err error) {
  32. arg := &favmdl.ArgFavs{Type: favmdl.Article, Mid: mid, Fid: fid, Pn: pn, Ps: ps}
  33. if favs, err = s.favRPC.Favorites(c, arg); err != nil {
  34. dao.PromError("rpc:获取收藏列表")
  35. log.Error("s.favRPC.Favorites(%+v) error(%+v)", arg, err)
  36. }
  37. return
  38. }
  39. // IsFav return user is fav article
  40. func (s *Service) IsFav(c context.Context, mid, aid int64) (res bool, err error) {
  41. arg := &favmdl.ArgIsFav{Type: favmdl.Article, Mid: mid, Oid: aid}
  42. if res, err = s.favRPC.IsFav(c, arg); err != nil {
  43. dao.PromError("rpc:是否已收藏")
  44. log.Error("s.favRPC.IsFav(%+v) error(%+v)", arg, err)
  45. }
  46. return
  47. }
  48. // Favs gets user favorite article list.
  49. func (s *Service) Favs(c context.Context, mid, fid int64, pn, ps int, ip string) (favs []*artmdl.Favorite, page *artmdl.Page, err error) {
  50. var (
  51. a *artmdl.Meta
  52. ok bool
  53. fs *favmdl.Favorites
  54. aids []int64
  55. as = make(map[int64]*artmdl.Meta)
  56. ts = make(map[int64]int64)
  57. )
  58. favs = make([]*artmdl.Favorite, 0)
  59. if fs, err = s.Favorites(c, mid, fid, pn, ps, ip); err != nil {
  60. return
  61. }
  62. page = &artmdl.Page{
  63. Pn: fs.Page.Num,
  64. Ps: fs.Page.Size,
  65. Total: fs.Page.Count,
  66. }
  67. if len(fs.List) == 0 {
  68. return
  69. }
  70. aids = make([]int64, 0, len(fs.List))
  71. for _, v := range fs.List {
  72. aids = append(aids, v.Oid)
  73. ts[v.Oid] = v.MTime.Time().Unix()
  74. }
  75. if as, err = s.ArticleMetas(c, aids); err != nil {
  76. return
  77. }
  78. for _, aid := range aids {
  79. var (
  80. valid = true
  81. meta *artmdl.Meta
  82. )
  83. if a, ok = as[aid]; !ok {
  84. meta = &artmdl.Meta{ID: aid}
  85. valid = false
  86. } else {
  87. meta = a
  88. }
  89. favs = append(favs, &artmdl.Favorite{
  90. Meta: meta,
  91. FavoriteTime: ts[aid],
  92. Valid: valid,
  93. })
  94. }
  95. return
  96. }
  97. // ValidFavs get valid favorites
  98. func (s *Service) ValidFavs(c context.Context, mid, fid int64, pn, ps int, ip string) (res []*artmdl.Favorite, page *artmdl.Page, err error) {
  99. defer func() {
  100. if res == nil {
  101. res = make([]*artmdl.Favorite, 0)
  102. }
  103. }()
  104. var favs []*artmdl.Favorite
  105. if favs, page, err = s.Favs(c, mid, fid, pn, ps, ip); err != nil {
  106. return
  107. }
  108. for _, fav := range favs {
  109. if fav.Valid {
  110. res = append(res, fav)
  111. }
  112. }
  113. if page.Total <= ps {
  114. page.Total -= (len(favs) - len(res))
  115. }
  116. return
  117. }