banner.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "strconv"
  6. "time"
  7. "go-common/app/interface/openplatform/article/dao"
  8. "go-common/app/interface/openplatform/article/model"
  9. resmdl "go-common/app/service/main/resource/model"
  10. "go-common/library/log"
  11. )
  12. func (s *Service) loadBannersproc() {
  13. for {
  14. now := time.Now()
  15. ts := now.Unix()
  16. if (s.bannersMap == nil) || (ts%s.dao.UpdateBannersInterval == 0) {
  17. banners, err := s.banners(context.TODO(), s.c.Article.BannerIDs)
  18. if err != nil {
  19. dao.PromError("service:更新banner数据")
  20. time.Sleep(time.Second)
  21. continue
  22. }
  23. s.bannersMap = banners
  24. }
  25. // 这里不是每秒钟一更新
  26. time.Sleep(time.Second)
  27. }
  28. }
  29. func (s *Service) loadActBannersproc() {
  30. for {
  31. now := time.Now()
  32. ts := now.Unix()
  33. if (s.actBannersMap == nil) || (ts%s.dao.UpdateBannersInterval == 0) {
  34. banners, err := s.banners(context.TODO(), s.c.Article.ActBannerIDs)
  35. if err != nil {
  36. dao.PromError("service:更新actBanner数据")
  37. time.Sleep(time.Second)
  38. continue
  39. }
  40. s.actBannersMap = banners
  41. }
  42. // 这里不是每秒钟一更新
  43. time.Sleep(time.Second)
  44. }
  45. }
  46. // Banners get banners
  47. func (s *Service) Banners(c context.Context, plat int8, build int, t time.Time) (res []*model.Banner, err error) {
  48. tStr := strconv.FormatInt((t.UnixNano() / 1e6), 10)
  49. for _, banner := range s.bannersMap[int8(plat)] {
  50. if !invalidBuild(build, banner.Build, banner.Condition) {
  51. b := &model.Banner{}
  52. *b = *banner
  53. b.RequestID = tStr
  54. res = append(res, b)
  55. }
  56. }
  57. return
  58. }
  59. func (s *Service) actBanners(c context.Context, plat int8, t time.Time) (res []*model.Banner, err error) {
  60. tStr := strconv.FormatInt((t.UnixNano() / 1e6), 10)
  61. for _, banner := range s.actBannersMap[int8(plat)] {
  62. b := &model.Banner{}
  63. *b = *banner
  64. b.RequestID = tStr
  65. res = append(res, b)
  66. }
  67. return
  68. }
  69. func invalidBuild(srcBuild, cfgBuild int, cfgCond string) bool {
  70. if cfgBuild != 0 && cfgCond != "" {
  71. switch cfgCond {
  72. case "gt":
  73. if cfgBuild >= srcBuild {
  74. return true
  75. }
  76. case "lt":
  77. if cfgBuild <= srcBuild {
  78. return true
  79. }
  80. case "eq":
  81. if cfgBuild != srcBuild {
  82. return true
  83. }
  84. case "ne":
  85. if cfgBuild == srcBuild {
  86. return true
  87. }
  88. }
  89. }
  90. return false
  91. }
  92. func (s *Service) banners(c context.Context, resIDs []int) (res map[int8][]*model.Banner, err error) {
  93. arg := &resmdl.ArgRess{ResIDs: resIDs}
  94. bs, err := s.resRPC.Resources(c, arg)
  95. if err != nil {
  96. dao.PromError("banner:RPC")
  97. log.Error("s.resRPC.Resources(%+v) err: %+v", arg, err)
  98. return
  99. }
  100. res = make(map[int8][]*model.Banner)
  101. for _, r := range bs {
  102. for i, a := range r.Assignments {
  103. b := &model.Banner{
  104. ID: a.ID,
  105. Title: a.Name,
  106. URL: a.URL,
  107. Image: a.Pic,
  108. Position: i + 1,
  109. Plat: int8(r.Platform),
  110. Rule: string(a.Rule),
  111. ResID: r.ID,
  112. }
  113. if b.Rule != "" {
  114. var tmp *model.BannerRule
  115. if json.Unmarshal([]byte(b.Rule), &tmp) == nil {
  116. b.Build = tmp.Build
  117. b.Condition = tmp.Condition
  118. }
  119. }
  120. b.Plat = model.ConvertPlat(b.Plat)
  121. res[b.Plat] = append(res[b.Plat], b)
  122. }
  123. }
  124. return
  125. }