update 角色相关接口
This commit is contained in:
parent
11a0146ed4
commit
70fe35510b
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.tacit.admin.controller;
|
||||||
|
|
||||||
|
import com.tacit.admin.entity.Role;
|
||||||
|
import com.tacit.admin.service.RoleService;
|
||||||
|
import com.tacit.common.entity.ResponseResult;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/role")
|
||||||
|
@Tag(name = "角色管理", description = "角色管理相关接口")
|
||||||
|
public class RoleController {
|
||||||
|
@Resource
|
||||||
|
private RoleService roleService;
|
||||||
|
//新增角色
|
||||||
|
@Operation(summary = "创建角色", description = "创建角色")
|
||||||
|
@PostMapping("/create")
|
||||||
|
public ResponseResult<Boolean> createRole(@RequestBody Role role) {
|
||||||
|
boolean result = roleService.createRole(role);
|
||||||
|
return ResponseResult.success(result);
|
||||||
|
}
|
||||||
|
//修改角色
|
||||||
|
@Operation(summary = "修改角色", description = "修改角色")
|
||||||
|
@PutMapping("/update")
|
||||||
|
public ResponseResult<Boolean> updateRole(@RequestBody Role role) {
|
||||||
|
boolean result = roleService.updateRole(role);
|
||||||
|
return ResponseResult.success(result);
|
||||||
|
}
|
||||||
|
//删除角色
|
||||||
|
@Operation(summary = "删除角色", description = "删除角色")
|
||||||
|
@DeleteMapping("/delete/{id}")
|
||||||
|
public ResponseResult<Boolean> deleteRole(@PathVariable Long id) {
|
||||||
|
boolean result = roleService.deleteRole(id);
|
||||||
|
return ResponseResult.success(result);
|
||||||
|
}
|
||||||
|
//查询角色列表
|
||||||
|
@Operation(summary = "查询角色列表", description = "查询角色列表")
|
||||||
|
@GetMapping("/list")
|
||||||
|
public ResponseResult<List<Role>> getRoleList(@RequestBody Role role) {
|
||||||
|
List<Role> roleList = roleService.getRoleList(role);
|
||||||
|
return ResponseResult.success(roleList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,11 +3,21 @@ package com.tacit.admin.service;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
import com.tacit.admin.entity.Role;
|
import com.tacit.admin.entity.Role;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface RoleService extends IService<Role> {
|
public interface RoleService extends IService<Role> {
|
||||||
//根据用户id获取角色
|
//根据用户id获取角色
|
||||||
Role getRoleByUserId(Long userId);
|
Role getRoleByUserId(Long userId);
|
||||||
//根据用户id获取角色、菜单
|
//根据用户id获取角色、菜单
|
||||||
Role getRoleMenuByUserId(Long userId);
|
Role getRoleMenuByUserId(Long userId);
|
||||||
|
|
||||||
|
boolean createRole(Role role);
|
||||||
|
|
||||||
|
boolean updateRole(Role role);
|
||||||
|
|
||||||
|
boolean deleteRole(Long id);
|
||||||
|
|
||||||
|
List<Role> getRoleList(Role role);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -121,7 +121,7 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements Me
|
||||||
// 根据id查询菜单是否存在
|
// 根据id查询菜单是否存在
|
||||||
Menu menu = getById(menuId);
|
Menu menu = getById(menuId);
|
||||||
if (menu == null) {
|
if (menu == null) {
|
||||||
throw new BusinessException(ResCode.NO_PARAM.getResultCode(), "菜单不存在");
|
throw new BusinessException(ResCode.DATA_NOT_EXIST.getResultCode(), "菜单不存在");
|
||||||
}
|
}
|
||||||
menu.setDelFlag(1);
|
menu.setDelFlag(1);
|
||||||
return updateById(menu);
|
return updateById(menu);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package com.tacit.admin.service.impl;
|
package com.tacit.admin.service.impl;
|
||||||
|
|
||||||
|
import ch.qos.logback.classic.spi.EventArgUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.tacit.admin.entity.Menu;
|
import com.tacit.admin.entity.Menu;
|
||||||
|
|
@ -10,8 +11,11 @@ import com.tacit.admin.mapper.RoleMapper;
|
||||||
import com.tacit.admin.mapper.UserRoleMapper;
|
import com.tacit.admin.mapper.UserRoleMapper;
|
||||||
import com.tacit.admin.service.MenuService;
|
import com.tacit.admin.service.MenuService;
|
||||||
import com.tacit.admin.service.RoleService;
|
import com.tacit.admin.service.RoleService;
|
||||||
|
import com.tacit.common.exception.BusinessException;
|
||||||
import com.tacit.common.redis.utils.RedisUtils;
|
import com.tacit.common.redis.utils.RedisUtils;
|
||||||
|
import com.tacit.common.utils.ResCode;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -50,4 +54,69 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements Ro
|
||||||
role.setMenuList(menuList);
|
role.setMenuList(menuList);
|
||||||
return role;
|
return role;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean createRole(Role role) {
|
||||||
|
// 参数校验
|
||||||
|
this.checkData(role);
|
||||||
|
role.setDelFlag(0);
|
||||||
|
return save(role);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean updateRole(Role role) {
|
||||||
|
// 角色名称不能为空
|
||||||
|
String roleName = role.getRoleName();
|
||||||
|
if(StringUtils.isEmpty(roleName)){
|
||||||
|
throw new BusinessException(ResCode.NO_PARAM.getResultCode(), "角色名称不能为空");
|
||||||
|
}
|
||||||
|
//角色是否存在
|
||||||
|
Long roleId = role.getId();
|
||||||
|
if (roleId == null) {
|
||||||
|
throw new BusinessException(ResCode.NO_PARAM.getResultCode(), "角色ID不能为空");
|
||||||
|
}
|
||||||
|
Role byId = getById(roleId);
|
||||||
|
// 角色名称不能重复 除了自己
|
||||||
|
if(!roleName.equals(byId.getRoleName())){
|
||||||
|
Role one = getOne(new QueryWrapper<Role>().eq("role_name", roleName).eq("del_flag", 0));
|
||||||
|
if (one != null) {
|
||||||
|
throw new BusinessException(ResCode.DATA_EXIST.getResultCode(), "角色名称不能重复");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return updateById(role);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean deleteRole(Long id) {
|
||||||
|
Role role = getById(id);
|
||||||
|
if (role == null) {
|
||||||
|
throw new BusinessException(ResCode.DATA_NOT_EXIST.getResultCode(), "角色不存在");
|
||||||
|
}
|
||||||
|
role.setDelFlag(1);
|
||||||
|
return updateById(role);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Role> getRoleList(Role role) {
|
||||||
|
QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
|
||||||
|
queryWrapper.eq("del_flag", 0);
|
||||||
|
if (role != null) {
|
||||||
|
queryWrapper.like(role.getRoleName() != null, "role_name", role.getRoleName());
|
||||||
|
}
|
||||||
|
List<Role> roleList = roleMapper.selectList(queryWrapper);
|
||||||
|
return roleList;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkData(Role role) {
|
||||||
|
// 角色名称不能为空
|
||||||
|
String roleName = role.getRoleName();
|
||||||
|
if(StringUtils.isEmpty(roleName)){
|
||||||
|
throw new BusinessException(ResCode.NO_PARAM.getResultCode(), "角色名称不能为空");
|
||||||
|
}
|
||||||
|
// 角色名称不能重复
|
||||||
|
Role one = getOne(new QueryWrapper<Role>().eq("role_name", roleName).eq("del_flag", 0));
|
||||||
|
if (one != null) {
|
||||||
|
throw new BusinessException(ResCode.DATA_EXIST.getResultCode(), "角色名称不能重复");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue