service.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package space
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/interface/main/app-interface/conf"
  7. accdao "go-common/app/interface/main/app-interface/dao/account"
  8. audiodao "go-common/app/interface/main/app-interface/dao/audio"
  9. bplusdao "go-common/app/interface/main/app-interface/dao/bplus"
  10. memberdao "go-common/app/interface/main/app-interface/dao/member"
  11. paydao "go-common/app/interface/main/app-interface/dao/pay"
  12. reldao "go-common/app/interface/main/app-interface/dao/relation"
  13. sidedao "go-common/app/interface/main/app-interface/dao/sidebar"
  14. "go-common/app/interface/main/app-interface/model"
  15. resmodel "go-common/app/service/main/resource/model"
  16. "go-common/library/log"
  17. )
  18. // Service is space service
  19. type Service struct {
  20. c *conf.Config
  21. accDao *accdao.Dao
  22. audioDao *audiodao.Dao
  23. relDao *reldao.Dao
  24. bplusDao *bplusdao.Dao
  25. payDao *paydao.Dao
  26. memberDao *memberdao.Dao
  27. sideDao *sidedao.Dao
  28. sectionCache map[string][]*SectionItem
  29. white map[int8][]*SectionURL
  30. redDot map[int8][]*SectionURL
  31. }
  32. // SectionItem is
  33. type SectionItem struct {
  34. Item *resmodel.SideBar
  35. Limit []*resmodel.SideBarLimit
  36. }
  37. // SectionURL is
  38. type SectionURL struct {
  39. ID int64
  40. URL string
  41. }
  42. // CheckLimit is
  43. func (item *SectionItem) CheckLimit(build int) bool {
  44. for _, l := range item.Limit {
  45. if model.InvalidBuild(build, l.Build, l.Condition) {
  46. return false
  47. }
  48. }
  49. return true
  50. }
  51. // New new space
  52. func New(c *conf.Config) (s *Service) {
  53. s = &Service{
  54. c: c,
  55. accDao: accdao.New(c),
  56. audioDao: audiodao.New(c),
  57. relDao: reldao.New(c),
  58. bplusDao: bplusdao.New(c),
  59. payDao: paydao.New(c),
  60. memberDao: memberdao.New(c),
  61. sideDao: sidedao.New(c),
  62. sectionCache: map[string][]*SectionItem{},
  63. white: map[int8][]*SectionURL{},
  64. redDot: map[int8][]*SectionURL{},
  65. }
  66. if err := s.loadSidebar(); err != nil {
  67. panic(fmt.Sprintf("load sidebar error(%+v)", err))
  68. }
  69. go s.tickproc()
  70. return s
  71. }
  72. // tickproc tick load cache.
  73. func (s *Service) tickproc() {
  74. for {
  75. time.Sleep(time.Duration(s.c.Tick))
  76. s.loadSidebar()
  77. }
  78. }
  79. func (s *Service) loadSidebar() (err error) {
  80. var sidebar *resmodel.SideBars
  81. if sidebar, err = s.sideDao.Sidebars(context.TODO()); err != nil {
  82. log.Error("s.sideDao.SideBars error(%v)", err)
  83. return
  84. }
  85. ss := make(map[string][]*SectionItem)
  86. white := make(map[int8][]*SectionURL)
  87. redDot := make(map[int8][]*SectionURL)
  88. for _, item := range sidebar.SideBar {
  89. item.Plat = s.convertPlat(item.Plat)
  90. key := fmt.Sprintf(_initSidebarKey, item.Plat, item.Module)
  91. ss[key] = append(ss[key], &SectionItem{
  92. Item: item,
  93. Limit: sidebar.Limit[item.ID],
  94. })
  95. if item.WhiteURL != "" {
  96. white[item.Plat] = append(white[item.Plat], &SectionURL{ID: item.ID, URL: item.WhiteURL})
  97. }
  98. if item.Red != "" {
  99. redDot[item.Plat] = append(redDot[item.Plat], &SectionURL{ID: item.ID, URL: item.Red})
  100. }
  101. }
  102. s.sectionCache = ss
  103. s.white = white
  104. s.redDot = redDot
  105. return
  106. }
  107. func (s *Service) convertPlat(oldPlat int8) (newPlat int8) {
  108. newPlat = oldPlat
  109. if oldPlat == 9 {
  110. newPlat = model.PlatAndroidB
  111. } else if oldPlat == 10 {
  112. newPlat = model.PlatIPhoneB
  113. }
  114. return newPlat
  115. }