relations.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/admin/main/relation/model"
  5. relationPB "go-common/app/service/main/relation/api"
  6. )
  7. // Followers is.
  8. func (s *Service) Followers(ctx context.Context, param *model.FollowersParam) (*model.FollowersListPage, error) {
  9. list, err := s.dao.Followers(ctx, param.Fid, param.Mid)
  10. if err != nil {
  11. return nil, err
  12. }
  13. list.OrderByMTime(param.Desc())
  14. from, err := model.ParseTime(param.MTimeFrom)
  15. if err == nil {
  16. list = list.FilterMTimeFrom(from)
  17. }
  18. to, err := model.ParseTime(param.MTimeTo)
  19. if err == nil {
  20. list = list.FilterMTimeTo(to)
  21. }
  22. plist := list.Paginate(param.PS*(param.PN-1), param.PS)
  23. flist := plist.FollowersList()
  24. uids := make([]int64, 0, len(flist)*2)
  25. for _, r := range flist {
  26. uids = append(uids, r.Mid)
  27. uids = append(uids, r.Fid)
  28. }
  29. uinfos, err := s.dao.RPCInfos(ctx, uids)
  30. if err != nil {
  31. return nil, err
  32. }
  33. for _, r := range flist {
  34. if mi, ok := uinfos[r.Mid]; ok {
  35. r.MemberName = mi.Name
  36. }
  37. }
  38. for _, r := range flist {
  39. if fi, ok := uinfos[r.Fid]; ok {
  40. r.FollowerName = fi.Name
  41. }
  42. }
  43. page := &model.FollowersListPage{}
  44. page.Sort = param.Sort
  45. page.Order = param.Order
  46. page.List = flist
  47. page.TotalCount = len(list)
  48. page.PN = param.PN
  49. page.PS = param.PS
  50. return page, nil
  51. }
  52. // Followings is.
  53. func (s *Service) Followings(ctx context.Context, param *model.FollowingsParam) (*model.FollowingsListPage, error) {
  54. list, err := s.dao.Followings(ctx, param.Mid, param.Fid)
  55. if err != nil {
  56. return nil, err
  57. }
  58. list.OrderByMTime(param.Desc())
  59. from, err := model.ParseTime(param.MTimeFrom)
  60. if err == nil {
  61. list = list.FilterMTimeFrom(from)
  62. }
  63. to, err := model.ParseTime(param.MTimeTo)
  64. if err == nil {
  65. list = list.FilterMTimeTo(to)
  66. }
  67. plist := list.Paginate(param.PS*(param.PN-1), param.PS)
  68. flist := plist.FollowingsList()
  69. uids := make([]int64, 0, len(flist)*2)
  70. for _, r := range flist {
  71. uids = append(uids, r.Mid)
  72. uids = append(uids, r.Fid)
  73. }
  74. minfos, err := s.dao.RPCInfos(ctx, uids)
  75. if err != nil {
  76. return nil, err
  77. }
  78. for _, r := range flist {
  79. if mi, ok := minfos[r.Mid]; ok {
  80. r.MemberName = mi.Name
  81. }
  82. }
  83. for _, r := range flist {
  84. if fi, ok := minfos[r.Fid]; ok {
  85. r.FollowingName = fi.Name
  86. }
  87. }
  88. page := &model.FollowingsListPage{}
  89. page.Sort = param.Sort
  90. page.Order = param.Order
  91. page.List = flist
  92. page.TotalCount = len(list)
  93. page.PN = param.PN
  94. page.PS = param.PS
  95. return page, nil
  96. }
  97. // Stat is
  98. func (s *Service) Stat(ctx context.Context, param *model.ArgMid) (*relationPB.StatReply, error) {
  99. return s.dao.Stat(ctx, param.Mid)
  100. }
  101. // Stats is
  102. func (s *Service) Stats(ctx context.Context, param *model.ArgMids) (map[int64]*relationPB.StatReply, error) {
  103. return s.dao.Stats(ctx, param.Mids)
  104. }