From eeb3b46afe7da500bce62daad35ded0886df3b88 Mon Sep 17 00:00:00 2001 From: cloudroam <cloudroam> Date: 星期四, 09 一月 2025 18:31:12 +0800 Subject: [PATCH] Merge branch 'master-v4' of http://47.96.225.205:8888/r/flowerbackend-v2 into master-v4 --- src/main/java/com/mzl/flower/service/impl/district/DistrictTengxunServiceImpl.java | 221 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 220 insertions(+), 1 deletions(-) diff --git a/src/main/java/com/mzl/flower/service/impl/district/DistrictTengxunServiceImpl.java b/src/main/java/com/mzl/flower/service/impl/district/DistrictTengxunServiceImpl.java index b2ec3f5..0205509 100644 --- a/src/main/java/com/mzl/flower/service/impl/district/DistrictTengxunServiceImpl.java +++ b/src/main/java/com/mzl/flower/service/impl/district/DistrictTengxunServiceImpl.java @@ -4,11 +4,15 @@ 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; @@ -49,6 +53,9 @@ @Autowired private StringCacheClient stringCacheClient; + + @Autowired + private Gson gson; @Override public void refreshDistrict() { List<DistrictTengxunDO> districtTengxunDOList = getAllDistrict(); @@ -104,7 +111,7 @@ } // 映射当前节点 - AreaDTO targetNode = new AreaDTO(sourceNode.getName(), sourceNode.getName()); + AreaDTO targetNode = new AreaDTO(sourceNode.getCode(), sourceNode.getName()); // 递归映射子节点 if (sourceNode.getChildren() != null) { @@ -116,6 +123,60 @@ 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() { @@ -362,6 +423,164 @@ @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 ""; + } } } -- Gitblit v1.9.3