classify.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package controllers
  2. import (
  3. "github.com/rdlucklib/rdluck_tools/paging"
  4. "hongze/hongze_ETA_mobile_api/models"
  5. "hongze/hongze_ETA_mobile_api/utils"
  6. )
  7. // 分类
  8. type ClassifyController struct {
  9. BaseAuthController
  10. }
  11. // @Title 获取分类列表
  12. // @Description 获取分类列表
  13. // @Param PageSize query int true "每页数据条数"
  14. // @Param CurrentIndex query int true "当前页页码,从1开始"
  15. // @Param KeyWord query string true "检索关键词"
  16. // @Param CompanyType query string false "产品类型,枚举值:'ficc','权益';不传默认返回全部"
  17. // @Param HideDayWeek query int false "是否隐藏晨周报"
  18. // @Success 200 {object} models.Classify
  19. // @router /list [get]
  20. func (this *ClassifyController) ListClassify() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. this.Data["json"] = br
  24. this.ServeJSON()
  25. }()
  26. pageSize, _ := this.GetInt("PageSize")
  27. currentIndex, _ := this.GetInt("CurrentIndex")
  28. keyWord := this.GetString("KeyWord")
  29. companyType := this.GetString("CompanyType")
  30. hideDayWeek, _ := this.GetInt("HideDayWeek")
  31. var startSize int
  32. if pageSize <= 0 {
  33. pageSize = utils.PageSize20
  34. }
  35. if currentIndex <= 0 {
  36. currentIndex = 1
  37. }
  38. startSize = utils.StartIndex(currentIndex, pageSize)
  39. list, err := models.GetClassifyList(startSize, pageSize, keyWord, companyType, hideDayWeek)
  40. if err != nil {
  41. br.Msg = "获取失败"
  42. br.ErrMsg = "获取失败,Err:" + err.Error()
  43. return
  44. }
  45. total, err := models.GetClassifyListCount(keyWord, companyType, hideDayWeek)
  46. if err != nil {
  47. br.Msg = "获取失败"
  48. br.ErrMsg = "获取失败,Err:" + err.Error()
  49. return
  50. }
  51. parentIds := make([]int, 0)
  52. for i := range list {
  53. parentIds = append(parentIds, list[i].Id)
  54. }
  55. parentIdLen := len(parentIds)
  56. if parentIdLen == 0 {
  57. resp := &models.ClassifyListResp{
  58. List: list,
  59. Paging: paging.GetPaging(currentIndex, pageSize, 0),
  60. }
  61. br.Data = resp
  62. br.Ret = 200
  63. br.Success = true
  64. br.Msg = "获取成功"
  65. return
  66. }
  67. // 获取一级分类-子目录列表
  68. menuListMap := make(map[int][]*models.ClassifyMenu, 0)
  69. var menuCond string
  70. var menuPars []interface{}
  71. menuCond += ` AND classify_id IN (` + utils.GetOrmInReplace(parentIdLen) + `)`
  72. menuPars = append(menuPars, parentIds)
  73. parentMenus, e := models.GetClassifyMenuList(menuCond, menuPars)
  74. if e != nil {
  75. br.Msg = "获取失败"
  76. br.ErrMsg = "获取一级分类子目录列表失败"
  77. return
  78. }
  79. for i := range parentMenus {
  80. if menuListMap[parentMenus[i].ClassifyId] == nil {
  81. menuListMap[parentMenus[i].ClassifyId] = make([]*models.ClassifyMenu, 0)
  82. }
  83. menuListMap[parentMenus[i].ClassifyId] = append(menuListMap[parentMenus[i].ClassifyId], parentMenus[i])
  84. }
  85. // 获取子分类
  86. children, e := models.GetClassifyChildByParentIds(parentIds, keyWord)
  87. if e != nil {
  88. br.Msg = "获取失败"
  89. br.ErrMsg = "获取子分类失败"
  90. return
  91. }
  92. childrenIds := make([]int, 0)
  93. for i := range children {
  94. childrenIds = append(childrenIds, children[i].Id)
  95. }
  96. childrenIdsLen := len(childrenIds)
  97. // 获取二级分类-子目录关联
  98. relateMap := make(map[int]int, 0)
  99. if childrenIdsLen > 0 {
  100. var relateCond string
  101. var relatePars []interface{}
  102. relateCond += ` AND classify_id IN (` + utils.GetOrmInReplace(childrenIdsLen) + `)`
  103. relatePars = append(relatePars, childrenIds)
  104. relates, e := models.GetClassifyMenuRelationList(relateCond, relatePars)
  105. if e != nil {
  106. br.Msg = "获取失败"
  107. br.ErrMsg = "获取二级分类子目录关联失败, Err: " + e.Error()
  108. return
  109. }
  110. for i := range relates {
  111. relateMap[relates[i].ClassifyId] = relates[i].MenuId
  112. }
  113. }
  114. // 二级分类
  115. childrenMap := make(map[int][]*models.ClassifyItem, 0)
  116. for i := range children {
  117. if childrenMap[children[i].ParentId] == nil {
  118. childrenMap[children[i].ParentId] = make([]*models.ClassifyItem, 0)
  119. }
  120. childrenMap[children[i].ParentId] = append(childrenMap[children[i].ParentId], &models.ClassifyItem{
  121. Classify: *children[i],
  122. ClassifyMenuId: relateMap[children[i].Id],
  123. })
  124. }
  125. // 一级分类
  126. for i := range list {
  127. list[i].ClassifyMenuList = menuListMap[list[i].Id]
  128. list[i].Child = childrenMap[list[i].Id]
  129. }
  130. page := paging.GetPaging(currentIndex, pageSize, total)
  131. resp := new(models.ClassifyListResp)
  132. resp.List = list
  133. resp.Paging = page
  134. br.Data = resp
  135. br.Ret = 200
  136. br.Success = true
  137. br.Msg = "获取成功"
  138. }