|
|
@@ -12,10 +12,11 @@ import shop.alien.entity.store.LifeSysDept;
|
|
|
import shop.alien.entity.store.dto.LifeSysDeptAddDto;
|
|
|
import shop.alien.entity.store.dto.LifeSysDeptSortDto;
|
|
|
import shop.alien.entity.store.dto.LifeSysDeptUpdateDto;
|
|
|
+import shop.alien.entity.store.vo.LifeSysDeptVo;
|
|
|
import shop.alien.mapper.LifeSysDeptMapper;
|
|
|
import shop.alien.store.service.LifeSysDeptService;
|
|
|
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* 部门表 服务实现类
|
|
|
@@ -360,5 +361,140 @@ public class LifeSysDeptServiceImpl implements LifeSysDeptService {
|
|
|
}
|
|
|
return R.fail("编辑部门失败,请稍后重试");
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R<List<LifeSysDeptVo>> searchDeptByName(String deptName) {
|
|
|
+ log.info("LifeSysDeptServiceImpl.searchDeptByName?deptName={}", deptName);
|
|
|
+
|
|
|
+ // 构建查询条件
|
|
|
+ QueryWrapper<LifeSysDept> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("del_flag", "0"); // 只查询未删除的部门
|
|
|
+
|
|
|
+ // 如果部门名称不为空,添加模糊查询条件
|
|
|
+ if (StringUtils.hasText(deptName)) {
|
|
|
+ queryWrapper.like("dept_name", deptName.trim());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 按排序值升序排列
|
|
|
+ queryWrapper.orderByAsc("dept_sort");
|
|
|
+
|
|
|
+ // 执行查询,获取匹配的部门
|
|
|
+ List<LifeSysDept> matchedDeptList = lifeSysDeptMapper.selectList(queryWrapper);
|
|
|
+
|
|
|
+ // 如果部门名称为空,直接返回所有部门的树形结构
|
|
|
+ if (!StringUtils.hasText(deptName)) {
|
|
|
+ List<LifeSysDeptVo> treeList = buildTree(matchedDeptList);
|
|
|
+ log.info("搜索部门成功,查询条件:部门名称={}, 结果数量={}", deptName, matchedDeptList != null ? matchedDeptList.size() : 0);
|
|
|
+ return R.data(treeList, "查询成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 收集所有需要返回的部门ID(包括匹配的部门和它们的父部门)
|
|
|
+ java.util.Set<Long> deptIdSet = new java.util.HashSet<>();
|
|
|
+
|
|
|
+ if (matchedDeptList != null && !matchedDeptList.isEmpty()) {
|
|
|
+ for (LifeSysDept dept : matchedDeptList) {
|
|
|
+ // 添加匹配的部门ID
|
|
|
+ if (dept.getDeptId() != null) {
|
|
|
+ deptIdSet.add(dept.getDeptId());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析 ancestors 字段,获取所有父部门ID
|
|
|
+ if (StringUtils.hasText(dept.getAncestors())) {
|
|
|
+ String ancestors = dept.getAncestors();
|
|
|
+ // ancestors 格式:0,100,101 或 100,101
|
|
|
+ String[] ancestorIds = ancestors.split(",");
|
|
|
+ for (String ancestorIdStr : ancestorIds) {
|
|
|
+ try {
|
|
|
+ Long ancestorId = Long.parseLong(ancestorIdStr.trim());
|
|
|
+ // 排除根部门(0)
|
|
|
+ if (ancestorId != null && ancestorId != 0) {
|
|
|
+ deptIdSet.add(ancestorId);
|
|
|
+ }
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ log.warn("解析父部门ID失败:{}", ancestorIdStr, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果 parentId 不为空且不为0,也添加父部门ID(防止 ancestors 为空的情况)
|
|
|
+ if (dept.getParentId() != null && dept.getParentId() != 0) {
|
|
|
+ deptIdSet.add(dept.getParentId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果没有任何匹配结果,直接返回空列表
|
|
|
+ if (deptIdSet.isEmpty()) {
|
|
|
+ log.info("搜索部门成功,查询条件:部门名称={}, 结果数量=0", deptName);
|
|
|
+ return R.data(new ArrayList<>(), "查询成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询所有需要返回的部门(包括匹配的部门和它们的父部门)
|
|
|
+ QueryWrapper<LifeSysDept> finalQueryWrapper = new QueryWrapper<>();
|
|
|
+ finalQueryWrapper.in("dept_id", deptIdSet);
|
|
|
+ finalQueryWrapper.eq("del_flag", "0"); // 只查询未删除的部门
|
|
|
+ finalQueryWrapper.orderByAsc("dept_sort");
|
|
|
+
|
|
|
+ List<LifeSysDept> finalDeptList = lifeSysDeptMapper.selectList(finalQueryWrapper);
|
|
|
+
|
|
|
+ // 构建树形结构
|
|
|
+ List<LifeSysDeptVo> treeList = buildTree(finalDeptList);
|
|
|
+
|
|
|
+ log.info("搜索部门成功,查询条件:部门名称={}, 匹配数量={}, 最终结果数量={}",
|
|
|
+ deptName, matchedDeptList != null ? matchedDeptList.size() : 0,
|
|
|
+ finalDeptList != null ? finalDeptList.size() : 0);
|
|
|
+ return R.data(treeList, "查询成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建树形结构
|
|
|
+ *
|
|
|
+ * @param flatList 扁平列表
|
|
|
+ * @return 树形结构列表
|
|
|
+ */
|
|
|
+ private List<LifeSysDeptVo> buildTree(List<LifeSysDept> flatList) {
|
|
|
+ if (flatList == null || flatList.isEmpty()) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建映射:ID到节点的映射,父ID到子节点列表的映射
|
|
|
+ Map<Long, LifeSysDeptVo> nodeMap = new HashMap<>();
|
|
|
+ Map<Long, List<LifeSysDeptVo>> parentChildMap = new HashMap<>();
|
|
|
+ List<LifeSysDeptVo> result = new ArrayList<>();
|
|
|
+
|
|
|
+ // 第一步:将实体转换为VO,并建立映射关系
|
|
|
+ for (LifeSysDept dept : flatList) {
|
|
|
+ LifeSysDeptVo vo = LifeSysDeptVo.fromEntity(dept);
|
|
|
+ Long deptId = vo.getDeptId();
|
|
|
+ Long parentId = vo.getParentId() != null ? vo.getParentId() : 0L;
|
|
|
+
|
|
|
+ // 存入节点映射
|
|
|
+ nodeMap.put(deptId, vo);
|
|
|
+
|
|
|
+ // 如果是根节点(parentId为0或null),直接添加到结果
|
|
|
+ if (parentId == null || parentId == 0) {
|
|
|
+ result.add(vo);
|
|
|
+ } else {
|
|
|
+ // 否则,记录父子关系
|
|
|
+ parentChildMap.computeIfAbsent(parentId, k -> new ArrayList<>()).add(vo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 第二步:建立父子关系
|
|
|
+ for (LifeSysDeptVo vo : nodeMap.values()) {
|
|
|
+ Long deptId = vo.getDeptId();
|
|
|
+ if (parentChildMap.containsKey(deptId)) {
|
|
|
+ // 对子节点按排序值排序
|
|
|
+ List<LifeSysDeptVo> children = parentChildMap.get(deptId);
|
|
|
+ children.sort(Comparator.comparing(LifeSysDeptVo::getDeptSort, Comparator.nullsLast(Integer::compareTo)));
|
|
|
+ vo.setChildren(children);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 对根节点按排序值排序
|
|
|
+ result.sort(Comparator.comparing(LifeSysDeptVo::getDeptSort, Comparator.nullsLast(Integer::compareTo)));
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|
|
|
|