角色菜单
This commit is contained in:
parent
be2e373919
commit
aab1b953cb
|
@ -2,6 +2,7 @@
|
|||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="RawUseOfParameterizedType" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
<inspection_tool class="SqlWithoutWhereInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
<inspection_tool class="ThrowablePrintStackTrace" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
</profile>
|
||||
</component>
|
|
@ -5,19 +5,40 @@ import cn.bunny.common.spzx.model.vo.common.Result;
|
|||
import cn.bunny.common.spzx.model.vo.common.ResultCodeEnum;
|
||||
import cn.bunny.service.SysMenuService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Tag(name = "菜单操作")
|
||||
@RestController
|
||||
@RequestMapping("/admin/system.sysMenu")
|
||||
public class SysMenuController {
|
||||
@Autowired
|
||||
private SysMenuService sysMenuService;
|
||||
|
||||
@Operation(summary = "删除菜单", description = "删除某个菜单")
|
||||
@DeleteMapping("/removeById")
|
||||
public Result removeById(@RequestParam Long id) {
|
||||
sysMenuService.removeById(id);
|
||||
return Result.build(null, ResultCodeEnum.SUCCESS);
|
||||
}
|
||||
|
||||
@Operation(summary = "修改菜单", description = "修改菜单")
|
||||
@PutMapping("update")
|
||||
public Result update(@RequestBody SysMenu sysMenu) {
|
||||
sysMenuService.update(sysMenu);
|
||||
return Result.build(null, ResultCodeEnum.SUCCESS);
|
||||
}
|
||||
|
||||
@Operation(summary = "添加菜单", description = "添加菜单")
|
||||
@PostMapping("/save")
|
||||
public Result save(@RequestBody SysMenu sysMenu) {
|
||||
sysMenuService.save(sysMenu);
|
||||
return Result.build(null, ResultCodeEnum.SUCCESS);
|
||||
}
|
||||
|
||||
@Operation(summary = "查询所有子节点", description = "返回list集合")
|
||||
@GetMapping("findNodes")
|
||||
public Result findNodes() {
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
package cn.bunny.controller;
|
||||
|
||||
import cn.bunny.common.spzx.model.dto.system.AssginMenuDto;
|
||||
import cn.bunny.common.spzx.model.vo.common.Result;
|
||||
import cn.bunny.common.spzx.model.vo.common.ResultCodeEnum;
|
||||
import cn.bunny.service.SysRoleMenuService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Tag(name = "角色菜单")
|
||||
@RestController
|
||||
@RequestMapping("/admin/system/sysRoleMenu")
|
||||
public class SysRoleMenuController {
|
||||
@Autowired
|
||||
private SysRoleMenuService sysRoleMenuService;
|
||||
|
||||
@Operation(summary = "查询所有菜单", description = "查询角色分配过菜单id列表")
|
||||
@GetMapping("/findSysRoleMenuByRoleId/{roleId}")
|
||||
public Result findSysRoleMenuByRoleId(@PathVariable("roleId") String roleId) {
|
||||
Map<String, Object> map = sysRoleMenuService.findSysRoleMenuByRoleId(roleId);
|
||||
|
||||
return Result.build(map, ResultCodeEnum.SUCCESS);
|
||||
}
|
||||
|
||||
@Operation(summary = "保存角色分配菜单")
|
||||
@PostMapping("/doAssign")
|
||||
public Result doAssign(@RequestBody AssginMenuDto assginMenuDto) {
|
||||
// 删除角色之前分配过菜单数据
|
||||
sysRoleMenuService.doAssign(assginMenuDto);
|
||||
|
||||
return Result.build(null, ResultCodeEnum.SUCCESS);
|
||||
}
|
||||
}
|
|
@ -9,4 +9,16 @@ import java.util.List;
|
|||
public interface SysMenuMapper {
|
||||
// 查询所有子节点
|
||||
List<SysMenu> findAll();
|
||||
|
||||
// 保存菜单
|
||||
void save(SysMenu sysMenu);
|
||||
|
||||
// 修改菜单
|
||||
void update(SysMenu sysMenu);
|
||||
|
||||
// 删除菜单
|
||||
void removeById(Long id);
|
||||
|
||||
// 查询是否有子菜单
|
||||
Long selectById(Long id);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package cn.bunny.mapper;
|
||||
|
||||
import cn.bunny.common.spzx.model.dto.system.AssginMenuDto;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface SysRoleMenuMapper {
|
||||
// 查询角色分配过菜单id列表
|
||||
List<Long> findSysRoleMenuByRoleId(String roleId);
|
||||
|
||||
// 删除角色分配菜单数据
|
||||
void deleteByRoleId(Long roleId);
|
||||
|
||||
// 保存分配数据
|
||||
void doAssign(AssginMenuDto assginMenuDto);
|
||||
}
|
|
@ -7,4 +7,13 @@ import java.util.List;
|
|||
public interface SysMenuService {
|
||||
// 查询所有子节点
|
||||
List<SysMenu> findNodes();
|
||||
|
||||
// 添加
|
||||
void save(SysMenu sysMenu);
|
||||
|
||||
// 修改菜单
|
||||
void update(SysMenu sysMenu);
|
||||
|
||||
// 删除菜单
|
||||
void removeById(Long id);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package cn.bunny.service;
|
||||
|
||||
import cn.bunny.common.spzx.model.dto.system.AssginMenuDto;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface SysRoleMenuService {
|
||||
// 查询所有菜单
|
||||
Map<String, Object> findSysRoleMenuByRoleId(String roleId);
|
||||
|
||||
// 保存角色分配菜单
|
||||
void doAssign(AssginMenuDto assginMenuDto);
|
||||
}
|
|
@ -22,6 +22,6 @@ public interface SysRoleService {
|
|||
// 彻底删除角色
|
||||
void deleteSysRoleByRoleId(Long roleId);
|
||||
|
||||
// 查询所有角色信息
|
||||
// 查询所有角色
|
||||
Map<String, Object> findAll(Long userId);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package cn.bunny.service.impl;
|
||||
|
||||
import cn.bunny.common.exception.BunnyException;
|
||||
import cn.bunny.common.spzx.model.entity.system.SysMenu;
|
||||
import cn.bunny.common.spzx.model.vo.common.ResultCodeEnum;
|
||||
import cn.bunny.mapper.SysMenuMapper;
|
||||
import cn.bunny.service.SysMenuService;
|
||||
import cn.bunny.utils.MenuHelper;
|
||||
|
@ -25,4 +27,26 @@ public class SysMenuServiceImpl implements SysMenuService {
|
|||
|
||||
return MenuHelper.buildTree(sysMenuList);
|
||||
}
|
||||
|
||||
// 添加菜单
|
||||
@Override
|
||||
public void save(SysMenu sysMenu) {
|
||||
sysMenuMapper.save(sysMenu);
|
||||
}
|
||||
|
||||
// 修改菜单
|
||||
@Override
|
||||
public void update(SysMenu sysMenu) {
|
||||
sysMenuMapper.update(sysMenu);
|
||||
}
|
||||
|
||||
// 删除菜单
|
||||
@Override
|
||||
public void removeById(Long id) {
|
||||
Long count = sysMenuMapper.selectById(id);
|
||||
if (count > 0) {
|
||||
throw new BunnyException(ResultCodeEnum.NODE_ERROR);
|
||||
}
|
||||
sysMenuMapper.removeById(id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
package cn.bunny.service.impl;
|
||||
|
||||
import cn.bunny.common.spzx.model.dto.system.AssginMenuDto;
|
||||
import cn.bunny.common.spzx.model.entity.system.SysMenu;
|
||||
import cn.bunny.mapper.SysRoleMenuMapper;
|
||||
import cn.bunny.service.SysMenuService;
|
||||
import cn.bunny.service.SysRoleMenuService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class SysRoleMenuServiceImpl implements SysRoleMenuService {
|
||||
@Autowired
|
||||
private SysRoleMenuMapper sysRoleMenuMapper;
|
||||
|
||||
@Autowired
|
||||
private SysMenuService sysMenuService;
|
||||
|
||||
// 查询所有菜单 和查询所有角色分配过的id列表
|
||||
@Override
|
||||
public Map<String, Object> findSysRoleMenuByRoleId(String roleId) {
|
||||
// 查询所有
|
||||
List<SysMenu> sysMenuList = sysMenuService.findNodes();
|
||||
|
||||
// 查询角色分配过菜单id列表
|
||||
List<Long> roleMenuIds = sysRoleMenuMapper.findSysRoleMenuByRoleId(roleId);
|
||||
|
||||
// 整合数据返回
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("sysMenuList", sysMenuList);
|
||||
map.put("roleMenuIds", roleMenuIds);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
// 分配菜单
|
||||
@Override
|
||||
public void doAssign(AssginMenuDto assginMenuDto) {
|
||||
// 删除角色分配菜单数据
|
||||
sysRoleMenuMapper.deleteByRoleId(assginMenuDto.getRoleId());
|
||||
|
||||
// 保存分配数据
|
||||
List<Map<String, Number>> menuInfo = assginMenuDto.getMenuIdList();
|
||||
if (menuInfo != null && menuInfo.size() > 0) {
|
||||
sysRoleMenuMapper.doAssign(assginMenuDto);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,12 +3,51 @@
|
|||
<mapper namespace="cn.bunny.mapper.SysMenuMapper">
|
||||
|
||||
<!-- 映射查询到的字段 -->
|
||||
<resultMap id="sysRoleMap" type="cn.bunny.common.spzx.model.entity.system.SysRole" autoMapping="true"/>
|
||||
<!-- 用于select查询公用抽取列 -->
|
||||
<resultMap id="sysRoleMap" type="cn.bunny.common.spzx.model.entity.system.SysMenu" autoMapping="true"/>
|
||||
<!-- 用于select查询公用抽取的列 -->
|
||||
<sql id="columns">
|
||||
id,username userName ,password,name,phone,avatar,description,status,create_time,update_time,is_deleted
|
||||
id,parent_id,title,component,sort_value,status,create_time,update_time,is_deleted
|
||||
</sql>
|
||||
|
||||
<!-- 添加菜单 -->
|
||||
<insert id="save">
|
||||
insert into sys_menu (id, parent_id, title, component, sort_value, status)
|
||||
values (#{id},
|
||||
#{parentId},
|
||||
#{title},
|
||||
#{component},
|
||||
#{sortValue},
|
||||
#{status});
|
||||
</insert>
|
||||
|
||||
<!-- 修改菜单 -->
|
||||
<update id="update">
|
||||
update sys_menu set
|
||||
<if test="parentId != null and parentId != ''">
|
||||
parent_id=#{parentId},
|
||||
</if>
|
||||
<if test="title != null and title != ''">
|
||||
title=#{title},
|
||||
</if>
|
||||
<if test="component != null and component!=''">
|
||||
component=#{component},
|
||||
</if>
|
||||
<if test="sortValue != null">
|
||||
sort_value=#{sortValue},
|
||||
</if>
|
||||
<if test="status != null">
|
||||
status=#{status},
|
||||
</if>
|
||||
update_time=now();
|
||||
where id=#{id}
|
||||
</update>
|
||||
|
||||
<update id="removeById">
|
||||
update sys_menu
|
||||
set is_deleted=1
|
||||
where id = #{id}
|
||||
</update>
|
||||
|
||||
|
||||
<!-- 查询所有子节点,返回list集合 -->
|
||||
<select id="findAll" resultMap="sysRoleMap">
|
||||
|
@ -18,4 +57,12 @@
|
|||
where is_deleted=0
|
||||
order by sort_value
|
||||
</select>
|
||||
|
||||
<!-- 查询是否有子菜单 -->
|
||||
<select id="selectById" resultType="java.lang.Long">
|
||||
select count(*)
|
||||
from sys_menu
|
||||
where parent_id = #{id}
|
||||
and is_deleted = 0
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="cn.bunny.mapper.SysRoleMenuMapper">
|
||||
|
||||
<!-- 保存分配数据 -->
|
||||
<insert id="doAssign">
|
||||
insert into sys_role_menu (role_id, menu_id, create_time, update_time, is_deleted, is_half)
|
||||
values
|
||||
(
|
||||
<foreach collection="menuIdList" item="menuInfo" separator=",">
|
||||
#{roleId},#{menuInfo.id},now(),now(),0,#{menuInfo.isHalf}
|
||||
</foreach>
|
||||
)
|
||||
</insert>
|
||||
|
||||
<!-- 删除角色分配菜单数据 -->
|
||||
<delete id="deleteByRoleId">
|
||||
delete
|
||||
from sys_role_menu
|
||||
where role_id = #{roleId}
|
||||
</delete>
|
||||
|
||||
|
||||
<select id="findSysRoleMenuByRoleId" resultType="java.lang.Long">
|
||||
select menu_id
|
||||
from sys_role_menu
|
||||
where menu_id = #{roleId}
|
||||
and is_deleted = 0
|
||||
and is_half = 0
|
||||
</select>
|
||||
</mapper>
|
Loading…
Reference in New Issue