Compare commits
2 Commits
78153f8c69
...
95d4600a89
Author | SHA1 | Date |
---|---|---|
|
95d4600a89 | |
|
366402eb13 |
|
@ -25,71 +25,116 @@ import java.util.List;
|
|||
@AllArgsConstructor
|
||||
public class ApiGroupServiceImpl extends BaseServiceImpl<ApiGroupDao, ApiGroupEntity> implements ApiGroupService{
|
||||
private final ApiConfigService apiConfigService;
|
||||
/**
|
||||
* 获取API分组树形结构列表
|
||||
* @return API分组树形结构列表
|
||||
*/
|
||||
@Override
|
||||
public List<TreeNodeVo> listTree() {
|
||||
// 查询所有API分组实体对象,并转换为树节点VO对象列表
|
||||
List<TreeNodeVo> treeNodeVos = getTreeNodeVos();
|
||||
// 构建树形结构,并返回根节点列表
|
||||
return BuildTreeUtils.buildTree(treeNodeVos);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取API分组树节点VO对象列表
|
||||
* @return API分组树节点VO对象列表
|
||||
*/
|
||||
private List<TreeNodeVo> getTreeNodeVos() {
|
||||
LambdaQueryWrapper<ApiGroupEntity> wrapper = new LambdaQueryWrapper<>();
|
||||
dataScopeWithoutOrgId(wrapper);
|
||||
wrapper.orderByAsc(ApiGroupEntity::getOrderNo);
|
||||
dataScopeWithoutOrgId(wrapper); // 加入数据权限过滤
|
||||
wrapper.orderByAsc(ApiGroupEntity::getOrderNo); // 按orderNo升序排序
|
||||
List<ApiGroupEntity> apiGroupEntities = baseMapper.selectList(wrapper);
|
||||
// 将实体对象列表转换为VO对象列表
|
||||
return BeanUtil.copyListProperties(apiGroupEntities, TreeNodeVo::new, (oldItem, newItem) -> {
|
||||
// 设置节点名称
|
||||
newItem.setLabel(oldItem.getName());
|
||||
// 设置节点值
|
||||
newItem.setValue(oldItem.getId());
|
||||
newItem.setDisabled(oldItem.getType() == 1);
|
||||
if (newItem.getPath().contains("/")) {
|
||||
newItem.setDisabled(oldItem.getType() == 1); // 如果是虚拟节点,设置禁用状态
|
||||
if (newItem.getPath().contains("/")) { // 设置父级节点路径
|
||||
newItem.setParentPath(newItem.getPath().substring(0, newItem.getPath().lastIndexOf("/")));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增API分组
|
||||
* @param vo API分组VO对象
|
||||
*/
|
||||
@Override
|
||||
public void save(ApiGroupVo vo) {
|
||||
// 将VO对象转换为实体对象
|
||||
ApiGroupEntity entity = ApiGroupConvert.INSTANCE.convert(vo);
|
||||
// 递归生成路径
|
||||
entity.setPath(recursionPath(entity, null));
|
||||
// 设置项目ID
|
||||
entity.setProjectId(getProjectId());
|
||||
// 执行插入操作
|
||||
baseMapper.insert(entity); // 使用 insertSelective() 方法进行插入操作
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新API分组
|
||||
* @param vo API分组VO对象
|
||||
*/
|
||||
@Override
|
||||
public void update(ApiGroupVo vo) {
|
||||
// 将VO对象转换为实体对象
|
||||
ApiGroupEntity entity = ApiGroupConvert.INSTANCE.convert(vo);
|
||||
// 递归生成路径
|
||||
entity.setPath(recursionPath(entity, null));
|
||||
// 设置项目ID
|
||||
entity.setProjectId(getProjectId());
|
||||
// 执行更新操作
|
||||
updateById(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归生成API分组路径
|
||||
* @param groupEntity 分组实体对象
|
||||
* @param path 父级路径
|
||||
* @return 完整路径
|
||||
*/
|
||||
//1
|
||||
private String recursionPath(ApiGroupEntity groupEntity, String path) {
|
||||
if (StringUtil.isBlank(path)) {
|
||||
// 如果路径为空,则将路径设置为组实体的名称
|
||||
path = groupEntity.getName();
|
||||
}
|
||||
if (groupEntity.getParentId() != 0) {
|
||||
ApiGroupEntity parent = getById(groupEntity.getParentId());
|
||||
path = parent.getName() + "/" + path;
|
||||
return recursionPath(parent, path);
|
||||
// 如果组实体的父ID不为0
|
||||
// 查询父级组实体
|
||||
ApiGroupEntity parent = getById(groupEntity.getParentId()); // 通过父ID获取父级组实体
|
||||
path = parent.getName() + "/" + path; // 将父级组实体的名称与当前路径拼接起来,中间以斜杠分隔
|
||||
return recursionPath(parent, path); // 递归调用recursionPath方法,传入父级组实体和拼接后的路径
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID删除API分组
|
||||
* @param id API分组ID
|
||||
*/
|
||||
@Override
|
||||
public void delete(Long id) {
|
||||
//查询有没有子节点
|
||||
// 查询是否存在子节点
|
||||
LambdaQueryWrapper<ApiGroupEntity> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(ApiGroupEntity::getParentId, id).last(" limit 1");
|
||||
ApiGroupEntity one = baseMapper.selectOne(wrapper);
|
||||
if (one != null) {
|
||||
throw new ServerException("存在子节点,不允许删除!");
|
||||
}
|
||||
//查询有没有api与之关联
|
||||
// 查询是否有API配置与之关联
|
||||
LambdaQueryWrapper<ApiConfigEntity> serviceApiConfigWrapper = new LambdaQueryWrapper<>();
|
||||
serviceApiConfigWrapper.eq(ApiConfigEntity::getGroupId, id).last(" limit 1");
|
||||
ApiConfigEntity apiConfigEntity = apiConfigService.getOne(serviceApiConfigWrapper);
|
||||
if (apiConfigEntity != null) {
|
||||
throw new ServerException("节点下有 api 与之关联,不允许删除!");
|
||||
}
|
||||
// 执行删除操作
|
||||
removeById(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue