apprelationsortstrategy.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package v2
  2. import (
  3. "context"
  4. v2pb "go-common/app/interface/live/app-interface/api/http/v2"
  5. "go-common/library/log"
  6. "sort"
  7. )
  8. // const (
  9. // // AppSortDefaultT ...
  10. // // 默认排序
  11. // AppSortDefaultT = 0
  12. // // AppSortRuleLiveTimeT ...
  13. // // 开播时间倒序
  14. // AppSortRuleLiveTimeT = 1
  15. // // AppSortRuleOnlineT ...
  16. // // 人气值倒序
  17. // AppSortRuleOnlineT = 2
  18. // // AppSortRuleGoldT ...
  19. // // 金瓜子倒序
  20. // AppSortRuleGoldT = 3
  21. // )
  22. // SendGift ...
  23. // [app端关注二级页]按照金瓜子排序结构
  24. type SendGift struct {
  25. Mid int64
  26. gold int64
  27. }
  28. // SortLiveTime ... implementation
  29. // [app端关注二级页]按照开播时间排序
  30. type SortLiveTime []*v2pb.MyIdolItem
  31. // SortOnlineTime ... implementation
  32. // [app端关注二级页]按照房间人气值排序
  33. type SortOnlineTime []*v2pb.MyIdolItem
  34. // SortUIDGift ... implementation
  35. // [app端关注二级页]按照送礼排序
  36. type SortUIDGift []SendGift
  37. // AppSortRuleLiveTime implementation
  38. // [app端关注二级页]按照开播时间排序
  39. func (p SortLiveTime) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  40. // AppSortRuleLiveTime implementation
  41. // [app端关注二级页]按照开播时间排序
  42. func (p SortLiveTime) Len() int { return len(p) }
  43. // AppSortRuleLiveTime implementation
  44. // [app端关注二级页]按照开播时间排序
  45. func (p SortLiveTime) Less(i, j int) bool { return p[i].LiveTime > p[j].LiveTime }
  46. // AppSortRuleLiveTime implementation
  47. // [app端关注二级页]按照开播时间排序
  48. func (p SortOnlineTime) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  49. // AppSortRuleLiveTime implementation
  50. // [app端关注二级页]按照开播时间排序
  51. func (p SortOnlineTime) Len() int { return len(p) }
  52. // AppSortRuleLiveTime implementation
  53. // [app端关注二级页]按照开播时间排序
  54. func (p SortOnlineTime) Less(i, j int) bool { return p[i].Online > p[j].Online }
  55. // Swap
  56. // [app端关注二级页]自定义排序结构
  57. func (p SortUIDGift) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  58. // Len
  59. // [app端关注二级页]自定义排序结构
  60. func (p SortUIDGift) Len() int { return len(p) }
  61. // Less
  62. // [app端关注二级页]自定义排序结构
  63. func (p SortUIDGift) Less(i, j int) bool { return p[i].gold > p[j].gold }
  64. // AppSortRuleLiveTime implementation
  65. // [app端关注二级页]按照开播时间排序
  66. func (s *IndexService) AppSortRuleLiveTime(originResult []*v2pb.MyIdolItem) (resp []*v2pb.MyIdolItem) {
  67. resp = make([]*v2pb.MyIdolItem, 0)
  68. if originResult == nil {
  69. return
  70. }
  71. p := make(SortLiveTime, len(originResult))
  72. i := 0
  73. for _, v := range originResult {
  74. p[i] = &v2pb.MyIdolItem{
  75. Roomid: v.Roomid,
  76. Uid: v.Uid,
  77. Uname: v.Uname,
  78. Face: v.Face,
  79. Title: v.Title,
  80. LiveTagName: v.LiveTagName,
  81. LiveTime: v.LiveTime,
  82. Online: v.Online,
  83. PlayUrl: v.PlayUrl,
  84. AcceptQuality: v.AcceptQuality,
  85. CurrentQuality: v.CurrentQuality,
  86. PkId: v.PkId,
  87. SpecialAttention: v.SpecialAttention,
  88. Area: v.Area,
  89. AreaName: v.AreaName,
  90. AreaV2Id: v.AreaV2Id,
  91. AreaV2Name: v.AreaV2Name,
  92. AreaV2ParentName: v.AreaV2ParentName,
  93. AreaV2ParentId: v.AreaV2ParentId,
  94. BroadcastType: v.BroadcastType,
  95. OfficialVerify: v.OfficialVerify,
  96. Link: v.Link,
  97. Cover: v.Cover,
  98. PendentRu: v.PendentRu,
  99. PendentRuColor: v.PendentRuColor,
  100. PendentRuPic: v.PendentRuPic}
  101. i++
  102. }
  103. sort.Sort(p)
  104. resp = p
  105. return
  106. }
  107. // AppSortRuleOnline implementation
  108. // [app端关注二级页]按照人气值排序
  109. func AppSortRuleOnline(originResult []*v2pb.MyIdolItem) (resp []*v2pb.MyIdolItem) {
  110. resp = make([]*v2pb.MyIdolItem, 0)
  111. if originResult == nil {
  112. return
  113. }
  114. p := make(SortOnlineTime, len(originResult))
  115. i := 0
  116. for _, v := range originResult {
  117. p[i] = &v2pb.MyIdolItem{
  118. Roomid: v.Roomid,
  119. Uid: v.Uid,
  120. Uname: v.Uname,
  121. Face: v.Face,
  122. Title: v.Title,
  123. LiveTagName: v.LiveTagName,
  124. LiveTime: v.LiveTime,
  125. Online: v.Online,
  126. PlayUrl: v.PlayUrl,
  127. AcceptQuality: v.AcceptQuality,
  128. CurrentQuality: v.CurrentQuality,
  129. PkId: v.PkId,
  130. SpecialAttention: v.SpecialAttention,
  131. Area: v.Area,
  132. AreaName: v.AreaName,
  133. AreaV2Id: v.AreaV2Id,
  134. AreaV2Name: v.AreaV2Name,
  135. AreaV2ParentName: v.AreaV2ParentName,
  136. AreaV2ParentId: v.AreaV2ParentId,
  137. BroadcastType: v.BroadcastType,
  138. OfficialVerify: v.OfficialVerify,
  139. Link: v.Link,
  140. Cover: v.Cover,
  141. PendentRu: v.PendentRu,
  142. PlayUrlH265: v.PlayUrlH265,
  143. PendentRuColor: v.PendentRuColor,
  144. PendentRuPic: v.PendentRuPic}
  145. i++
  146. }
  147. sort.Sort(p)
  148. resp = p
  149. return
  150. }
  151. // AppSortRuleGold implementation
  152. // [app端关注二级页]按照送礼排序
  153. func (s *IndexService) AppSortRuleGold(ctx context.Context, originResult *v2pb.MMyIdol) (resp []*v2pb.MyIdolItem) {
  154. resp = make([]*v2pb.MyIdolItem, 0)
  155. if originResult == nil {
  156. return
  157. }
  158. giftInfo, err := GetGiftInfo(ctx)
  159. if err != nil {
  160. log.Error("[LiveAnchor][FilterType][AppSortRuleGold]get_RelationGift_rpc_error")
  161. resp = originResult.List
  162. return
  163. }
  164. if len(giftInfo) == 0 {
  165. resp = AppSortRuleOnline(originResult.List)
  166. return
  167. }
  168. respHasGold := make([]*v2pb.MyIdolItem, 0)
  169. respNoGold := make([]*v2pb.MyIdolItem, 0)
  170. GiftRank := make(map[int64]int64)
  171. GiftNoGold := make([]int64, 0)
  172. // 计算金瓜子排行榜,uid分key
  173. for _, v := range originResult.List {
  174. roomUID := v.Uid
  175. if _, exist := giftInfo[roomUID]; exist {
  176. GiftRank[roomUID] += giftInfo[roomUID]
  177. }
  178. }
  179. sorted := SortMap(GiftRank)
  180. // 没有送礼的用户
  181. for _, v := range originResult.List {
  182. if _, exist := GiftRank[v.Uid]; !exist {
  183. GiftNoGold = append(GiftNoGold, v.Uid)
  184. }
  185. }
  186. for _, vv := range sorted {
  187. for _, v := range originResult.List {
  188. if v.Uid == vv.Key {
  189. respHasGold = append(respHasGold, v)
  190. }
  191. }
  192. }
  193. for _, v := range originResult.List {
  194. for _, vv := range GiftNoGold {
  195. if v.Uid == vv {
  196. respNoGold = append(respNoGold, v)
  197. }
  198. }
  199. }
  200. tempLiveAnchor := &v2pb.MMyIdol{}
  201. tempLiveAnchor.List = respNoGold
  202. respNoGoldSorted := AppSortRuleOnline(tempLiveAnchor.List)
  203. resp = append(resp, respHasGold...)
  204. resp = append(resp, respNoGoldSorted...)
  205. return
  206. }