| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.reflect.TypeToken; |
| | | import com.mzl.flower.base.cache.StringCacheClient; |
| | | import com.mzl.flower.config.exception.ValidationException; |
| | | import com.mzl.flower.config.security.SecurityUtils; |
| | | import com.mzl.flower.dto.map.tengxun.TengxunDistrict; |
| | | import com.mzl.flower.dto.request.district.CreateDistrictTengxunDTO; |
| | | import com.mzl.flower.dto.response.district.DistrictTengxunVO; |
| | | import com.mzl.flower.dto.response.system.AreaAppDTO; |
| | | import com.mzl.flower.dto.response.system.AreaAppTmpDTO; |
| | | import com.mzl.flower.dto.response.system.AreaDTO; |
| | | import com.mzl.flower.entity.district.DistrictTengxunDO; |
| | | import com.mzl.flower.entity.district.DistrictTengxunDO; |
| | |
| | | |
| | | @Autowired |
| | | private StringCacheClient stringCacheClient; |
| | | |
| | | @Autowired |
| | | private Gson gson; |
| | | @Override |
| | | public void refreshDistrict() { |
| | | List<DistrictTengxunDO> districtTengxunDOList = getAllDistrict(); |
| | |
| | | } |
| | | |
| | | // 映射当前节点 |
| | | AreaDTO targetNode = new AreaDTO(sourceNode.getName(), sourceNode.getName()); |
| | | AreaDTO targetNode = new AreaDTO(sourceNode.getCode(), sourceNode.getName()); |
| | | |
| | | // 递归映射子节点 |
| | | if (sourceNode.getChildren() != null) { |
| | |
| | | return targetNode; |
| | | } |
| | | |
| | | public static AreaDTO transformCodeEqName(DistrictTengxunVO sourceNode) { |
| | | if (sourceNode == null) { |
| | | return null; |
| | | } |
| | | |
| | | // 映射当前节点 |
| | | AreaDTO targetNode = new AreaDTO(sourceNode.getName(), sourceNode.getName()); |
| | | |
| | | // 递归映射子节点 |
| | | if (sourceNode.getChildren() != null) { |
| | | for (DistrictTengxunVO child : sourceNode.getChildren()) { |
| | | targetNode.getChildren().add(transformCodeEqName(child)); |
| | | } |
| | | } |
| | | |
| | | return targetNode; |
| | | } |
| | | |
| | | public static AreaAppTmpDTO transformApp(DistrictTengxunVO sourceNode,int level) { |
| | | if (sourceNode == null) { |
| | | return null; |
| | | } |
| | | |
| | | // 映射当前节点 |
| | | AreaAppTmpDTO targetNode = new AreaAppTmpDTO(sourceNode.getId(), level+"", sourceNode.getName(), sourceNode.getParentId()); |
| | | |
| | | // 递归映射子节点 |
| | | if (sourceNode.getChildren() != null) { |
| | | for (DistrictTengxunVO child : sourceNode.getChildren()) { |
| | | targetNode.getChildren().add(transformApp(child,level++)); |
| | | } |
| | | } |
| | | |
| | | return targetNode; |
| | | } |
| | | |
| | | public static AreaAppDTO transformApp2(DistrictTengxunVO sourceNode) { |
| | | if (sourceNode == null) { |
| | | return null; |
| | | } |
| | | |
| | | // 映射当前节点 |
| | | AreaAppDTO targetNode = new AreaAppDTO(sourceNode.getCode(), sourceNode.getName()); |
| | | |
| | | // 递归映射子节点 |
| | | if (sourceNode.getChildren() != null) { |
| | | for (DistrictTengxunVO child : sourceNode.getChildren()) { |
| | | |
| | | targetNode.getChildren().add(transformApp2(child)); |
| | | } |
| | | } |
| | | |
| | | return targetNode; |
| | | } |
| | | |
| | | @Override |
| | | public List<DistrictTengxunDO> getAllDistrict() { |
| | |
| | | @Override |
| | | public void clearChineseDataCache() { |
| | | stringCacheClient.delete("CHINA_AREA_DATA"); |
| | | stringCacheClient.delete("CHINA_AREA_DATA_APP"); |
| | | stringCacheClient.delete("CHINA_AREA_DATA_WEB"); |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public String getChineseAreaForApp() { |
| | | String result = stringCacheClient.get("CHINA_AREA_DATA_APP"); |
| | | if (StringUtils.isNotBlank(result)) { |
| | | return result; |
| | | } else { |
| | | // 打印每个根节点对应的树形 JSON |
| | | Gson gson = new Gson(); |
| | | |
| | | List<DistrictTengxunDO> districtTengxunDOList = getAllDistrict(); |
| | | List<DistrictTengxunVO> districtTengxunVOList = ConverterUtil.transList(districtTengxunDOList, DistrictTengxunVO.class); |
| | | |
| | | if (CollectionUtils.isNotEmpty(districtTengxunDOList)) { |
| | | // 找出所有 parent_id 为 null 的节点作为根节点集合 |
| | | List<DistrictTengxunVO> roots = districtTengxunVOList.stream() |
| | | .filter(districtTengxunVO -> districtTengxunVO.getParentId() == null) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 遍历根节点,构建每棵树形结构 |
| | | List<AreaAppTmpDTO> areaDTOList = new ArrayList<>(); |
| | | for (DistrictTengxunVO rootNode : roots) { |
| | | // 构建树形结构 |
| | | buildTree(rootNode, districtTengxunVOList); |
| | | |
| | | // 将树形转换成前端需要的结构 |
| | | AreaAppTmpDTO areaDTO = transformApp(rootNode,1); |
| | | areaDTOList.add(areaDTO); |
| | | } |
| | | result=gson.toJson(areaDTOList); |
| | | if(StringUtils.isNotBlank(result)){ |
| | | stringCacheClient.set("CHINA_AREA_DATA_APP", result); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | return ""; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public String getChineseAreaForWeb() { |
| | | String result = stringCacheClient.get("CHINA_AREA_DATA_WEB"); |
| | | if (StringUtils.isNotBlank(result)) { |
| | | return result; |
| | | } else { |
| | | // 打印每个根节点对应的树形 JSON |
| | | Gson gson = new Gson(); |
| | | |
| | | List<DistrictTengxunDO> districtTengxunDOList = getAllDistrict(); |
| | | List<DistrictTengxunVO> districtTengxunVOList = ConverterUtil.transList(districtTengxunDOList, DistrictTengxunVO.class); |
| | | |
| | | if (CollectionUtils.isNotEmpty(districtTengxunDOList)) { |
| | | // 找出所有 parent_id 为 null 的节点作为根节点集合 |
| | | List<DistrictTengxunVO> roots = districtTengxunVOList.stream() |
| | | .filter(districtTengxunVO -> districtTengxunVO.getParentId() == null) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 遍历根节点,构建每棵树形结构 |
| | | List<AreaDTO> areaDTOList = new ArrayList<>(); |
| | | for (DistrictTengxunVO rootNode : roots) { |
| | | // 构建树形结构 |
| | | buildTree(rootNode, districtTengxunVOList); |
| | | |
| | | // 将树形转换成前端需要的结构 |
| | | AreaDTO areaDTO = transformCodeEqName(rootNode); |
| | | areaDTOList.add(areaDTO); |
| | | } |
| | | |
| | | List<AreaDTO> formatterDistrict =normalizeTreeToThreeLevels(areaDTOList); |
| | | // result=gson.toJson(areaDTOList); |
| | | result=gson.toJson(formatterDistrict); |
| | | if(StringUtils.isNotBlank(result)){ |
| | | stringCacheClient.set("CHINA_AREA_DATA_WEB", result); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | return ""; |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | public static List<AreaDTO> normalizeTreeToThreeLevels(List<AreaDTO> tree) { |
| | | List<AreaDTO> result = new ArrayList<>(); |
| | | for (AreaDTO node : tree) { |
| | | int depth = getTreeDepth(node); |
| | | if(depth<3){ |
| | | List<AreaDTO> childrenList=new ArrayList<>(); |
| | | childrenList.add(node); |
| | | |
| | | AreaDTO tengxunDistrict= new AreaDTO(); |
| | | BeanUtils.copyProperties(node, tengxunDistrict); |
| | | tengxunDistrict.setChildren(childrenList); |
| | | // 虚拟的id、code |
| | | tengxunDistrict.setCode(tengxunDistrict.getCode()); |
| | | result.add(tengxunDistrict); |
| | | continue; |
| | | }else{ |
| | | result.add(node); |
| | | } |
| | | |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | private static int getTreeDepth(AreaDTO node) { |
| | | if (node == null || node.getChildren() == null || node.getChildren().isEmpty()) { |
| | | return 1; |
| | | } |
| | | int maxDepth = 0; |
| | | for (AreaDTO child : node.getChildren()) { |
| | | maxDepth = Math.max(maxDepth, getTreeDepth(child)); |
| | | } |
| | | return maxDepth + 1; |
| | | } |
| | | |
| | | public String getChineseAreaForApp2() { |
| | | String result = stringCacheClient.get("CHINA_AREA_DATA_APP"); |
| | | if (StringUtils.isNotBlank(result)) { |
| | | return result; |
| | | } else { |
| | | // 打印每个根节点对应的树形 JSON |
| | | Gson gson = new Gson(); |
| | | |
| | | List<DistrictTengxunDO> districtTengxunDOList = getAllDistrict(); |
| | | List<DistrictTengxunVO> districtTengxunVOList = ConverterUtil.transList(districtTengxunDOList, DistrictTengxunVO.class); |
| | | |
| | | if (CollectionUtils.isNotEmpty(districtTengxunDOList)) { |
| | | // 找出所有 parent_id 为 null 的节点作为根节点集合 |
| | | List<DistrictTengxunVO> roots = districtTengxunVOList.stream() |
| | | .filter(districtTengxunVO -> districtTengxunVO.getParentId() == null) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 遍历根节点,构建每棵树形结构 |
| | | List<AreaAppDTO> areaDTOList = new ArrayList<>(); |
| | | for (DistrictTengxunVO rootNode : roots) { |
| | | // 构建树形结构 |
| | | buildTree(rootNode, districtTengxunVOList); |
| | | |
| | | // 将树形转换成前端需要的结构 |
| | | AreaAppDTO areaDTO = transformApp2(rootNode); |
| | | areaDTOList.add(areaDTO); |
| | | } |
| | | result=gson.toJson(areaDTOList); |
| | | if(StringUtils.isNotBlank(result)){ |
| | | stringCacheClient.set("CHINA_AREA_DATA_APP", result); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | return ""; |
| | | } |
| | | } |
| | | |
| | | } |