recommend.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/mcn/model"
  6. accgrpc "go-common/app/service/main/account/api"
  7. memgrpc "go-common/app/service/main/member/api"
  8. blkmdl "go-common/app/service/main/member/model/block"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. "go-common/library/xstr"
  12. )
  13. // RecommendAdd .
  14. func (s *Service) RecommendAdd(c context.Context, arg *model.RecommendUpReq) error {
  15. var (
  16. err error
  17. ok bool
  18. bindMids []int64
  19. blockInfo *memgrpc.BlockInfoReply
  20. bi *model.UpBaseInfo
  21. pi *model.UpPlayInfo
  22. mbi map[int64]*model.UpBaseInfo
  23. mpi map[int64]*model.UpPlayInfo
  24. rpp *model.McnUpRecommendPool
  25. )
  26. if blockInfo, err = s.memGRPC.BlockInfo(c, &memgrpc.MemberMidReq{Mid: arg.UpMid}); err != nil {
  27. return err
  28. }
  29. if blockInfo.BlockStatus > int32(blkmdl.BlockStatusFalse) {
  30. return fmt.Errorf("添加到推荐池的mid为(%d)的up主已经被封禁", arg.UpMid)
  31. }
  32. if bindMids, err = s.dao.McnUpBindMids(c, []int64{arg.UpMid}); err != nil {
  33. return err
  34. }
  35. if len(bindMids) > 0 {
  36. return fmt.Errorf("添加到推荐池的mid为(%s)的up主已经被绑定", xstr.JoinInts(bindMids))
  37. }
  38. if rpp, err = s.dao.McnUpRecommendMid(c, arg.UpMid); err != nil {
  39. return err
  40. }
  41. if rpp != nil && rpp.State != model.MCNUPRecommendStateDel {
  42. return ecode.MCNRecommendUpInPool
  43. }
  44. if mbi, err = s.dao.UpBaseInfoMap(c, []int64{arg.UpMid}); err != nil {
  45. return err
  46. }
  47. if mpi, err = s.dao.UpPlayInfoMap(c, []int64{arg.UpMid}); err != nil {
  48. return err
  49. }
  50. rp := &model.McnUpRecommendPool{UpMid: arg.UpMid}
  51. if bi, ok = mbi[arg.UpMid]; ok {
  52. rp.FansCount = bi.FansCount
  53. rp.ActiveTid = bi.ActiveTid
  54. }
  55. if pi, ok = mpi[arg.UpMid]; ok {
  56. rp.ArchiveCount = pi.ArticleCount
  57. rp.PlayCountAccumulate = pi.PlayCountAccumulate
  58. rp.PlayCountAverage = pi.PlayCountAverage
  59. }
  60. if _, err = s.dao.AddMcnUpRecommend(c, rp); err != nil {
  61. return err
  62. }
  63. s.worker.Add(func() {
  64. index := []interface{}{int8(model.MCNUPRecommendStateOff), arg.UpMid}
  65. content := map[string]interface{}{
  66. "up_mid": arg.UpMid,
  67. "fans_count": rp.FansCount,
  68. "archive_count": rp.ArchiveCount,
  69. "play_count_accumulate": rp.PlayCountAccumulate,
  70. "play_count_average": rp.PlayCountAverage,
  71. "active_tid": rp.ActiveTid,
  72. "source": model.MCNUPRecommendStateManual,
  73. }
  74. s.AddAuditLog(context.Background(), model.MCNRecommendLogBizID, int8(model.MCNUPRecommendActionAdd), model.MCNUPRecommendActionAdd.String(), arg.UID, arg.UserName, []int64{arg.UpMid}, index, content)
  75. })
  76. return nil
  77. }
  78. // RecommendOP .
  79. func (s *Service) RecommendOP(c context.Context, arg *model.RecommendStateOpReq) error {
  80. var (
  81. err error
  82. blockInfosReply *memgrpc.BlockBatchInfoReply
  83. blockMids, bindMids, banMids, recMids []int64
  84. mrp map[int64]*model.McnUpRecommendPool
  85. )
  86. if len(arg.UpMids) == 0 {
  87. return ecode.MCNRecommendUpMidsIsEmpty
  88. }
  89. if arg.Action == model.MCNUPRecommendActionOn || arg.Action == model.MCNUPRecommendActionRestore {
  90. if blockInfosReply, err = s.memGRPC.BlockBatchInfo(c, &memgrpc.MemberMidsReq{Mids: arg.UpMids}); err != nil {
  91. return err
  92. }
  93. for _, v := range blockInfosReply.BlockInfos {
  94. if v.BlockStatus > int32(blkmdl.BlockStatusFalse) {
  95. blockMids = append(blockMids, v.MID)
  96. }
  97. }
  98. if len(blockMids) > 0 {
  99. return fmt.Errorf("推荐的mid为(%s)的up主已经被封禁", xstr.JoinInts(blockMids))
  100. }
  101. if bindMids, err = s.dao.McnUpBindMids(c, arg.UpMids); err != nil {
  102. return err
  103. }
  104. if len(bindMids) > 0 {
  105. return fmt.Errorf("推荐的mid为(%s)的up主已经被绑定", xstr.JoinInts(bindMids))
  106. }
  107. }
  108. state := arg.Action.GetState()
  109. if state == model.MCNUPRecommendStateUnKnown {
  110. return ecode.MCNRecommendUpStateFlowErr
  111. }
  112. if mrp, err = s.dao.McnUpRecommendMids(c, arg.UpMids); err != nil {
  113. return err
  114. }
  115. for _, upMids := range arg.UpMids {
  116. if rp, ok := mrp[upMids]; ok {
  117. if rp.State == model.MCNUPRecommendStateBan && arg.Action == model.MCNUPRecommendActionOn {
  118. banMids = append(banMids, upMids)
  119. }
  120. if rp.State == model.MCNUPRecommendStateOn && arg.Action == model.MCNUPRecommendActionRestore {
  121. recMids = append(recMids, upMids)
  122. }
  123. }
  124. }
  125. if len(banMids) > 0 {
  126. return fmt.Errorf("推荐的mid为(%s)的up主已被禁止推荐,不能推荐", xstr.JoinInts(banMids))
  127. }
  128. if len(recMids) > 0 {
  129. return fmt.Errorf("推荐的mid为(%s)的up主已被推荐,不需要恢复", xstr.JoinInts(recMids))
  130. }
  131. if _, err = s.dao.UpMcnUpsRecommendOP(c, arg.UpMids, state); err != nil {
  132. return err
  133. }
  134. for _, mid := range arg.UpMids {
  135. s.worker.Add(func() {
  136. index := []interface{}{int8(state), mid}
  137. content := map[string]interface{}{
  138. "up_mid": mid,
  139. }
  140. s.AddAuditLog(context.Background(), model.MCNRecommendLogBizID, int8(state), arg.Action.String(), arg.UID, arg.UserName, []int64{mid}, index, content)
  141. })
  142. }
  143. return nil
  144. }
  145. // RecommendList .
  146. func (s *Service) RecommendList(c context.Context, arg *model.MCNUPRecommendReq) (res *model.McnUpRecommendListReply, err error) {
  147. var (
  148. mids, tids []int64
  149. tpNames map[int64]string
  150. accsReply *accgrpc.InfosReply
  151. )
  152. res = new(model.McnUpRecommendListReply)
  153. res.Page = arg.Page
  154. if res.TotalCount, err = s.dao.McnUpRecommendTotal(c, arg); err != nil {
  155. return
  156. }
  157. if res.TotalCount <= 0 {
  158. return
  159. }
  160. if res.List, err = s.dao.McnUpRecommends(c, arg); err != nil {
  161. return
  162. }
  163. if len(res.List) <= 0 {
  164. return
  165. }
  166. for _, v := range res.List {
  167. mids = append(mids, v.UpMid)
  168. tids = append(tids, int64(v.ActiveTid))
  169. }
  170. if accsReply, err = s.accGRPC.Infos3(c, &accgrpc.MidsReq{Mids: mids}); err != nil {
  171. log.Error("s.accGRPC.Infos3(%+v) err(%v)", mids, err)
  172. err = nil
  173. }
  174. tpNames = s.videoup.GetTidName(tids)
  175. infos := accsReply.Infos
  176. for _, v := range res.List {
  177. if info, ok := infos[v.UpMid]; ok {
  178. v.UpName = info.Name
  179. }
  180. if tyName, ok := tpNames[int64(v.ActiveTid)]; ok {
  181. v.TpName = tyName
  182. } else {
  183. v.TpName = model.DefaultTyName
  184. }
  185. }
  186. return
  187. }