notice.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "go-common/app/interface/bbq/app-bbq/api/http/v1"
  6. "go-common/app/interface/bbq/app-bbq/conf"
  7. "go-common/app/interface/bbq/app-bbq/model"
  8. "go-common/app/service/bbq/common"
  9. notice "go-common/app/service/bbq/notice-service/api/v1"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. )
  13. var noticeTypes map[int32]bool
  14. const (
  15. _noticeTypeLike = 1
  16. _noticeTypeComment = 2
  17. _noticeTypeFan = 3
  18. _noticeTypeSysMsg = 4
  19. )
  20. func init() {
  21. noticeTypes = map[int32]bool{
  22. _noticeTypeLike: true,
  23. _noticeTypeComment: true,
  24. _noticeTypeFan: true,
  25. _noticeTypeSysMsg: true,
  26. }
  27. }
  28. // GetNoticeNum 通知红点
  29. func (s *Service) GetNoticeNum(ctx context.Context, mid int64) (res *v1.NoticeNumResponse, err error) {
  30. res = new(v1.NoticeNumResponse)
  31. unreadList, err := s.dao.GetNoticeUnread(ctx, mid)
  32. if err != nil {
  33. log.Warnv(ctx, log.KV("log", "get notice unread fail: err="+err.Error()))
  34. return
  35. }
  36. var redDot int64
  37. for _, v := range unreadList {
  38. redDot += v.UnreadNum
  39. }
  40. res.RedDot = redDot
  41. return
  42. }
  43. // NoticeOverview 通知中心概述
  44. func (s *Service) NoticeOverview(ctx context.Context, mid int64) (res *v1.NoticeOverviewResponse, err error) {
  45. res = new(v1.NoticeOverviewResponse)
  46. unreadList, err := s.dao.GetNoticeUnread(ctx, mid)
  47. if err != nil {
  48. log.Warnv(ctx, log.KV("log", "get notice unread fail: err="+err.Error()))
  49. return
  50. }
  51. unreadMap := make(map[int32]*notice.UnreadItem)
  52. for _, item := range unreadList {
  53. unreadMap[item.NoticeType] = item
  54. }
  55. for _, v := range conf.Conf.Notices {
  56. var unreadNum int64
  57. if unreadInfo, exists := unreadMap[v.NoticeType]; exists {
  58. unreadNum = unreadInfo.UnreadNum
  59. }
  60. res.Notices = append(res.Notices, &v1.NoticeOverview{NoticeType: v.NoticeType, ShowType: v.ShowType, Name: v.Name, UnreadNum: unreadNum})
  61. }
  62. return
  63. }
  64. // NoticeList 请求通知列表,组装成通知消息列表
  65. func (s *Service) NoticeList(ctx context.Context, req *v1.NoticeListRequest) (res *v1.NoticeListResponse, err error) {
  66. res = new(v1.NoticeListResponse)
  67. // 0. 校验请求合法性
  68. if _, exists := noticeTypes[req.NoticeType]; !exists {
  69. return nil, ecode.NoticeTypeErr
  70. }
  71. cursor, _, err := parseCursor("", req.CursorNext)
  72. if err != nil {
  73. log.Errorv(ctx, log.KV("log", "parse cursor fail: cursor_next="+req.CursorNext))
  74. return
  75. }
  76. // 1. 请求notice列表
  77. list, err := s.dao.NoticeList(ctx, req.NoticeType, req.Mid, cursor.CursorID)
  78. if err != nil {
  79. return
  80. }
  81. // 2. 根据notice请求相应业务方
  82. // 2.0 整理notice列表
  83. var actionMIDs []int64 // 不是map,有可能重复
  84. var svIDs []int64
  85. for _, item := range list {
  86. if item.ActionMid != 0 {
  87. actionMIDs = append(actionMIDs, item.ActionMid)
  88. }
  89. if item.SvId != 0 {
  90. svIDs = append(svIDs, item.SvId)
  91. }
  92. }
  93. needUserInfo := true
  94. needFollowState := false
  95. needSvInfo := false
  96. switch req.NoticeType {
  97. case _noticeTypeLike:
  98. needSvInfo = true
  99. case _noticeTypeComment:
  100. needSvInfo = true
  101. case _noticeTypeFan:
  102. needFollowState = true
  103. case _noticeTypeSysMsg:
  104. needUserInfo = false
  105. }
  106. // 2. 请求用户信息
  107. var userInfos map[int64]*v1.UserInfo
  108. if needUserInfo && len(actionMIDs) > 0 {
  109. userInfos, err = s.dao.BatchUserInfo(ctx, req.Mid, actionMIDs, false, false, needFollowState)
  110. if err != nil {
  111. log.Errorv(ctx, log.KV("log", "batch fetch user infos fail"))
  112. err = nil
  113. }
  114. }
  115. // 2. 请求视频信息
  116. svInfos := make(map[int64]*model.SvInfo)
  117. if needSvInfo && len(svIDs) > 0 {
  118. svList, _, err := s.dao.GetVideoDetail(ctx, svIDs)
  119. if err != nil {
  120. log.Warnv(ctx, log.KV("log", "batch fetch sv detail fail"))
  121. err = nil
  122. } else {
  123. for _, sv := range svList {
  124. svInfos[sv.SVID] = sv
  125. }
  126. }
  127. }
  128. // 3. 组成回包
  129. for _, item := range list {
  130. var noticeMsg v1.NoticeMsg
  131. noticeMsg.NoticeBase = item
  132. // showtype没有用,这里就按照noticetype返回
  133. // md,想的好好的扩展灵活性,完全没被践行
  134. noticeMsg.ShowType = req.NoticeType
  135. if needUserInfo && (noticeMsg.ActionMid != 0) {
  136. if val, exists := userInfos[noticeMsg.ActionMid]; exists {
  137. noticeMsg.UserInfo = val
  138. }
  139. }
  140. if needSvInfo && (noticeMsg.SvId != 0) {
  141. if val, exists := svInfos[noticeMsg.SvId]; exists && noticeMsg.State >= common.VideoStPendingPassReview {
  142. noticeMsg.Pic = val.CoverURL
  143. } else {
  144. noticeMsg.State = 1
  145. noticeMsg.ErrMsg = "视频不见了"
  146. }
  147. }
  148. // cursor_value
  149. var cursor model.CursorValue
  150. cursor.CursorID = item.Id
  151. jsonBytes, _ := json.Marshal(cursor)
  152. noticeMsg.CursorValue = string(jsonBytes)
  153. res.List = append(res.List, &noticeMsg)
  154. }
  155. return
  156. }