banner.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package banner
  2. import (
  3. "context"
  4. "strings"
  5. "time"
  6. "go-common/app/interface/main/app-show/conf"
  7. resdao "go-common/app/interface/main/app-show/dao/resource"
  8. "go-common/app/interface/main/app-show/model"
  9. "go-common/app/interface/main/app-show/model/banner"
  10. resource "go-common/app/service/main/resource/model"
  11. "go-common/library/log"
  12. "go-common/library/net/metadata"
  13. "go-common/library/stat/prom"
  14. )
  15. var (
  16. _banners = map[string]map[string]map[int8]int{
  17. "discover": map[string]map[int8]int{
  18. "bottom": map[int8]int{
  19. model.PlatIPhone: 452,
  20. model.PlatIPad: 800,
  21. model.PlatIPhoneI: 1085,
  22. model.PlatIPadI: 1255,
  23. },
  24. },
  25. "mine": map[string]map[int8]int{
  26. "top": map[int8]int{
  27. model.PlatIPhone: 449,
  28. model.PlatIPad: 801,
  29. model.PlatIPhoneI: 1089,
  30. model.PlatIPadI: 1259,
  31. },
  32. "center": map[int8]int{
  33. model.PlatIPhone: 450,
  34. model.PlatIPad: 802,
  35. model.PlatIPhoneI: 1093,
  36. model.PlatIPadI: 1263,
  37. },
  38. "bottom": map[int8]int{
  39. model.PlatIPhone: 451,
  40. model.PlatIPad: 803,
  41. model.PlatIPhoneI: 1097,
  42. model.PlatIPadI: 1267,
  43. },
  44. },
  45. }
  46. _bannersPlat = map[int8]string{
  47. model.PlatIPhone: "452,499,450,451",
  48. model.PlatIPad: "800,801,802,803",
  49. model.PlatIPhoneI: "1085,1089,1093,1097",
  50. model.PlatIPadI: "1255,1259,1263,1267",
  51. }
  52. )
  53. // Service is banner service.
  54. type Service struct {
  55. // dao *bndao.Dao
  56. res *resdao.Dao
  57. tick time.Duration
  58. bannerCache map[int8]map[int][]*resource.Banner
  59. // prom
  60. prmobi *prom.Prom
  61. }
  62. // New new a banner service.
  63. func New(c *conf.Config) (s *Service) {
  64. s = &Service{
  65. // dao: bndao.New(c),
  66. res: resdao.New(c),
  67. tick: time.Duration(c.Tick),
  68. bannerCache: map[int8]map[int][]*resource.Banner{},
  69. // prom
  70. prmobi: prom.BusinessInfoCount,
  71. }
  72. s.load()
  73. go s.loadproc()
  74. return
  75. }
  76. // Display get banner.
  77. func (s *Service) Display(c context.Context, plat int8, build int, channel, module, position, mobiApp string) (res map[string][]*banner.Banner) {
  78. ip := metadata.String(c, metadata.RemoteIP)
  79. res = s.getCache(c, plat, build, channel, module, position, ip)
  80. s.prmobi.Incr("banner_plat_" + mobiApp)
  81. return
  82. }
  83. // getCahce get banner from cache.
  84. func (s *Service) getCache(c context.Context, plat int8, build int, channel, module, position, ip string) (res map[string][]*banner.Banner) {
  85. res = map[string][]*banner.Banner{}
  86. var (
  87. resIDs = _bannersPlat[plat]
  88. err error
  89. resbs map[int][]*resource.Banner
  90. plm = s.bannerCache[plat]
  91. resID int
  92. )
  93. if resbs, err = s.res.ResBanner(c, plat, build, 0, resIDs, channel, ip, "", "", "", "", "", false); err != nil || len(resbs) == 0 {
  94. log.Error("s.res.ResBanner is null or err(%v)", err)
  95. resbs = plm
  96. }
  97. mds := strings.Split(module, ",")
  98. poss := strings.Split(position, ",")
  99. for _, md := range mds {
  100. for _, pos := range poss {
  101. resID = _banners[md][pos][plat]
  102. res[md+"_"+pos] = s.resBanners(resbs[resID])
  103. }
  104. }
  105. return
  106. }
  107. // resBannersplat
  108. func (s *Service) resBanners(rbs []*resource.Banner) (res []*banner.Banner) {
  109. for _, rb := range rbs {
  110. b := &banner.Banner{}
  111. b.ResChangeBanner(rb)
  112. res = append(res, b)
  113. }
  114. return
  115. }
  116. // load load all banner.
  117. func (s *Service) load() {
  118. var (
  119. resbs = map[int8]map[int][]*resource.Banner{}
  120. )
  121. for plat, resIDStr := range _bannersPlat {
  122. mobiApp := model.MobiApp(plat)
  123. res, err := s.res.ResBanner(context.TODO(), plat, 515007, 0, resIDStr, "master", "", "", "", mobiApp, "", "", false)
  124. if err != nil || len(res) == 0 {
  125. log.Error("s.res.ResBanner is null or err(%v)", err)
  126. return
  127. }
  128. resbs[plat] = res
  129. }
  130. if len(resbs) > 0 {
  131. s.bannerCache = resbs
  132. }
  133. log.Info("banner cacheproc success")
  134. }
  135. // cacheproc load cache.
  136. func (s *Service) loadproc() {
  137. for {
  138. time.Sleep(s.tick)
  139. s.load()
  140. }
  141. }