资产授权

master
冷调 2024-09-03 16:03:40 +08:00
parent caa259256d
commit 67a0301908
5 changed files with 134 additions and 131 deletions

View File

@ -4,6 +4,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
/**
* @author Lenovo
@ -14,6 +15,7 @@ import lombok.NoArgsConstructor;
* @ Description
*/
@Data
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
@Tag(name = "资产授权响应对象")

View File

@ -17,6 +17,7 @@ public class TableInfoResp {
* id
*/
private Long id;
private Long basicId;
/**

View File

@ -7,7 +7,6 @@ import com.muyu.common.core.domain.Result;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.common.system.domain.SysUser;
import com.muyu.source.domain.AssetAuthorization;
import com.muyu.source.domain.TableInfo;
import com.muyu.source.domain.rep.AssetAuthorizationRep;
import com.muyu.source.domain.rep.TableInfoResp;
@ -43,169 +42,168 @@ public class TableInfoController {
* @return
*/
@GetMapping("/findAsset")
@Operation(summary = "查询资产信息", description = "查询资产信息")
@Operation(summary = "查询数据库/表 级联查询用于数据资产授权" +
"自己授权用" ,
description = "查询数据库/表 级联查询用于数据资产授权" +
"自己授权用")
public Result findAsset(){
// 从tableInfoService中获取所有的TableInfo对象列表
List<TableInfo> list = tableInfoService.list();
// 使用Java 8的Stream API对list进行处理
List<TableInfoResp> respList = list.stream()
// 过滤出parentId为0的TableInfo对象即顶层资产
//从服务中获取TableInfo对象的列表
return getListResult();
}
/**
*
*
* @return
*/
@Operation(summary = "过滤级联查询方法,资产授权,获取当前用户的信息" ,
description = "过滤级联查询方法,资产授权,获取当前用户的信息内容 赋予权限")
@GetMapping("findAssetByTableName")
public Result findAsserByTableName(){
// //从服务中获取TableInfo对象的列表
// List<TableInfo> list = tableInfoService.list();
//使用安全管理工具获取当前登录的用户对象
LoginUser loginUser = SecurityUtils.getLoginUser();
//从登录用户对象中获取系统用户对象
SysUser sysUser = loginUser.getSysUser();
//从系统用户对象中获取用户ID
Long userId = sysUser.getUserId();
//从系统用户对象中获取部门ID
Long deptId = sysUser.getDeptId();
// 调用assetAuthorizationService服务的findTableIdAndBasicIdByUserId方法
List<AssetAuthorizationRep> idByUserId = assetAuthorizationService.findTableIdAndBasicIdByUserId(userId);
// 调用assetAuthorizationService服务的findTableIdAndBasicIdByDeptId方法
List<AssetAuthorizationRep> idByDeptId = assetAuthorizationService.findTableIdAndBasicIdByDeptId(deptId);
//创建一个HashSet集合用于存储唯一的TableInfo对象
HashSet<TableInfo> hashSet = new HashSet<>();
//遍历idByUserId列表这个列表可能包含了用户授权的资产信息 findBasicIdAndTableId
extracted(idByUserId, hashSet);
//遍历包含用户授权信息的集合这个集合可能是根据部门ID获取的
extracted(idByDeptId, hashSet);
//使用Stream API来处理列表
List<TableInfoResp> respHashSet = hashSet.stream()
//筛选出parentId为0的TableInfo对象
.filter(tableInfo -> tableInfo.getParentId()==0)
// 将过滤后的TableInfo对象转换为TableInfoResp对象并设置其子资产
//将筛选出的TableInfo对象转换为响应对象TableInfoListResp
.map(tableInfo -> {
// 将TableInfo对象转换为TableInfoResp对象
// 将TableInfo对象转换为TableInfoListResp对象
TableInfoResp tableInfoResp = TableInfo.toTableInfoResp(tableInfo);
// 调用getAssetChildren方法获取当前资产的子资产列表并设置到tableInfoResp对象中
tableInfoResp.setChildren(getAssetChildren(tableInfo, list));
// 返回处理后的tableInfoResp对象
//递归地为每个TableInfoListResp对象设置子对象列表
tableInfoResp.setChildren(getChildren(tableInfo, hashSet));
return tableInfoResp;
})
// 将处理后的对象收集到一个新的列表中
//将Stream转换回List
.toList();
// 返回一个包含处理后的TableInfoResp列表的成功结果
return Result.success(respList);
//返回成功的结果,包含处理后的列表
return Result.success(respHashSet);
}
// 获取指定表信息的子表信息列表
private List<TableInfoResp> getAssetChildren(TableInfo tableInfo, List<TableInfo> list) {
// 使用Java 8的Stream API对传入的list进行处理
return list.stream()
// 过滤出所有父ID等于传入tableInfo的ID的表信息
.filter(tableInfo1 -> tableInfo1.getParentId().equals(tableInfo.getId()))
// 将过滤后的表信息转换为TableInfoResp对象
.map(tableInfo2 -> TableInfo.toTableInfoResp(tableInfo2))
// 将转换后的对象收集到一个新的List中并返回
.toList();
}
/**
*
* @return
*/
@GetMapping("findAssetByTableName")
@Operation(summary = "查询授权表信息", description = "查询资产信息")
public Result findAsserByTableName(){
// 从tableInfoService中获取所有的TableInfo对象列表
List<TableInfo> list = tableInfoService.list();
// 获取当前登录用户的信息
LoginUser loginUser = SecurityUtils.getLoginUser();
// 从登录用户信息中获取SysUser对象
SysUser sysUser = loginUser.getSysUser();
// 获取用户的ID
Long userId = sysUser.getUserId();
// 获取用户的部门ID
Long deptId = sysUser.getDeptId();
// 根据用户ID查询授权的表ID和基本信息
List<AssetAuthorizationRep> idByUserId = assetAuthorizationService.findTableIdAndBasicIdByUserId(userId);
// 根据部门ID查询授权的表ID和基本信息
List<AssetAuthorizationRep> idByDeptId = assetAuthorizationService.findTableIdAndBasicIdByDeptId(deptId);
// 创建一个HashSet用于存储授权的表信息
HashSet<TableInfo> hashSet = new HashSet<>();
// 将用户ID授权的表信息添加到hashSet中
extracted(idByUserId, hashSet);
// 将部门ID授权的表信息添加到hashSet中
extracted(idByDeptId, hashSet);
// 将hashSet中的表信息转换为TableInfoResp对象列表
List<TableInfoResp> respList = getTableInfoResps(list);
// 返回成功的结果包含转换后的TableInfoResp对象列表
return Result.success(respList);
}
/**
* /
* @return
*/
@GetMapping("/selectSourceOrTable")
@Operation(summary = "查询数据库或表" , description = "查查询数据库或表内容")
public Result<List<TableInfoResp>> findByTableName() {
// 调用tableInfoService的list方法获取所有的TableInfo对象列表
List<TableInfo> list = tableInfoService.list();
// 调用getTableInfoResps方法将TableInfo对象列表转换为TableInfoResp对象列表
List<TableInfoResp> respList = getTableInfoResps(list);
// 使用Result类的success方法创建一个成功的结果并将转换后的TableInfoResp对象列表作为数据返回
return Result.success(respList);
}
/**
*
* @param idByDeptId
* @param hashSet
*/
private void extracted(List<AssetAuthorizationRep> idByDeptId, HashSet<TableInfo> hashSet) {
// 遍历AssetAuthorizationReq列表
for (AssetAuthorizationRep assetImpowerResp : idByDeptId) {
// 获取当前AssetAuthorizationReq对象的basicId和tableId
private void extracted(List<AssetAuthorizationRep> idByUserId, HashSet<TableInfo> hashSet) {
for (AssetAuthorizationRep assetImpowerResp : idByUserId) {
//获取资产信息中的基本ID
Long basicId = assetImpowerResp.getBasicId();
//获取资产信息中的表ID
Long tableId = assetImpowerResp.getTableId();
// 如果basicId不为空
//如果基本原来表的ID不为空则进行查询
if (null != basicId) {
// 创建一个LambdaQueryWrapper对象,用于构建查询条件
//创建一个LambdaQueryWrapper用于构建查询条件
LambdaQueryWrapper<TableInfo> queryWrapper = new LambdaQueryWrapper<>();
// 设置查询条件根据basicId查找TableInfo对象
//添加查询条件筛选出基本ID等于当前基本ID的TableInfo对象
queryWrapper.eq(TableInfo::getBasicId, basicId);
// 使用tableInfoService的list方法根据查询条件获取符合条件的TableInfo列表
//调用服务方法获取符合条件的TableInfo列表
List<TableInfo> tableInfoBasicIdList = tableInfoService.list(queryWrapper);
// 将查询到的TableInfo列表添加到hashSet
//将查询结果添加到HashSet集合中
hashSet.addAll(tableInfoBasicIdList);
}
// 如果tableId不为空
//如果当前表ID主键不为空则进行查询
if (null != tableId) {
// 使用tableInfoService的getById方法根据tableId获取对应的TableInfo对象
//调用服务方法根据表ID获取TableInfo对象
TableInfo tableInfoById = tableInfoService.getById(tableId);
// 将获取到的TableInfo对象添加到hashSet
//将查询到的TableInfo对象添加到HashSet集合中
hashSet.add(tableInfoById);
// 获取当前TableInfo对象的parentId
//获取TableInfo对象的父ID
Long parentId = tableInfoById.getParentId();
// 使用tableInfoService的getById方法根据parentId获取对应的TableInfo对象
//如果父ID不为空则根据父ID查询父TableInfo对象
TableInfo tableInfoByIdParentId = tableInfoService.getById(parentId);
// 将获取到的父级TableInfo对象添加到hashSet
//将父TableInfo对象添加到HashSet集合中
hashSet.add(tableInfoByIdParentId);
}
}
}
/**
*
* @param list
* @return
* /
* @return
*/
@NotNull
private static List<TableInfoResp> getTableInfoResps(List<TableInfo> list) {
// 使用Java 8的Stream API对输入的list进行处理
@GetMapping("findByTableName")
@Operation(summary = "任务接口调用查询数据库/表 级联查询" ,
description = "任务接口调用查询数据库/表 级联查询内容")
public Result<List<TableInfoResp>> findByTableName() {
//从服务中获取TableInfo对象的列表
return getListResult();
}
private Result<List<TableInfoResp>> getListResult() {
List<TableInfo> list = tableInfoService.list();
//使用Stream API来处理列表
List<TableInfoResp> respList = list.stream()
// 过滤出parentId为0的元素即顶级元素
//筛选出parentId为0的TableInfo对象
.filter(tableInfo -> tableInfo.getParentId()==0)
// 对每个符合条件的元素进行映射操作
//将筛选出的TableInfo对象转换为响应对象TableInfoListResp
.map(tableInfo -> {
// 将TableInfo对象转换为TableInfoResp对象
// 将TableInfo对象转换为TableInfoListResp对象
TableInfoResp tableInfoResp = TableInfo.toTableInfoResp(tableInfo);
// 设置子元素列表通过调用getChildren方法获取子元素
tableInfoResp.setChildren(getChildren(tableInfo, list));
// 返回处理后的TableInfoResp对象
//递归地为每个TableInfoListResp对象设置子对象列表
tableInfoResp.setChildren(getAssetChildren(tableInfo, list));
return tableInfoResp;
})
// 将流中的元素收集到一个新的列表中
.toList();
// 返回处理后的列表
return respList;
return Result.success(respList);
}
/**
*
* @param tableInfo
* @param list
* @return
*
*
*
* @param tableInfo
* @param set
* @return
*/
@NotNull
private static List<TableInfoResp> getChildren(TableInfo tableInfo, List<TableInfo> list) {
return list.stream().filter(tableInfo1 -> tableInfo1.getParentId().equals(tableInfo.getId())).map(
TableInfo::toTableInfoResp
).toList();
private static List<TableInfoResp> getChildren(TableInfo tableInfo, HashSet<TableInfo> set) {
//使用Stream API处理list集合
return set.stream()
//筛选出那些parentId等于tableInfo.getId()的TableInfo对象
.filter(tableInfo1 -> tableInfo1.getParentId().equals(tableInfo.getId()))
//将TableInfo对象转换为TableInfoListResp对象
.map(
tableInfo2 -> TableInfo.toTableInfoResp(tableInfo2))
.toList();
}
/**
*
* /
*
* @param tableInfo
* @param list
* @return
*/
@NotNull
private static List<TableInfoResp> getAssetChildren(TableInfo tableInfo, List<TableInfo> list) {
//使用Stream API处理list集合
return list.stream()//将list转换为Stream
//使用filter方法筛选出那些parentId等于tableInfo.getId()的TableInfo对象
.filter(tableInfo1 -> tableInfo1.getParentId().equals(tableInfo.getId()))
//使用map方法将筛选出的TableInfo对象转换为TableInfoListResp对象
.map(
tableInfo2 -> TableInfo.toTableInfoResp(tableInfo2))
.toList();
}
/**

View File

@ -20,7 +20,6 @@ import java.util.List;
public interface AssetAuthorizationMapper extends BaseMapper<AssetAuthorization> {
List<Long> findUserIdList(AssetAuthorizationReq req);
List<Long> findDeptIdList(AssetAuthorizationReq req);
List<AssetAuthorizationRep> findTableIdAndBasicIdByUserId(Long userId);

View File

@ -50,7 +50,10 @@ public class TableServiceImpl extends ServiceImpl<TableInfoMapper, TableInfo> im
@Override
public List<TableInfo> findTableNameById(Long id) {
return null;
LambdaQueryWrapper<TableInfo> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(TableInfo::getId, id);
List<TableInfo> list = tableInfoMapper.selectList(queryWrapper);
return list;
}
/**