account.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/interface/main/web/model"
  5. artmdl "go-common/app/interface/openplatform/article/model"
  6. accmdl "go-common/app/service/main/account/api"
  7. "go-common/app/service/main/archive/model/archive"
  8. relmdl "go-common/app/service/main/relation/model"
  9. "go-common/library/log"
  10. "go-common/library/net/metadata"
  11. "go-common/library/sync/errgroup"
  12. )
  13. const _cardBakCacheRand = 10
  14. // Attentions get attention list.
  15. func (s *Service) Attentions(c context.Context, mid int64) (rs []int64, err error) {
  16. var (
  17. attentions []*relmdl.Following
  18. remoteIP = metadata.String(c, metadata.RemoteIP)
  19. )
  20. if attentions, err = s.relation.Followings(c, &relmdl.ArgMid{Mid: mid, RealIP: remoteIP}); err != nil {
  21. log.Error("s.relation.Followings(%d,%s) error %v", mid, remoteIP, err)
  22. } else {
  23. rs = make([]int64, 0)
  24. for _, v := range attentions {
  25. rs = append(rs, v.Mid)
  26. }
  27. }
  28. return
  29. }
  30. // Card get card relation archive count data.
  31. func (s *Service) Card(c context.Context, mid, loginID int64, topPhoto, article bool) (rs *model.Card, err error) {
  32. var (
  33. cardReply *accmdl.CardReply
  34. relResp *accmdl.RelationReply
  35. card *model.AccountCard
  36. space *model.Space
  37. arcCount int
  38. group *errgroup.Group
  39. infoErr, statErr, spaceErr, relErr, upcErr, artErr error
  40. cacheRs *model.Card
  41. remoteIP = metadata.String(c, metadata.RemoteIP)
  42. )
  43. relation := &accmdl.RelationReply{}
  44. stat := &relmdl.Stat{}
  45. upArts := &artmdl.UpArtMetas{}
  46. group, errCtx := errgroup.WithContext(c)
  47. card = new(model.AccountCard)
  48. card.Attentions = make([]int64, 0)
  49. group.Go(func() error {
  50. if cardReply, infoErr = s.accClient.Card3(errCtx, &accmdl.MidReq{Mid: mid}); infoErr != nil {
  51. log.Error("s.accClient.Card3(%d,%s) error %v", mid, remoteIP, infoErr)
  52. } else {
  53. card.FromCard(cardReply.Card)
  54. }
  55. return nil
  56. })
  57. group.Go(func() error {
  58. if stat, statErr = s.relation.Stat(errCtx, &relmdl.ArgMid{Mid: mid, RealIP: remoteIP}); statErr != nil {
  59. log.Error("s.relation.Stat(%d) error(%v)", mid, statErr)
  60. } else {
  61. card.Fans = int(stat.Follower)
  62. card.Attention = int(stat.Following)
  63. card.Friend = int(stat.Following)
  64. }
  65. return nil
  66. })
  67. if topPhoto {
  68. group.Go(func() error {
  69. space, spaceErr = s.dao.TopPhoto(errCtx, mid)
  70. return nil
  71. })
  72. }
  73. if loginID > 0 {
  74. group.Go(func() error {
  75. if relResp, relErr = s.accClient.Relation3(errCtx, &accmdl.RelationReq{Mid: loginID, Owner: mid, RealIp: remoteIP}); relErr != nil {
  76. log.Error("s.accClient.Relation3(%d,%d,%s) error %v", loginID, mid, remoteIP, relErr)
  77. } else if relResp != nil {
  78. relation = relResp
  79. }
  80. return nil
  81. })
  82. }
  83. group.Go(func() error {
  84. if arcCount, upcErr = s.arc.UpCount2(errCtx, &archive.ArgUpCount2{Mid: mid}); upcErr != nil {
  85. log.Error("s.arc.UpCount2(%d) error %v", mid, upcErr)
  86. }
  87. return nil
  88. })
  89. if article {
  90. group.Go(func() error {
  91. if upArts, artErr = s.art.UpArtMetas(errCtx, &artmdl.ArgUpArts{Mid: mid, Pn: _samplePn, Ps: _samplePs, RealIP: remoteIP}); artErr != nil {
  92. log.Error("s.art.UpArtMetas(%d) error(%v)", mid, artErr)
  93. }
  94. if upArts == nil {
  95. upArts = &artmdl.UpArtMetas{Count: 0}
  96. }
  97. return nil
  98. })
  99. }
  100. group.Wait()
  101. addCache := true
  102. if infoErr != nil || (topPhoto && spaceErr != nil) || (loginID > 0 && relErr != nil) || upcErr != nil {
  103. if cacheRs, err = s.dao.CardBakCache(c, mid); err != nil {
  104. addCache = false
  105. log.Error("s.dao.CardBakCache(%d) error (%v)", mid, err)
  106. err = nil
  107. } else if cacheRs != nil {
  108. if infoErr != nil {
  109. card = cacheRs.Card
  110. }
  111. if statErr != nil {
  112. stat = &relmdl.Stat{Follower: cacheRs.Follower}
  113. }
  114. if topPhoto && spaceErr != nil {
  115. space = cacheRs.Space
  116. }
  117. if loginID > 0 && relErr != nil {
  118. relation = &accmdl.RelationReply{Following: cacheRs.Following}
  119. }
  120. if upcErr != nil {
  121. arcCount = cacheRs.ArchiveCount
  122. }
  123. if artErr != nil {
  124. upArts = &artmdl.UpArtMetas{Count: cacheRs.ArticleCount}
  125. }
  126. }
  127. if topPhoto && space == nil {
  128. space = &model.Space{SImg: s.c.DefaultTop.SImg, LImg: s.c.DefaultTop.LImg}
  129. }
  130. }
  131. rs = &model.Card{
  132. Card: card,
  133. Space: space,
  134. Following: relation.Following,
  135. ArchiveCount: arcCount,
  136. ArticleCount: upArts.Count,
  137. Follower: stat.Follower,
  138. }
  139. if addCache {
  140. s.cache.Do(c, func(c context.Context) {
  141. if s.r.Intn(_cardBakCacheRand) == 1 {
  142. s.dao.SetCardBakCache(c, mid, rs)
  143. }
  144. })
  145. }
  146. return
  147. }