category.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package service
  2. import (
  3. "context"
  4. "sort"
  5. "time"
  6. "go-common/app/interface/openplatform/article/conf"
  7. "go-common/app/interface/openplatform/article/dao"
  8. artmdl "go-common/app/interface/openplatform/article/model"
  9. )
  10. func (s *Service) loadCategoriesproc() {
  11. for {
  12. time.Sleep(time.Minute * 10)
  13. s.loadCategories()
  14. }
  15. }
  16. func (s *Service) loadCategories() {
  17. for {
  18. c, err := s.dao.Categories(context.TODO())
  19. if err != nil || len(c) == 0 {
  20. dao.PromError("service:获取分类")
  21. time.Sleep(time.Second)
  22. continue
  23. }
  24. s.primaryCategories = transformPrimaryCategory(c)
  25. removeCategoryBanner(c)
  26. s.categoriesMap = c
  27. s.Categories = transformCategory(c)
  28. s.categoriesReverseMap = transformReverseCategory(c)
  29. s.categoryParents = transformCategoryParents(c)
  30. return
  31. }
  32. }
  33. func removeCategoryBanner(cs map[int64]*artmdl.Category) {
  34. for _, c := range cs {
  35. c.BannerURL = ""
  36. }
  37. }
  38. // transformPrimaryCategory 生成一级分区列表
  39. func transformPrimaryCategory(cs map[int64]*artmdl.Category) (res []*artmdl.Category) {
  40. res = make([]*artmdl.Category, 0, len(cs))
  41. for _, c := range cs {
  42. if c.ParentID == 0 {
  43. nc := new(artmdl.Category)
  44. *nc = *c
  45. res = append(res, nc)
  46. }
  47. }
  48. sort.Sort(artmdl.Categories(res))
  49. if len(res) > 4 {
  50. res[4].Name = conf.Conf.Article.AppCategoryName
  51. res[4].BannerURL = conf.Conf.Article.AppCategoryURL
  52. }
  53. return
  54. }
  55. // 生成一级分类含children的数组
  56. func transformCategory(cs map[int64]*artmdl.Category) (res artmdl.Categories) {
  57. newCs := make(map[int64]*artmdl.Category)
  58. for k, c := range cs {
  59. n := *c
  60. newCs[k] = &n
  61. }
  62. m := make(map[int64][]*artmdl.Category)
  63. for _, c := range newCs {
  64. m[c.ParentID] = append(m[c.ParentID], c)
  65. }
  66. for _, x := range m {
  67. sort.Sort(artmdl.Categories(x))
  68. }
  69. res = m[0]
  70. for id, cate := range newCs {
  71. if len(m[id]) != 0 {
  72. cate.Children = m[id]
  73. }
  74. }
  75. return
  76. }
  77. // 生成某个分类下的所有子分类
  78. func transformReverseCategory(cs map[int64]*artmdl.Category) (res map[int64][]*artmdl.Category) {
  79. res = make(map[int64][]*artmdl.Category)
  80. for _, c := range cs {
  81. n := c
  82. old := c
  83. for (n != nil) && (n.ParentID != 0) {
  84. res[n.ParentID] = append(res[n.ParentID], old)
  85. n = cs[n.ParentID]
  86. }
  87. }
  88. return
  89. }
  90. // 生成每个分区的父分区列表(含本分区)
  91. func transformCategoryParents(cs map[int64]*artmdl.Category) (res map[int64][]*artmdl.Category) {
  92. res = make(map[int64][]*artmdl.Category)
  93. for _, c := range cs {
  94. categories := []*artmdl.Category{c}
  95. parentID := c.ParentID
  96. for parentID != 0 {
  97. if cs[parentID] != nil {
  98. categories = append(categories, cs[parentID])
  99. parentID = cs[parentID].ParentID
  100. continue
  101. }
  102. break
  103. }
  104. //reverse categories
  105. for i := len(categories)/2 - 1; i >= 0; i-- {
  106. opp := len(categories) - 1 - i
  107. categories[i], categories[opp] = categories[opp], categories[i]
  108. }
  109. res[c.ID] = categories
  110. }
  111. return
  112. }