开放接口新增获取组数据相关接口

This commit is contained in:
chenxing
2026-08-13 19:15:47 +08:00
parent 8113045471
commit 5cfed17956
5 changed files with 152 additions and 0 deletions
@@ -2,6 +2,7 @@ package com.yunhai.userbase.service.impl;
import com.yunhai.userbase.entity.SysGroup;
import com.yunhai.platform.utils.exception.DException;
import com.yunhai.platform.utils.model.BoaGroup;
import com.yunhai.userbase.enums.GroupTypeEnum;
import com.yunhai.userbase.enums.IEnum;
import com.yunhai.userbase.model.sysgroup.SysGroupBatchRequest;
@@ -223,6 +224,77 @@ public class SysGroupService implements ISysGroupService {
return vo;
}
// ===================== 开放接口:组树(BoaGroup) =====================
/**
* 获取整个平台的组树:从根节点(pid=0)开始,children 递归嵌套全部后代。<br>
* 根节点唯一,取最小 id 作为树入口。表为空或无根时返回 null。
*/
@Override
public BoaGroup getGroups() {
List<SysGroup> all = sysGroupRepository.findAll();
if (all.isEmpty()) {
return null;
}
Map<Integer, List<SysGroup>> childrenMap = buildSortedChildrenMap(all);
List<SysGroup> roots = childrenMap.getOrDefault(ROOT_PARENT_ID, new ArrayList<>());
if (roots.isEmpty()) {
return null;
}
// 根节点唯一,取最小 id 作为整棵树入口
SysGroup root = roots.stream()
.min(Comparator.comparingInt(SysGroup::getId))
.orElse(roots.get(0));
return buildBoaNode(root, childrenMap, true);
}
/**
* 获取单个组;withSub=true 时递归挂上全部子孙(整棵子树),false/不传时只返回节点本身(children 为 null)。<br>
* 组不存在时返回 null。
*/
@Override
public BoaGroup getGroupById(Integer groupId, Boolean withSub) {
if (groupId == null) {
return null;
}
SysGroup entity = sysGroupRepository.findById(groupId).orElse(null);
if (entity == null) {
return null;
}
if (!Boolean.TRUE.equals(withSub)) {
return convertToBoaGroup(entity);
}
List<SysGroup> all = sysGroupRepository.findAll();
Map<Integer, List<SysGroup>> childrenMap = buildSortedChildrenMap(all);
return buildBoaNode(entity, childrenMap, true);
}
/**
* 批量获取组;ids 逗号/空格分隔,按入参顺序返回,不存在的 id 跳过。<br>
* withSub 语义同 {@link #getGroupById(Integer, Boolean)}。
*/
@Override
public List<BoaGroup> getGroupList(String ids, Boolean withSub) {
List<Integer> idList = parseIds(ids);
if (idList.isEmpty()) {
return new ArrayList<>();
}
boolean expand = Boolean.TRUE.equals(withSub);
List<SysGroup> all = sysGroupRepository.findAll();
Map<Integer, SysGroup> allMap = all.stream()
.collect(Collectors.toMap(SysGroup::getId, g -> g, (a, b) -> a));
Map<Integer, List<SysGroup>> childrenMap = buildSortedChildrenMap(all);
List<BoaGroup> result = new ArrayList<>();
for (Integer id : idList) {
SysGroup g = allMap.get(id);
if (g == null) {
continue;
}
result.add(expand ? buildBoaNode(g, childrenMap, true) : convertToBoaGroup(g));
}
return result;
}
// ===================== 批量新增 =====================
@Override
@@ -374,6 +446,56 @@ public class SysGroupService implements ISysGroupService {
// ===================== 私有工具 =====================
/** SysGroup -> BoaGroup(id/name/parentId/type/sort);children 默认 null(未展开)。 */
private BoaGroup convertToBoaGroup(SysGroup entity) {
BoaGroup bg = new BoaGroup();
bg.setId(entity.getId());
bg.setName(entity.getName());
bg.setParentId(entity.getParentId());
bg.setType(entity.getType() == null ? null : entity.getType().getKey());
bg.setSort(entity.getSort());
return bg;
}
/**
* 递归组装 BoaGroup 树节点。withSub=true 时挂上全部子孙(子树),children 为列表(无子则空列表);
* withSub=false 时直接返回节点,children 保持 null(表示未展开)。
*/
private BoaGroup buildBoaNode(SysGroup node, Map<Integer, List<SysGroup>> childrenMap, boolean withSub) {
BoaGroup bg = convertToBoaGroup(node);
if (!withSub) {
return bg;
}
List<BoaGroup> childBgs = new ArrayList<>();
List<SysGroup> children = childrenMap.get(node.getId());
if (children != null) {
for (SysGroup child : children) {
childBgs.add(buildBoaNode(child, childrenMap, true));
}
}
bg.setChildren(childBgs);
return bg;
}
/** 解析逗号/空格分隔的 id 串为 Integer 列表;非数字片段忽略。 */
private List<Integer> parseIds(String ids) {
List<Integer> result = new ArrayList<>();
if (ids == null || ids.trim().isEmpty()) {
return result;
}
for (String s : ids.split("[,\\s]+")) {
if (s == null || s.trim().isEmpty()) {
continue;
}
try {
result.add(Integer.parseInt(s.trim()));
} catch (NumberFormatException ignored) {
// 跳过非数字片段
}
}
return result;
}
/** pid 下已有子节点的最大 sort +1;无子节点返回 1。 */
private int nextSort(Integer pid) {
Integer max = sysGroupRepository.findMaxSortByParentId(pid);