update 菜单相关接口
This commit is contained in:
parent
d41a39fb5e
commit
33b2a56dc8
|
|
@ -23,6 +23,7 @@ public enum ResCode {
|
||||||
NO_PERMISSION(401, "用户没有该权限"),
|
NO_PERMISSION(401, "用户没有该权限"),
|
||||||
// 查询的数据或者前端传入的数据不存在
|
// 查询的数据或者前端传入的数据不存在
|
||||||
DATA_NOT_EXIST(9999, "不存在"),
|
DATA_NOT_EXIST(9999, "不存在"),
|
||||||
|
DATA_EXIST(9990, "已存在"),
|
||||||
|
|
||||||
SYSTEM_ERROR(9999, "系统内部错误"),
|
SYSTEM_ERROR(9999, "系统内部错误"),
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,60 @@
|
||||||
|
package com.tacit.admin.controller;
|
||||||
|
|
||||||
|
import com.tacit.admin.entity.Menu;
|
||||||
|
import com.tacit.admin.entity.User;
|
||||||
|
import com.tacit.admin.service.MenuService;
|
||||||
|
import com.tacit.admin.service.UserService;
|
||||||
|
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("/menu")
|
||||||
|
@Tag(name = "菜单管理", description = "用户管理相关接口")
|
||||||
|
public class MenuController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private MenuService menuService;
|
||||||
|
|
||||||
|
@Operation(summary = "获取所有菜单", description = "获取系统中所有菜单列表")
|
||||||
|
@GetMapping("/list")
|
||||||
|
public ResponseResult<List<Menu>> getAllMenus(@RequestBody Menu menu) {
|
||||||
|
List<Menu> menus = menuService.getAllMenus(menu);
|
||||||
|
return ResponseResult.success(menus);
|
||||||
|
}
|
||||||
|
@Operation(summary = "获取所有菜单", description = "获取系统中所有菜单树形列表")
|
||||||
|
@GetMapping("/listTree")
|
||||||
|
public ResponseResult<List<Menu>> getListTree() {
|
||||||
|
List<Menu> menus = menuService.getListTree();
|
||||||
|
return ResponseResult.success(menus);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "根据角色id获取所有菜单", description = "根据角色id获取所有菜单列表")
|
||||||
|
@GetMapping("/listByRoleId/{roleId}")
|
||||||
|
public ResponseResult<List<Menu>> getAllMenus(@PathVariable Long roleId) {
|
||||||
|
List<Menu> menus = menuService.getMenuListByRoleId(roleId);
|
||||||
|
return ResponseResult.success(menus);
|
||||||
|
}
|
||||||
|
@Operation(summary = "创建菜单", description = "创建新菜单")
|
||||||
|
@PostMapping("/create")
|
||||||
|
public ResponseResult<Boolean> createMenu(@RequestBody Menu menu) {
|
||||||
|
boolean result = menuService.createMenu(menu);
|
||||||
|
return ResponseResult.success(result);
|
||||||
|
}
|
||||||
|
@Operation(summary = "更新菜单", description = "更新菜单")
|
||||||
|
@PutMapping("/update")
|
||||||
|
public ResponseResult<Boolean> updateMenu(@RequestBody Menu menu) {
|
||||||
|
boolean result = menuService.updateMenu(menu);
|
||||||
|
return ResponseResult.success(result);
|
||||||
|
}
|
||||||
|
@Operation(summary = "删除菜单", description = "删除菜单")
|
||||||
|
@DeleteMapping("/delete/{menuId}")
|
||||||
|
public ResponseResult<Boolean> deleteMenu(@PathVariable Long menuId) {
|
||||||
|
boolean result = menuService.deleteMenu(menuId);
|
||||||
|
return ResponseResult.success(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -8,6 +8,16 @@ import java.util.List;
|
||||||
public interface MenuService extends IService<Menu> {
|
public interface MenuService extends IService<Menu> {
|
||||||
//根据角色id获取菜单列表
|
//根据角色id获取菜单列表
|
||||||
List<Menu> getMenuListByRoleId(Long roleId);
|
List<Menu> getMenuListByRoleId(Long roleId);
|
||||||
|
//获取所有菜单
|
||||||
|
List<Menu> getAllMenus(Menu menu);
|
||||||
|
//获取所有菜单树形列表
|
||||||
|
List<Menu> getListTree();
|
||||||
|
|
||||||
|
boolean createMenu(Menu menu);
|
||||||
|
|
||||||
|
boolean updateMenu(Menu menu);
|
||||||
|
|
||||||
|
boolean deleteMenu(Long menuId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,12 @@ import com.tacit.admin.mapper.RoleMenuMapper;
|
||||||
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 com.tacit.common.utils.TreeUtils;
|
import com.tacit.common.utils.TreeUtils;
|
||||||
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;
|
||||||
|
|
@ -38,11 +41,106 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements Me
|
||||||
if (roleMenuList != null && roleMenuList.size() > 0) {
|
if (roleMenuList != null && roleMenuList.size() > 0) {
|
||||||
// 获取菜单id
|
// 获取菜单id
|
||||||
List<Long> menuIdList = roleMenuList.stream().map(RoleMenu::getMenuId).collect(Collectors.toList());
|
List<Long> menuIdList = roleMenuList.stream().map(RoleMenu::getMenuId).collect(Collectors.toList());
|
||||||
List<Menu> menuList = menuMapper.selectBatchIds(menuIdList);
|
// 根据菜单ids获取没删除的菜单列表
|
||||||
|
QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
|
||||||
|
menuQueryWrapper.eq("del_flag", 0);
|
||||||
|
menuQueryWrapper.in("menu_id", menuIdList);
|
||||||
|
List<Menu> menuList = menuMapper.selectList(menuQueryWrapper);
|
||||||
// 需要将MenuList转换成树形结构
|
// 需要将MenuList转换成树形结构
|
||||||
menuList = TreeUtils.buildTree(menuList);
|
menuList = TreeUtils.buildTree(menuList);
|
||||||
return menuList;
|
return menuList;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Menu> getListTree() {
|
||||||
|
QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
|
||||||
|
queryWrapper.eq("del_flag", 0);
|
||||||
|
List<Menu> menuList = menuMapper.selectList(queryWrapper);
|
||||||
|
// 需要将MenuList转换成树形结构
|
||||||
|
menuList = TreeUtils.buildTree(menuList);
|
||||||
|
return menuList;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Menu> getAllMenus(Menu menu) {
|
||||||
|
QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
|
||||||
|
queryWrapper.eq("del_flag", 0);
|
||||||
|
if (menu != null) {
|
||||||
|
queryWrapper.like(menu.getMenuName() != null, "menu_name", menu.getMenuName());
|
||||||
|
}
|
||||||
|
List<Menu> menuList = menuMapper.selectList(queryWrapper);
|
||||||
|
return menuList;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean createMenu(Menu menu) {
|
||||||
|
// 每个参数都要进行非空校验
|
||||||
|
this.checkData(menu);
|
||||||
|
|
||||||
|
menu.setDelFlag(0);
|
||||||
|
return this.save(menu);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean updateMenu(Menu menu) {
|
||||||
|
// 菜单名称非空校验
|
||||||
|
String menuName = menu.getMenuName();
|
||||||
|
if (StringUtils.isEmpty(menuName)){
|
||||||
|
throw new BusinessException(ResCode.NO_PARAM.getResultCode(), "菜单名称不能为空");
|
||||||
|
}
|
||||||
|
// 菜单id非空校验
|
||||||
|
Long menuId = menu.getMenuId();
|
||||||
|
if (menuId == null){
|
||||||
|
throw new BusinessException(ResCode.NO_PARAM.getResultCode(), "菜单id不能为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据id查询菜单是否存在
|
||||||
|
Menu menuById = getById(menuId);
|
||||||
|
if (menuById == null) {
|
||||||
|
throw new BusinessException(ResCode.NO_PARAM.getResultCode(), "菜单不存在");
|
||||||
|
}
|
||||||
|
// 菜单名称唯一性校验 如果是自己就不用校验
|
||||||
|
if (!menuName.equals(menuById.getMenuName())) {
|
||||||
|
Menu menuByName = getOne(new QueryWrapper<Menu>().eq("menu_name", menuName).eq("del_flag", 0));
|
||||||
|
if (menuByName != null) {
|
||||||
|
throw new BusinessException(ResCode.DATA_EXIST.getResultCode(), "菜单已存在");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this.updateById(menu);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean deleteMenu(Long menuId) {
|
||||||
|
// 菜单id非空校验
|
||||||
|
if (menuId == null){
|
||||||
|
throw new BusinessException(ResCode.NO_PARAM.getResultCode(), "菜单id不能为空");
|
||||||
|
}
|
||||||
|
// 根据id查询菜单是否存在
|
||||||
|
Menu menu = getById(menuId);
|
||||||
|
if (menu == null) {
|
||||||
|
throw new BusinessException(ResCode.NO_PARAM.getResultCode(), "菜单不存在");
|
||||||
|
}
|
||||||
|
menu.setDelFlag(1);
|
||||||
|
return updateById(menu);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkData(Menu menu) {
|
||||||
|
String menuName = menu.getMenuName();
|
||||||
|
if (StringUtils.isEmpty(menuName)){
|
||||||
|
throw new BusinessException(ResCode.NO_PARAM.getResultCode(), "菜单名称不能为空");
|
||||||
|
}
|
||||||
|
// 根据菜单名称查询菜单是否存在
|
||||||
|
Menu menuByName = getOne(new QueryWrapper<Menu>().eq("menu_name", menuName).eq("del_flag", 0));
|
||||||
|
if (menuByName != null) {
|
||||||
|
throw new BusinessException(ResCode.DATA_EXIST.getResultCode(), "菜单已存在");
|
||||||
|
}
|
||||||
|
Long parentId = menu.getParentId();
|
||||||
|
if (parentId == null){
|
||||||
|
throw new BusinessException(ResCode.NO_PARAM.getResultCode(), "父级菜单id不能为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue