refactor(role): 重构角色服务的数据访问层实现

This commit is contained in:
2025-12-27 11:55:19 +08:00
parent fc951973a4
commit 79cb44c522
4 changed files with 134 additions and 137 deletions
@@ -19,13 +19,9 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.odboy.system.dal.dataobject.SystemRoleTb;
import cn.odboy.system.dal.model.SystemQueryRoleArgs;
import cn.odboy.system.dal.model.SystemRoleVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.List;
import java.util.Set;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* 角色 Mapper
@@ -49,13 +45,7 @@ public interface SystemRoleMapper extends BaseMapper<SystemRoleTb> {
return selectCount(wrapper);
}
List<SystemRoleVo> selectRoleByArgs(@Param("criteria") SystemQueryRoleArgs criteria);
List<SystemRoleVo> selectRoleByUserId(@Param("userId") Long userId);
List<SystemRoleVo> selectRoleByMenuId(@Param("menuId") Long menuId);
SystemRoleVo getRoleById(@Param("roleId") Long roleId);
Integer countRoleByDeptIds(@Param("deptIds") Set<Long> deptIds);
default SystemRoleTb getRoleById(Long roleId) {
return selectById(roleId);
}
}
@@ -60,7 +60,7 @@ public class SystemDeptService {
@Autowired
private SystemUserMapper systemUserMapper;
@Autowired
private SystemRoleMapper systemRoleMapper;
private SystemRoleService systemRoleService;
/**
* 创建
@@ -320,7 +320,7 @@ public class SystemDeptService {
if (systemUserMapper.countUserByDeptIds(deptIds) > 0) {
throw new BadRequestException("所选部门存在用户关联,请解除后再试!");
}
if (systemRoleMapper.countRoleByDeptIds(deptIds) > 0) {
if (systemRoleService.countRoleByDeptIds(deptIds) > 0) {
throw new BadRequestException("所选部门存在角色关联,请解除后再试!");
}
}
@@ -24,12 +24,19 @@ import cn.odboy.framework.exception.BadRequestException;
import cn.odboy.system.dal.dataobject.SystemMenuTb;
import cn.odboy.system.dal.dataobject.SystemRoleTb;
import cn.odboy.system.dal.dataobject.SystemUserTb;
import cn.odboy.system.dal.dataobject.SystemRoleMenuTb;
import cn.odboy.system.dal.dataobject.SystemRoleDeptTb;
import cn.odboy.system.dal.dataobject.SystemUserRoleTb;
import cn.odboy.system.dal.dataobject.SystemDeptTb;
import cn.odboy.system.dal.model.SystemCreateRoleArgs;
import cn.odboy.system.dal.model.SystemQueryRoleArgs;
import cn.odboy.system.dal.model.SystemRoleCodeVo;
import cn.odboy.system.dal.model.SystemRoleVo;
import cn.odboy.system.dal.mysql.SystemRoleMapper;
import cn.odboy.system.dal.mysql.SystemUserMapper;
import cn.odboy.system.dal.mysql.SystemRoleMenuMapper;
import cn.odboy.system.dal.mysql.SystemRoleDeptMapper;
import cn.odboy.system.dal.mysql.SystemUserRoleMapper;
import cn.odboy.util.KitFileUtil;
import cn.odboy.util.KitPageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -39,6 +46,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -59,6 +67,12 @@ public class SystemRoleService {
private SystemUserMapper systemUserMapper;
@Autowired
private SystemRoleDeptService systemRoleDeptService;
@Autowired
private SystemRoleMenuMapper systemRoleMenuMapper;
@Autowired
private SystemRoleDeptMapper systemRoleDeptMapper;
@Autowired
private SystemUserRoleMapper systemUserRoleMapper;
/**
* 创建
@@ -179,7 +193,29 @@ public class SystemRoleService {
* @return
*/
public List<SystemRoleVo> queryRoleByArgs(SystemQueryRoleArgs criteria) {
return systemRoleMapper.selectRoleByArgs(criteria);
// 查询角色基本信息
LambdaQueryWrapper<SystemRoleTb> wrapper = new LambdaQueryWrapper<>();
if (criteria != null) {
wrapper.and(StrUtil.isNotBlank(criteria.getBlurry()),
c -> c.like(SystemRoleTb::getName, criteria.getBlurry()).or()
.like(SystemRoleTb::getDescription, criteria.getBlurry()));
if (CollUtil.isNotEmpty(criteria.getCreateTime()) && criteria.getCreateTime().size() >= 2) {
wrapper.between(SystemRoleTb::getCreateTime, criteria.getCreateTime().get(0),
criteria.getCreateTime().get(1));
}
}
wrapper.orderByAsc(SystemRoleTb::getLevel);
// 分页处理
if (criteria != null && criteria.getOffset() > 0 && criteria.getSize() != null) {
Page<SystemRoleTb> rolePage = new Page<>(criteria.getOffset() / criteria.getSize() + 1, criteria.getSize());
Page<SystemRoleTb> page = systemRoleMapper.selectPage(rolePage, wrapper);
List<SystemRoleTb> roles = page.getRecords();
return roles.stream().map(this::convertToRoleVo).collect(Collectors.toList());
} else {
List<SystemRoleTb> roles = systemRoleMapper.selectList(wrapper);
return roles.stream().map(this::convertToRoleVo).collect(Collectors.toList());
}
}
/**
@@ -191,7 +227,7 @@ public class SystemRoleService {
*/
public KitPageResult<SystemRoleVo> queryRoleByArgs(SystemQueryRoleArgs criteria, Page<Object> page) {
criteria.setOffset(page.offset());
List<SystemRoleVo> roles = systemRoleMapper.selectRoleByArgs(criteria);
List<SystemRoleVo> roles = this.queryRoleByArgs(criteria);
Long total = systemRoleMapper.countRoleByArgs(criteria);
return KitPageUtil.toPage(roles, total);
}
@@ -203,7 +239,19 @@ public class SystemRoleService {
* @return /
*/
public List<SystemRoleVo> queryRoleByUsersId(Long userId) {
return systemRoleMapper.selectRoleByUserId(userId);
// 查询用户角色关联
LambdaQueryWrapper<SystemUserRoleTb> userRoleWrapper = new LambdaQueryWrapper<>();
userRoleWrapper.eq(SystemUserRoleTb::getUserId, userId);
List<SystemUserRoleTb> userRoles = systemUserRoleMapper.selectList(userRoleWrapper);
if (CollUtil.isEmpty(userRoles)) {
return new ArrayList<>();
}
Set<Long> roleIds = userRoles.stream().map(SystemUserRoleTb::getRoleId).collect(Collectors.toSet());
List<SystemRoleTb> roles = systemRoleMapper.selectByIds(roleIds);
return roles.stream().map(this::convertToRoleVo).collect(Collectors.toList());
}
/**
@@ -236,7 +284,7 @@ public class SystemRoleService {
permissions.add("admin");
return permissions.stream().map(SystemRoleCodeVo::new).collect(Collectors.toList());
}
List<SystemRoleVo> roles = systemRoleMapper.selectRoleByUserId(user.getId());
List<SystemRoleVo> roles = this.queryRoleByUsersId(user.getId());
permissions = roles.stream().flatMap(role -> role.getMenus().stream()).map(SystemMenuTb::getPermission)
.filter(StrUtil::isNotBlank).collect(Collectors.toSet());
return permissions.stream().map(SystemRoleCodeVo::new).collect(Collectors.toList());
@@ -262,4 +310,81 @@ public class SystemRoleService {
public SystemRoleTb getRoleById(Long id) {
return systemRoleMapper.selectById(id);
}
/**
* 根据ID查询角色详情(包含关联信息)
*
* @param id /
* @return /
*/
public SystemRoleVo getRoleVoById(Long id) {
SystemRoleTb role = systemRoleMapper.selectById(id);
if (role == null) {
return null;
}
return convertToRoleVo(role);
}
/**
* 根据菜单ID查询角色
*
* @param menuId 菜单ID
* @return /
*/
public List<SystemRoleVo> queryRoleByMenuId(Long menuId) {
// 查询包含该菜单的角色ID列表
LambdaQueryWrapper<SystemRoleMenuTb> roleMenuWrapper = new LambdaQueryWrapper<>();
roleMenuWrapper.eq(SystemRoleMenuTb::getMenuId, menuId);
List<SystemRoleMenuTb> roleMenuList = systemRoleMenuMapper.selectList(roleMenuWrapper);
if (CollUtil.isEmpty(roleMenuList)) {
return new ArrayList<>();
}
Set<Long> roleIds = roleMenuList.stream().map(SystemRoleMenuTb::getRoleId).collect(Collectors.toSet());
List<SystemRoleTb> roles = systemRoleMapper.selectByIds(roleIds);
return roles.stream().map(this::convertToRoleVo).collect(Collectors.toList());
}
/**
* 根据部门ID统计角色数量
*
* @param deptIds 部门ID集合
* @return /
*/
public Integer countRoleByDeptIds(Set<Long> deptIds) {
if (CollUtil.isEmpty(deptIds)) {
return 0;
}
LambdaQueryWrapper<SystemRoleDeptTb> wrapper = new LambdaQueryWrapper<>();
wrapper.in(SystemRoleDeptTb::getDeptId, deptIds);
return systemRoleDeptMapper.selectCount(wrapper).intValue();
}
/**
* 转换为SystemRoleVo并关联查询菜单和部门信息
*
* @param role 角色基本信息
* @return 包含关联信息的SystemRoleVo
*/
private SystemRoleVo convertToRoleVo(SystemRoleTb role) {
if (role == null) {
return null;
}
SystemRoleVo roleVo = BeanUtil.copyProperties(role, SystemRoleVo.class);
// 查询关联的菜单信息
Set<SystemMenuTb> menus = systemRoleMenuService.queryMenuByRoleIdsAndType(
Collections.singleton(role.getId()), -1);
roleVo.setMenus(menus);
// 查询关联的部门信息
List<SystemDeptTb> depts = systemRoleDeptService.selectDeptByRoleId(role.getId());
roleVo.setDepts(new LinkedHashSet<>(depts));
return roleVo;
}
}
@@ -1,118 +0,0 @@
<?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.odboy.system.dal.mysql.SystemRoleMapper">
<resultMap id="BaseResultMap" type="cn.odboy.system.dal.model.SystemRoleVo">
<id column="role_role_id" property="id"/>
<result column="role_name" property="name"/>
<result column="role_data_scope" property="dataScope"/>
<result column="role_level" property="level"/>
<result column="role_description" property="description"/>
<result column="role_create_by" property="createBy"/>
<result column="role_update_by" property="updateBy"/>
<result column="role_create_time" property="createTime"/>
<result column="role_update_time" property="updateTime"/>
<collection property="menus" ofType="cn.odboy.system.dal.dataobject.SystemMenuTb">
<id column="menu_id" property="id"/>
<result column="menu_title" property="title"/>
<result column="menu_permission" property="permission"/>
</collection>
<collection property="depts" ofType="cn.odboy.system.dal.dataobject.SystemDeptTb">
<id column="dept_id" property="id"/>
<result column="dept_name" property="name"/>
</collection>
</resultMap>
<select id="selectRoleByArgs" resultMap="BaseResultMap">
select role.*,
dept.dept_id as dept_id, dept.name as dept_name,
menu.menu_id as menu_id, menu.title as menu_title, menu.permission as menu_permission
from (
select
role.role_id as role_role_id, role.name as role_name, role.data_scope as role_data_scope,
role.level as role_level, role.description as role_description, role.create_by as role_create_by,
role.update_by as role_update_by, role.create_time as role_create_time, role.update_time as role_update_time
from system_role role
<where>
<if test="criteria.blurry != null and criteria.blurry != ''">
and (
role.name like concat('%', #{criteria.blurry}, '%')
or role.description like concat('%', #{criteria.blurry}, '%')
)
</if>
<if test="criteria.createTime != null and criteria.createTime.size() != 0">
and role.create_time between #{criteria.createTime[0]} and #{criteria.createTime[1]}
</if>
</where>
order by role.level
<if test="criteria.offset != null">
limit #{criteria.offset}, #{criteria.size}
</if>
) role
left join system_roles_menus srm on role.role_role_id = srm.role_id
left join system_menu menu on menu.menu_id = srm.menu_id
left join system_roles_depts srd on role.role_role_id = srd.role_id
left join system_dept dept on dept.dept_id = srd.dept_id
order by role.role_level
</select>
<select id="getRoleById" resultMap="BaseResultMap">
select role.role_id as role_role_id,
role.name as role_name,
role.data_scope as role_data_scope,
role.level as role_level,
role.description as role_description,
role.create_by as role_create_by,
role.update_by as role_update_by,
role.create_time as role_create_time,
role.update_time as role_update_time,
dept.dept_id as dept_id,
dept.name as dept_name,
menu.menu_id as menu_id,
menu.title as menu_title,
menu.permission as menu_permission
from system_role role
left join system_roles_menus srm on role.role_id = srm.role_id
left join system_menu menu on menu.menu_id = srm.menu_id
left join system_roles_depts srd on role.role_id = srd.role_id
left join system_dept dept on dept.dept_id = srd.dept_id
where role.role_id = #{roleId}
</select>
<select id="selectRoleByUserId" resultMap="BaseResultMap">
SELECT role.role_id as role_role_id,
role.name as role_name,
role.data_scope as role_data_scope,
role.level as role_level,
role.description as role_description,
role.create_by as role_create_by,
role.update_by as role_update_by,
role.create_time as role_create_time,
role.update_time as role_update_time,
dept.dept_id as dept_id,
dept.name as dept_name,
menu.menu_id as menu_id,
menu.title as menu_title,
menu.permission as menu_permission
from system_role role
left join system_roles_menus srm on role.role_id = srm.role_id
left join system_menu menu on menu.menu_id = srm.menu_id
left join system_roles_depts srd on role.role_id = srd.role_id
left join system_dept dept on dept.dept_id = srd.dept_id
left join system_users_roles ur on role.role_id = ur.role_id
WHERE role.role_id = ur.role_id
AND ur.user_id = #{userId}
</select>
<select id="countRoleByDeptIds" resultType="int">
select count(*)
from system_role r, system_roles_depts d
where r.role_id = d.role_id
and d.dept_id in
<foreach collection="deptIds" item="deptId" open="(" separator="," close=")">
#{deptId}
</foreach>
</select>
<select id="selectRoleByMenuId" resultType="cn.odboy.system.dal.model.SystemRoleVo">
SELECT role.role_id as id
FROM system_role role,
system_roles_menus rm
WHERE role.role_id = rm.role_id
AND rm.menu_id = #{menuId}
</select>
</mapper>