添加行业相关
This commit is contained in:
@@ -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<IndustryTreeVo> getIndustryTree() {
|
||||
return industryService.getIndustryTree();
|
||||
}
|
||||
|
||||
/**
|
||||
* 按父级查询行业列表
|
||||
* @param parentId 父级ID,不传=查一级
|
||||
*/
|
||||
@GetMapping
|
||||
public List<IndustryVo> getIndustriesByParent(@RequestParam(required = false) Long parentId) {
|
||||
return industryService.getIndustriesByParent(parentId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模糊搜索行业
|
||||
* @param keyword 行业名关键字
|
||||
*/
|
||||
@GetMapping("/search")
|
||||
public List<IndustryVo> searchIndustries(@RequestParam String keyword) {
|
||||
return industryService.searchIndustries(keyword);
|
||||
}
|
||||
}
|
||||
@@ -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<IndustryTreeVo> children;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
* 行业服务
|
||||
* <p>主要功能:行业数据查询(树形、层级、搜索)</p>
|
||||
* <p>依赖:无</p>
|
||||
* <p>使用表:bg_industry</p>
|
||||
*
|
||||
* @author zk
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class IndustryService {
|
||||
@Autowired
|
||||
private IndustryMapper industryMapper;
|
||||
|
||||
/**
|
||||
* 获取全部行业树(一级-二级嵌套)
|
||||
* <p>1. 查询全部行业 2. 按level分组 3. 构建二层树</p>
|
||||
*/
|
||||
public List<IndustryTreeVo> getIndustryTree() {
|
||||
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());
|
||||
}
|
||||
|
||||
/**
|
||||
* 按父级查询行业列表
|
||||
* <p>parentId为null查一级,否则查下一级</p>
|
||||
*/
|
||||
public List<IndustryVo> getIndustriesByParent(Long parentId) {
|
||||
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());
|
||||
}
|
||||
|
||||
/**
|
||||
* 模糊搜索行业
|
||||
* <p>按行业名关键字模糊查询</p>
|
||||
*/
|
||||
public List<IndustryVo> searchIndustries(String keyword) {
|
||||
List<Industry> industries = industryMapper.selectList(new LambdaQueryWrapper<Industry>().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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user