SysRoleController.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package com.ruoyi.web.controller.system;
  2. import com.ruoyi.common.annotation.Log;
  3. import com.ruoyi.common.constant.UserConstants;
  4. import com.ruoyi.common.core.controller.BaseController;
  5. import com.ruoyi.common.core.domain.AjaxResult;
  6. import com.ruoyi.common.core.domain.entity.SysDept;
  7. import com.ruoyi.common.core.domain.entity.SysRole;
  8. import com.ruoyi.common.core.domain.entity.SysUser;
  9. import com.ruoyi.common.core.page.TableDataInfo;
  10. import com.ruoyi.common.enums.BusinessType;
  11. import com.ruoyi.common.utils.poi.ExcelUtil;
  12. import com.ruoyi.framework.web.service.SysPermissionService;
  13. import com.ruoyi.framework.web.service.TokenService;
  14. import com.ruoyi.system.domain.SysUserRole;
  15. import com.ruoyi.system.service.ISysDeptService;
  16. import com.ruoyi.system.service.ISysRoleService;
  17. import com.ruoyi.system.service.ISysUserService;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.security.access.prepost.PreAuthorize;
  20. import org.springframework.validation.annotation.Validated;
  21. import org.springframework.web.bind.annotation.*;
  22. import javax.servlet.http.HttpServletResponse;
  23. import java.util.Arrays;
  24. import java.util.List;
  25. /**
  26. * 角色信息
  27. *
  28. * @author ruoyi
  29. */
  30. @RestController
  31. @RequestMapping("/system/role")
  32. public class SysRoleController extends BaseController {
  33. @Autowired
  34. private ISysRoleService roleService;
  35. @Autowired
  36. private TokenService tokenService;
  37. @Autowired
  38. private SysPermissionService permissionService;
  39. @Autowired
  40. private ISysUserService userService;
  41. @Autowired
  42. private ISysDeptService deptService;
  43. @PreAuthorize("@ss.hasPermi('system:role:list')")
  44. @GetMapping("/list")
  45. public TableDataInfo list(SysRole role) {
  46. startPage();
  47. List<SysRole> list = roleService.selectRoleList(role);
  48. return getDataTable(list);
  49. }
  50. @Log(title = "角色管理", businessType = BusinessType.EXPORT)
  51. @PreAuthorize("@ss.hasPermi('system:role:export')")
  52. @PostMapping("/export")
  53. public void export(HttpServletResponse response, SysRole role) {
  54. List<SysRole> list = roleService.selectRoleList(role);
  55. ExcelUtil<SysRole> util = new ExcelUtil<SysRole>(SysRole.class);
  56. util.exportExcel(response, list, "角色数据");
  57. }
  58. /**
  59. * 根据角色编号获取详细信息
  60. */
  61. @PreAuthorize("@ss.hasPermi('system:role:query')")
  62. @GetMapping(value = "/{roleId}")
  63. public AjaxResult getInfo(@PathVariable Long roleId) {
  64. roleService.checkRoleDataScope(roleId);
  65. return AjaxResult.success(roleService.selectRoleById(roleId));
  66. }
  67. /**
  68. * 新增角色
  69. */
  70. @PreAuthorize("@ss.hasPermi('system:role:add')")
  71. @Log(title = "角色管理", businessType = BusinessType.INSERT)
  72. @PostMapping
  73. public AjaxResult add(@Validated @RequestBody SysRole role) {
  74. if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
  75. return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败,角色名称已存在");
  76. } else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
  77. return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败,角色权限已存在");
  78. }
  79. role.setCreateBy(getUsername());
  80. return toAjax(roleService.insertRole(role));
  81. }
  82. /**
  83. * 修改保存角色
  84. */
  85. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  86. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  87. @PutMapping
  88. public AjaxResult edit(@Validated @RequestBody SysRole role) {
  89. roleService.checkRoleAllowed(role);
  90. roleService.checkRoleDataScope(role.getRoleId());
  91. if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
  92. return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,角色名称已存在");
  93. } else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
  94. return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,角色权限已存在");
  95. }
  96. role.setUpdateBy(getUsername());
  97. if (roleService.updateRole(role) > 0) {
  98. permissionService.refreshRolePermission(role.getRoleId());
  99. return AjaxResult.success();
  100. }
  101. return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,请联系管理员");
  102. }
  103. /**
  104. * 修改保存数据权限
  105. */
  106. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  107. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  108. @PutMapping("/dataScope")
  109. public AjaxResult dataScope(@RequestBody SysRole role) {
  110. roleService.checkRoleAllowed(role);
  111. roleService.checkRoleDataScope(role.getRoleId());
  112. return toAjax(roleService.authDataScope(role));
  113. }
  114. /**
  115. * 状态修改
  116. */
  117. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  118. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  119. @PutMapping("/changeStatus")
  120. public AjaxResult changeStatus(@RequestBody SysRole role) {
  121. roleService.checkRoleAllowed(role);
  122. roleService.checkRoleDataScope(role.getRoleId());
  123. role.setUpdateBy(getUsername());
  124. int row = roleService.updateRoleStatus(role);
  125. if (row > 0) {
  126. permissionService.refreshRolePermission(role.getRoleId());
  127. }
  128. return toAjax(row);
  129. }
  130. /**
  131. * 删除角色
  132. */
  133. @PreAuthorize("@ss.hasPermi('system:role:remove')")
  134. @Log(title = "角色管理", businessType = BusinessType.DELETE)
  135. @DeleteMapping("/{roleIds}")
  136. public AjaxResult remove(@PathVariable Long[] roleIds) {
  137. if (Arrays.asList(roleIds).contains(4l)) {
  138. return AjaxResult.error("不支持删除医生这个角色");
  139. }
  140. int row = roleService.deleteRoleByIds(roleIds);
  141. if (row > 0) {
  142. for (Long roleId : roleIds) {
  143. permissionService.refreshRolePermission(roleId);
  144. }
  145. }
  146. return toAjax(row);
  147. }
  148. /**
  149. * 获取角色选择框列表
  150. */
  151. @PreAuthorize("@ss.hasPermi('system:role:query')")
  152. @GetMapping("/optionselect")
  153. public AjaxResult optionselect() {
  154. return AjaxResult.success(roleService.selectRoleAll());
  155. }
  156. /**
  157. * 查询已分配用户角色列表
  158. */
  159. @PreAuthorize("@ss.hasPermi('system:role:list')")
  160. @GetMapping("/authUser/allocatedList")
  161. public TableDataInfo allocatedList(SysUser user) {
  162. startPage();
  163. List<SysUser> list = userService.selectAllocatedList(user);
  164. return getDataTable(list);
  165. }
  166. /**
  167. * 查询未分配用户角色列表
  168. */
  169. @PreAuthorize("@ss.hasPermi('system:role:list')")
  170. @GetMapping("/authUser/unallocatedList")
  171. public TableDataInfo unallocatedList(SysUser user) {
  172. startPage();
  173. List<SysUser> list = userService.selectUnallocatedList(user);
  174. return getDataTable(list);
  175. }
  176. /**
  177. * 取消授权用户
  178. */
  179. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  180. @Log(title = "角色管理", businessType = BusinessType.GRANT)
  181. @PutMapping("/authUser/cancel")
  182. public AjaxResult cancelAuthUser(@RequestBody SysUserRole userRole) {
  183. int row = roleService.deleteAuthUser(userRole);
  184. if (row > 0) {
  185. permissionService.refreshUserPermission(userRole.getUserId());
  186. }
  187. return toAjax(row);
  188. }
  189. /**
  190. * 批量取消授权用户
  191. */
  192. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  193. @Log(title = "角色管理", businessType = BusinessType.GRANT)
  194. @PutMapping("/authUser/cancelAll")
  195. public AjaxResult cancelAuthUserAll(Long roleId, Long[] userIds) {
  196. int row = roleService.deleteAuthUsers(roleId, userIds);
  197. if (row > 0) {
  198. for (Long userId : userIds) {
  199. permissionService.refreshUserPermission(userId);
  200. }
  201. }
  202. return toAjax(row);
  203. }
  204. /**
  205. * 批量选择用户授权
  206. */
  207. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  208. @Log(title = "角色管理", businessType = BusinessType.GRANT)
  209. @PutMapping("/authUser/selectAll")
  210. public AjaxResult selectAuthUserAll(Long roleId, Long[] userIds) {
  211. roleService.checkRoleDataScope(roleId);
  212. int row = roleService.insertAuthUsers(roleId, userIds);
  213. if (row > 0) {
  214. for (Long userId : userIds) {
  215. permissionService.refreshUserPermission(userId);
  216. }
  217. }
  218. return toAjax(row);
  219. }
  220. /**
  221. * 获取对应角色部门树列表
  222. */
  223. @PreAuthorize("@ss.hasPermi('system:role:query')")
  224. @GetMapping(value = "/deptTree/{roleId}")
  225. public AjaxResult deptTree(@PathVariable("roleId") Long roleId) {
  226. AjaxResult ajax = AjaxResult.success();
  227. ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
  228. ajax.put("depts", deptService.selectDeptTreeList(new SysDept()));
  229. return ajax;
  230. }
  231. }