RoleController.groovy 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package com.jd.brume.controller
  2. import com.jd.brume.entity.func.UserFunc
  3. import com.jd.brume.service.UserService
  4. import javax.annotation.Resource
  5. import javax.validation.constraints.NotNull
  6. import org.springframework.validation.annotation.Validated
  7. import org.springframework.web.bind.annotation.DeleteMapping
  8. import org.springframework.web.bind.annotation.GetMapping
  9. import org.springframework.web.bind.annotation.PostMapping
  10. import org.springframework.web.bind.annotation.RequestMapping
  11. import org.springframework.web.bind.annotation.RestController
  12. import com.baomidou.mybatisplus.core.conditions.Wrapper
  13. import com.baomidou.mybatisplus.core.toolkit.StringUtils
  14. import com.baomidou.mybatisplus.core.toolkit.Wrappers
  15. import com.baomidou.mybatisplus.extension.plugins.pagination.Page
  16. import com.jd.brume.entity.RoleEntity
  17. import com.jd.brume.entity.RoleMenuEntity
  18. import com.jd.brume.entity.func.RoleFunc
  19. import com.jd.brume.entity.func.RoleMenuFunc
  20. import com.jd.brume.service.RoleMenuService
  21. import com.jd.brume.service.RoleService
  22. import com.jd.brume.util.Result
  23. import com.jd.brume.util.ResultEnum
  24. import com.jd.brume.vo.RoleVo
  25. import com.jd.brume.vo.group.PagingGroup
  26. import com.jd.brume.vo.group.SaveGroup
  27. import com.jd.brume.vo.group.UpdateGroup
  28. /**
  29. * ???????
  30. * @author ???
  31. *
  32. */
  33. @Validated
  34. @RestController
  35. @RequestMapping('role')
  36. class RoleController {
  37. @Resource
  38. RoleService roleService
  39. @Resource
  40. RoleMenuService roleMenuService
  41. @Resource
  42. UserService userService
  43. /**
  44. * 分页查询
  45. * @param roleVo
  46. * @return
  47. */
  48. @GetMapping('paging')
  49. def getRolePaging(@Validated(PagingGroup) RoleVo roleVo) {
  50. Wrapper<RoleEntity> wrapper = Wrappers.lambdaQuery()
  51. .like(StringUtils.isNotBlank(roleVo.queryVal), RoleFunc.roleName(), roleVo.queryVal)
  52. def count = roleService.count(wrapper)
  53. if (count == 0) return new Result().msg(ResultEnum.NO_DATA_TABLE)
  54. wrapper.orderByDesc(RoleFunc.createTime())
  55. def data = roleService.page(new Page(roleVo.page, roleVo.limit, count), wrapper)
  56. return new Result().layTable([list: data.records, count: data.total])
  57. }
  58. /**
  59. * 删除角色
  60. * @param roleId
  61. * @return
  62. */
  63. @DeleteMapping('delete')
  64. def deleteRole(@NotNull Integer roleId) {
  65. def count = userService.count(Wrappers.lambdaQuery().eq(UserFunc.roleId(), roleId))
  66. if (count > 0){
  67. return new Result().msg(100, '角色已被用户使用')
  68. }
  69. return new Result().ok(roleService.removeById(roleId))
  70. }
  71. @GetMapping('one')
  72. def getRole(@NotNull Integer roleId) {
  73. return new Result().ok([role: roleService.getById(roleId), menus: roleMenuService.lambdaQuery().eq(RoleMenuFunc.roleId(), roleId).list()])
  74. }
  75. /**
  76. * 保存角色
  77. * @param roleVo
  78. * @return
  79. */
  80. @PostMapping('save')
  81. def saveRole(@Validated(SaveGroup) RoleVo roleVo) {
  82. def count = roleService.count(Wrappers.lambdaQuery().eq(RoleFunc.roleName(), roleVo.roleName))
  83. if (count > 0) return new Result().msg(100, '角色名称重复')
  84. // ??????
  85. RoleEntity role = new RoleEntity(roleName: roleVo.roleName, description: roleVo.description)
  86. def status = roleService.save(role)
  87. // ???????
  88. if (roleVo.roleMenu) {
  89. RoleMenuEntity roleMenu
  90. def arr = []
  91. for (int i = 0; i < roleVo.roleMenu.size(); i++) {
  92. def id = roleVo.roleMenu.get(i)
  93. roleMenu = new RoleMenuEntity(menuId: Integer.valueOf(id), buttonIds: roleVo.roleBtn[String.valueOf(id)], roleId: role.roleId)
  94. arr.add(roleMenu)
  95. }
  96. status = roleMenuService.saveBatch(arr)
  97. }
  98. return new Result().ok(status)
  99. }
  100. /**
  101. * 修改角色
  102. * @param roleVo
  103. * @return
  104. */
  105. @PostMapping('update')
  106. def updateRole(@Validated(UpdateGroup) RoleVo roleVo) {
  107. def count = roleService.count(Wrappers.lambdaQuery().eq(RoleFunc.roleName(), roleVo.roleName).ne(RoleFunc.roleId(), roleVo.roleId))
  108. if (count > 0) return new Result().msg(100, '角色名称重复')
  109. // ?????
  110. def status = roleService.updateById(new RoleEntity(roleId: roleVo.roleId, roleName: roleVo.roleName, description: roleVo.description))
  111. // ??????
  112. status = roleMenuService.remove(Wrappers.lambdaQuery().eq(RoleMenuFunc.roleId(), roleVo.roleId))
  113. // ???????
  114. if (roleVo.roleMenu) {
  115. RoleMenuEntity roleMenu
  116. def arr = []
  117. for (int i = 0; i < roleVo.roleMenu.size(); i++) {
  118. def id = roleVo.roleMenu.get(i)
  119. roleMenu = new RoleMenuEntity(menuId: Integer.valueOf(id), buttonIds: roleVo.roleBtn[String.valueOf(id)], roleId: roleVo.roleId)
  120. arr.add(roleMenu)
  121. }
  122. status = roleMenuService.saveBatch(arr)
  123. }
  124. return new Result().ok(status)
  125. }
  126. }