SysRoleController.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package com.ruoyi.web.controller.system;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.security.access.prepost.PreAuthorize;
  5. import org.springframework.validation.annotation.Validated;
  6. import org.springframework.web.bind.annotation.DeleteMapping;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.PostMapping;
  10. import org.springframework.web.bind.annotation.PutMapping;
  11. import org.springframework.web.bind.annotation.RequestBody;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import com.ruoyi.common.annotation.Log;
  15. import com.ruoyi.common.constant.UserConstants;
  16. import com.ruoyi.common.core.controller.BaseController;
  17. import com.ruoyi.common.core.domain.AjaxResult;
  18. import com.ruoyi.common.core.domain.entity.SysRole;
  19. import com.ruoyi.common.core.page.TableDataInfo;
  20. import com.ruoyi.common.enums.BusinessType;
  21. import com.ruoyi.common.utils.SecurityUtils;
  22. import com.ruoyi.common.utils.poi.ExcelUtil;
  23. import com.ruoyi.system.service.ISysRoleService;
  24. /**
  25. * 角色信息
  26. *
  27. * @author ruoyi
  28. */
  29. @RestController
  30. @RequestMapping("/system/role")
  31. public class SysRoleController extends BaseController
  32. {
  33. @Autowired
  34. private ISysRoleService roleService;
  35. @PreAuthorize("@ss.hasPermi('system:role:list')")
  36. @GetMapping("/list")
  37. public TableDataInfo list(SysRole role)
  38. {
  39. startPage();
  40. List<SysRole> list = roleService.selectRoleList(role);
  41. return getDataTable(list);
  42. }
  43. @Log(title = "角色管理", businessType = BusinessType.EXPORT)
  44. @PreAuthorize("@ss.hasPermi('system:role:export')")
  45. @GetMapping("/export")
  46. public AjaxResult export(SysRole role)
  47. {
  48. List<SysRole> list = roleService.selectRoleList(role);
  49. ExcelUtil<SysRole> util = new ExcelUtil<SysRole>(SysRole.class);
  50. return util.exportExcel(list, "角色数据");
  51. }
  52. /**
  53. * 根据角色编号获取详细信息
  54. */
  55. @PreAuthorize("@ss.hasPermi('system:role:query')")
  56. @GetMapping(value = "/{roleId}")
  57. public AjaxResult getInfo(@PathVariable Long roleId)
  58. {
  59. return AjaxResult.success(roleService.selectRoleById(roleId));
  60. }
  61. /**
  62. * 新增角色
  63. */
  64. @PreAuthorize("@ss.hasPermi('system:role:add')")
  65. @Log(title = "角色管理", businessType = BusinessType.INSERT)
  66. @PostMapping
  67. public AjaxResult add(@Validated @RequestBody SysRole role)
  68. {
  69. if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role)))
  70. {
  71. return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败,角色名称已存在");
  72. }
  73. else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role)))
  74. {
  75. return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败,角色权限已存在");
  76. }
  77. role.setCreateBy(SecurityUtils.getUsername());
  78. return toAjax(roleService.insertRole(role));
  79. }
  80. /**
  81. * 修改保存角色
  82. */
  83. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  84. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  85. @PutMapping
  86. public AjaxResult edit(@Validated @RequestBody SysRole role)
  87. {
  88. roleService.checkRoleAllowed(role);
  89. if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role)))
  90. {
  91. return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,角色名称已存在");
  92. }
  93. else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role)))
  94. {
  95. return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,角色权限已存在");
  96. }
  97. role.setUpdateBy(SecurityUtils.getUsername());
  98. return toAjax(roleService.updateRole(role));
  99. }
  100. /**
  101. * 修改保存数据权限
  102. */
  103. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  104. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  105. @PutMapping("/dataScope")
  106. public AjaxResult dataScope(@RequestBody SysRole role)
  107. {
  108. roleService.checkRoleAllowed(role);
  109. return toAjax(roleService.authDataScope(role));
  110. }
  111. /**
  112. * 状态修改
  113. */
  114. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  115. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  116. @PutMapping("/changeStatus")
  117. public AjaxResult changeStatus(@RequestBody SysRole role)
  118. {
  119. roleService.checkRoleAllowed(role);
  120. role.setUpdateBy(SecurityUtils.getUsername());
  121. return toAjax(roleService.updateRoleStatus(role));
  122. }
  123. /**
  124. * 删除角色
  125. */
  126. @PreAuthorize("@ss.hasPermi('system:role:remove')")
  127. @Log(title = "角色管理", businessType = BusinessType.DELETE)
  128. @DeleteMapping("/{roleIds}")
  129. public AjaxResult remove(@PathVariable Long[] roleIds)
  130. {
  131. return toAjax(roleService.deleteRoleByIds(roleIds));
  132. }
  133. /**
  134. * 获取角色选择框列表
  135. */
  136. @PreAuthorize("@ss.hasPermi('system:role:query')")
  137. @GetMapping("/optionselect")
  138. public AjaxResult optionselect()
  139. {
  140. return AjaxResult.success(roleService.selectRoleAll());
  141. }
  142. }