添加行业默认过滤空数据
This commit is contained in:
@@ -21,36 +21,43 @@ public class IndustryController {
|
||||
|
||||
/**
|
||||
* 获取全部行业树(一级-二级嵌套)
|
||||
* @param includeEmpty 是否包含空行业(无公司关联),默认false(滤空)
|
||||
*/
|
||||
@GetMapping("/tree")
|
||||
public List<IndustryTreeVo> getIndustryTree() {
|
||||
return industryService.getIndustryTree();
|
||||
public List<IndustryTreeVo> getIndustryTree(@RequestParam(required = false) Boolean includeEmpty) {
|
||||
return industryService.getIndustryTree(includeEmpty);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按父级查询行业列表
|
||||
* @param parentId 父级ID,不传=查一级
|
||||
* @param includeEmpty 是否包含空行业(无公司关联),默认false(滤空)
|
||||
*/
|
||||
@GetMapping
|
||||
public List<IndustryVo> getIndustriesByParent(@RequestParam(required = false) Long parentId) {
|
||||
return industryService.getIndustriesByParent(parentId);
|
||||
public List<IndustryVo> getIndustriesByParent(@RequestParam(required = false) Long parentId,
|
||||
@RequestParam(required = false) Boolean includeEmpty) {
|
||||
return industryService.getIndustriesByParent(parentId, includeEmpty);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模糊搜索行业
|
||||
* @param keyword 行业名关键字
|
||||
* @param includeEmpty 是否包含空行业(无公司关联),默认false(滤空)
|
||||
*/
|
||||
@GetMapping("/search")
|
||||
public List<IndustryVo> searchIndustries(@RequestParam String keyword) {
|
||||
return industryService.searchIndustries(keyword);
|
||||
public List<IndustryVo> searchIndustries(@RequestParam String keyword,
|
||||
@RequestParam(required = false) Boolean includeEmpty) {
|
||||
return industryService.searchIndustries(keyword, includeEmpty);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量查询行业
|
||||
* @param ids 行业ID列表
|
||||
* @param includeEmpty 是否包含空行业(无公司关联),默认false(滤空)
|
||||
*/
|
||||
@PostMapping("/batch")
|
||||
public List<IndustryVo> getIndustriesByIds(@RequestBody List<Long> ids) {
|
||||
return industryService.getIndustriesByIds(ids);
|
||||
public List<IndustryVo> getIndustriesByIds(@RequestBody List<Long> 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 lombok.extern.slf4j.Slf4j;
|
||||
import org.jiayunet.mapper.CompanyMapper;
|
||||
import org.jiayunet.mapper.IndustryMapper;
|
||||
import org.jiayunet.pojo.po.Company;
|
||||
import org.jiayunet.pojo.po.Industry;
|
||||
import org.jiayunet.pojo.vo.IndustryTreeVo;
|
||||
import org.jiayunet.pojo.vo.IndustryVo;
|
||||
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_industry</p>
|
||||
* <p>主要功能:行业数据查询(树形、层级、搜索),支持滤空(过滤无公司关联的行业)</p>
|
||||
* <p>依赖:CompanyMapper(查询有公司关联的行业ID)</p>
|
||||
* <p>使用表:bg_industry、bg_company</p>
|
||||
*
|
||||
* @author zk
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class IndustryService {
|
||||
|
||||
@Autowired
|
||||
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> level1 = allIndustries.stream().filter(i -> i.getLevel() == 1).collect(Collectors.toList());
|
||||
Map<Long, List<Industry>> level2ByParent = allIndustries.stream().filter(i -> i.getLevel() == 2).collect(Collectors.groupingBy(Industry::getParentId));
|
||||
return level1.stream().map(ind1 -> {
|
||||
IndustryTreeVo vo1 = new IndustryTreeVo();
|
||||
vo1.setId(ind1.getId());
|
||||
vo1.setName(ind1.getName());
|
||||
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());
|
||||
if (!Boolean.TRUE.equals(includeEmpty)) {
|
||||
Set<Long> activeIds = getActiveIndustryIds(allIndustries);
|
||||
allIndustries = allIndustries.stream().filter(i -> activeIds.contains(i.getId())).toList();
|
||||
}
|
||||
Map<Long, List<Industry>> childrenMap = allIndustries.stream().collect(Collectors.groupingBy(Industry::getParentId));
|
||||
return buildChildren(0L, childrenMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按父级查询行业列表
|
||||
* <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;
|
||||
if (parentId == null) {
|
||||
industries = industryMapper.selectList(new LambdaQueryWrapper<Industry>().eq(Industry::getLevel, 1));
|
||||
} else {
|
||||
industries = industryMapper.selectList(new LambdaQueryWrapper<Industry>().eq(Industry::getParentId, parentId));
|
||||
}
|
||||
return industries.stream().map(i -> {
|
||||
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());
|
||||
return filterAndConvert(industries, includeEmpty);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模糊搜索行业
|
||||
* <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));
|
||||
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 -> {
|
||||
IndustryVo vo = new IndustryVo();
|
||||
vo.setId(i.getId());
|
||||
@@ -92,20 +102,47 @@ public class IndustryService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量查询行业
|
||||
* <p>根据ID列表批量查询行业详情</p>
|
||||
* 获取有公司关联的有效行业ID集合(含祖先链路)
|
||||
* <p>1. 查询bg_company中有效公司的distinct industryId</p>
|
||||
* <p>2. 从末级ID沿parentId向上追溯,构建完整有效ID集合</p>
|
||||
*
|
||||
* @param allIndustries 全量行业数据,传null则内部查询
|
||||
*/
|
||||
public List<IndustryVo> getIndustriesByIds(List<Long> ids) {
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
private Set<Long> getActiveIndustryIds(List<Industry> allIndustries) {
|
||||
// 1. 查询有公司关联的末级行业ID
|
||||
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));
|
||||
return industries.stream().map(i -> {
|
||||
IndustryVo vo = new IndustryVo();
|
||||
Map<Long, Long> parentMap = allIndustries.stream().collect(Collectors.toMap(Industry::getId, Industry::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<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.setName(i.getName());
|
||||
vo.setLevel(i.getLevel());
|
||||
vo.setParentId(i.getParentId());
|
||||
List<IndustryTreeVo> subChildren = buildChildren(i.getId(), childrenMap);
|
||||
vo.setChildren(subChildren.isEmpty() ? null : subChildren);
|
||||
return vo;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user