label.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package goblin
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/tv/model"
  6. "go-common/app/interface/main/tv/model/goblin"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _pgcLabel = 1
  12. _ugcLabel = 2
  13. )
  14. func (s *Service) labelsproc() {
  15. for {
  16. time.Sleep(time.Duration(s.conf.Cfg.IndexLabel.Fre))
  17. log.Info("Reload Label Data!")
  18. s.prepareLabels()
  19. }
  20. }
  21. // prepareLabels refreshes memory labels
  22. func (s *Service) prepareLabels() {
  23. pgcLbs, errpgc := s.catLabels(_pgcLabel)
  24. if errpgc != nil {
  25. log.Error("loadLabels PGC Err %v", errpgc)
  26. return
  27. }
  28. ugcLbs, errugc := s.catLabels(_ugcLabel)
  29. if errugc != nil {
  30. log.Error("loadLabels PGC Err %v", errugc)
  31. return
  32. }
  33. if len(pgcLbs) > 0 && len(ugcLbs) > 0 {
  34. s.labels = &goblin.IndexLabels{
  35. PGC: pgcLbs,
  36. UGC: ugcLbs,
  37. }
  38. }
  39. }
  40. // catLabels picks ugc/pgc all categories labels
  41. func (s *Service) catLabels(catType int) (result map[int][]*goblin.TypeLabels, err error) {
  42. var (
  43. cats []int
  44. typeMap map[int32]*model.ArcType
  45. )
  46. result = make(map[int][]*goblin.TypeLabels)
  47. if catType == _pgcLabel {
  48. cats = s.conf.Cfg.ZonesInfo.PGCZonesID
  49. } else if catType == _ugcLabel {
  50. if typeMap, err = s.arcDao.FirstTypes(); err != nil {
  51. log.Error("loadLabels ArcDao FirstTypes Err %v", err)
  52. return
  53. }
  54. for k := range typeMap {
  55. cats = append(cats, int(k))
  56. }
  57. } else {
  58. err = ecode.TvDangbeiWrongType
  59. return
  60. }
  61. if len(cats) == 0 {
  62. err = ecode.TvDangbeiPageNotExist
  63. return
  64. }
  65. for _, category := range cats {
  66. if result[category], err = s.loadLabels(catType, category); err != nil {
  67. log.Error("loadLabels Err %v", err)
  68. return
  69. }
  70. }
  71. return
  72. }
  73. // getTypeLabel builds an typeLabels object from a slice of labels
  74. func getTypeLabel(in []*goblin.Label) *goblin.TypeLabels {
  75. typeLbs := &goblin.TypeLabels{}
  76. typeLbs.FromLabels(in)
  77. return typeLbs
  78. }
  79. // loadLabels
  80. func (s *Service) loadLabels(catType, category int) (result []*goblin.TypeLabels, err error) {
  81. var (
  82. ctx = context.Background()
  83. labelMap = make(map[string][]*goblin.Label)
  84. labels []*goblin.Label
  85. showOrder []string
  86. cfg = s.conf.Cfg.IndexLabel
  87. )
  88. if labels, err = s.dao.Label(ctx, category, catType); err != nil {
  89. log.Error("loadLabels Dao Label Cat %d, %d, Err %v", category, catType, err)
  90. return
  91. }
  92. for _, v := range labels { // gather labels by their param
  93. v.TransYear(cfg)
  94. labelMap[v.Param] = append(labelMap[v.Param], v)
  95. }
  96. if catType == _pgcLabel {
  97. showOrder = cfg.PGCOrder
  98. } else {
  99. showOrder = cfg.UGCOrder
  100. }
  101. for _, v := range showOrder {
  102. if line, ok := labelMap[v]; ok {
  103. result = append(result, getTypeLabel(line))
  104. delete(labelMap, v)
  105. }
  106. }
  107. if len(labelMap) > 0 {
  108. for _, v := range labelMap {
  109. result = append(result, getTypeLabel(v))
  110. }
  111. }
  112. return
  113. }
  114. // Labels picks label
  115. func (s *Service) Labels(c context.Context, catType, category int) (result []*goblin.TypeLabels, err error) {
  116. var (
  117. indexLbs map[int][]*goblin.TypeLabels
  118. ok bool
  119. )
  120. if catType == _pgcLabel {
  121. indexLbs = s.labels.PGC
  122. } else {
  123. indexLbs = s.labels.UGC
  124. }
  125. if result, ok = indexLbs[category]; !ok {
  126. err = ecode.NothingFound
  127. }
  128. return
  129. }