添加地区管理
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package org.jiayunet.controller;
|
||||
|
||||
import org.jiayunet.pojo.vo.RegionTreeVo;
|
||||
import org.jiayunet.pojo.vo.RegionVo;
|
||||
import org.jiayunet.service.DictService;
|
||||
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("/dict")
|
||||
public class DictController {
|
||||
@Autowired
|
||||
private DictService dictService;
|
||||
|
||||
/**
|
||||
* 获取全部地区树(省-市-区三层嵌套)
|
||||
*/
|
||||
@GetMapping("/regions/tree")
|
||||
public List<RegionTreeVo> getRegionTree() {
|
||||
return dictService.getRegionTree();
|
||||
}
|
||||
|
||||
/**
|
||||
* 按父级查询地区列表
|
||||
* @param parentCode 父级编码,不传=查省级
|
||||
*/
|
||||
@GetMapping("/regions")
|
||||
public List<RegionVo> getRegionsByParent(@RequestParam(required = false) String parentCode) {
|
||||
return dictService.getRegionsByParent(parentCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模糊搜索地区
|
||||
* @param keyword 地区名关键字
|
||||
*/
|
||||
@GetMapping("/regions/search")
|
||||
public List<RegionVo> searchRegions(@RequestParam String keyword) {
|
||||
return dictService.searchRegions(keyword);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.jiayunet.pojo.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 地区树形VO
|
||||
*
|
||||
* @author zk
|
||||
*/
|
||||
@Data
|
||||
public class RegionTreeVo {
|
||||
/** 地区编码 */
|
||||
private String code;
|
||||
/** 地区名称 */
|
||||
private String name;
|
||||
/** 子级地区列表 */
|
||||
private List<RegionTreeVo> children;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.jiayunet.pojo.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 地区VO(平铺结构)
|
||||
*
|
||||
* @author zk
|
||||
*/
|
||||
@Data
|
||||
public class RegionVo {
|
||||
/** 地区编码 */
|
||||
private String code;
|
||||
/** 地区名称 */
|
||||
private String name;
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package org.jiayunet.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jiayunet.mapper.ChinaRegionsCodeMapper;
|
||||
import org.jiayunet.pojo.po.ChinaRegionsCode;
|
||||
import org.jiayunet.pojo.vo.RegionTreeVo;
|
||||
import org.jiayunet.pojo.vo.RegionVo;
|
||||
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_china_regions_code、bg_job_category、bg_industry</p>
|
||||
*
|
||||
* @author zk
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class DictService {
|
||||
@Autowired
|
||||
private ChinaRegionsCodeMapper regionMapper;
|
||||
|
||||
/**
|
||||
* 获取全部地区树(省-市-区三层嵌套)
|
||||
* <p>1. 查询全部地区 2. 按provinceCode/cityCode分组 3. 构建三层树</p>
|
||||
*/
|
||||
public List<RegionTreeVo> getRegionTree() {
|
||||
List<ChinaRegionsCode> allRegions = regionMapper.selectList(null);
|
||||
List<ChinaRegionsCode> provinces = allRegions.stream().filter(r -> r.getProvinceCode() == null).collect(Collectors.toList());
|
||||
Map<String, List<ChinaRegionsCode>> citiesByProvince = allRegions.stream().filter(r -> r.getProvinceCode() != null && r.getCityCode() == null).collect(Collectors.groupingBy(ChinaRegionsCode::getProvinceCode));
|
||||
Map<String, List<ChinaRegionsCode>> districtsByCity = allRegions.stream().filter(r -> r.getCityCode() != null).collect(Collectors.groupingBy(ChinaRegionsCode::getCityCode));
|
||||
return provinces.stream().map(province -> {
|
||||
RegionTreeVo provinceVo = new RegionTreeVo();
|
||||
provinceVo.setCode(province.getCode());
|
||||
provinceVo.setName(province.getName());
|
||||
List<ChinaRegionsCode> cities = citiesByProvince.getOrDefault(province.getCode(), new ArrayList<>());
|
||||
provinceVo.setChildren(cities.stream().map(city -> {
|
||||
RegionTreeVo cityVo = new RegionTreeVo();
|
||||
cityVo.setCode(city.getCode());
|
||||
cityVo.setName(city.getName());
|
||||
List<ChinaRegionsCode> districts = districtsByCity.getOrDefault(city.getCode(), new ArrayList<>());
|
||||
cityVo.setChildren(districts.stream().map(district -> {
|
||||
RegionTreeVo districtVo = new RegionTreeVo();
|
||||
districtVo.setCode(district.getCode());
|
||||
districtVo.setName(district.getName());
|
||||
districtVo.setChildren(null);
|
||||
return districtVo;
|
||||
}).collect(Collectors.toList()));
|
||||
return cityVo;
|
||||
}).collect(Collectors.toList()));
|
||||
return provinceVo;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 按父级查询地区列表
|
||||
* <p>parentCode为null查省级,否则查下一级(先尝试作为provinceCode查市,再尝试作为cityCode查区)</p>
|
||||
*/
|
||||
public List<RegionVo> getRegionsByParent(String parentCode) {
|
||||
List<ChinaRegionsCode> regions;
|
||||
if (parentCode == null) {
|
||||
regions = regionMapper.selectList(new LambdaQueryWrapper<ChinaRegionsCode>().isNull(ChinaRegionsCode::getProvinceCode));
|
||||
} else {
|
||||
regions = regionMapper.selectList(new LambdaQueryWrapper<ChinaRegionsCode>().eq(ChinaRegionsCode::getProvinceCode, parentCode).isNull(ChinaRegionsCode::getCityCode));
|
||||
if (regions.isEmpty()) {
|
||||
regions = regionMapper.selectList(new LambdaQueryWrapper<ChinaRegionsCode>().eq(ChinaRegionsCode::getCityCode, parentCode));
|
||||
}
|
||||
}
|
||||
return regions.stream().map(r -> {
|
||||
RegionVo vo = new RegionVo();
|
||||
vo.setCode(r.getCode());
|
||||
vo.setName(r.getName());
|
||||
return vo;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 模糊搜索地区
|
||||
* <p>按地区名关键字模糊查询</p>
|
||||
*/
|
||||
public List<RegionVo> searchRegions(String keyword) {
|
||||
List<ChinaRegionsCode> regions = regionMapper.selectList(new LambdaQueryWrapper<ChinaRegionsCode>().like(ChinaRegionsCode::getName, keyword));
|
||||
return regions.stream().map(r -> {
|
||||
RegionVo vo = new RegionVo();
|
||||
vo.setCode(r.getCode());
|
||||
vo.setName(r.getName());
|
||||
return vo;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user