classify.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package controllers
  2. import (
  3. "eta/eta_mobile/models"
  4. "eta/eta_mobile/services"
  5. "eta/eta_mobile/utils"
  6. )
  7. // 分类
  8. type ClassifyController struct {
  9. BaseAuthController
  10. }
  11. // @Title 获取分类列表
  12. // @Description 获取分类列表
  13. // @Param KeyWord query string true "检索关键词"
  14. // @Param CompanyType query string false "产品类型,枚举值:'ficc','权益';不传默认返回全部"
  15. // @Param HideDayWeek query int false "是否隐藏晨周报"
  16. // @Success 200 {object} models.Classify
  17. // @router /list [get]
  18. func (this *ClassifyController) ListClassify() {
  19. br := new(models.BaseResponse).Init()
  20. defer func() {
  21. this.Data["json"] = br
  22. this.ServeJSON()
  23. }()
  24. keyWord := this.GetString("KeyWord")
  25. reqEnabled, _ := this.GetInt("Enabled", -1)
  26. enabled := -1
  27. if reqEnabled == 1 {
  28. enabled = reqEnabled
  29. }
  30. list, err := models.GetClassifyListByKeyword(keyWord, enabled)
  31. if err != nil {
  32. br.Msg = "获取失败"
  33. br.ErrMsg = "获取失败,Err:" + err.Error()
  34. return
  35. }
  36. if keyWord != `` {
  37. idMap := make(map[int]bool)
  38. currParentClassifyIdList := make([]int, 0)
  39. for _, v := range list {
  40. idMap[v.Id] = true
  41. if v.ParentId > 0 {
  42. currParentClassifyIdList = append(currParentClassifyIdList, v.ParentId)
  43. }
  44. }
  45. findList := list
  46. list = make([]*models.ClassifyList, 0)
  47. tmpList, tmpErr := services.GetParentClassifyListByParentIdList(currParentClassifyIdList)
  48. if tmpErr != nil {
  49. br.Msg = "获取失败"
  50. br.ErrMsg = "获取上级分类信息失败,Err:" + tmpErr.Error()
  51. return
  52. }
  53. for _, v := range tmpList {
  54. if _, ok := idMap[v.Id]; !ok {
  55. list = append(list, v)
  56. }
  57. }
  58. list = append(list, findList...)
  59. }
  60. classifyIdList := make([]int, 0)
  61. for i := range list {
  62. classifyIdList = append(classifyIdList, list[i].Id)
  63. }
  64. parentIdLen := len(classifyIdList)
  65. if parentIdLen == 0 {
  66. resp := &models.ClassifyListResp{
  67. List: list,
  68. }
  69. br.Data = resp
  70. br.Ret = 200
  71. br.Success = true
  72. br.Msg = "获取成功"
  73. return
  74. }
  75. // 获取子目录列表
  76. menuListMap := make(map[int][]*models.ClassifyMenu, 0)
  77. var menuCond string
  78. var menuPars []interface{}
  79. menuCond += ` AND classify_id IN (` + utils.GetOrmInReplace(parentIdLen) + `)`
  80. menuPars = append(menuPars, classifyIdList)
  81. parentMenus, e := models.GetClassifyMenuList(menuCond, menuPars)
  82. if e != nil {
  83. br.Msg = "获取失败"
  84. br.ErrMsg = "获取一级分类子目录列表失败"
  85. return
  86. }
  87. for i := range parentMenus {
  88. if menuListMap[parentMenus[i].ClassifyId] == nil {
  89. menuListMap[parentMenus[i].ClassifyId] = make([]*models.ClassifyMenu, 0)
  90. }
  91. menuListMap[parentMenus[i].ClassifyId] = append(menuListMap[parentMenus[i].ClassifyId], parentMenus[i])
  92. }
  93. // 分类与子目录关联
  94. relateMap := make(map[int]int, 0)
  95. {
  96. var relateCond string
  97. var relatePars []interface{}
  98. relateCond += ` AND classify_id IN (` + utils.GetOrmInReplace(parentIdLen) + `)`
  99. relatePars = append(relatePars, classifyIdList)
  100. relates, e := models.GetClassifyMenuRelationList(relateCond, relatePars)
  101. if e != nil {
  102. br.Msg = "获取失败"
  103. br.ErrMsg = "获取二级分类子目录关联失败, Err: " + e.Error()
  104. return
  105. }
  106. for i := range relates {
  107. relateMap[relates[i].ClassifyId] = relates[i].MenuId
  108. }
  109. }
  110. // 查询分类绑定的权限
  111. permissionList, _ := models.GetAllPermissionMapping()
  112. classifyPermissionMap := make(map[int][]int)
  113. if len(permissionList) > 0 {
  114. for _, v := range permissionList {
  115. classifyPermissionMap[v.ClassifyId] = append(classifyPermissionMap[v.ClassifyId], v.ChartPermissionId)
  116. }
  117. }
  118. // 遍历分类并绑定子目录和权限
  119. for i, v := range list {
  120. list[i].ClassifyMenuList = menuListMap[v.Id]
  121. list[i].ClassifyMenuId = relateMap[v.Id]
  122. if permissionIds, ok := classifyPermissionMap[v.Id]; ok {
  123. list[i].ChartPermissionIdList = permissionIds
  124. }
  125. }
  126. // 先将分类列表排序
  127. services.SortClassifyListBySortAndCreateTime(list)
  128. // 接着转换结构
  129. list = services.GetClassifyListTreeRecursive(list, 0)
  130. resp := new(models.ClassifyListResp)
  131. resp.List = list
  132. br.Data = resp
  133. br.Ret = 200
  134. br.Success = true
  135. br.Msg = "获取成功"
  136. }
  137. // ClassifyPermission
  138. // @Title 获取分类权限列表
  139. // @Description 获取分类权限列表
  140. // @Param ClassifyId query int true "分类id"
  141. // @Success 200 {object} []models.ChartPermissionSearchKeyWordMapping
  142. // @router /permission_list [get]
  143. func (this *ClassifyController) ClassifyPermission() {
  144. br := new(models.BaseResponse).Init()
  145. defer func() {
  146. this.Data["json"] = br
  147. this.ServeJSON()
  148. }()
  149. classifyId, _ := this.GetInt("ClassifyId")
  150. if classifyId <= 0 {
  151. br.Msg = "参数错误"
  152. return
  153. }
  154. //获取报告分类详情
  155. classifyInfo, err := models.GetClassifyById(classifyId)
  156. if err != nil {
  157. br.Msg = "获取分类信息失败"
  158. br.ErrMsg = "获取失败,Err:" + err.Error()
  159. return
  160. }
  161. //获取报告分类权限列表
  162. list, err := models.GetPermissionByClassifyId(classifyInfo.Id)
  163. if err != nil {
  164. br.Msg = "获取分类信息失败"
  165. br.ErrMsg = "获取失败,Err:" + err.Error()
  166. return
  167. }
  168. br.Data = list
  169. br.Ret = 200
  170. br.Success = true
  171. br.Msg = "获取成功"
  172. }