添加行业默认过滤空数据

This commit is contained in:
zk
2026-05-29 12:12:29 +08:00
parent 9451578765
commit 9a58722282
2 changed files with 100 additions and 56 deletions
@@ -21,36 +21,43 @@ public class IndustryController {
/** /**
* 获取全部行业树(一级-二级嵌套) * 获取全部行业树(一级-二级嵌套)
* @param includeEmpty 是否包含空行业(无公司关联),默认false(滤空)
*/ */
@GetMapping("/tree") @GetMapping("/tree")
public List<IndustryTreeVo> getIndustryTree() { public List<IndustryTreeVo> getIndustryTree(@RequestParam(required = false) Boolean includeEmpty) {
return industryService.getIndustryTree(); return industryService.getIndustryTree(includeEmpty);
} }
/** /**
* 按父级查询行业列表 * 按父级查询行业列表
* @param parentId 父级ID,不传=查一级 * @param parentId 父级ID,不传=查一级
* @param includeEmpty 是否包含空行业(无公司关联),默认false(滤空)
*/ */
@GetMapping @GetMapping
public List<IndustryVo> getIndustriesByParent(@RequestParam(required = false) Long parentId) { public List<IndustryVo> getIndustriesByParent(@RequestParam(required = false) Long parentId,
return industryService.getIndustriesByParent(parentId); @RequestParam(required = false) Boolean includeEmpty) {
return industryService.getIndustriesByParent(parentId, includeEmpty);
} }
/** /**
* 模糊搜索行业 * 模糊搜索行业
* @param keyword 行业名关键字 * @param keyword 行业名关键字
* @param includeEmpty 是否包含空行业(无公司关联),默认false(滤空)
*/ */
@GetMapping("/search") @GetMapping("/search")
public List<IndustryVo> searchIndustries(@RequestParam String keyword) { public List<IndustryVo> searchIndustries(@RequestParam String keyword,
return industryService.searchIndustries(keyword); @RequestParam(required = false) Boolean includeEmpty) {
return industryService.searchIndustries(keyword, includeEmpty);
} }
/** /**
* 批量查询行业 * 批量查询行业
* @param ids 行业ID列表 * @param ids 行业ID列表
* @param includeEmpty 是否包含空行业(无公司关联),默认false(滤空)
*/ */
@PostMapping("/batch") @PostMapping("/batch")
public List<IndustryVo> getIndustriesByIds(@RequestBody List<Long> ids) { public List<IndustryVo> getIndustriesByIds(@RequestBody List<Long> ids,
return industryService.getIndustriesByIds(ids); @RequestParam(required = false) Boolean includeEmpty) {
return industryService.getIndustriesByIds(ids, includeEmpty);
} }
} }
@@ -2,85 +2,95 @@ package org.jiayunet.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.jiayunet.mapper.CompanyMapper;
import org.jiayunet.mapper.IndustryMapper; import org.jiayunet.mapper.IndustryMapper;
import org.jiayunet.pojo.po.Company;
import org.jiayunet.pojo.po.Industry; import org.jiayunet.pojo.po.Industry;
import org.jiayunet.pojo.vo.IndustryTreeVo; import org.jiayunet.pojo.vo.IndustryTreeVo;
import org.jiayunet.pojo.vo.IndustryVo; import org.jiayunet.pojo.vo.IndustryVo;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
* 行业服务 * 行业服务
* <p>主要功能:行业数据查询(树形、层级、搜索)</p> * <p>主要功能:行业数据查询(树形、层级、搜索),支持滤空(过滤无公司关联的行业)</p>
* <p>依赖:</p> * <p>依赖:CompanyMapper(查询有公司关联的行业ID)</p>
* <p>使用表:bg_industry</p> * <p>使用表:bg_industry、bg_company</p>
* *
* @author zk * @author zk
*/ */
@Slf4j @Slf4j
@Service @Service
public class IndustryService { public class IndustryService {
@Autowired @Autowired
private IndustryMapper industryMapper; private IndustryMapper industryMapper;
@Autowired
private CompanyMapper companyMapper;
/** /**
* 获取全部行业树(一级-二级嵌套 * 获取全部行业树(递归构建,支持任意层级
* <p>1. 查询全部行业 2. 按level分组 3. 构建二层树</p> * <p>1. 查询全部行业 2. 滤空(可选) 3. 按parentId分组递归构建树</p>
*/ */
public List<IndustryTreeVo> getIndustryTree() { public List<IndustryTreeVo> getIndustryTree(Boolean includeEmpty) {
List<Industry> allIndustries = industryMapper.selectList(null); List<Industry> allIndustries = industryMapper.selectList(null);
List<Industry> level1 = allIndustries.stream().filter(i -> i.getLevel() == 1).collect(Collectors.toList()); if (!Boolean.TRUE.equals(includeEmpty)) {
Map<Long, List<Industry>> level2ByParent = allIndustries.stream().filter(i -> i.getLevel() == 2).collect(Collectors.groupingBy(Industry::getParentId)); Set<Long> activeIds = getActiveIndustryIds(allIndustries);
return level1.stream().map(ind1 -> { allIndustries = allIndustries.stream().filter(i -> activeIds.contains(i.getId())).toList();
IndustryTreeVo vo1 = new IndustryTreeVo(); }
vo1.setId(ind1.getId()); Map<Long, List<Industry>> childrenMap = allIndustries.stream().collect(Collectors.groupingBy(Industry::getParentId));
vo1.setName(ind1.getName()); return buildChildren(0L, childrenMap);
vo1.setLevel(ind1.getLevel());
List<Industry> level2List = level2ByParent.getOrDefault(ind1.getId(), new ArrayList<>());
vo1.setChildren(level2List.stream().map(ind2 -> {
IndustryTreeVo vo2 = new IndustryTreeVo();
vo2.setId(ind2.getId());
vo2.setName(ind2.getName());
vo2.setLevel(ind2.getLevel());
vo2.setChildren(null);
return vo2;
}).collect(Collectors.toList()));
return vo1;
}).collect(Collectors.toList());
} }
/** /**
* 按父级查询行业列表 * 按父级查询行业列表
* <p>parentId为null查一级,否则查下一级</p> * <p>parentId为null查一级,否则查下一级;默认滤空</p>
*/ */
public List<IndustryVo> getIndustriesByParent(Long parentId) { public List<IndustryVo> getIndustriesByParent(Long parentId, Boolean includeEmpty) {
List<Industry> industries; List<Industry> industries;
if (parentId == null) { if (parentId == null) {
industries = industryMapper.selectList(new LambdaQueryWrapper<Industry>().eq(Industry::getLevel, 1)); industries = industryMapper.selectList(new LambdaQueryWrapper<Industry>().eq(Industry::getLevel, 1));
} else { } else {
industries = industryMapper.selectList(new LambdaQueryWrapper<Industry>().eq(Industry::getParentId, parentId)); industries = industryMapper.selectList(new LambdaQueryWrapper<Industry>().eq(Industry::getParentId, parentId));
} }
return industries.stream().map(i -> { return filterAndConvert(industries, includeEmpty);
IndustryVo vo = new IndustryVo();
vo.setId(i.getId());
vo.setName(i.getName());
vo.setLevel(i.getLevel());
vo.setParentId(i.getParentId());
return vo;
}).collect(Collectors.toList());
} }
/** /**
* 模糊搜索行业 * 模糊搜索行业
* <p>按行业名关键字模糊查询</p> * <p>按行业名关键字模糊查询,默认滤空</p>
*/ */
public List<IndustryVo> searchIndustries(String keyword) { public List<IndustryVo> searchIndustries(String keyword, Boolean includeEmpty) {
List<Industry> industries = industryMapper.selectList(new LambdaQueryWrapper<Industry>().like(Industry::getName, keyword)); List<Industry> industries = industryMapper.selectList(new LambdaQueryWrapper<Industry>().like(Industry::getName, keyword));
return filterAndConvert(industries, includeEmpty);
}
/**
* 批量查询行业
* <p>根据ID列表批量查询行业详情,默认滤空</p>
*/
public List<IndustryVo> getIndustriesByIds(List<Long> ids, Boolean includeEmpty) {
if (ids == null || ids.isEmpty()) {
return new ArrayList<>();
}
List<Industry> industries = industryMapper.selectList(new LambdaQueryWrapper<Industry>().in(Industry::getId, ids));
return filterAndConvert(industries, includeEmpty);
}
// ==================== 私有方法 ====================
/**
* 滤空 + 转VO
*/
private List<IndustryVo> filterAndConvert(List<Industry> industries, Boolean includeEmpty) {
if (!Boolean.TRUE.equals(includeEmpty)) {
Set<Long> activeIds = getActiveIndustryIds(null);
industries = industries.stream().filter(i -> activeIds.contains(i.getId())).toList();
}
return industries.stream().map(i -> { return industries.stream().map(i -> {
IndustryVo vo = new IndustryVo(); IndustryVo vo = new IndustryVo();
vo.setId(i.getId()); vo.setId(i.getId());
@@ -92,20 +102,47 @@ public class IndustryService {
} }
/** /**
* 批量查询行业 * 获取有公司关联的有效行业ID集合(含祖先链路)
* <p>根据ID列表批量查询行业详情</p> * <p>1. 查询bg_company中有效公司的distinct industryId</p>
* <p>2. 从末级ID沿parentId向上追溯,构建完整有效ID集合</p>
*
* @param allIndustries 全量行业数据,传null则内部查询
*/ */
public List<IndustryVo> getIndustriesByIds(List<Long> ids) { private Set<Long> getActiveIndustryIds(List<Industry> allIndustries) {
if (ids == null || ids.isEmpty()) { // 1. 查询有公司关联的末级行业ID
return new ArrayList<>(); List<Company> companies = companyMapper.selectList(new LambdaQueryWrapper<Company>().select(Company::getIndustryId).eq(Company::getStatus, 1).groupBy(Company::getIndustryId));
Set<Long> leafActiveIds = companies.stream().map(Company::getIndustryId).filter(Objects::nonNull).collect(Collectors.toSet());
// 2. 查全量行业,构建id->parentId映射
if (allIndustries == null) {
allIndustries = industryMapper.selectList(null);
} }
List<Industry> industries = industryMapper.selectList(new LambdaQueryWrapper<Industry>().in(Industry::getId, ids)); Map<Long, Long> parentMap = allIndustries.stream().collect(Collectors.toMap(Industry::getId, Industry::getParentId));
return industries.stream().map(i -> {
IndustryVo vo = new IndustryVo(); // 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<IndustryTreeVo> buildChildren(Long parentId, Map<Long, List<Industry>> childrenMap) {
List<Industry> children = childrenMap.getOrDefault(parentId, Collections.emptyList());
return children.stream().map(i -> {
IndustryTreeVo vo = new IndustryTreeVo();
vo.setId(i.getId()); vo.setId(i.getId());
vo.setName(i.getName()); vo.setName(i.getName());
vo.setLevel(i.getLevel()); vo.setLevel(i.getLevel());
vo.setParentId(i.getParentId()); List<IndustryTreeVo> subChildren = buildChildren(i.getId(), childrenMap);
vo.setChildren(subChildren.isEmpty() ? null : subChildren);
return vo; return vo;
}).collect(Collectors.toList()); }).collect(Collectors.toList());
} }