diff --git a/luoo_user/src/main/java/com/luoo/user/controller/RegionController.java b/luoo_user/src/main/java/com/luoo/user/controller/RegionController.java new file mode 100644 index 0000000..346543a --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/controller/RegionController.java @@ -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> getRegionTreeList(@PathVariable Integer level) { + return Result.success(regionService.getRegionTreeList(level)); + } + +} diff --git a/luoo_user/src/main/java/com/luoo/user/dao/RegionDao.java b/luoo_user/src/main/java/com/luoo/user/dao/RegionDao.java index 8481e07..1a3e89e 100644 --- a/luoo_user/src/main/java/com/luoo/user/dao/RegionDao.java +++ b/luoo_user/src/main/java/com/luoo/user/dao/RegionDao.java @@ -1,10 +1,13 @@ package com.luoo.user.dao; import com.luoo.user.pojo.Region; +import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; public interface RegionDao extends JpaRepository, JpaSpecificationExecutor { + List getRegionsByLevel(Integer level); + } diff --git a/luoo_user/src/main/java/com/luoo/user/service/RegionService.java b/luoo_user/src/main/java/com/luoo/user/service/RegionService.java index e9b4765..056d5da 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/RegionService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/RegionService.java @@ -1,6 +1,18 @@ 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 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; /** @@ -13,4 +25,131 @@ import org.springframework.stereotype.Service; @Slf4j 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; + } + + /** + * 获取2级地区tree + * @return + */ + public List getRegionTreeListForTwoLevels() { + + List o = (List) 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 getRegionTreeListForThreeLevels() { + + List o = (List) 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 getRegionTreeList(Integer levels) { + + // 获取所有的Region + List regions = new ArrayList<>(); + + if (levels == 2) { + // 2级选项 + List regions1 = regionDao.getRegionsByLevel(1); + List regions2 = regionDao.getRegionsByLevel(2); + regions.addAll(regions1); + regions.addAll(regions2); + } else if (levels == 3) { + // 3级选项 + regions = regionDao.findAll(); + } + + // 把Region转换成RegionVo + List regionVos = new ArrayList<>(); + regions.forEach(region -> { + RegionVo regionVo = new RegionVo(); + BeanUtils.copyProperties(region, regionVo); + regionVos.add(regionVo); + }); + + // Region的pid为父id,递归把他们放到对应的父节点下, 最后将生成的数据放入result中 + List returnList = new ArrayList(); + List 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 list, RegionVo t) { + // 得到子节点列表 + List childList = getChildList(list, t); + t.setChildren(childList); + for (RegionVo tChild : childList) { + if (hasChild(list, tChild)) { + recursionFn(list, tChild); + } + } + } + + /** + * 得到子节点列表 + */ + private List getChildList(List list, RegionVo t) { + List tlist = new ArrayList(); + Iterator 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 list, RegionVo t) { + return !getChildList(list, t).isEmpty(); + } + + } diff --git a/luoo_user/src/main/java/com/luoo/user/vo/RegionVo.java b/luoo_user/src/main/java/com/luoo/user/vo/RegionVo.java new file mode 100644 index 0000000..de5405a --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/vo/RegionVo.java @@ -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 children; +} \ No newline at end of file