From de7dd99da67ff3c97195be688018ce7bc8cacf8d Mon Sep 17 00:00:00 2001 From: zk Date: Fri, 20 Mar 2026 14:35:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=A1=8C=E4=B8=9A=E7=9B=B8?= =?UTF-8?q?=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/IndustryController.java | 50 ++++++++++ .../org/jiayunet/pojo/vo/IndustryTreeVo.java | 22 +++++ .../java/org/jiayunet/pojo/vo/IndustryVo.java | 20 ++++ .../org/jiayunet/service/IndustryService.java | 93 +++++++++++++++++++ 4 files changed, 185 insertions(+) create mode 100644 manager/src/main/java/org/jiayunet/controller/IndustryController.java create mode 100644 manager/src/main/java/org/jiayunet/pojo/vo/IndustryTreeVo.java create mode 100644 manager/src/main/java/org/jiayunet/pojo/vo/IndustryVo.java create mode 100644 manager/src/main/java/org/jiayunet/service/IndustryService.java diff --git a/manager/src/main/java/org/jiayunet/controller/IndustryController.java b/manager/src/main/java/org/jiayunet/controller/IndustryController.java new file mode 100644 index 0000000..2b14d3f --- /dev/null +++ b/manager/src/main/java/org/jiayunet/controller/IndustryController.java @@ -0,0 +1,50 @@ +package org.jiayunet.controller; + +import org.jiayunet.pojo.vo.IndustryTreeVo; +import org.jiayunet.pojo.vo.IndustryVo; +import org.jiayunet.service.IndustryService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +/** + * 行业接口 + * + * @author zk + */ +@RestController +@RequestMapping("/industries") +public class IndustryController { + @Autowired + private IndustryService industryService; + + /** + * 获取全部行业树(一级-二级嵌套) + */ + @GetMapping("/tree") + public List getIndustryTree() { + return industryService.getIndustryTree(); + } + + /** + * 按父级查询行业列表 + * @param parentId 父级ID,不传=查一级 + */ + @GetMapping + public List getIndustriesByParent(@RequestParam(required = false) Long parentId) { + return industryService.getIndustriesByParent(parentId); + } + + /** + * 模糊搜索行业 + * @param keyword 行业名关键字 + */ + @GetMapping("/search") + public List searchIndustries(@RequestParam String keyword) { + return industryService.searchIndustries(keyword); + } +} diff --git a/manager/src/main/java/org/jiayunet/pojo/vo/IndustryTreeVo.java b/manager/src/main/java/org/jiayunet/pojo/vo/IndustryTreeVo.java new file mode 100644 index 0000000..184c26c --- /dev/null +++ b/manager/src/main/java/org/jiayunet/pojo/vo/IndustryTreeVo.java @@ -0,0 +1,22 @@ +package org.jiayunet.pojo.vo; + +import lombok.Data; + +import java.util.List; + +/** + * 行业树形VO + * + * @author zk + */ +@Data +public class IndustryTreeVo { + /** 行业ID */ + private Long id; + /** 行业名称 */ + private String name; + /** 层级 */ + private Integer level; + /** 子级行业列表 */ + private List children; +} diff --git a/manager/src/main/java/org/jiayunet/pojo/vo/IndustryVo.java b/manager/src/main/java/org/jiayunet/pojo/vo/IndustryVo.java new file mode 100644 index 0000000..18e3ad9 --- /dev/null +++ b/manager/src/main/java/org/jiayunet/pojo/vo/IndustryVo.java @@ -0,0 +1,20 @@ +package org.jiayunet.pojo.vo; + +import lombok.Data; + +/** + * 行业VO(平铺结构) + * + * @author zk + */ +@Data +public class IndustryVo { + /** 行业ID */ + private Long id; + /** 行业名称 */ + private String name; + /** 层级 */ + private Integer level; + /** 父级ID */ + private Long parentId; +} diff --git a/manager/src/main/java/org/jiayunet/service/IndustryService.java b/manager/src/main/java/org/jiayunet/service/IndustryService.java new file mode 100644 index 0000000..64b698e --- /dev/null +++ b/manager/src/main/java/org/jiayunet/service/IndustryService.java @@ -0,0 +1,93 @@ +package org.jiayunet.service; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import lombok.extern.slf4j.Slf4j; +import org.jiayunet.mapper.IndustryMapper; +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.stream.Collectors; + +/** + * 行业服务 + *

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

+ *

依赖:无

+ *

使用表:bg_industry

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

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

+ */ + public List getIndustryTree() { + 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()); + } + + /** + * 按父级查询行业列表 + *

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

+ */ + public List getIndustriesByParent(Long parentId) { + 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()); + } + + /** + * 模糊搜索行业 + *

按行业名关键字模糊查询

+ */ + public List searchIndustries(String keyword) { + List industries = industryMapper.selectList(new LambdaQueryWrapper().like(Industry::getName, keyword)); + 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()); + } +}