article.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/interface/main/web/conf"
  5. "go-common/app/interface/main/web/model"
  6. artmdl "go-common/app/interface/openplatform/article/model"
  7. accmdl "go-common/app/service/main/account/api"
  8. "go-common/library/log"
  9. "go-common/library/net/metadata"
  10. )
  11. const (
  12. _sortNew = 1
  13. _firstPn = 1
  14. _samplePn = 1
  15. _samplePs = 1
  16. _cacheCnt = 20
  17. )
  18. var (
  19. _emptyArticleList = make([]*model.Meta, 0)
  20. _emptyAuthorList = make([]*model.Info, 0)
  21. _emptyArtMetas = make([]*artmdl.Meta, 0)
  22. )
  23. // ArticleList get article list.
  24. func (s *Service) ArticleList(c context.Context, rid, mid int64, sort, pn, ps int, aids []int64) (res []*model.Meta, err error) {
  25. var (
  26. artMetas []*artmdl.Meta
  27. ip = metadata.String(c, metadata.RemoteIP)
  28. )
  29. if pn == _firstPn {
  30. var arts []*artmdl.Meta
  31. arg := &artmdl.ArgRecommends{Aids: aids, Cid: rid, Pn: _firstPn, Ps: _cacheCnt, Sort: sort, RealIP: ip}
  32. if arts, err = s.art.Recommends(c, arg); err != nil {
  33. log.Error("s.art.Recommends(%d,%d,%d,%d) error(%v)", rid, pn, ps, sort, err)
  34. err = nil
  35. } else if len(arts) > 0 {
  36. s.cache.Do(c, func(c context.Context) {
  37. s.dao.SetArticleListCache(c, rid, sort, arts)
  38. })
  39. } else {
  40. arts, err = s.dao.ArticleListCache(c, rid, sort)
  41. }
  42. if len(arts) > ps {
  43. artMetas = arts[:ps-1]
  44. } else {
  45. artMetas = arts
  46. }
  47. } else {
  48. arg := &artmdl.ArgRecommends{Aids: aids, Cid: rid, Pn: pn, Ps: ps, Sort: sort, RealIP: ip}
  49. if artMetas, err = s.art.Recommends(c, arg); err != nil {
  50. log.Error("s.art.Recommends(%d,%d,%d,%d) error(%v)", rid, pn, ps, sort, err)
  51. return
  52. }
  53. }
  54. if len(artMetas) == 0 {
  55. res = _emptyArticleList
  56. } else {
  57. var item *model.Meta
  58. if mid > 0 {
  59. var (
  60. likes map[int64]int
  61. aids []int64
  62. )
  63. for _, art := range artMetas {
  64. if art != nil {
  65. aids = append(aids, art.ID)
  66. }
  67. }
  68. if likes, err = s.art.HadLikesByMid(c, &artmdl.ArgMidAids{Mid: mid, Aids: aids, RealIP: ip}); err != nil {
  69. log.Error("s.art.HadLikesByMid(%d,%v) error(%v)", mid, aids, err)
  70. err = nil
  71. } else {
  72. for _, art := range artMetas {
  73. if art != nil {
  74. if like, ok := likes[art.ID]; ok {
  75. item = &model.Meta{Meta: art, Like: like}
  76. } else {
  77. item = &model.Meta{Meta: art}
  78. }
  79. res = append(res, item)
  80. }
  81. }
  82. }
  83. } else {
  84. for _, art := range artMetas {
  85. if art != nil {
  86. res = append(res, &model.Meta{Meta: art})
  87. }
  88. }
  89. }
  90. }
  91. return
  92. }
  93. // ArticleUpList get article up list.
  94. func (s *Service) ArticleUpList(c context.Context, mid int64) (res []*model.Info, err error) {
  95. if res, err = s.articleUps(c, mid); err != nil {
  96. err = nil
  97. } else if len(res) > 0 {
  98. s.cache.Do(c, func(c context.Context) {
  99. s.dao.SetArticleUpListCache(c, res)
  100. })
  101. return
  102. }
  103. res, err = s.dao.ArticleUpListCache(c)
  104. if len(res) == 0 {
  105. res = _emptyAuthorList
  106. }
  107. return
  108. }
  109. // Categories get article categories list
  110. func (s *Service) Categories(c context.Context) (res *artmdl.Categories, err error) {
  111. ip := metadata.String(c, metadata.RemoteIP)
  112. if res, err = s.art.Categories(c, &artmdl.ArgIP{RealIP: ip}); err != nil {
  113. log.Error("s.art.Categories error(%v)", err)
  114. }
  115. return
  116. }
  117. func (s *Service) articleUps(c context.Context, mid int64) (res []*model.Info, err error) {
  118. var (
  119. mids []int64
  120. list []*artmdl.Meta
  121. cardsReply *accmdl.CardsReply
  122. relaReply *accmdl.RelationsReply
  123. ip = metadata.String(c, metadata.RemoteIP)
  124. )
  125. res = make([]*model.Info, 0)
  126. arg := &artmdl.ArgRecommends{Sort: _sortNew, Pn: 1, Ps: conf.Conf.Rule.ArtUpListGetCnt, RealIP: ip}
  127. if list, err = s.art.Recommends(c, arg); err != nil {
  128. log.Error("s.art.Recommends() error(%v)", err)
  129. return
  130. }
  131. listMap := make(map[int64]*artmdl.Meta, conf.Conf.Rule.ArtUpListCnt)
  132. for _, v := range list {
  133. if len(listMap) == conf.Conf.Rule.ArtUpListCnt {
  134. break
  135. }
  136. if _, ok := listMap[v.Author.Mid]; ok {
  137. continue
  138. }
  139. listMap[v.Author.Mid] = v
  140. mids = append(mids, v.Author.Mid)
  141. }
  142. if cardsReply, err = s.accClient.Cards3(c, &accmdl.MidsReq{Mids: mids}); err != nil {
  143. log.Error("s.accClient.Cards3(%v) error(%v)", mids, err)
  144. return
  145. }
  146. if mid > 0 {
  147. if relaReply, err = s.accClient.Relations3(c, &accmdl.RelationsReq{Mid: mid, Owners: mids, RealIp: ip}); err != nil {
  148. log.Error("s.accClient.Relations3(%d,%v) error(%v)", mid, mids, err)
  149. err = nil
  150. }
  151. }
  152. for _, mid := range mids {
  153. if card, ok := cardsReply.Cards[mid]; ok {
  154. info := &model.Info{ID: listMap[mid].ID, Title: listMap[mid].Title, PublishTime: listMap[mid].PublishTime}
  155. info.FromCard(card)
  156. if relaReply != nil {
  157. if relation, ok := relaReply.Relations[mid]; ok {
  158. info.Following = relation.Following
  159. }
  160. }
  161. res = append(res, info)
  162. }
  163. }
  164. return
  165. }
  166. // NewCount get new publish article count
  167. func (s *Service) NewCount(c context.Context, pubTime int64) (count int64, err error) {
  168. ip := metadata.String(c, metadata.RemoteIP)
  169. if count, err = s.art.NewArticleCount(c, &artmdl.ArgNewArt{PubTime: pubTime, RealIP: ip}); err != nil {
  170. log.Error("s.art.NewArticleCount(%d) error(%v)", pubTime, err)
  171. }
  172. return
  173. }
  174. // UpMoreArts get up more articles
  175. func (s *Service) UpMoreArts(c context.Context, aid int64) (res []*artmdl.Meta, err error) {
  176. ip := metadata.String(c, metadata.RemoteIP)
  177. if res, err = s.art.UpMoreArts(c, &artmdl.ArgAid{Aid: aid, RealIP: ip}); err != nil {
  178. log.Error("s.art.UpMoreArts(%d) error(%v)", aid, err)
  179. return
  180. }
  181. if len(res) == 0 {
  182. res = _emptyArtMetas
  183. }
  184. return
  185. }