service.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package academy
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/creative/conf"
  6. "go-common/app/interface/main/creative/dao/academy"
  7. "go-common/app/interface/main/creative/dao/archive"
  8. "go-common/app/interface/main/creative/dao/article"
  9. "go-common/app/interface/main/creative/dao/resource"
  10. acaMdl "go-common/app/interface/main/creative/model/academy"
  11. "go-common/app/interface/main/creative/service"
  12. "go-common/library/log"
  13. )
  14. //Service struct
  15. type Service struct {
  16. c *conf.Config
  17. aca *academy.Dao
  18. arc *archive.Dao
  19. art *article.Dao
  20. resource *resource.Dao
  21. TagsCache map[string][]*acaMdl.Tag
  22. TagMapCache map[int64]*acaMdl.Tag
  23. parentChildMapCache map[int64]*acaMdl.Tag
  24. ResourceMapCache map[int64]struct{}
  25. OccCache []*acaMdl.Occupation
  26. OccMapCache map[int64]*acaMdl.Occupation
  27. SkillCache []*acaMdl.Skill
  28. SkillMapCache map[int64]*acaMdl.Skill
  29. OfficialID int64
  30. EditorChoiceID int64
  31. NewbCourseID int64
  32. ResourceID int64
  33. //for recommend
  34. Seed int64
  35. RecommendArcs []*acaMdl.RecArchive
  36. //task
  37. p *service.Public
  38. //keywords
  39. KWsCache []interface{}
  40. }
  41. //New get service
  42. func New(c *conf.Config, rpcdaos *service.RPCDaos, p *service.Public) *Service {
  43. s := &Service{
  44. c: c,
  45. aca: academy.New(c),
  46. arc: rpcdaos.Arc,
  47. art: rpcdaos.Art,
  48. resource: resource.New(c),
  49. OccMapCache: make(map[int64]*acaMdl.Occupation),
  50. SkillMapCache: make(map[int64]*acaMdl.Skill),
  51. OfficialID: c.Academy.OfficialID,
  52. EditorChoiceID: c.Academy.EditorChoiceID,
  53. NewbCourseID: c.Academy.NewbCourseID,
  54. ResourceID: c.Academy.ResourceID,
  55. p: p,
  56. }
  57. s.loadTags()
  58. s.loadOccupations()
  59. s.loadSkills()
  60. s.loadResources()
  61. s.loadKeyWords()
  62. go s.loadProc()
  63. return s
  64. }
  65. // Ping service
  66. func (s *Service) Ping(c context.Context) (err error) {
  67. if err = s.aca.Ping(c); err != nil {
  68. log.Error("s.aca.Ping err(%v)", err)
  69. }
  70. return
  71. }
  72. // Close dao
  73. func (s *Service) Close() {
  74. s.aca.Close()
  75. }
  76. // loadproc
  77. func (s *Service) loadProc() {
  78. for {
  79. time.Sleep(3 * time.Minute)
  80. s.loadTags()
  81. s.loadOccupations()
  82. s.loadSkills()
  83. s.loadResources()
  84. s.loadKeyWords()
  85. }
  86. }
  87. //load tags
  88. func (s *Service) loadTags() {
  89. tgList, tgMap, pcMap, err := s.aca.TagList(context.Background())
  90. if err != nil {
  91. log.Error("s.aca.TagList error(%v)", err)
  92. return
  93. }
  94. s.TagsCache = tgList
  95. s.TagMapCache = tgMap
  96. s.parentChildMapCache = pcMap
  97. }
  98. //load occupations
  99. func (s *Service) loadOccupations() {
  100. ocs, err := s.aca.Occupations(context.Background())
  101. if err != nil {
  102. log.Error("s.aca.Occupations error(%v)", err)
  103. return
  104. }
  105. s.OccCache = ocs
  106. for _, v := range s.OccCache {
  107. s.OccMapCache[v.ID] = v
  108. }
  109. }
  110. //load skills
  111. func (s *Service) loadSkills() {
  112. sks, err := s.aca.Skills(context.Background())
  113. if err != nil {
  114. log.Error("s.aca.Skills error(%v)", err)
  115. return
  116. }
  117. s.SkillCache = sks
  118. for _, v := range s.SkillCache {
  119. s.SkillMapCache[v.ID] = v
  120. }
  121. }
  122. //load skills
  123. func (s *Service) loadResources() {
  124. res, err := s.resource.Resource(context.Background(), int(s.ResourceID))
  125. if err != nil {
  126. log.Error("loadResources ResourceID(%d) error(%v)", int(s.ResourceID), err)
  127. return
  128. }
  129. if res == nil {
  130. return
  131. }
  132. s.ResourceMapCache = res
  133. }
  134. //load keywords
  135. func (s *Service) loadKeyWords() {
  136. res, err := s.aca.Keywords(context.Background())
  137. if err != nil {
  138. return
  139. }
  140. if res == nil {
  141. return
  142. }
  143. s.KWsCache = acaMdl.Trees(res, "ID", "ParentID", "Children")
  144. }