添加岗位分类相关
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package org.jiayunet.controller;
|
||||
|
||||
import org.jiayunet.pojo.vo.JobCategoryTreeVo;
|
||||
import org.jiayunet.pojo.vo.JobCategoryVo;
|
||||
import org.jiayunet.service.JobCategoryService;
|
||||
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("/job-categories")
|
||||
public class JobCategoryController {
|
||||
@Autowired
|
||||
private JobCategoryService categoryService;
|
||||
|
||||
/**
|
||||
* 获取全部岗位分类树(一级-二级-三级嵌套)
|
||||
*/
|
||||
@GetMapping("/tree")
|
||||
public List<JobCategoryTreeVo> getCategoryTree() {
|
||||
return categoryService.getCategoryTree();
|
||||
}
|
||||
|
||||
/**
|
||||
* 按父级查询岗位分类列表
|
||||
* @param parentId 父级ID,不传=查一级
|
||||
*/
|
||||
@GetMapping
|
||||
public List<JobCategoryVo> getCategoriesByParent(@RequestParam(required = false) Long parentId) {
|
||||
return categoryService.getCategoriesByParent(parentId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模糊搜索岗位分类
|
||||
* @param keyword 分类名关键字
|
||||
*/
|
||||
@GetMapping("/search")
|
||||
public List<JobCategoryVo> searchCategories(@RequestParam String keyword) {
|
||||
return categoryService.searchCategories(keyword);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.jiayunet.pojo.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 岗位分类树形VO
|
||||
*
|
||||
* @author zk
|
||||
*/
|
||||
@Data
|
||||
public class JobCategoryTreeVo {
|
||||
/** 分类ID */
|
||||
private Long id;
|
||||
/** 分类名称 */
|
||||
private String name;
|
||||
/** 层级 */
|
||||
private Integer level;
|
||||
/** 子级分类列表 */
|
||||
private List<JobCategoryTreeVo> children;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.jiayunet.pojo.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 岗位分类VO(平铺结构)
|
||||
*
|
||||
* @author zk
|
||||
*/
|
||||
@Data
|
||||
public class JobCategoryVo {
|
||||
/** 分类ID */
|
||||
private Long id;
|
||||
/** 分类名称 */
|
||||
private String name;
|
||||
/** 层级 */
|
||||
private Integer level;
|
||||
/** 父级ID */
|
||||
private Long parentId;
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package org.jiayunet.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jiayunet.mapper.JobCategoryMapper;
|
||||
import org.jiayunet.pojo.po.JobCategory;
|
||||
import org.jiayunet.pojo.vo.JobCategoryTreeVo;
|
||||
import org.jiayunet.pojo.vo.JobCategoryVo;
|
||||
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_job_category</p>
|
||||
*
|
||||
* @author zk
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class JobCategoryService {
|
||||
@Autowired
|
||||
private JobCategoryMapper categoryMapper;
|
||||
|
||||
/**
|
||||
* 获取全部岗位分类树(一级-二级-三级嵌套)
|
||||
* <p>1. 查询全部分类 2. 按level分组 3. 构建三层树</p>
|
||||
*/
|
||||
public List<JobCategoryTreeVo> getCategoryTree() {
|
||||
List<JobCategory> allCategories = categoryMapper.selectList(null);
|
||||
List<JobCategory> level1 = allCategories.stream().filter(c -> c.getLevel() == 1).collect(Collectors.toList());
|
||||
Map<Long, List<JobCategory>> level2ByParent = allCategories.stream().filter(c -> c.getLevel() == 2).collect(Collectors.groupingBy(JobCategory::getParentId));
|
||||
Map<Long, List<JobCategory>> level3ByParent = allCategories.stream().filter(c -> c.getLevel() == 3).collect(Collectors.groupingBy(JobCategory::getParentId));
|
||||
return level1.stream().map(cat1 -> {
|
||||
JobCategoryTreeVo vo1 = new JobCategoryTreeVo();
|
||||
vo1.setId(cat1.getId());
|
||||
vo1.setName(cat1.getName());
|
||||
vo1.setLevel(cat1.getLevel());
|
||||
List<JobCategory> level2List = level2ByParent.getOrDefault(cat1.getId(), new ArrayList<>());
|
||||
vo1.setChildren(level2List.stream().map(cat2 -> {
|
||||
JobCategoryTreeVo vo2 = new JobCategoryTreeVo();
|
||||
vo2.setId(cat2.getId());
|
||||
vo2.setName(cat2.getName());
|
||||
vo2.setLevel(cat2.getLevel());
|
||||
List<JobCategory> level3List = level3ByParent.getOrDefault(cat2.getId(), new ArrayList<>());
|
||||
vo2.setChildren(level3List.stream().map(cat3 -> {
|
||||
JobCategoryTreeVo vo3 = new JobCategoryTreeVo();
|
||||
vo3.setId(cat3.getId());
|
||||
vo3.setName(cat3.getName());
|
||||
vo3.setLevel(cat3.getLevel());
|
||||
vo3.setChildren(null);
|
||||
return vo3;
|
||||
}).collect(Collectors.toList()));
|
||||
return vo2;
|
||||
}).collect(Collectors.toList()));
|
||||
return vo1;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 按父级查询岗位分类列表
|
||||
* <p>parentId为null查一级,否则查下一级</p>
|
||||
*/
|
||||
public List<JobCategoryVo> getCategoriesByParent(Long parentId) {
|
||||
List<JobCategory> categories;
|
||||
if (parentId == null) {
|
||||
categories = categoryMapper.selectList(new LambdaQueryWrapper<JobCategory>().eq(JobCategory::getLevel, 1));
|
||||
} else {
|
||||
categories = categoryMapper.selectList(new LambdaQueryWrapper<JobCategory>().eq(JobCategory::getParentId, parentId));
|
||||
}
|
||||
return categories.stream().map(c -> {
|
||||
JobCategoryVo vo = new JobCategoryVo();
|
||||
vo.setId(c.getId());
|
||||
vo.setName(c.getName());
|
||||
vo.setLevel(c.getLevel());
|
||||
vo.setParentId(c.getParentId());
|
||||
return vo;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 模糊搜索岗位分类
|
||||
* <p>按分类名关键字模糊查询</p>
|
||||
*/
|
||||
public List<JobCategoryVo> searchCategories(String keyword) {
|
||||
List<JobCategory> categories = categoryMapper.selectList(new LambdaQueryWrapper<JobCategory>().like(JobCategory::getName, keyword));
|
||||
return categories.stream().map(c -> {
|
||||
JobCategoryVo vo = new JobCategoryVo();
|
||||
vo.setId(c.getId());
|
||||
vo.setName(c.getName());
|
||||
vo.setLevel(c.getLevel());
|
||||
vo.setParentId(c.getParentId());
|
||||
return vo;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user