mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-08 22:57:12 +08:00
refactor(system): 优化角色和用户相关查询数据填充
Closes #ICIK3X
This commit is contained in:
@@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package top.continew.admin.system.container;
|
||||||
|
|
||||||
|
import cn.crane4j.annotation.ContainerMethod;
|
||||||
|
import cn.crane4j.annotation.MappingType;
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import top.continew.admin.common.constant.ContainerConstants;
|
||||||
|
import top.continew.admin.system.mapper.RoleMapper;
|
||||||
|
import top.continew.admin.system.mapper.UserRoleMapper;
|
||||||
|
import top.continew.admin.system.model.entity.RoleDO;
|
||||||
|
import top.continew.admin.system.model.entity.UserRoleDO;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 系统管理容器(Crane4j 数据填充)
|
||||||
|
* <p>不建议复用,Crane4j 对列表填充聚合查询,优化性能</p>
|
||||||
|
*
|
||||||
|
* @author Charles7c
|
||||||
|
* @since 2025/6/29 11:51
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class SystemContainer {
|
||||||
|
|
||||||
|
private final UserRoleMapper userRoleMapper;
|
||||||
|
private final RoleMapper roleMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据用户 ID 列表获取角色 ID 列表
|
||||||
|
*
|
||||||
|
* @param userIds 用户 ID 列表
|
||||||
|
* @return 角色 ID 列表
|
||||||
|
*/
|
||||||
|
@ContainerMethod(namespace = ContainerConstants.USER_ROLE_ID_LIST, resultKey = "userId", resultType = UserRoleDO.class, type = MappingType.ONE_TO_MANY)
|
||||||
|
public List<UserRoleDO> listRoleIdByUserId(List<Long> userIds) {
|
||||||
|
return userRoleMapper.lambdaQuery()
|
||||||
|
.select(UserRoleDO::getRoleId, UserRoleDO::getUserId)
|
||||||
|
.in(UserRoleDO::getUserId, userIds)
|
||||||
|
.list();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据角色 ID 列表获取角色名称列表
|
||||||
|
*
|
||||||
|
* @param ids 角色 ID 列表
|
||||||
|
* @return 角色名称列表
|
||||||
|
*/
|
||||||
|
@ContainerMethod(namespace = ContainerConstants.USER_ROLE_NAME_LIST, resultType = RoleDO.class)
|
||||||
|
public List<RoleDO> listRoleNameByIds(List<Long> ids) {
|
||||||
|
if (CollUtil.isEmpty(ids)) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
return roleMapper.lambdaQuery().select(RoleDO::getName, RoleDO::getId).in(RoleDO::getId, ids).list();
|
||||||
|
}
|
||||||
|
}
|
@@ -19,6 +19,7 @@ package top.continew.admin.system.model.resp.role;
|
|||||||
import cn.crane4j.annotation.Assemble;
|
import cn.crane4j.annotation.Assemble;
|
||||||
import cn.crane4j.annotation.Mapping;
|
import cn.crane4j.annotation.Mapping;
|
||||||
import cn.crane4j.core.executor.handler.ManyToManyAssembleOperationHandler;
|
import cn.crane4j.core.executor.handler.ManyToManyAssembleOperationHandler;
|
||||||
|
import cn.crane4j.core.executor.handler.OneToManyAssembleOperationHandler;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import top.continew.admin.common.constant.ContainerConstants;
|
import top.continew.admin.common.constant.ContainerConstants;
|
||||||
@@ -60,7 +61,7 @@ public class RoleUserResp implements Serializable {
|
|||||||
* 用户 ID
|
* 用户 ID
|
||||||
*/
|
*/
|
||||||
@Schema(description = "用户 ID", example = "1")
|
@Schema(description = "用户 ID", example = "1")
|
||||||
@Assemble(prop = ":roleIds", sort = 0, container = ContainerConstants.USER_ROLE_ID_LIST)
|
@Assemble(props = @Mapping(src = "roleId", ref = "roleIds"), sort = 0, container = ContainerConstants.USER_ROLE_ID_LIST, handlerType = OneToManyAssembleOperationHandler.class)
|
||||||
private Long userId;
|
private Long userId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -22,6 +22,7 @@ import cn.crane4j.annotation.ContainerMethod;
|
|||||||
import cn.crane4j.annotation.Mapping;
|
import cn.crane4j.annotation.Mapping;
|
||||||
import cn.crane4j.annotation.condition.ConditionOnExpression;
|
import cn.crane4j.annotation.condition.ConditionOnExpression;
|
||||||
import cn.crane4j.core.executor.handler.ManyToManyAssembleOperationHandler;
|
import cn.crane4j.core.executor.handler.ManyToManyAssembleOperationHandler;
|
||||||
|
import cn.crane4j.core.executor.handler.OneToManyAssembleOperationHandler;
|
||||||
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
|
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
|
||||||
import com.alibaba.excel.annotation.ExcelProperty;
|
import com.alibaba.excel.annotation.ExcelProperty;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
@@ -51,7 +52,7 @@ import java.util.Objects;
|
|||||||
@Data
|
@Data
|
||||||
@ExcelIgnoreUnannotated
|
@ExcelIgnoreUnannotated
|
||||||
@Schema(description = "用户详情响应参数")
|
@Schema(description = "用户详情响应参数")
|
||||||
@Assemble(key = "id", prop = ":roleIds", sort = 0, container = ContainerConstants.USER_ROLE_ID_LIST)
|
@Assemble(key = "id", props = @Mapping(src = "roleId", ref = "roleIds"), sort = 0, container = ContainerConstants.USER_ROLE_ID_LIST, handlerType = OneToManyAssembleOperationHandler.class)
|
||||||
public class UserDetailResp extends BaseDetailResp {
|
public class UserDetailResp extends BaseDetailResp {
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
@@ -105,7 +106,7 @@ public class UserDetailResp extends BaseDetailResp {
|
|||||||
* 角色 ID 列表
|
* 角色 ID 列表
|
||||||
*/
|
*/
|
||||||
@Schema(description = "角色 ID 列表", example = "2")
|
@Schema(description = "角色 ID 列表", example = "2")
|
||||||
@Assemble(prop = ":roleNames", container = ContainerConstants.USER_ROLE_NAME_LIST, handlerType = ManyToManyAssembleOperationHandler.class)
|
@Assemble(props = @Mapping(src = "name", ref = "roleNames"), container = ContainerConstants.USER_ROLE_NAME_LIST, handlerType = ManyToManyAssembleOperationHandler.class)
|
||||||
@ExcelProperty(value = "角色 ID 列表", converter = ExcelListConverter.class, order = 8)
|
@ExcelProperty(value = "角色 ID 列表", converter = ExcelListConverter.class, order = 8)
|
||||||
private List<Long> roleIds;
|
private List<Long> roleIds;
|
||||||
|
|
||||||
|
@@ -17,7 +17,9 @@
|
|||||||
package top.continew.admin.system.model.resp.user;
|
package top.continew.admin.system.model.resp.user;
|
||||||
|
|
||||||
import cn.crane4j.annotation.Assemble;
|
import cn.crane4j.annotation.Assemble;
|
||||||
|
import cn.crane4j.annotation.Mapping;
|
||||||
import cn.crane4j.core.executor.handler.ManyToManyAssembleOperationHandler;
|
import cn.crane4j.core.executor.handler.ManyToManyAssembleOperationHandler;
|
||||||
|
import cn.crane4j.core.executor.handler.OneToManyAssembleOperationHandler;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import top.continew.admin.common.model.resp.BaseDetailResp;
|
import top.continew.admin.common.model.resp.BaseDetailResp;
|
||||||
@@ -40,7 +42,7 @@ import java.util.Objects;
|
|||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@Schema(description = "用户响应参数")
|
@Schema(description = "用户响应参数")
|
||||||
@Assemble(key = "id", prop = ":roleIds", sort = 0, container = ContainerConstants.USER_ROLE_ID_LIST)
|
@Assemble(key = "id", props = @Mapping(src = "roleId", ref = "roleIds"), sort = 0, container = ContainerConstants.USER_ROLE_ID_LIST, handlerType = OneToManyAssembleOperationHandler.class)
|
||||||
public class UserResp extends BaseDetailResp {
|
public class UserResp extends BaseDetailResp {
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
@@ -118,7 +120,7 @@ public class UserResp extends BaseDetailResp {
|
|||||||
* 角色 ID 列表
|
* 角色 ID 列表
|
||||||
*/
|
*/
|
||||||
@Schema(description = "角色 ID 列表", example = "2")
|
@Schema(description = "角色 ID 列表", example = "2")
|
||||||
@Assemble(prop = ":roleNames", container = ContainerConstants.USER_ROLE_NAME_LIST, handlerType = ManyToManyAssembleOperationHandler.class)
|
@Assemble(props = @Mapping(src = "name", ref = "roleNames"), container = ContainerConstants.USER_ROLE_NAME_LIST, handlerType = ManyToManyAssembleOperationHandler.class)
|
||||||
private List<Long> roleIds;
|
private List<Long> roleIds;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -61,14 +61,6 @@ public interface RoleService extends BaseService<RoleResp, RoleDetailResp, RoleQ
|
|||||||
*/
|
*/
|
||||||
Set<String> listPermissionByUserId(Long userId);
|
Set<String> listPermissionByUserId(Long userId);
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据 ID 列表查询
|
|
||||||
*
|
|
||||||
* @param ids ID 列表
|
|
||||||
* @return 名称列表
|
|
||||||
*/
|
|
||||||
List<RoleDO> listNameByIds(List<Long> ids);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据用户 ID 查询角色编码
|
* 根据用户 ID 查询角色编码
|
||||||
*
|
*
|
||||||
|
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package top.continew.admin.system.service.impl;
|
package top.continew.admin.system.service.impl;
|
||||||
|
|
||||||
import cn.crane4j.annotation.ContainerMethod;
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import com.alicp.jetcache.anno.CacheInvalidate;
|
import com.alicp.jetcache.anno.CacheInvalidate;
|
||||||
@@ -25,7 +24,6 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import top.continew.admin.common.constant.CacheConstants;
|
import top.continew.admin.common.constant.CacheConstants;
|
||||||
import top.continew.admin.common.constant.ContainerConstants;
|
|
||||||
import top.continew.admin.common.constant.SysConstants;
|
import top.continew.admin.common.constant.SysConstants;
|
||||||
import top.continew.admin.common.context.RoleContext;
|
import top.continew.admin.common.context.RoleContext;
|
||||||
import top.continew.admin.common.context.UserContext;
|
import top.continew.admin.common.context.UserContext;
|
||||||
@@ -165,15 +163,6 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleRes
|
|||||||
return menuService.listPermissionByUserId(userId);
|
return menuService.listPermissionByUserId(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
@ContainerMethod(namespace = ContainerConstants.USER_ROLE_NAME_LIST, resultType = RoleDO.class)
|
|
||||||
public List<RoleDO> listNameByIds(List<Long> ids) {
|
|
||||||
if (CollUtil.isEmpty(ids)) {
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
return baseMapper.lambdaQuery().select(RoleDO::getName, RoleDO::getId).in(RoleDO::getId, ids).list();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<String> listCodeByUserId(Long userId) {
|
public Set<String> listCodeByUserId(Long userId) {
|
||||||
List<Long> roleIdList = userRoleService.listRoleIdByUserId(userId);
|
List<Long> roleIdList = userRoleService.listRoleIdByUserId(userId);
|
||||||
|
@@ -17,8 +17,6 @@
|
|||||||
package top.continew.admin.system.service.impl;
|
package top.continew.admin.system.service.impl;
|
||||||
|
|
||||||
import cn.crane4j.annotation.AutoOperate;
|
import cn.crane4j.annotation.AutoOperate;
|
||||||
import cn.crane4j.annotation.ContainerMethod;
|
|
||||||
import cn.crane4j.annotation.MappingType;
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
@@ -27,7 +25,6 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import top.continew.admin.common.constant.ContainerConstants;
|
|
||||||
import top.continew.admin.common.constant.SysConstants;
|
import top.continew.admin.common.constant.SysConstants;
|
||||||
import top.continew.admin.system.mapper.UserRoleMapper;
|
import top.continew.admin.system.mapper.UserRoleMapper;
|
||||||
import top.continew.admin.system.model.entity.UserRoleDO;
|
import top.continew.admin.system.model.entity.UserRoleDO;
|
||||||
@@ -117,7 +114,6 @@ public class UserRoleServiceImpl implements UserRoleService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ContainerMethod(namespace = ContainerConstants.USER_ROLE_ID_LIST, type = MappingType.ORDER_OF_KEYS)
|
|
||||||
public List<Long> listRoleIdByUserId(Long userId) {
|
public List<Long> listRoleIdByUserId(Long userId) {
|
||||||
return baseMapper.lambdaQuery()
|
return baseMapper.lambdaQuery()
|
||||||
.select(UserRoleDO::getRoleId)
|
.select(UserRoleDO::getRoleId)
|
||||||
|
Reference in New Issue
Block a user