relation.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package relation
  2. import (
  3. "context"
  4. "fmt"
  5. "hash/crc32"
  6. "sort"
  7. "go-common/app/interface/main/app-interface/model/live"
  8. model "go-common/app/interface/main/app-interface/model/relation"
  9. account "go-common/app/service/main/account/model"
  10. relation "go-common/app/service/main/relation/model"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. "go-common/library/sync/errgroup"
  14. )
  15. var (
  16. _emptyFollowings = []*model.Following{}
  17. _emptyTagInfos = []*model.Tag{}
  18. )
  19. // Followings get user followings list.
  20. func (s *Service) Followings(c context.Context, vmid, mid int64, pn, ps int, version uint64, order string) (f []*model.Following, crc32v uint32, total int, err error) {
  21. res, err := s.relDao.Followings(c, vmid)
  22. if err != nil {
  23. return
  24. }
  25. total = len(res)
  26. if order == "asc" {
  27. sort.Sort(model.ByMTime(res))
  28. }
  29. start, end := (pn-1)*ps, pn*ps
  30. switch {
  31. case start >= len(res):
  32. res = res[:0]
  33. case end >= len(res):
  34. res = res[start:]
  35. default:
  36. res = res[start:end]
  37. }
  38. if len(res) == 0 {
  39. f = _emptyFollowings
  40. return
  41. }
  42. data := []byte(fmt.Sprintf("%s", res))
  43. crc32v = crc32.Checksum(data, crc32.IEEETable)
  44. if uint64(crc32v) == version {
  45. err = ecode.NotModified
  46. return
  47. }
  48. var fids []int64
  49. for _, r := range res {
  50. fids = append(fids, r.Mid)
  51. }
  52. var (
  53. flm map[int64]*relation.Following
  54. cardm map[int64]*account.Card
  55. sm map[int64]*live.Status
  56. )
  57. g, ctx := errgroup.WithContext(c)
  58. g.Go(func() (err error) {
  59. cardm, err = s.accDao.Cards3(ctx, fids)
  60. return
  61. })
  62. // !self, compute !self user and up's followings' attr
  63. if mid != vmid && mid != 0 {
  64. g.Go(func() (err error) {
  65. if flm, err = s.relDao.Relations(ctx, mid, fids); err != nil {
  66. log.Error("%+v", err)
  67. err = nil
  68. }
  69. return
  70. })
  71. }
  72. g.Go(func() error {
  73. sm, _ = s.liveDao.StatusInfo(ctx, fids)
  74. return nil
  75. })
  76. if err = g.Wait(); err != nil {
  77. log.Error("%+v", err)
  78. return
  79. }
  80. f = make([]*model.Following, 0, len(res))
  81. for _, r := range res {
  82. fl := &model.Following{Following: r}
  83. if _, ok := cardm[r.Mid]; ok {
  84. fl.Face = cardm[r.Mid].Face
  85. fl.Uname = cardm[r.Mid].Name
  86. fl.Sign = cardm[r.Mid].Sign
  87. if mid != vmid {
  88. if f, ok := flm[r.Mid]; ok {
  89. fl.Attribute = f.Attribute
  90. } else {
  91. fl.Attribute = relation.AttrNoRelation
  92. }
  93. }
  94. fl.OfficialVerify = cardm[r.Mid].Official
  95. fl.Vip.Type = int(cardm[r.Mid].Vip.Type)
  96. fl.Vip.VipStatus = int(cardm[r.Mid].Vip.Status)
  97. fl.Vip.DueDate = cardm[r.Mid].Vip.DueDate
  98. if status, ok := sm[r.Mid]; ok {
  99. fl.Live = status.LiveStatus
  100. }
  101. }
  102. f = append(f, fl)
  103. }
  104. return
  105. }
  106. func (s *Service) Tag(c context.Context, mid, tid int64, pn, ps int) (t []*model.Tag, err error) {
  107. mids, err := s.relDao.Tag(c, mid, tid)
  108. if err != nil {
  109. return
  110. }
  111. start, end := (pn-1)*ps, pn*ps
  112. switch {
  113. case start >= len(mids):
  114. mids = mids[:0]
  115. case end >= len(mids):
  116. mids = mids[start:]
  117. default:
  118. mids = mids[start:end]
  119. }
  120. if len(mids) == 0 {
  121. t = _emptyTagInfos
  122. return
  123. }
  124. var (
  125. cardm map[int64]*account.Card
  126. sm map[int64]*live.Status
  127. )
  128. g, ctx := errgroup.WithContext(c)
  129. g.Go(func() (err error) {
  130. cardm, err = s.accDao.Cards3(ctx, mids)
  131. return
  132. })
  133. g.Go(func() error {
  134. sm, _ = s.liveDao.StatusInfo(ctx, mids)
  135. return nil
  136. })
  137. if err = g.Wait(); err != nil {
  138. log.Error("%+v", err)
  139. return
  140. }
  141. t = make([]*model.Tag, 0, len(mids))
  142. for _, mid = range mids {
  143. tag := &model.Tag{Mid: mid}
  144. if _, ok := cardm[mid]; ok {
  145. tag.Mid = mid
  146. tag.Face = cardm[mid].Face
  147. tag.Uname = cardm[mid].Name
  148. tag.Sign = cardm[mid].Sign
  149. tag.OfficialVerify = cardm[mid].Official
  150. tag.Vip.Type = int(cardm[mid].Vip.Type)
  151. tag.Vip.VipStatus = int(cardm[mid].Vip.Status)
  152. tag.Vip.DueDate = cardm[mid].Vip.DueDate
  153. }
  154. if status, ok := sm[mid]; ok {
  155. tag.Live = status.LiveStatus
  156. }
  157. t = append(t, tag)
  158. }
  159. return
  160. }