| | |
| | | return treeData; |
| | | } |
| | | |
| | | @Override |
| | | public List<Region> addressTree1(List<String> cityCodeList) { |
| | | // 获取所有地区信息 省市区三级 |
| | | List<Region> regions = getReginList(cityCodeList); |
| | | // 所有地区 |
| | | Map<String, Region> courseTypeMap = regions.stream(). |
| | | collect(Collectors.toMap(region -> region.getId().toString() |
| | | , region -> region)); |
| | | redisTemplate.opsForHash().putAll(RedisConstants.ADDRESS_TREE, courseTypeMap); |
| | | redisTemplate.expire(RedisConstants.ADDRESS_TREE, 30, TimeUnit.MINUTES); |
| | | // 生成map集合 |
| | | Map<Integer, Region> map = regions.stream().collect(Collectors.toMap(Region::getId, region -> region)); |
| | | // 存放无限级树 |
| | | List<Region> treeData = new ArrayList<>(); |
| | | // 遍历地区集合 |
| | | regions.forEach(e -> { |
| | | if (e.getParentId() == null || e.getParentId().equals(0)||!cityCodeList.isEmpty()) { |
| | | treeData.add(e); |
| | | } else { |
| | | Region region = map.get(e.getParentId()); |
| | | region.getChildren().add(e); |
| | | } |
| | | }); |
| | | for (Region region : treeData) { |
| | | List<Region> children = region.getChildren(); |
| | | for (Region child : children) { |
| | | child.setChildren(new ArrayList<>()); |
| | | } |
| | | } |
| | | return treeData; |
| | | } |
| | | |
| | | /** |
| | | * 获取redis数据进行封装 |
| | | */ |
| | | private List<Region> getReginList(List<String> cityCodeList) { |
| | | List<Region> regions; |
| | | if (cityCodeList==null) { |
| | | if (cityCodeList==null||cityCodeList.isEmpty()) { |
| | | regions = this.list(); |
| | | } else { |
| | | regions = this.lambdaQuery() |