添加默认过滤空数据
This commit is contained in:
@@ -21,36 +21,40 @@ public class JobCategoryController {
|
||||
|
||||
/**
|
||||
* 获取全部岗位分类树(一级-二级-三级嵌套)
|
||||
* @param includeEmpty 是否包含空分类(无岗位关联),默认false(滤空)
|
||||
*/
|
||||
@GetMapping("/tree")
|
||||
public List<JobCategoryTreeVo> getCategoryTree() {
|
||||
return categoryService.getCategoryTree();
|
||||
public List<JobCategoryTreeVo> getCategoryTree(@RequestParam(required = false) Boolean includeEmpty) {
|
||||
return categoryService.getCategoryTree(includeEmpty);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按父级查询岗位分类列表
|
||||
* @param parentId 父级ID,不传=查一级
|
||||
* @param includeEmpty 是否包含空分类(无岗位关联),默认false(滤空)
|
||||
*/
|
||||
@GetMapping
|
||||
public List<JobCategoryVo> getCategoriesByParent(@RequestParam(required = false) Long parentId) {
|
||||
return categoryService.getCategoriesByParent(parentId);
|
||||
public List<JobCategoryVo> getCategoriesByParent(@RequestParam(required = false) Long parentId, @RequestParam(required = false) Boolean includeEmpty) {
|
||||
return categoryService.getCategoriesByParent(parentId, includeEmpty);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模糊搜索岗位分类
|
||||
* @param keyword 分类名关键字
|
||||
* @param includeEmpty 是否包含空分类(无岗位关联),默认false(滤空)
|
||||
*/
|
||||
@GetMapping("/search")
|
||||
public List<JobCategoryVo> searchCategories(@RequestParam String keyword) {
|
||||
return categoryService.searchCategories(keyword);
|
||||
public List<JobCategoryVo> searchCategories(@RequestParam String keyword, @RequestParam(required = false) Boolean includeEmpty) {
|
||||
return categoryService.searchCategories(keyword, includeEmpty);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量查询岗位分类
|
||||
* @param ids 分类ID列表
|
||||
* @param includeEmpty 是否包含空分类(无岗位关联),默认false(滤空)
|
||||
*/
|
||||
@PostMapping("/batch")
|
||||
public List<JobCategoryVo> getCategoriesByIds(@RequestBody List<Long> ids) {
|
||||
return categoryService.getCategoriesByIds(ids);
|
||||
public List<JobCategoryVo> getCategoriesByIds(@RequestBody List<Long> ids, @RequestParam(required = false) Boolean includeEmpty) {
|
||||
return categoryService.getCategoriesByIds(ids, includeEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,22 +3,22 @@ package org.jiayunet.service;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jiayunet.mapper.JobCategoryMapper;
|
||||
import org.jiayunet.mapper.JobMapper;
|
||||
import org.jiayunet.pojo.po.Job;
|
||||
import org.jiayunet.pojo.po.JobCategory;
|
||||
import org.jiayunet.pojo.vo.JobCategoryTreeVo;
|
||||
import org.jiayunet.pojo.vo.JobCategoryVo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 岗位分类服务
|
||||
* <p>主要功能:岗位分类数据查询(树形、层级、搜索)</p>
|
||||
* <p>依赖:无</p>
|
||||
* <p>使用表:bg_job_category</p>
|
||||
* <p>主要功能:岗位分类数据查询(树形、层级、搜索),支持滤空(过滤无岗位关联的分类)</p>
|
||||
* <p>依赖:JobMapper(查询有岗位关联的分类ID)</p>
|
||||
* <p>使用表:bg_job_category、bg_job</p>
|
||||
*
|
||||
* @author zk
|
||||
*/
|
||||
@@ -28,68 +28,68 @@ public class JobCategoryService {
|
||||
@Autowired
|
||||
private JobCategoryMapper categoryMapper;
|
||||
|
||||
@Autowired
|
||||
private JobMapper jobMapper;
|
||||
|
||||
/**
|
||||
* 获取全部岗位分类树(一级-二级-三级嵌套)
|
||||
* <p>1. 查询全部分类 2. 按level分组 3. 构建三层树</p>
|
||||
* 获取全部岗位分类树(递归构建,支持任意层级)
|
||||
* <p>1. 查询全部分类 2. 滤空(可选) 3. 按parentId分组递归构建树</p>
|
||||
*/
|
||||
public List<JobCategoryTreeVo> getCategoryTree() {
|
||||
public List<JobCategoryTreeVo> getCategoryTree(Boolean includeEmpty) {
|
||||
List<JobCategory> allCategories = categoryMapper.selectList(null);
|
||||
List<JobCategory> level1 = allCategories.stream().filter(c -> c.getLevel() == 1).collect(Collectors.toList());
|
||||
Map<Long, List<JobCategory>> level2ByParent = allCategories.stream().filter(c -> c.getLevel() == 2).collect(Collectors.groupingBy(JobCategory::getParentId));
|
||||
Map<Long, List<JobCategory>> level3ByParent = allCategories.stream().filter(c -> c.getLevel() == 3).collect(Collectors.groupingBy(JobCategory::getParentId));
|
||||
return level1.stream().map(cat1 -> {
|
||||
JobCategoryTreeVo vo1 = new JobCategoryTreeVo();
|
||||
vo1.setId(cat1.getId());
|
||||
vo1.setName(cat1.getName());
|
||||
vo1.setLevel(cat1.getLevel());
|
||||
List<JobCategory> level2List = level2ByParent.getOrDefault(cat1.getId(), new ArrayList<>());
|
||||
vo1.setChildren(level2List.stream().map(cat2 -> {
|
||||
JobCategoryTreeVo vo2 = new JobCategoryTreeVo();
|
||||
vo2.setId(cat2.getId());
|
||||
vo2.setName(cat2.getName());
|
||||
vo2.setLevel(cat2.getLevel());
|
||||
List<JobCategory> level3List = level3ByParent.getOrDefault(cat2.getId(), new ArrayList<>());
|
||||
vo2.setChildren(level3List.stream().map(cat3 -> {
|
||||
JobCategoryTreeVo vo3 = new JobCategoryTreeVo();
|
||||
vo3.setId(cat3.getId());
|
||||
vo3.setName(cat3.getName());
|
||||
vo3.setLevel(cat3.getLevel());
|
||||
vo3.setChildren(null);
|
||||
return vo3;
|
||||
}).collect(Collectors.toList()));
|
||||
return vo2;
|
||||
}).collect(Collectors.toList()));
|
||||
return vo1;
|
||||
}).collect(Collectors.toList());
|
||||
if (!Boolean.TRUE.equals(includeEmpty)) {
|
||||
Set<Long> activeIds = getActiveCategoryIds(allCategories);
|
||||
allCategories = allCategories.stream().filter(c -> activeIds.contains(c.getId())).toList();
|
||||
}
|
||||
Map<Long, List<JobCategory>> childrenMap = allCategories.stream().collect(Collectors.groupingBy(JobCategory::getParentId));
|
||||
return buildChildren(0L, childrenMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按父级查询岗位分类列表
|
||||
* <p>parentId为null查一级,否则查下一级</p>
|
||||
* <p>parentId为null查一级,否则查下一级;默认滤空</p>
|
||||
*/
|
||||
public List<JobCategoryVo> getCategoriesByParent(Long parentId) {
|
||||
public List<JobCategoryVo> getCategoriesByParent(Long parentId, Boolean includeEmpty) {
|
||||
List<JobCategory> categories;
|
||||
if (parentId == null) {
|
||||
categories = categoryMapper.selectList(new LambdaQueryWrapper<JobCategory>().eq(JobCategory::getLevel, 1));
|
||||
} else {
|
||||
categories = categoryMapper.selectList(new LambdaQueryWrapper<JobCategory>().eq(JobCategory::getParentId, parentId));
|
||||
}
|
||||
return categories.stream().map(c -> {
|
||||
JobCategoryVo vo = new JobCategoryVo();
|
||||
vo.setId(c.getId());
|
||||
vo.setName(c.getName());
|
||||
vo.setLevel(c.getLevel());
|
||||
vo.setParentId(c.getParentId());
|
||||
return vo;
|
||||
}).collect(Collectors.toList());
|
||||
return filterAndConvert(categories, includeEmpty);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模糊搜索岗位分类
|
||||
* <p>按分类名关键字模糊查询</p>
|
||||
* <p>按分类名关键字模糊查询,默认滤空</p>
|
||||
*/
|
||||
public List<JobCategoryVo> searchCategories(String keyword) {
|
||||
public List<JobCategoryVo> searchCategories(String keyword, Boolean includeEmpty) {
|
||||
List<JobCategory> categories = categoryMapper.selectList(new LambdaQueryWrapper<JobCategory>().like(JobCategory::getName, keyword));
|
||||
return filterAndConvert(categories, includeEmpty);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量查询岗位分类
|
||||
* <p>根据ID列表批量查询分类详情,默认滤空</p>
|
||||
*/
|
||||
public List<JobCategoryVo> getCategoriesByIds(List<Long> ids, Boolean includeEmpty) {
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
List<JobCategory> categories = categoryMapper.selectList(new LambdaQueryWrapper<JobCategory>().in(JobCategory::getId, ids));
|
||||
return filterAndConvert(categories, includeEmpty);
|
||||
}
|
||||
|
||||
// ==================== 私有方法 ====================
|
||||
|
||||
/**
|
||||
* 滤空 + 转VO
|
||||
*/
|
||||
private List<JobCategoryVo> filterAndConvert(List<JobCategory> categories, Boolean includeEmpty) {
|
||||
if (!Boolean.TRUE.equals(includeEmpty)) {
|
||||
Set<Long> activeIds = getActiveCategoryIds(null);
|
||||
categories = categories.stream().filter(c -> activeIds.contains(c.getId())).toList();
|
||||
}
|
||||
return categories.stream().map(c -> {
|
||||
JobCategoryVo vo = new JobCategoryVo();
|
||||
vo.setId(c.getId());
|
||||
@@ -101,20 +101,47 @@ public class JobCategoryService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量查询岗位分类
|
||||
* <p>根据ID列表批量查询分类详情</p>
|
||||
* 获取有岗位关联的有效分类ID集合(含祖先链路)
|
||||
* <p>1. 查询bg_job中有效岗位的distinct categoryId</p>
|
||||
* <p>2. 从末级ID沿parentId向上追溯,构建完整有效ID集合</p>
|
||||
*
|
||||
* @param allCategories 全量分类数据,传null则内部查询
|
||||
*/
|
||||
public List<JobCategoryVo> getCategoriesByIds(List<Long> ids) {
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
private Set<Long> getActiveCategoryIds(List<JobCategory> allCategories) {
|
||||
// 1. 查询有岗位关联的末级分类ID
|
||||
List<Job> jobs = jobMapper.selectList(new LambdaQueryWrapper<Job>().select(Job::getCategoryId).eq(Job::getStatus, 0).groupBy(Job::getCategoryId));
|
||||
Set<Long> leafActiveIds = jobs.stream().map(Job::getCategoryId).filter(Objects::nonNull).collect(Collectors.toSet());
|
||||
|
||||
// 2. 查全量分类,构建id->parentId映射
|
||||
if (allCategories == null) {
|
||||
allCategories = categoryMapper.selectList(null);
|
||||
}
|
||||
List<JobCategory> categories = categoryMapper.selectList(new LambdaQueryWrapper<JobCategory>().in(JobCategory::getId, ids));
|
||||
return categories.stream().map(c -> {
|
||||
JobCategoryVo vo = new JobCategoryVo();
|
||||
Map<Long, Long> parentMap = allCategories.stream().collect(Collectors.toMap(JobCategory::getId, JobCategory::getParentId));
|
||||
|
||||
// 3. 从末级向上追溯,收集所有有效ID(含祖先)
|
||||
Set<Long> activeIds = new HashSet<>();
|
||||
for (Long id : leafActiveIds) {
|
||||
Long current = id;
|
||||
while (current != null && current != 0L && !activeIds.contains(current)) {
|
||||
activeIds.add(current);
|
||||
current = parentMap.get(current);
|
||||
}
|
||||
}
|
||||
return activeIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归构建子树
|
||||
*/
|
||||
private List<JobCategoryTreeVo> buildChildren(Long parentId, Map<Long, List<JobCategory>> childrenMap) {
|
||||
List<JobCategory> children = childrenMap.getOrDefault(parentId, Collections.emptyList());
|
||||
return children.stream().map(c -> {
|
||||
JobCategoryTreeVo vo = new JobCategoryTreeVo();
|
||||
vo.setId(c.getId());
|
||||
vo.setName(c.getName());
|
||||
vo.setLevel(c.getLevel());
|
||||
vo.setParentId(c.getParentId());
|
||||
List<JobCategoryTreeVo> subChildren = buildChildren(c.getId(), childrenMap);
|
||||
vo.setChildren(subChildren.isEmpty() ? null : subChildren);
|
||||
return vo;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user