fan.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package data
  2. import (
  3. "context"
  4. "sort"
  5. "go-common/app/interface/main/creative/model/data"
  6. "go-common/app/interface/main/creative/model/medal"
  7. account "go-common/app/service/main/account/model"
  8. "go-common/library/log"
  9. "go-common/library/sync/errgroup"
  10. )
  11. var (
  12. rankKeys = []string{data.PlayDuration, data.VideoAct, data.DynamicAct}
  13. )
  14. // UpFansAnalysisForWeb get web fans data.
  15. func (s *Service) UpFansAnalysisForWeb(c context.Context, mid int64, ip string) (res *data.WebFan, err error) {
  16. var (
  17. origin *data.WebFan
  18. rkList map[string][]*data.RankInfo
  19. mdlRank []*medal.FansRank
  20. )
  21. if origin, err = s.data.UpFansAnalysisForWeb(c, mid, data.Thirty); err != nil {
  22. log.Error("s.data.UpFansAnalysisForWeb err(%v)", err)
  23. return
  24. }
  25. if origin == nil {
  26. return
  27. }
  28. group, ctx := errgroup.WithContext(c)
  29. group.Go(func() (err error) {
  30. if rkList, err = s.getTopList(ctx, mid, origin.RankMap, ip); err != nil {
  31. log.Error("s.getTopList mid(%d)|err(%v)", mid, err)
  32. err = nil
  33. }
  34. return
  35. })
  36. group.Go(func() (err error) {
  37. if mdlRank, err = s.medal.Rank(ctx, mid); err != nil {
  38. log.Error("s.medal.Rank mid(%d)|err(%v)", mid, err)
  39. err = nil
  40. }
  41. return
  42. })
  43. group.Wait()
  44. if len(rkList) == 0 {
  45. log.Info("s.getTopList is empty rkList(%+v) ", rkList)
  46. rkList = make(map[string][]*data.RankInfo)
  47. }
  48. for _, key := range rankKeys {
  49. if v, ok := rkList[key]; ok {
  50. rkList[key] = v
  51. } else {
  52. rkList[key] = nil
  53. }
  54. }
  55. log.Info("s.getTopList rkList(%+v)|len(%d)", rkList, len(rkList))
  56. res = &data.WebFan{
  57. Summary: origin.Summary,
  58. Source: origin.Source,
  59. RankList: rkList,
  60. RankMedal: map[string][]*medal.FansRank{
  61. "medal": mdlRank,
  62. },
  63. }
  64. return
  65. }
  66. // sort mid list and get uname/avatar info
  67. func (s *Service) getTopList(c context.Context, mid int64, RankMap map[string]map[string]int32, ip string) (rkList map[string][]*data.RankInfo, err error) {
  68. rkList = make(map[string][]*data.RankInfo)
  69. for _, key := range rankKeys {
  70. vd, ok := RankMap[key] //map playduration videoact dynamicact top10 mids.
  71. if !ok {
  72. continue
  73. }
  74. sortK := make([]string, 0, 10)
  75. for key := range vd { //sort top mids by key, for example (dr2,dr1,dr3...)
  76. sortK = append(sortK, key)
  77. }
  78. sort.Slice(sortK, func(i, j int) bool { //for example (dr1,dr2,dr3...)
  79. return sortK[i] < sortK[j]
  80. })
  81. mids := make([]int64, 0, 10)
  82. for _, k := range sortK { //get sort mids slice.
  83. if mid, ok := vd[k]; ok {
  84. mids = append(mids, int64(mid))
  85. }
  86. }
  87. if len(mids) == 0 {
  88. continue
  89. }
  90. var (
  91. g, ctx = errgroup.WithContext(c)
  92. users map[int64]*account.Info
  93. followers map[int64]int
  94. )
  95. g.Go(func() error { //获取用户信息
  96. if users, err = s.acc.Infos(ctx, mids, ip); err != nil {
  97. log.Error("s.acc.Infos mid(%d)|mids(%v)|ip(%s)|err(%v)", mid, mids, ip, err)
  98. }
  99. return err
  100. })
  101. g.Go(func() error { //获取关注状态
  102. if followers, err = s.acc.Relations2(ctx, mid, mids, ip); err != nil {
  103. log.Error("s.acc.Relations mid(%d)|ip(%s)|err(%v)", mid, ip, err)
  104. }
  105. return err
  106. })
  107. if g.Wait() != nil {
  108. return
  109. }
  110. rkInfos := make([]*data.RankInfo, 0, 10)
  111. for _, m := range mids {
  112. ri := &data.RankInfo{}
  113. if v, ok := users[m]; ok { //get userinfo by sorted mids.
  114. ri.MID = v.Mid
  115. ri.Uname = v.Name
  116. ri.Photo = v.Face
  117. }
  118. if fl, ok := followers[m]; ok { //get relation by sorted mids.
  119. ri.Relation = fl
  120. }
  121. rkInfos = append(rkInfos, ri) //append top10 useinfo.
  122. if len(rkInfos) == 10 {
  123. break
  124. }
  125. }
  126. rkList[key] = rkInfos //map playduration videoact dynamicact top10 userinfo.
  127. }
  128. log.Info("getTopList mid(%d)|rkList(%+v) ", mid, rkList)
  129. return
  130. }
  131. //UpFansMedal for get fan medel count.
  132. func (s *Service) UpFansMedal(c context.Context, mid int64) (fansMdl *data.UpFansMedal, err error) {
  133. if fansMdl, err = s.data.UpFansMedal(c, mid); err != nil {
  134. log.Error("s.data.UpFansMedal mid(%d)|err(%v)", mid, err)
  135. }
  136. return
  137. }