ad.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package resource
  2. import (
  3. "context"
  4. "math/rand"
  5. "go-common/app/interface/main/web-show/dao/ad"
  6. resmdl "go-common/app/interface/main/web-show/model/resource"
  7. account "go-common/app/service/main/account/model"
  8. "go-common/library/log"
  9. "go-common/library/net/metadata"
  10. )
  11. var (
  12. _emptyVideoAds = []*resmdl.VideoAD{}
  13. )
  14. // VideoAd get videoad by aid
  15. func (s *Service) VideoAd(c context.Context, arg *resmdl.ArgAid) (res []*resmdl.VideoAD) {
  16. arg.IP = metadata.String(c, metadata.RemoteIP)
  17. if arg.Mid > 0 {
  18. // ignore error
  19. var (
  20. resPro *account.Card
  21. err error
  22. )
  23. if resPro, err = s.user(c, arg.Mid, arg.IP); err == nil {
  24. if s.normalVip(c, resPro) {
  25. return
  26. }
  27. }
  28. // NOTE cache?
  29. if isBp := s.bangumiDao.IsBp(c, arg.Mid, arg.Aid, arg.IP); isBp {
  30. log.Info("mid(%d) aid(%d) is bp", arg.Mid, arg.IP)
  31. res = _emptyVideoAds
  32. return
  33. }
  34. }
  35. if res = s.videoAdByAid(arg.Aid); len(res) == 0 {
  36. res = _emptyVideoAds
  37. }
  38. return
  39. }
  40. func (s *Service) user(c context.Context, mid int64, ip string) (resPro *account.Card, err error) {
  41. arg := &account.ArgMid{
  42. Mid: mid,
  43. }
  44. resPro, err = s.accRPC.Card3(c, arg)
  45. if err != nil {
  46. ad.PromError("accRPC.Info2", "s.accRPC.Info2() err(%v)", err)
  47. log.Error("s.accRPC.Info2() err(%v)", err)
  48. }
  49. return
  50. }
  51. // checkVip check normal vip
  52. func (s *Service) normalVip(c context.Context, pro *account.Card) bool {
  53. if pro.Vip.Type != 0 && pro.Vip.Status == 1 {
  54. return true
  55. }
  56. return false
  57. }
  58. func (s *Service) videoAdByAid(aid int64) (res []*resmdl.VideoAD) {
  59. ss := s.videoCache[aid]
  60. l := len(ss)
  61. if l == 0 {
  62. return
  63. }
  64. // NOTE this means StrategyOnly
  65. if l == 1 {
  66. res = ss[0]
  67. return
  68. }
  69. // NOTE this means StrategyShare
  70. res = ss[rand.Intn(l)]
  71. return
  72. }
  73. // loadVideoAd load videoad to cache
  74. func (s *Service) loadVideoAd() (err error) {
  75. ads, err := s.resdao.VideoAds(context.Background())
  76. if err != nil {
  77. log.Error("s.resdao.VideoAds error(%v)", err)
  78. return
  79. }
  80. tmp := make(map[int64][][]*resmdl.VideoAD)
  81. for aid, vads := range ads {
  82. if len(vads) < 1 {
  83. continue
  84. }
  85. if vads[0].Strategy == resmdl.StrategyOnly || vads[0].Strategy == resmdl.StrategyRank {
  86. tmp[aid] = append(tmp[aid], vads)
  87. } else if vads[0].Strategy == resmdl.StrategyShare {
  88. for _, vad := range vads {
  89. tmp[aid] = append(tmp[aid], []*resmdl.VideoAD{vad})
  90. }
  91. }
  92. }
  93. s.videoCache = tmp
  94. return
  95. }