classify.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_bridge/models"
  5. "eta/eta_mini_bridge/models/request"
  6. "eta/eta_mini_bridge/models/response"
  7. "eta/eta_mini_bridge/utils"
  8. )
  9. type ClassifyController struct {
  10. BaseAuthController
  11. }
  12. // @Title 根据分类id获取一级品种
  13. // @Description 根据分类id获取一级品种
  14. // @Param request body request.ClassifyReq true "type json string"
  15. // @Success 200 {object} response.ReportDetailResp
  16. // @router /chart_permission/first [post]
  17. func (this *ClassifyController) FirstChartPermission() {
  18. br := new(models.BaseResponse).Init()
  19. defer func() {
  20. this.Data["json"] = br
  21. this.ServeJSON()
  22. }()
  23. var req request.ClassifyReq
  24. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  25. br.Msg = "参数错误"
  26. br.ErrMsg = "参数错误,Err:" + err.Error()
  27. return
  28. }
  29. if len(req.ClassifyIds) == 0 {
  30. br.Msg = "分类id错误,获取失败"
  31. return
  32. }
  33. permissionClassifyMap, err := models.GetChartPermissionListByClassifyIds(req.ClassifyIds)
  34. if err != nil {
  35. br.Msg = "品种获取失败"
  36. br.ErrMsg = "获取品种和分类的映射失败,系统错误,Err:" + err.Error()
  37. return
  38. }
  39. classifyToPermissionMap2 := make(map[int][]int)
  40. chartPermissionIds := make([]int, 0)
  41. for _, v := range permissionClassifyMap {
  42. classifyToPermissionMap2[v.ClassifyId] = append(classifyToPermissionMap2[v.ClassifyId], v.ChartPermissionId)
  43. chartPermissionIds = append(chartPermissionIds, v.ChartPermissionId)
  44. }
  45. // 获取二级品种的权限,并建立映射
  46. chartPermissionList2, err := models.GetChartPermissionListByIds(chartPermissionIds)
  47. if err != nil {
  48. br.Msg = "二级品种获取失败"
  49. br.ErrMsg = "二级品种获取失败,系统错误,Err:" + err.Error()
  50. return
  51. }
  52. chartPermissionViewMap2 := make(map[int]*models.ChartPermission)
  53. for _, v := range chartPermissionList2 {
  54. chartPermissionViewMap2[v.ChartPermissionId] = v
  55. }
  56. // 获取一级品种的权限,并建立映射
  57. chartPermissionList1, err := models.GetChildChartPermissionListById(0)
  58. if err != nil {
  59. br.Msg = "获取研报一级品种权限失败"
  60. br.ErrMsg = "获取研报一级品种权限失败,Err:" + err.Error()
  61. return
  62. }
  63. chartPermissionMap1 := make(map[int]*models.ChartPermission)
  64. for _, v := range chartPermissionList1 {
  65. chartPermissionMap1[v.ChartPermissionId] = v
  66. }
  67. var resp []*response.ClassifyPermissionItemResp
  68. for _, v := range req.ClassifyIds {
  69. t := new(response.ClassifyPermissionItemResp)
  70. t.ClassifyId = v
  71. for _, vv := range classifyToPermissionMap2[v] {
  72. if item, ok := chartPermissionViewMap2[vv]; ok {
  73. parent2 := item.ParentId
  74. t.PermissionNames = append(t.PermissionNames, chartPermissionMap1[parent2].PermissionName)
  75. }
  76. }
  77. t.PermissionNames = utils.Unique(t.PermissionNames)
  78. resp = append(resp, t)
  79. }
  80. br.Data = resp
  81. br.Success = true
  82. br.Ret = 200
  83. br.Msg = "获取成功"
  84. }
  85. // @Title 获取所有分类
  86. // @Description 获取所有分类
  87. // @Success 200 {object} []models.ClassifyView
  88. // @router /list [get]
  89. func (this *ClassifyController) AllClassify() {
  90. br := new(models.BaseResponse).Init()
  91. defer func() {
  92. this.Data["json"] = br
  93. this.ServeJSON()
  94. }()
  95. classifyList, err := models.GetClassifyList()
  96. if err != nil {
  97. br.Msg = "获取分类失败"
  98. br.ErrMsg = "获取分类失败,系统错误,Err:" + err.Error()
  99. return
  100. }
  101. br.Data = classifyList
  102. br.Success = true
  103. br.Ret = 200
  104. br.Msg = "获取成功"
  105. }