service.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package service
  2. import (
  3. "context"
  4. "go-common/library/log"
  5. "time"
  6. "go-common/app/admin/main/tv/conf"
  7. "go-common/app/admin/main/tv/dao"
  8. "go-common/app/admin/main/tv/model"
  9. acccli "go-common/app/service/main/account/api"
  10. arccli "go-common/app/service/main/archive/api"
  11. httpx "go-common/library/net/http/blademaster"
  12. "github.com/jinzhu/gorm"
  13. )
  14. var ctx = context.Background()
  15. // Service biz service def.
  16. type Service struct {
  17. c *conf.Config
  18. dao *dao.Dao
  19. DB, DBShow *gorm.DB
  20. accClient acccli.AccountClient
  21. arcClient arccli.ArchiveClient
  22. SupCats []*model.ParentCat
  23. supCatMap *model.SupCats
  24. IntervLimit int
  25. arcPTids map[int32][]int32 // archive parent type ids
  26. ArcTypes map[int32]*arccli.Tp
  27. avaiTps *model.AvailTps
  28. snsInfo map[int64]*model.TVEpSeason
  29. snsCats map[int][]int64
  30. abnCids []*model.AbnorCids // abnormal cids
  31. pgcCatName map[int]string // pgc category name
  32. labelTps map[int][]*model.TpLabel
  33. client *httpx.Client
  34. }
  35. // New new a Service and return.
  36. func New(c *conf.Config) (s *Service) {
  37. s = &Service{
  38. c: c,
  39. dao: dao.New(c),
  40. IntervLimit: c.Cfg.IntervLimit,
  41. SupCats: make([]*model.ParentCat, 0),
  42. ArcTypes: make(map[int32]*arccli.Tp),
  43. arcPTids: make(map[int32][]int32),
  44. avaiTps: &model.AvailTps{},
  45. snsInfo: make(map[int64]*model.TVEpSeason),
  46. snsCats: make(map[int][]int64),
  47. pgcCatName: make(map[int]string),
  48. labelTps: make(map[int][]*model.TpLabel),
  49. client: httpx.NewClient(conf.Conf.HTTPClient),
  50. }
  51. s.DB = s.dao.DB
  52. s.DBShow = s.dao.DBShow
  53. var err error
  54. if s.accClient, err = acccli.NewClient(c.AccClient); err != nil {
  55. panic(err)
  56. }
  57. if s.arcClient, err = arccli.NewClient(c.ArcClient); err != nil {
  58. panic(err)
  59. }
  60. for k, v := range c.Cfg.PgcNames {
  61. s.pgcCatName[atoi(k)] = v
  62. }
  63. s.loadData()
  64. go s.loadDataproc()
  65. s.loadSns(context.Background()) // load season info
  66. go s.loadSnsproc()
  67. s.loadAbnCids() // load abnormal cids
  68. go s.loadAbnCidsproc()
  69. go s.refLabelproc() // refresh ugc + pgc labels
  70. go s.checkPanel()
  71. return s
  72. }
  73. func (s *Service) loadDataproc() {
  74. for {
  75. time.Sleep(time.Duration(s.c.Cfg.SupportCat.ReloadFre))
  76. s.loadData()
  77. }
  78. }
  79. func (s *Service) refLabelproc() {
  80. for {
  81. s.ugcLabels()
  82. s.pgcLabels()
  83. time.Sleep(time.Duration(s.c.Cfg.RefLabel.Fre))
  84. }
  85. }
  86. func (s *Service) checkPanel() {
  87. for {
  88. time.Sleep(time.Duration(3600) * time.Second)
  89. log.Info("check panel info start!")
  90. s.checkRemotePanel(ctx)
  91. log.Info("check panel info end!")
  92. }
  93. }
  94. func (s *Service) loadData() {
  95. s.loadTypes() // load ugc types
  96. s.loadTps() // load passed tps and all tps for cms type list
  97. s.loadCats() // load support categorys ( pgc & ugc)
  98. s.loadLabel() // load pgc label types
  99. }
  100. // Wait wait all closed.
  101. func (s *Service) Wait() {
  102. }
  103. // Close close all dao.
  104. func (s *Service) Close() {
  105. }