Compare commits

...

2 Commits

Author SHA1 Message Date
liyongbin 95d4600a89 Merge remote-tracking branch 'origin/dev' into dev
# Conflicts:
#	srt-cloud-data-service/src/main/java/net/srt/service/impl/ApiGroupServiceImpl.java
2023-12-26 22:31:15 +08:00
liyongbin 366402eb13 my 2023-12-26 22:30:47 +08:00
1 changed files with 54 additions and 9 deletions

View File

@ -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);
} }
/**
* APIVO
* @return APIVO
*/
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 APIVO
*/
@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 APIVO
*/
@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;
} }
/**
* IDAPI
* @param id APIID
*/
@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);
} }
} }