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