SysRoleController.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package com.yunzhi.demo.controller;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.yunzhi.demo.common.BaseController;
  6. import com.yunzhi.demo.common.Constants;
  7. import com.yunzhi.demo.common.ResponseBean;
  8. import com.yunzhi.demo.common.StringUtils;
  9. import com.yunzhi.demo.entity.*;
  10. import com.yunzhi.demo.service.ISysMenuService;
  11. import com.yunzhi.demo.service.ISysUserRoleService;
  12. import io.swagger.annotations.Api;
  13. import io.swagger.annotations.ApiOperation;
  14. import io.swagger.annotations.ApiParam;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.web.bind.annotation.PathVariable;
  19. import org.springframework.web.bind.annotation.RequestBody;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.RequestMethod;
  22. import org.springframework.web.bind.annotation.RequestParam;
  23. import com.yunzhi.demo.service.ISysRoleService;
  24. import org.springframework.web.bind.annotation.RestController;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. /**
  28. * <p>
  29. * 角色 前端控制器
  30. * </p>
  31. *
  32. * @author yansen
  33. * @since 2021-04-15
  34. */
  35. @Api(tags = "角色")
  36. @RestController
  37. @RequestMapping("/")
  38. public class SysRoleController extends BaseController {
  39. private final Logger logger = LoggerFactory.getLogger(SysRoleController.class);
  40. @Autowired
  41. ISysRoleService iSysRoleService;
  42. @Autowired
  43. ISysUserRoleService iSysUserRoleService;
  44. @Autowired
  45. ISysMenuService iSysMenuService;
  46. /**
  47. * 分页查询列表
  48. * @param pageNum
  49. * @param pageSize
  50. * @return
  51. */
  52. @RequestMapping(value="/admin/role",method= RequestMethod.GET)
  53. @ApiOperation(value="列表", notes = "列表", httpMethod = "GET", response = ResponseBean.class)
  54. public ResponseBean sysRoleList(@ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
  55. @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize) throws Exception{
  56. SysUser sysUser = getCurrentUser();
  57. boolean isSuperAdmin = Constants.SUPER_ADMIN_ID.equals(sysUser.getUserId());
  58. // 非管理员, 只能看到自己授权的角色
  59. List<Integer> roleIds = new ArrayList<>();
  60. if (!isSuperAdmin) {
  61. List<SysUserRole> userRoleList = iSysUserRoleService.getListByUser(sysUser.getUserId());
  62. if (userRoleList != null) {
  63. for (SysUserRole userRole : userRoleList) {
  64. roleIds.add(userRole.getRoleId());
  65. }
  66. }
  67. if (roleIds.size() == 0) {
  68. // 设置一个不存在的 id, 目的是为了让 sql 查询不到数据
  69. roleIds.add(-1);
  70. }
  71. }
  72. IPage<SysRole> pg = new Page<>(pageNum, pageSize);
  73. QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
  74. queryWrapper.in(!isSuperAdmin, "role_id", roleIds);
  75. queryWrapper.gt("role_id", Constants.SUPER_ADMIN_ROLE);
  76. queryWrapper.gt("status", Constants.STATUS_DELETED);
  77. queryWrapper.orderByAsc("sort_no");
  78. queryWrapper.orderByDesc("create_date");
  79. IPage<SysRole> result = iSysRoleService.page(pg, queryWrapper);
  80. return ResponseBean.success(result);
  81. }
  82. /**
  83. * 保存对象
  84. * @param sysRole 实体对象
  85. * @return
  86. */
  87. @RequestMapping(value="/admin/role",method= RequestMethod.POST)
  88. @ApiOperation(value="保存", notes = "保存", httpMethod = "POST", response = ResponseBean.class)
  89. public ResponseBean sysRoleAdd(@ApiParam("保存内容") @RequestBody SysRole sysRole) throws Exception{
  90. if (StringUtils.isEmpty(sysRole.getName())) {
  91. throw new Exception("角色名称不能为空");
  92. }
  93. if (iSysRoleService.save(sysRole)){
  94. return ResponseBean.success(sysRole);
  95. }else {
  96. return ResponseBean.error("保存失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
  97. }
  98. }
  99. /**
  100. * 根据id删除对象
  101. * @param id 实体ID
  102. */
  103. @RequestMapping(value="/admin/role/{id}", method= RequestMethod.DELETE)
  104. @ApiOperation(value="删除", notes = "删除", httpMethod = "DELETE", response = ResponseBean.class)
  105. public ResponseBean sysRoleDelete(@ApiParam("对象ID") @PathVariable Integer id) throws Exception{
  106. SysRole sysRole = iSysRoleService.getById(id);
  107. if (null == sysRole || Constants.STATUS_DELETED.equals(sysRole.getStatus())) {
  108. return ResponseBean.success("success");
  109. }
  110. sysRole.setStatus(Constants.STATUS_DELETED);
  111. if(iSysRoleService.updateById(sysRole)){
  112. return ResponseBean.success("success");
  113. }else {
  114. return ResponseBean.error("删除失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
  115. }
  116. }
  117. /**
  118. * 修改对象
  119. * @param id 实体ID
  120. * @param sysRole 实体对象
  121. * @return
  122. */
  123. @RequestMapping(value="/admin/role/{id}",method= RequestMethod.PUT)
  124. @ApiOperation(value="更新", notes = "更新", httpMethod = "PUT", response = ResponseBean.class)
  125. public ResponseBean sysRoleUpdate(@ApiParam("对象ID") @PathVariable Integer id,
  126. @ApiParam("更新内容") @RequestBody SysRole sysRole) throws Exception{
  127. if (StringUtils.isEmpty(sysRole.getName())) {
  128. throw new Exception("角色名称不能为空");
  129. }
  130. if (!id.equals(sysRole.getRoleId())) {
  131. throw new Exception("校验角色信息失败");
  132. }
  133. if (iSysRoleService.updateById(sysRole)){
  134. return ResponseBean.success(iSysRoleService.getById(id));
  135. }else {
  136. return ResponseBean.error("修改失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
  137. }
  138. }
  139. /**
  140. * 根据id查询对象
  141. * @param id 实体ID
  142. */
  143. @RequestMapping(value="/admin/role/{id}",method= RequestMethod.GET)
  144. @ApiOperation(value="查询角色信息", notes = "详情", httpMethod = "GET", response = ResponseBean.class)
  145. public ResponseBean sysRoleGet(@ApiParam("对象ID") @PathVariable Integer id) throws Exception{
  146. SysRole sysRole = iSysRoleService.getById(id);
  147. if (null == sysRole || Constants.STATUS_DELETED.equals(sysRole.getStatus())) {
  148. throw new Exception("未找到指定的角色信息");
  149. }
  150. List<SysMenu> menuList = iSysMenuService.getListByRole(id);
  151. if (null != menuList) {
  152. sysRole.setMenuList(menuList);
  153. }
  154. return ResponseBean.success(sysRole);
  155. }
  156. }