chart_permission.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/services"
  6. "eta/eta_api/utils"
  7. )
  8. // ChartPermissionController 品种列表
  9. type ChartPermissionController struct {
  10. BaseAuthController
  11. }
  12. // List
  13. // @Title 品种列表
  14. // @Description 品种列表
  15. // @Param Keyword query string false "关键词"
  16. // @Success 200 Ret=200 操作成功
  17. // @router /list [get]
  18. func (this *ChartPermissionController) List() {
  19. br := new(models.BaseResponse).Init()
  20. defer func() {
  21. if br.ErrMsg == "" {
  22. br.IsSendEmail = false
  23. }
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. sysUser := this.SysUser
  28. if sysUser == nil {
  29. br.Msg = "请登录"
  30. br.ErrMsg = "请登录,SysUser Is Empty"
  31. br.Ret = 408
  32. return
  33. }
  34. cond := ` and product_id=1`
  35. pars := make([]interface{}, 0)
  36. list, e := services.GetChartPermissionList(cond, pars)
  37. if e != nil {
  38. br.Msg = "获取失败"
  39. br.ErrMsg = "获取品种列表失败, Err: " + e.Error()
  40. return
  41. }
  42. // 品种树
  43. resp := make([]*models.ChartPermissionItem, 0)
  44. childMap := make(map[int][]*models.ChartPermissionItem)
  45. for _, v := range list {
  46. t := new(models.ChartPermissionItem)
  47. t.PermissionId = v.ChartPermissionId
  48. t.PermissionName = v.PermissionName
  49. t.ParentId = v.ParentId
  50. t.IsPublic = v.IsPublic
  51. t.Enabled = v.Enabled
  52. t.Sort = v.Sort
  53. t.CreateTime = v.CreatedTime.Format(utils.FormatDateTime)
  54. t.Child = make([]*models.ChartPermissionItem, 0)
  55. if v.ParentId == 0 {
  56. resp = append(resp, t)
  57. continue
  58. }
  59. if v.ParentId > 0 {
  60. if childMap[v.ParentId] == nil {
  61. childMap[v.ParentId] = make([]*models.ChartPermissionItem, 0)
  62. }
  63. childMap[v.ParentId] = append(childMap[v.ParentId], t)
  64. }
  65. }
  66. for _, r := range resp {
  67. r.Child = childMap[r.PermissionId]
  68. }
  69. br.Ret = 200
  70. br.Success = true
  71. br.Msg = "获取成功"
  72. br.Data = resp
  73. }
  74. // Add
  75. // @Title 新增品种列表
  76. // @Description 新增品种列表
  77. // @Param request body models.PermissionAddReq true "type json string"
  78. // @Success 200 Ret=200 操作成功
  79. // @router /add [post]
  80. func (this *ChartPermissionController) Add() {
  81. br := new(models.BaseResponse).Init()
  82. defer func() {
  83. if br.ErrMsg == "" {
  84. br.IsSendEmail = false
  85. }
  86. this.Data["json"] = br
  87. this.ServeJSON()
  88. }()
  89. sysUser := this.SysUser
  90. if sysUser == nil {
  91. br.Msg = "请登录"
  92. br.ErrMsg = "请登录,SysUser Is Empty"
  93. br.Ret = 408
  94. return
  95. }
  96. var req models.PermissionAddReq
  97. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  98. br.Msg = "参数解析异常!"
  99. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  100. return
  101. }
  102. if req.PermissionName == "" {
  103. br.Msg = "请输入品种名称"
  104. return
  105. }
  106. e, msg := services.AddChartPermission(req)
  107. if e != nil {
  108. br.Msg = msg
  109. br.ErrMsg = "新增品种失败, Err: " + e.Error()
  110. return
  111. }
  112. br.Ret = 200
  113. br.Success = true
  114. br.Msg = "获取成功"
  115. }
  116. // Edit
  117. // @Title 编辑品种
  118. // @Description 编辑品种
  119. // @Param request body models.PermissionEditReq true "type json string"
  120. // @Success 200 Ret=200 操作成功
  121. // @router /edit [post]
  122. func (this *ChartPermissionController) Edit() {
  123. br := new(models.BaseResponse).Init()
  124. defer func() {
  125. if br.ErrMsg == "" {
  126. br.IsSendEmail = false
  127. }
  128. this.Data["json"] = br
  129. this.ServeJSON()
  130. }()
  131. sysUser := this.SysUser
  132. if sysUser == nil {
  133. br.Msg = "请登录"
  134. br.ErrMsg = "请登录,SysUser Is Empty"
  135. br.Ret = 408
  136. return
  137. }
  138. var req models.PermissionEditReq
  139. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  140. br.Msg = "参数解析异常!"
  141. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  142. return
  143. }
  144. if req.PermissionId <= 0 {
  145. br.Msg = "请选择要编辑的品种"
  146. return
  147. }
  148. if req.PermissionName == "" {
  149. br.Msg = "请输入品种名称"
  150. return
  151. }
  152. e, msg := services.EditChartPermission(req)
  153. if e != nil {
  154. br.Msg = msg
  155. br.ErrMsg = "编辑品种失败, Err: " + e.Error()
  156. return
  157. }
  158. br.Ret = 200
  159. br.Success = true
  160. br.Msg = "获取成功"
  161. }
  162. // Move
  163. // @Title 移动品种
  164. // @Description 移动品种
  165. // @Param request body models.PermissionMoveReq true "type json string"
  166. // @Success 200 Ret=200 操作成功
  167. // @router /move [post]
  168. func (this *ChartPermissionController) Move() {
  169. br := new(models.BaseResponse).Init()
  170. defer func() {
  171. if br.ErrMsg == "" {
  172. br.IsSendEmail = false
  173. }
  174. this.Data["json"] = br
  175. this.ServeJSON()
  176. }()
  177. sysUser := this.SysUser
  178. if sysUser == nil {
  179. br.Msg = "请登录"
  180. br.ErrMsg = "请登录,SysUser Is Empty"
  181. br.Ret = 408
  182. return
  183. }
  184. var req models.PermissionMoveReq
  185. // todo 限制修改一级品种
  186. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  187. br.Msg = "参数解析异常!"
  188. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  189. return
  190. }
  191. if req.PermissionId == 0 {
  192. br.Msg = "请选择要移动的品种"
  193. return
  194. }
  195. e, msg := services.MoveChartPermission(req)
  196. if e != nil {
  197. br.Msg = msg
  198. br.ErrMsg = "移动品种失败, Err: " + e.Error()
  199. return
  200. }
  201. br.Ret = 200
  202. br.Success = true
  203. br.Msg = "获取成功"
  204. }
  205. // SetEnabled
  206. // @Title 启用/禁用品种
  207. // @Description 启用/禁用品种
  208. // @Param request body models.PermissionEnabledReq true "type json string"
  209. // @Success 200 Ret=200 操作成功
  210. // @router /enabled/set [post]
  211. func (this *ChartPermissionController) SetEnabled() {
  212. br := new(models.BaseResponse).Init()
  213. defer func() {
  214. if br.ErrMsg == "" {
  215. br.IsSendEmail = false
  216. }
  217. this.Data["json"] = br
  218. this.ServeJSON()
  219. }()
  220. sysUser := this.SysUser
  221. if sysUser == nil {
  222. br.Msg = "请登录"
  223. br.ErrMsg = "请登录,SysUser Is Empty"
  224. br.Ret = 408
  225. return
  226. }
  227. var req models.PermissionEnabledReq
  228. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  229. br.Msg = "参数解析异常!"
  230. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  231. return
  232. }
  233. if req.PermissionId <= 0 {
  234. br.Msg = "请选择要编辑的品种"
  235. return
  236. }
  237. if req.Enabled != 0 && req.Enabled != 1 {
  238. br.Msg = "请选择正确的启用禁用状态"
  239. return
  240. }
  241. e, msg := services.SetEnabledChartPermission(req)
  242. if e != nil {
  243. br.Msg = msg
  244. br.ErrMsg = "编辑品种失败, Err: " + e.Error()
  245. return
  246. }
  247. br.Ret = 200
  248. br.Success = true
  249. br.Msg = "操作成功"
  250. }