classify.go 3.9 KB

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