From 9a587222823cd2da827fd44b1813015d471d6983 Mon Sep 17 00:00:00 2001 From: zk Date: Fri, 29 May 2026 12:12:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=A1=8C=E4=B8=9A=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E8=BF=87=E6=BB=A4=E7=A9=BA=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/IndustryController.java | 23 +-- .../org/jiayunet/service/IndustryService.java | 133 +++++++++++------- 2 files changed, 100 insertions(+), 56 deletions(-) diff --git a/manager/src/main/java/org/jiayunet/controller/IndustryController.java b/manager/src/main/java/org/jiayunet/controller/IndustryController.java index c4aa23e..f88a53a 100644 --- a/manager/src/main/java/org/jiayunet/controller/IndustryController.java +++ b/manager/src/main/java/org/jiayunet/controller/IndustryController.java @@ -21,36 +21,43 @@ public class IndustryController { /** * 获取全部行业树(一级-二级嵌套) + * @param includeEmpty 是否包含空行业(无公司关联),默认false(滤空) */ @GetMapping("/tree") - public List getIndustryTree() { - return industryService.getIndustryTree(); + public List getIndustryTree(@RequestParam(required = false) Boolean includeEmpty) { + return industryService.getIndustryTree(includeEmpty); } /** * 按父级查询行业列表 * @param parentId 父级ID,不传=查一级 + * @param includeEmpty 是否包含空行业(无公司关联),默认false(滤空) */ @GetMapping - public List getIndustriesByParent(@RequestParam(required = false) Long parentId) { - return industryService.getIndustriesByParent(parentId); + public List 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 searchIndustries(@RequestParam String keyword) { - return industryService.searchIndustries(keyword); + public List searchIndustries(@RequestParam String keyword, + @RequestParam(required = false) Boolean includeEmpty) { + return industryService.searchIndustries(keyword, includeEmpty); } /** * 批量查询行业 * @param ids 行业ID列表 + * @param includeEmpty 是否包含空行业(无公司关联),默认false(滤空) */ @PostMapping("/batch") - public List getIndustriesByIds(@RequestBody List ids) { - return industryService.getIndustriesByIds(ids); + public List getIndustriesByIds(@RequestBody List ids, + @RequestParam(required = false) Boolean includeEmpty) { + return industryService.getIndustriesByIds(ids, includeEmpty); } } diff --git a/manager/src/main/java/org/jiayunet/service/IndustryService.java b/manager/src/main/java/org/jiayunet/service/IndustryService.java index ae34cc8..45f3bae 100644 --- a/manager/src/main/java/org/jiayunet/service/IndustryService.java +++ b/manager/src/main/java/org/jiayunet/service/IndustryService.java @@ -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; /** * 行业服务 - *

主要功能:行业数据查询(树形、层级、搜索)

- *

依赖:无

- *

使用表:bg_industry

+ *

主要功能:行业数据查询(树形、层级、搜索),支持滤空(过滤无公司关联的行业)

+ *

依赖:CompanyMapper(查询有公司关联的行业ID)

+ *

使用表:bg_industry、bg_company

* * @author zk */ @Slf4j @Service public class IndustryService { + @Autowired private IndustryMapper industryMapper; + @Autowired + private CompanyMapper companyMapper; + /** - * 获取全部行业树(一级-二级嵌套) - *

1. 查询全部行业 2. 按level分组 3. 构建二层树

+ * 获取全部行业树(递归构建,支持任意层级) + *

1. 查询全部行业 2. 滤空(可选) 3. 按parentId分组递归构建树

*/ - public List getIndustryTree() { + public List getIndustryTree(Boolean includeEmpty) { List allIndustries = industryMapper.selectList(null); - List level1 = allIndustries.stream().filter(i -> i.getLevel() == 1).collect(Collectors.toList()); - Map> 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 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 activeIds = getActiveIndustryIds(allIndustries); + allIndustries = allIndustries.stream().filter(i -> activeIds.contains(i.getId())).toList(); + } + Map> childrenMap = allIndustries.stream().collect(Collectors.groupingBy(Industry::getParentId)); + return buildChildren(0L, childrenMap); } /** * 按父级查询行业列表 - *

parentId为null查一级,否则查下一级

+ *

parentId为null查一级,否则查下一级;默认滤空

*/ - public List getIndustriesByParent(Long parentId) { + public List getIndustriesByParent(Long parentId, Boolean includeEmpty) { List industries; if (parentId == null) { industries = industryMapper.selectList(new LambdaQueryWrapper().eq(Industry::getLevel, 1)); } else { industries = industryMapper.selectList(new LambdaQueryWrapper().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); } /** * 模糊搜索行业 - *

按行业名关键字模糊查询

+ *

按行业名关键字模糊查询,默认滤空

*/ - public List searchIndustries(String keyword) { + public List searchIndustries(String keyword, Boolean includeEmpty) { List industries = industryMapper.selectList(new LambdaQueryWrapper().like(Industry::getName, keyword)); + return filterAndConvert(industries, includeEmpty); + } + + /** + * 批量查询行业 + *

根据ID列表批量查询行业详情,默认滤空

+ */ + public List getIndustriesByIds(List ids, Boolean includeEmpty) { + if (ids == null || ids.isEmpty()) { + return new ArrayList<>(); + } + List industries = industryMapper.selectList(new LambdaQueryWrapper().in(Industry::getId, ids)); + return filterAndConvert(industries, includeEmpty); + } + + // ==================== 私有方法 ==================== + + /** + * 滤空 + 转VO + */ + private List filterAndConvert(List industries, Boolean includeEmpty) { + if (!Boolean.TRUE.equals(includeEmpty)) { + Set 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 { } /** - * 批量查询行业 - *

根据ID列表批量查询行业详情

+ * 获取有公司关联的有效行业ID集合(含祖先链路) + *

1. 查询bg_company中有效公司的distinct industryId

+ *

2. 从末级ID沿parentId向上追溯,构建完整有效ID集合

+ * + * @param allIndustries 全量行业数据,传null则内部查询 */ - public List getIndustriesByIds(List ids) { - if (ids == null || ids.isEmpty()) { - return new ArrayList<>(); + private Set getActiveIndustryIds(List allIndustries) { + // 1. 查询有公司关联的末级行业ID + List companies = companyMapper.selectList(new LambdaQueryWrapper().select(Company::getIndustryId).eq(Company::getStatus, 1).groupBy(Company::getIndustryId)); + Set leafActiveIds = companies.stream().map(Company::getIndustryId).filter(Objects::nonNull).collect(Collectors.toSet()); + + // 2. 查全量行业,构建id->parentId映射 + if (allIndustries == null) { + allIndustries = industryMapper.selectList(null); } - List industries = industryMapper.selectList(new LambdaQueryWrapper().in(Industry::getId, ids)); - return industries.stream().map(i -> { - IndustryVo vo = new IndustryVo(); + Map parentMap = allIndustries.stream().collect(Collectors.toMap(Industry::getId, Industry::getParentId)); + + // 3. 从末级向上追溯,收集所有有效ID(含祖先) + Set 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 buildChildren(Long parentId, Map> childrenMap) { + List 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 subChildren = buildChildren(i.getId(), childrenMap); + vo.setChildren(subChildren.isEmpty() ? null : subChildren); return vo; }).collect(Collectors.toList()); }