search.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/interface/main/web/model"
  5. accmdl "go-common/app/service/main/account/api"
  6. relmdl "go-common/app/service/main/relation/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "go-common/library/sync/errgroup"
  10. )
  11. const _searchEggWebPlat = 6
  12. var _emptyUpRec = make([]*model.UpRecInfo, 0)
  13. // SearchAll search type all.
  14. func (s *Service) SearchAll(c context.Context, mid int64, arg *model.SearchAllArg, buvid, ua, typ string) (data *model.Search, err error) {
  15. data, err = s.dao.SearchAll(c, mid, arg, buvid, ua, typ)
  16. return
  17. }
  18. // SearchByType type video,bangumi,pgc,live,live_user,article,special,topic,bili_user,photo
  19. func (s *Service) SearchByType(c context.Context, mid int64, arg *model.SearchTypeArg, buvid, ua string) (res *model.SearchTypeRes, err error) {
  20. switch arg.SearchType {
  21. case model.SearchTypeVideo:
  22. if res, err = s.dao.SearchVideo(c, mid, arg, buvid, ua); err != nil {
  23. return
  24. }
  25. case model.SearchTypeBangumi:
  26. if res, err = s.dao.SearchBangumi(c, mid, arg, buvid, ua); err != nil {
  27. return
  28. }
  29. case model.SearchTypePGC:
  30. if res, err = s.dao.SearchPGC(c, mid, arg, buvid, ua); err != nil {
  31. return
  32. }
  33. case model.SearchTypeLive:
  34. if res, err = s.dao.SearchLive(c, mid, arg, buvid, ua); err != nil {
  35. return
  36. }
  37. case model.SearchTypeLiveRoom:
  38. if res, err = s.dao.SearchLiveRoom(c, mid, arg, buvid, ua); err != nil {
  39. return
  40. }
  41. case model.SearchTypeLiveUser:
  42. if res, err = s.dao.SearchLiveUser(c, mid, arg, buvid, ua); err != nil {
  43. return
  44. }
  45. case model.SearchTypeArticle:
  46. if res, err = s.dao.SearchArticle(c, mid, arg, buvid, ua); err != nil {
  47. return
  48. }
  49. case model.SearchTypeSpecial:
  50. if res, err = s.dao.SearchSpecial(c, mid, arg, buvid, ua); err != nil {
  51. return
  52. }
  53. case model.SearchTypeTopic:
  54. if res, err = s.dao.SearchTopic(c, mid, arg, buvid, ua); err != nil {
  55. return
  56. }
  57. case model.SearchTypeUser:
  58. if res, err = s.dao.SearchUser(c, mid, arg, buvid, ua); err != nil {
  59. return
  60. }
  61. case model.SearchTypePhoto:
  62. if res, err = s.dao.SearchPhoto(c, mid, arg, buvid, ua); err != nil {
  63. return
  64. }
  65. default:
  66. err = ecode.RequestErr
  67. return
  68. }
  69. return
  70. }
  71. // SearchRec search recommend data.
  72. func (s *Service) SearchRec(c context.Context, mid int64, pn, ps int, keyword, fromSource, buvid, ua string) (data *model.SearchRec, err error) {
  73. data, err = s.dao.SearchRec(c, mid, pn, ps, keyword, fromSource, buvid, ua)
  74. return
  75. }
  76. // SearchDefault get search default word.
  77. func (s *Service) SearchDefault(c context.Context, mid int64, fromSource, buvid, ua string) (data *model.SearchDefault, err error) {
  78. data, err = s.dao.SearchDefault(c, mid, fromSource, buvid, ua)
  79. return
  80. }
  81. // UpRec get up recommend
  82. func (s *Service) UpRec(c context.Context, mid int64, arg *model.SearchUpRecArg, buvid string) (data *model.UpRecData, err error) {
  83. var (
  84. ups []*model.SearchUpRecRes
  85. trackID string
  86. mids []int64
  87. cardsReply *accmdl.CardsReply
  88. cardErr error
  89. )
  90. if ups, trackID, err = s.dao.UpRecommend(c, mid, arg, buvid); err != nil {
  91. return
  92. }
  93. data = &model.UpRecData{TrackID: trackID}
  94. if len(ups) == 0 {
  95. data.List = _emptyUpRec
  96. return
  97. }
  98. for _, v := range ups {
  99. mids = append(mids, v.UpID)
  100. }
  101. relInfos := make(map[int64]*relmdl.Stat, len(mids))
  102. group, errCtx := errgroup.WithContext(c)
  103. group.Go(func() error {
  104. if cardsReply, cardErr = s.accClient.Cards3(errCtx, &accmdl.MidsReq{Mids: mids}); cardErr != nil {
  105. log.Error("UpRec s.accClient.Cards3(%v) error(%v)", mids, cardErr)
  106. return cardErr
  107. }
  108. return nil
  109. })
  110. group.Go(func() error {
  111. if relReply, relErr := s.relation.Stats(errCtx, &relmdl.ArgMids{Mids: mids}); relErr != nil {
  112. log.Error("UpRec s.relation.Stats(%d,%v) error(%v)", mid, mids, relErr)
  113. } else if relReply != nil {
  114. relInfos = relReply
  115. }
  116. return nil
  117. })
  118. if err = group.Wait(); err != nil {
  119. return
  120. }
  121. for _, v := range ups {
  122. if info, ok := cardsReply.Cards[v.UpID]; ok && info != nil && info.Silence == 0 {
  123. upInfo := &model.UpRecInfo{
  124. Mid: info.Mid,
  125. Name: info.Name,
  126. Face: info.Face,
  127. Official: info.Official,
  128. RecReason: v.RecReason,
  129. Tid: v.Tid,
  130. SecondTid: v.SecondTid,
  131. Sign: info.Sign,
  132. }
  133. upInfo.Vip.Type = info.Vip.Type
  134. upInfo.Vip.Status = info.Vip.Status
  135. if stat, ok := relInfos[v.UpID]; ok {
  136. upInfo.Follower = stat.Follower
  137. }
  138. if typ, ok := s.typeNames[int32(v.Tid)]; ok {
  139. upInfo.Tname = typ.Name
  140. }
  141. if typ, ok := s.typeNames[int32(v.SecondTid)]; ok {
  142. upInfo.SecondTname = typ.Name
  143. }
  144. data.List = append(data.List, upInfo)
  145. }
  146. }
  147. if len(data.List) == 0 {
  148. data.List = _emptyUpRec
  149. }
  150. return
  151. }
  152. //SearchEgg get search egg by egg id.
  153. func (s *Service) SearchEgg(c context.Context, eggID int64) (data *model.SearchEggRes, err error) {
  154. if _, ok := s.searchEggs[eggID]; !ok {
  155. err = ecode.NothingFound
  156. return
  157. }
  158. data = s.searchEggs[eggID]
  159. return
  160. }