rec_pool.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package v2
  2. import (
  3. "math"
  4. "strconv"
  5. v2pb "go-common/app/interface/live/app-interface/api/http/v2"
  6. "go-common/app/interface/live/app-interface/dao"
  7. xrf "go-common/app/service/live/xroom-feed/api"
  8. "go-common/library/sync/errgroup"
  9. "go-common/library/xstr"
  10. "context"
  11. "go-common/library/log"
  12. )
  13. const _appModuleType = 1
  14. // getRecPoolList
  15. func (s *IndexService) getRecPoolList(ctx context.Context, module int64, num int64, moduleExists []int64, otherExists []int64) (list map[int64]*xrf.RoomItem) {
  16. list = make(map[int64]*xrf.RoomItem)
  17. req := &xrf.RecPoolReq{
  18. ModuleType: module,
  19. PositionNum: num,
  20. ModuleExistRooms: xstr.JoinInts(moduleExists),
  21. OtherExistRooms: xstr.JoinInts(otherExists),
  22. From: "app-interface",
  23. }
  24. resp, ferr := s.xrfClient.RecPoolClient.GetList(ctx, req)
  25. if ferr != nil {
  26. log.Error("[rec_pool]GetList from xroom-feed err:%+v", ferr.Error())
  27. return
  28. }
  29. list = resp.List
  30. return
  31. }
  32. func (s *IndexService) getRecPoolAllPosition(ctx context.Context, moduleRoomIDs, otherRoomIDs []int64) (recPool map[int64]*v2pb.CommonRoomItem) {
  33. recPool = make(map[int64]*v2pb.CommonRoomItem)
  34. uids := make([]int64, 0)
  35. roomIds := make([]int64, 0)
  36. mapRoomIdToUid := make(map[int64]int64)
  37. roomItem := s.getRecPoolList(ctx, _appModuleType, 6, moduleRoomIDs, otherRoomIDs)
  38. if len(roomItem) <= 0 {
  39. log.Info("[getRecPoolAllPosition]getRecPoolList roomItem empty")
  40. return
  41. }
  42. for i, item := range roomItem {
  43. uids = append(uids, item.Uid)
  44. roomIds = append(roomIds, item.RoomId)
  45. mapRoomIdToUid[item.RoomId] = item.Uid
  46. recPool[i] = &v2pb.CommonRoomItem{
  47. Roomid: item.RoomId,
  48. Title: item.Title,
  49. Link: "/" + strconv.Itoa(int(item.RoomId)),
  50. AreaV2Id: item.AreaId,
  51. AreaV2Name: item.AreaName,
  52. AreaV2ParentId: item.ParentAreaId,
  53. AreaV2ParentName: item.ParentAreaName,
  54. Online: item.PopularityCount,
  55. Cover: s.getCover(item.Cover, item.Keyframe),
  56. RecType: item.RecType,
  57. AcceptQuality: []int64{2, 4},
  58. }
  59. }
  60. if len(recPool) <= 0 {
  61. return
  62. }
  63. extraInfo := s.getExtraDataForRoom(ctx, roomIds, uids, mapRoomIdToUid)
  64. for _, pool := range recPool {
  65. if _, ok := extraInfo[pool.Roomid]; ok {
  66. pool.Uname = extraInfo[pool.Roomid].UName
  67. pool.Face = extraInfo[pool.Roomid].Face
  68. pool.PendentRu = extraInfo[pool.Roomid].PendentRu
  69. pool.PendentRuPic = extraInfo[pool.Roomid].PendentRuPic
  70. pool.PendentRuColor = extraInfo[pool.Roomid].PendentRuColor
  71. }
  72. }
  73. return
  74. }
  75. func (s *IndexService) handleCommonRoomList(ctx context.Context, respMyIdol *v2pb.MMyIdol, respCommonRoomList []*v2pb.MRoomBlock, quality, build int64, platform string, device string) []*v2pb.MRoomBlock {
  76. moduleExistIds := make([]int64, 0)
  77. otherExistIds := make([]int64, 0)
  78. duplicateMap := make(map[int64]bool)
  79. for _, idol := range respMyIdol.List {
  80. otherExistIds = append(otherExistIds, idol.Roomid)
  81. duplicateMap[idol.Roomid] = true
  82. }
  83. for _, roomBlock := range respCommonRoomList {
  84. if roomBlock.ModuleInfo.Type == _recFormType || roomBlock.ModuleInfo.Type == _recSquareType {
  85. newRecRoomList := make([]*v2pb.CommonRoomItem, 0)
  86. for _, item := range roomBlock.List {
  87. if len(newRecRoomList) >= 24 {
  88. break
  89. }
  90. if _, ok := duplicateMap[item.Roomid]; !ok {
  91. newRecRoomList = append(newRecRoomList, item)
  92. moduleExistIds = append(moduleExistIds, item.Roomid)
  93. }
  94. }
  95. // 投放位
  96. if device != "pad" {
  97. recPool, recPoolRooms := s.fourTimeRecPoolForYumo(ctx, moduleExistIds, otherExistIds)
  98. log.Info("[handleCommonRoomList]投放位 recPool: %+v, moduleExistIds: %+v, otherExistIds:%+v", recPoolRooms, xstr.JoinInts(moduleExistIds), xstr.JoinInts(otherExistIds))
  99. duplicateRecMap := make(map[int64]bool)
  100. for _, id := range recPoolRooms {
  101. duplicateRecMap[id] = true
  102. }
  103. newRecFilterCommonList := make([]*v2pb.CommonRoomItem, 0)
  104. for _, room := range newRecRoomList {
  105. if room == nil {
  106. continue
  107. }
  108. if _, ok := duplicateRecMap[room.Roomid]; !ok {
  109. newRecFilterCommonList = append(newRecFilterCommonList, room)
  110. }
  111. }
  112. filterList := make([]*v2pb.CommonRoomItem, 0)
  113. for i := 0; i < 24; i++ {
  114. position := int64(i) + 1
  115. if item, ok := recPool[position]; ok {
  116. filterList = append(filterList, item)
  117. continue
  118. }
  119. if len(newRecFilterCommonList) <= 0 {
  120. continue
  121. }
  122. filterList = append(filterList, newRecFilterCommonList[0:1][0])
  123. newRecFilterCommonList = newRecFilterCommonList[1:]
  124. }
  125. roomBlock.List = filterList
  126. } else {
  127. roomBlock.List = newRecRoomList
  128. }
  129. }
  130. }
  131. // 拼playurl
  132. roomIds := make([]int64, 0)
  133. for _, commRoomBlock := range respCommonRoomList {
  134. for _, roomList := range commRoomBlock.List {
  135. roomIds = append(roomIds, roomList.Roomid)
  136. }
  137. }
  138. changeRoomListPlayURLMap := dao.BvcApi.GetPlayUrlMulti(ctx, roomIds, 0, quality, build, platform)
  139. for _, v := range respCommonRoomList {
  140. for _, vv := range v.List {
  141. if changeRoomListPlayURLMap[vv.Roomid] != nil {
  142. vv.AcceptQuality = changeRoomListPlayURLMap[vv.Roomid].AcceptQuality
  143. vv.CurrentQuality = changeRoomListPlayURLMap[vv.Roomid].CurrentQuality
  144. vv.PlayUrl = changeRoomListPlayURLMap[vv.Roomid].Url["h264"]
  145. vv.PlayUrlH265 = changeRoomListPlayURLMap[vv.Roomid].Url["h265"]
  146. }
  147. }
  148. }
  149. return respCommonRoomList
  150. }
  151. // 一次请求 变为四次请求xroom feed
  152. func (s *IndexService) fourTimeRecPoolForYumo(ctx context.Context, moduleExistIds []int64, otherExistIds []int64) (recPool map[int64]*v2pb.CommonRoomItem, recPoolRooms map[int64]int64) {
  153. recPool = make(map[int64]*v2pb.CommonRoomItem)
  154. recPoolRooms = make(map[int64]int64)
  155. pageSize := 6
  156. if len(moduleExistIds) < pageSize {
  157. return
  158. }
  159. page := int64(math.Floor(float64(len(moduleExistIds)) / float64(pageSize)))
  160. result := make([]map[int64]*v2pb.CommonRoomItem, page)
  161. wg := errgroup.Group{}
  162. var i int64
  163. for i = 1; i <= page; i++ {
  164. p := i - 1
  165. start := p * int64(pageSize)
  166. end := start + int64(pageSize)
  167. duplicateIds := moduleExistIds[start:end]
  168. wg.Go(func() (err error) {
  169. result[p] = s.getRecPoolAllPosition(ctx, duplicateIds, otherExistIds)
  170. return nil
  171. })
  172. }
  173. err := wg.Wait()
  174. if err != nil {
  175. log.Error("[fourTimeRecPoolForYumo]moduleExistIds: %+v, otherExistIds:%+v", xstr.JoinInts(moduleExistIds), xstr.JoinInts(otherExistIds))
  176. return
  177. }
  178. for page, recPoolMap := range result {
  179. for position, item := range recPoolMap {
  180. if item == nil {
  181. continue
  182. }
  183. p := position + int64(page*pageSize)
  184. recPool[p] = item
  185. recPoolRooms[p] = item.Roomid
  186. }
  187. }
  188. return
  189. }