123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- package com.yunzhi.demo.controller;
-
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.yunzhi.demo.common.BaseController;
- import com.yunzhi.demo.common.Constants;
- import com.yunzhi.demo.common.ResponseBean;
- import com.yunzhi.demo.common.StringUtils;
- import com.yunzhi.demo.entity.*;
- import com.yunzhi.demo.service.ISysMenuService;
- import com.yunzhi.demo.service.ISysUserRoleService;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import io.swagger.annotations.ApiParam;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import com.yunzhi.demo.service.ISysRoleService;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * <p>
- * 角色 前端控制器
- * </p>
- *
- * @author yansen
- * @since 2021-04-15
- */
-
- @Api(tags = "角色")
- @RestController
- @RequestMapping("/")
- public class SysRoleController extends BaseController {
-
- private final Logger logger = LoggerFactory.getLogger(SysRoleController.class);
-
- @Autowired
- ISysRoleService iSysRoleService;
-
- @Autowired
- ISysUserRoleService iSysUserRoleService;
-
- @Autowired
- ISysMenuService iSysMenuService;
-
- /**
- * 分页查询列表
- * @param pageNum
- * @param pageSize
- * @return
- */
- @RequestMapping(value="/admin/role",method= RequestMethod.GET)
- @ApiOperation(value="列表", notes = "列表", httpMethod = "GET", response = ResponseBean.class)
- public ResponseBean sysRoleList(@ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
- @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize) throws Exception{
- SysUser sysUser = getCurrentUser();
- boolean isSuperAdmin = Constants.SUPER_ADMIN_ID.equals(sysUser.getUserId());
-
- // 非管理员, 只能看到自己授权的角色
- List<Integer> roleIds = new ArrayList<>();
- if (!isSuperAdmin) {
- List<SysUserRole> userRoleList = iSysUserRoleService.getListByUser(sysUser.getUserId());
- if (userRoleList != null) {
- for (SysUserRole userRole : userRoleList) {
- roleIds.add(userRole.getRoleId());
- }
- }
- if (roleIds.size() == 0) {
- // 设置一个不存在的 id, 目的是为了让 sql 查询不到数据
- roleIds.add(-1);
- }
- }
-
- IPage<SysRole> pg = new Page<>(pageNum, pageSize);
- QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
- queryWrapper.in(!isSuperAdmin, "role_id", roleIds);
- queryWrapper.gt("role_id", Constants.SUPER_ADMIN_ROLE);
- queryWrapper.gt("status", Constants.STATUS_DELETED);
- queryWrapper.orderByAsc("sort_no");
- queryWrapper.orderByDesc("create_date");
-
- IPage<SysRole> result = iSysRoleService.page(pg, queryWrapper);
- return ResponseBean.success(result);
- }
-
- /**
- * 保存对象
- * @param sysRole 实体对象
- * @return
- */
- @RequestMapping(value="/admin/role",method= RequestMethod.POST)
- @ApiOperation(value="保存", notes = "保存", httpMethod = "POST", response = ResponseBean.class)
- public ResponseBean sysRoleAdd(@ApiParam("保存内容") @RequestBody SysRole sysRole) throws Exception{
- if (StringUtils.isEmpty(sysRole.getName())) {
- throw new Exception("角色名称不能为空");
- }
-
- if (iSysRoleService.save(sysRole)){
- return ResponseBean.success(sysRole);
- }else {
- return ResponseBean.error("保存失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
- }
- }
-
- /**
- * 根据id删除对象
- * @param id 实体ID
- */
- @RequestMapping(value="/admin/role/{id}", method= RequestMethod.DELETE)
- @ApiOperation(value="删除", notes = "删除", httpMethod = "DELETE", response = ResponseBean.class)
- public ResponseBean sysRoleDelete(@ApiParam("对象ID") @PathVariable Integer id) throws Exception{
- SysRole sysRole = iSysRoleService.getById(id);
- if (null == sysRole || Constants.STATUS_DELETED.equals(sysRole.getStatus())) {
- return ResponseBean.success("success");
- }
-
- sysRole.setStatus(Constants.STATUS_DELETED);
-
- if(iSysRoleService.updateById(sysRole)){
- return ResponseBean.success("success");
- }else {
- return ResponseBean.error("删除失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
- }
- }
-
- /**
- * 修改对象
- * @param id 实体ID
- * @param sysRole 实体对象
- * @return
- */
- @RequestMapping(value="/admin/role/{id}",method= RequestMethod.PUT)
- @ApiOperation(value="更新", notes = "更新", httpMethod = "PUT", response = ResponseBean.class)
- public ResponseBean sysRoleUpdate(@ApiParam("对象ID") @PathVariable Integer id,
- @ApiParam("更新内容") @RequestBody SysRole sysRole) throws Exception{
- if (StringUtils.isEmpty(sysRole.getName())) {
- throw new Exception("角色名称不能为空");
- }
-
- if (!id.equals(sysRole.getRoleId())) {
- throw new Exception("校验角色信息失败");
- }
-
- if (iSysRoleService.updateById(sysRole)){
- return ResponseBean.success(iSysRoleService.getById(id));
- }else {
- return ResponseBean.error("修改失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
- }
- }
-
- /**
- * 根据id查询对象
- * @param id 实体ID
- */
- @RequestMapping(value="/admin/role/{id}",method= RequestMethod.GET)
- @ApiOperation(value="查询角色信息", notes = "详情", httpMethod = "GET", response = ResponseBean.class)
- public ResponseBean sysRoleGet(@ApiParam("对象ID") @PathVariable Integer id) throws Exception{
- SysRole sysRole = iSysRoleService.getById(id);
- if (null == sysRole || Constants.STATUS_DELETED.equals(sysRole.getStatus())) {
- throw new Exception("未找到指定的角色信息");
- }
-
- List<SysMenu> menuList = iSysMenuService.getListByRole(id);
- if (null != menuList) {
- sysRole.setMenuList(menuList);
- }
-
- return ResponseBean.success(sysRole);
- }
- }
|