release: 获取地区TREE

release-2024-08-08
huangyw 4 months ago
parent 3504afe58c
commit dbbdbe4665

@ -0,0 +1,45 @@
package com.luoo.user.controller;
import api.Result;
import com.luoo.user.service.RegionService;
import com.luoo.user.vo.RegionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @program: luoo_parent
* @description:
* @author: yawei.huang
* @create: 2024-07-30 11:25
**/
@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/region")
@Api(tags = "地区")
public class RegionController {
@Autowired
private RegionService regionService;
/**
* TREE
*
* @param level 2+ or 3++
* @return
*/
@ApiOperation(value = "获取地区TREE")
@RequestMapping("/tree/{level}")
public Result<List<RegionVo>> getRegionTreeList(@PathVariable Integer level) {
return Result.success(regionService.getRegionTreeList(level));
}
}

@ -1,10 +1,13 @@
package com.luoo.user.dao; package com.luoo.user.dao;
import com.luoo.user.pojo.Region; import com.luoo.user.pojo.Region;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface RegionDao extends JpaRepository<Region, String>, public interface RegionDao extends JpaRepository<Region, String>,
JpaSpecificationExecutor<Region> { JpaSpecificationExecutor<Region> {
List<Region> getRegionsByLevel(Integer level);
} }

@ -1,6 +1,18 @@
package com.luoo.user.service; package com.luoo.user.service;
import cn.hutool.core.util.ObjectUtil;
import com.luoo.user.dao.RegionDao;
import com.luoo.user.pojo.Region;
import com.luoo.user.vo.RegionVo;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/** /**
@ -13,4 +25,131 @@ import org.springframework.stereotype.Service;
@Slf4j @Slf4j
public class RegionService { public class RegionService {
public static final String REGION_TREE_LIST_FOR = "REGION_TREE_LIST_FOR_";
private final RegionDao regionDao;
private final RedisTemplate redisTemplate;
public RegionService(RegionDao regionDao, RedisTemplate redisTemplate) {
this.regionDao = regionDao;
this.redisTemplate = redisTemplate;
}
/**
* 2tree
* @return
*/
public List<RegionVo> getRegionTreeListForTwoLevels() {
List<RegionVo> o = (List<RegionVo>) redisTemplate.opsForValue().get(REGION_TREE_LIST_FOR + 2);
if(o == null) {
o = getRegionTreeList(2);
redisTemplate.opsForValue().set(REGION_TREE_LIST_FOR + 2, o);
}
return o;
}
/**
* tree
* @return
*/
public List<RegionVo> getRegionTreeListForThreeLevels() {
List<RegionVo> o = (List<RegionVo>) redisTemplate.opsForValue().get(REGION_TREE_LIST_FOR + 3);
if(o == null) {
o = getRegionTreeList(3);
redisTemplate.opsForValue().set(REGION_TREE_LIST_FOR + 3, o);
}
return o;
}
/**
* tree
*
* @param levels
* @return tree
*/
public List<RegionVo> getRegionTreeList(Integer levels) {
// 获取所有的Region
List<Region> regions = new ArrayList<>();
if (levels == 2) {
// 2级选项
List<Region> regions1 = regionDao.getRegionsByLevel(1);
List<Region> regions2 = regionDao.getRegionsByLevel(2);
regions.addAll(regions1);
regions.addAll(regions2);
} else if (levels == 3) {
// 3级选项
regions = regionDao.findAll();
}
// 把Region转换成RegionVo
List<RegionVo> regionVos = new ArrayList<>();
regions.forEach(region -> {
RegionVo regionVo = new RegionVo();
BeanUtils.copyProperties(region, regionVo);
regionVos.add(regionVo);
});
// Region的pid为父id递归把他们放到对应的父节点下, 最后将生成的数据放入result中
List<RegionVo> returnList = new ArrayList<RegionVo>();
List<Integer> tempList = regionVos.stream().map(RegionVo::getId).collect(Collectors.toList());
for (RegionVo regionVo : regionVos) {
// 如果是顶级节点, 遍历该父节点的所有子节点
if (!tempList.contains(regionVo.getPid())) {
recursionFn(regionVos, regionVo);
returnList.add(regionVo);
}
}
if (returnList.isEmpty()) {
returnList = regionVos;
}
return returnList;
}
/**
*
*/
private void recursionFn(List<RegionVo> list, RegionVo t) {
// 得到子节点列表
List<RegionVo> childList = getChildList(list, t);
t.setChildren(childList);
for (RegionVo tChild : childList) {
if (hasChild(list, tChild)) {
recursionFn(list, tChild);
}
}
}
/**
*
*/
private List<RegionVo> getChildList(List<RegionVo> list, RegionVo t) {
List<RegionVo> tlist = new ArrayList<RegionVo>();
Iterator<RegionVo> it = list.iterator();
while (it.hasNext()) {
RegionVo n = (RegionVo) it.next();
if ((n.getPid() != null) && n.getPid().equals(t.getId())) {
tlist.add(n);
}
}
return tlist;
}
/**
*
*/
private boolean hasChild(List<RegionVo> list, RegionVo t) {
return !getChildList(list, t).isEmpty();
}
} }

@ -0,0 +1,69 @@
package com.luoo.user.vo;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.util.List;
import javax.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
/**
* DTO for {@link com.luoo.user.pojo.Region}
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@Builder
public class RegionVo implements Serializable {
private Integer id;
@ApiModelProperty(value = "父id")
private Integer pid;
@Size(max = 100)
@ApiModelProperty(value = "简称")
private String shortname;
@Size(max = 100)
@ApiModelProperty(value = "名称")
private String name;
@Size(max = 255)
@ApiModelProperty(value = "全称")
private String mergerName;
@ApiModelProperty(value = "层级 1 2 3 省市区县")
private Integer level;
@Size(max = 100)
@ApiModelProperty(value = "拼音")
private String pinyin;
@Size(max = 100)
@ApiModelProperty(value = "长途区号")
private String code;
@Size(max = 100)
@ApiModelProperty(value = "邮编")
private String zipCode;
@Size(max = 50)
@ApiModelProperty(value = "首字母")
private String first;
@Size(max = 100)
@ApiModelProperty(value = "经度")
private String lng;
@Size(max = 100)
@ApiModelProperty(value = "纬度")
private String lat;
@ApiModelProperty(value = "子节点")
private List<RegionVo> children;
}
Loading…
Cancel
Save