SysRoleController.java 5.5 KB

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