author.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/openplatform/article/dao"
  6. "go-common/app/interface/openplatform/article/model"
  7. account "go-common/app/service/main/account/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _serviceArea = "article_info"
  13. _recType = "up_rec"
  14. _rapagesize = 20
  15. )
  16. // UpdateAuthorCache update author cache
  17. func (s *Service) UpdateAuthorCache(c context.Context, mid int64) (err error) {
  18. err = s.dao.DelCacheAuthor(c, mid)
  19. dao.PromInfo("author:更新作者状态")
  20. return
  21. }
  22. // AddAuthor add author to db
  23. func (s *Service) AddAuthor(c context.Context, mid int64) (err error) {
  24. if len(s.activities) == 0 {
  25. err = ecode.ArtNoActivity
  26. return
  27. }
  28. res, forbid, _ := s.IsAuthor(c, mid)
  29. if res {
  30. return
  31. }
  32. if forbid {
  33. err = ecode.ArtUserDisabled
  34. return
  35. }
  36. author, err := s.dao.RawAuthor(c, mid)
  37. if err != nil {
  38. return
  39. }
  40. if author != nil {
  41. if (author.State == model.AuthorStateReject) || (author.State == model.AuthorStateClose) {
  42. err = ecode.ArtAuthorReject
  43. return
  44. }
  45. }
  46. err = s.dao.AddAuthor(c, mid)
  47. if err == nil {
  48. s.dao.DelCacheAuthor(c, mid)
  49. }
  50. return
  51. }
  52. // IsAuthor check that whether user has permission to write article.
  53. func (s *Service) IsAuthor(c context.Context, mid int64) (res bool, forbid bool, err error) {
  54. var level int
  55. forbid, level, _ = s.UserDisabled(c, mid)
  56. if forbid {
  57. return
  58. }
  59. var limit *model.AuthorLimit
  60. limit, _ = s.dao.Author(c, mid)
  61. if limit.Pass() {
  62. res = true
  63. return
  64. }
  65. if limit.Forbid() {
  66. return
  67. }
  68. if level >= 2 {
  69. res = true
  70. return
  71. }
  72. res, err = s.isUpper(c, mid)
  73. return
  74. }
  75. // Authors recommends similar authors by categories
  76. func (s *Service) Authors(c context.Context, mid int64, author int64) (res []*model.AccountCard) {
  77. var (
  78. categories []int64
  79. authors []int64
  80. attentions map[int64]bool
  81. filterAuthors []int64
  82. blacks map[int64]struct{}
  83. mids []int64
  84. err error
  85. )
  86. if categories, err = s.dao.AuthorMostCategories(c, author); err != nil {
  87. return
  88. }
  89. for _, category := range categories {
  90. if authors, err = s.dao.CategoryAuthors(c, category, s.c.Article.RecommendAuthors); err != nil {
  91. return
  92. }
  93. if attentions, err = s.isAttentions(c, mid, authors); err != nil {
  94. return
  95. }
  96. if blacks, err = s.isBlacks(c, mid, authors); err != nil {
  97. return
  98. }
  99. for _, a := range authors {
  100. if _, ok := blacks[a]; !attentions[a] && !ok {
  101. filterAuthors = append(filterAuthors, a)
  102. }
  103. }
  104. }
  105. for _, filterAuthor := range filterAuthors {
  106. if filterAuthor == mid || filterAuthor == author {
  107. continue
  108. }
  109. if forbid, _, _ := s.UserDisabled(c, filterAuthor); !forbid {
  110. mids = append(mids, filterAuthor)
  111. }
  112. }
  113. if len(mids) < 2 {
  114. return
  115. }
  116. for _, m := range mids {
  117. var (
  118. accountCard = &model.AccountCard{}
  119. profile *account.ProfileStat
  120. )
  121. if profile, err = s.accountRPC.ProfileWithStat3(c, &account.ArgMid{Mid: m}); err != nil {
  122. dao.PromError("article:ProfileWithStat3")
  123. log.Error("s.acc.ProfileWithStat3(%d) error %v", m, err)
  124. return
  125. }
  126. if profile != nil {
  127. accountCard.FromProfileStat(profile)
  128. }
  129. res = append(res, accountCard)
  130. }
  131. if len(res) > 3 {
  132. res = res[0:3]
  133. }
  134. return
  135. }
  136. // LevelRequired .
  137. func (s *Service) LevelRequired(c context.Context, mid int64) (ok bool, err error) {
  138. var (
  139. card *account.Card
  140. arg = account.ArgMid{Mid: mid}
  141. )
  142. if card, err = s.accountRPC.Card3(c, &arg); err != nil {
  143. dao.PromError("accountRPC.Card3")
  144. log.Error("s.LevelRequired.accountRPC.Card3(%d) error %v", mid, err)
  145. return
  146. }
  147. if card.Level >= 4 {
  148. ok = true
  149. }
  150. return
  151. }
  152. // RecommendAuthors get recommends from search.
  153. func (s *Service) RecommendAuthors(c context.Context, platform string, mobiApp string, device string, build int, clientIP string, userID int64, buvid string, mid int64) (res *model.RecommendAuthors, err error) {
  154. var ra []*model.RecommendAuthor
  155. if ra, err = s.dao.RecommendAuthors(c, platform, mobiApp, device, build, clientIP, userID, buvid, _recType, _serviceArea, _rapagesize, mid); err != nil {
  156. return
  157. }
  158. if ra == nil {
  159. return
  160. }
  161. res = &model.RecommendAuthors{}
  162. for _, r := range ra {
  163. var (
  164. author = &model.RecAuthor{}
  165. profile *account.ProfileStat
  166. )
  167. if profile, err = s.accountRPC.ProfileWithStat3(c, &account.ArgMid{Mid: r.UpID}); err != nil {
  168. dao.PromError("article:ProfileWithStat3")
  169. log.Error("s.acc.ProfileWithStat3(%d) error %v", r.UpID, err)
  170. return
  171. }
  172. if profile != nil {
  173. author.AccountCard.FromProfileStat(profile)
  174. }
  175. if r.RecReason == "" {
  176. var (
  177. view int64
  178. fans int
  179. viewStr, fansStr string
  180. )
  181. fans = author.Fans
  182. if view, err = s.readCount(c, r.UpID); err != nil {
  183. continue
  184. }
  185. if fans < 1e4 {
  186. fansStr = fmt.Sprintf("粉丝:%d", fans)
  187. } else if fans < 1e8 {
  188. fansStr = fmt.Sprintf("粉丝:%.1f万", float64(fans/1e4))
  189. } else {
  190. fansStr = fmt.Sprintf("粉丝:%.1f亿", float64(fans/1e8))
  191. }
  192. if view < 1e4 {
  193. viewStr = fmt.Sprintf("阅读:%d", view)
  194. } else if view < 1e8 {
  195. viewStr = fmt.Sprintf("阅读:%.1f万", float64(view/1e4))
  196. } else {
  197. viewStr = fmt.Sprintf("阅读:%.1f亿", float64(view/1e8))
  198. }
  199. r.RecReason = fansStr + " " + viewStr
  200. }
  201. author.RecReason = r.RecReason
  202. res.Authors = append(res.Authors, author)
  203. }
  204. res.Count = len(res.Authors)
  205. return
  206. }
  207. func (s *Service) readCount(c context.Context, mid int64) (res int64, err error) {
  208. var (
  209. stat *model.UpStat
  210. st model.UpStat
  211. )
  212. if stat, err = s.dao.CacheUpStatDaily(c, mid); err != nil {
  213. dao.PromError("article:CacheUpStatDaily")
  214. }
  215. if stat != nil {
  216. res = stat.View
  217. return
  218. }
  219. if st, err = s.dao.UpStat(c, mid); err != nil {
  220. dao.PromError("article:获取作者文章数")
  221. }
  222. res = st.View
  223. return
  224. }