member.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. "go-common/app/interface/main/dm2/model"
  8. "go-common/library/database/elastic"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. xtime "go-common/library/time"
  12. )
  13. const (
  14. _dmRecentLimit = 1000
  15. _dateTimeFormart = "2006-01-02 15:04:05"
  16. )
  17. // DMUpRecent recent dm of upper.
  18. func (s *Service) DMUpRecent(c context.Context, mid, pn, ps int64) (res *model.DmRecentResponse, err error) {
  19. var (
  20. mids, aids []int64
  21. aidmap = make(map[int64]struct{})
  22. midMap = make(map[int64]struct{})
  23. searchResult *model.SearchRecentDMResult
  24. )
  25. if ps < 0 || pn < 1 {
  26. err = ecode.RequestErr
  27. return
  28. }
  29. if (pn-1)*ps >= _dmRecentLimit {
  30. return
  31. }
  32. searchParam := &model.SearchRecentDMParam{
  33. Type: model.SubTypeVideo,
  34. UpMid: mid,
  35. States: []int32{model.StateNormal, model.StateHide, model.StateMonitorAfter},
  36. Ps: int(ps),
  37. Pn: int(pn),
  38. Field: "ctime",
  39. Sort: elastic.OrderDesc,
  40. }
  41. searchResult, err = s.dao.SearhcDmRecent(c, searchParam)
  42. if err != nil || searchResult == nil || len(searchResult.Result) == 0 || searchResult.Page == nil {
  43. return
  44. }
  45. for _, item := range searchResult.Result {
  46. if _, ok := aidmap[item.Aid]; !ok {
  47. aids = append(aids, item.Aid)
  48. aidmap[item.Aid] = struct{}{}
  49. }
  50. if _, ok := midMap[item.Mid]; !ok {
  51. mids = append(mids, item.Mid)
  52. midMap[item.Mid] = struct{}{}
  53. }
  54. }
  55. arcMap, err := s.archiveInfos(c, aids)
  56. if err != nil {
  57. return
  58. }
  59. infoMap, err := s.accountInfos(c, mids)
  60. if err != nil {
  61. return
  62. }
  63. memebers := make([]*model.DMMember, 0, len(searchResult.Result))
  64. for _, item := range searchResult.Result {
  65. member := &model.DMMember{
  66. ID: item.ID,
  67. IDStr: strconv.FormatInt(item.ID, 10),
  68. Type: item.Type,
  69. Aid: item.Aid,
  70. Oid: item.Oid,
  71. Mid: item.Mid,
  72. MidHash: model.Hash(item.Mid, 0),
  73. Pool: item.Pool,
  74. State: item.State,
  75. Attrs: model.DMAttrNtoA(item.Attr),
  76. Msg: item.Msg,
  77. Mode: item.Mode,
  78. Color: fmt.Sprintf("%06x", item.Color),
  79. Progress: item.Progress,
  80. FontSize: item.FontSize,
  81. }
  82. if ctime, err := time.ParseInLocation(_dateTimeFormart, item.Ctime, time.Now().Location()); err == nil {
  83. member.Ctime = xtime.Time(ctime.Unix())
  84. }
  85. if arc, ok := arcMap[item.Aid]; ok {
  86. member.Title = arc.Title
  87. }
  88. if info, ok := infoMap[item.Mid]; ok {
  89. member.Uname = info.Name
  90. }
  91. memebers = append(memebers, member)
  92. }
  93. res = &model.DmRecentResponse{
  94. Data: memebers,
  95. Page: searchResult.Page,
  96. }
  97. if res.Page.Total > _dmRecentLimit {
  98. res.Page.Total = _dmRecentLimit
  99. }
  100. return
  101. }
  102. // DMUpSearch danmu list from search.
  103. func (s *Service) DMUpSearch(c context.Context, mid int64, p *model.SearchDMParams) (res *model.SearchDMResult, err error) {
  104. var (
  105. mids, dmids []int64
  106. )
  107. sub, err := s.subject(c, p.Type, p.Oid)
  108. if err != nil {
  109. return
  110. }
  111. if sub.Mid != mid {
  112. err = ecode.AccessDenied
  113. return
  114. }
  115. res = &model.SearchDMResult{}
  116. srchData, err := s.dao.SearchDM(c, p)
  117. if err != nil || srchData == nil {
  118. return
  119. }
  120. for _, v := range srchData.Result {
  121. dmids = append(dmids, v.ID)
  122. }
  123. dms, err := s.dmList(c, p.Type, p.Oid, dmids)
  124. if err != nil {
  125. log.Error("s.dms(%d,%v) error(%v)", p.Oid, dmids, err)
  126. return
  127. }
  128. for _, dm := range dms {
  129. mids = append(mids, dm.Mid)
  130. }
  131. infoMap, err := s.accountInfos(c, mids)
  132. if err != nil {
  133. return
  134. }
  135. for _, dm := range dms {
  136. var msg string
  137. if dm.Content != nil {
  138. msg = dm.Content.Msg
  139. } else {
  140. continue
  141. }
  142. if dm.ContentSpe != nil {
  143. msg = dm.ContentSpe.Msg
  144. }
  145. item := &model.DMMember{
  146. ID: dm.ID,
  147. IDStr: strconv.FormatInt(dm.ID, 10),
  148. Type: dm.Type,
  149. Aid: sub.Pid,
  150. Oid: dm.Oid,
  151. Mid: dm.Mid,
  152. MidHash: model.Hash(dm.Mid, 0),
  153. Pool: dm.Pool,
  154. State: dm.State,
  155. Attrs: dm.AttrNtoA(),
  156. Msg: msg,
  157. Ctime: dm.Ctime,
  158. Mode: dm.Content.Mode,
  159. Color: fmt.Sprintf("%06x", dm.Content.Color),
  160. Progress: dm.Progress,
  161. FontSize: dm.Content.FontSize,
  162. }
  163. if info, ok := infoMap[dm.Mid]; ok {
  164. item.Uname = info.Name
  165. }
  166. res.Result = append(res.Result, item)
  167. }
  168. res.Page.Num = srchData.Page.Num
  169. res.Page.Size = srchData.Page.Size
  170. res.Page.Total = srchData.Page.Total
  171. return
  172. }
  173. // UptSearchDMState update dm search state
  174. func (s *Service) UptSearchDMState(c context.Context, dmids []int64, oid int64, state, tp int32) (err error) {
  175. if err = s.dao.UptSearchDMState(c, dmids, oid, state, tp); err != nil {
  176. return
  177. }
  178. if err = s.dao.UptSearchRecentState(c, dmids, oid, state, tp); err != nil {
  179. return
  180. }
  181. return
  182. }
  183. // UptSearchDMPool update dm search pool
  184. func (s *Service) UptSearchDMPool(c context.Context, dmids []int64, oid int64, pool, tp int32) (err error) {
  185. if err = s.dao.UptSearchDMPool(c, dmids, oid, pool, tp); err != nil {
  186. return
  187. }
  188. if err = s.dao.UptSearchRecentPool(c, dmids, oid, pool, tp); err != nil {
  189. return
  190. }
  191. return
  192. }
  193. // UptSearchDMAttr update dm search attr
  194. func (s *Service) UptSearchDMAttr(c context.Context, dmids []int64, oid int64, attr, tp int32) (err error) {
  195. if err = s.dao.UptSearchDMAttr(c, dmids, oid, attr, tp); err != nil {
  196. return
  197. }
  198. if err = s.dao.UptSearchRecentAttr(c, dmids, oid, attr, tp); err != nil {
  199. return
  200. }
  201. return
  202. }