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/dto/response/system/AreaAppTmpDTO.java | 27 ++++ src/main/java/com/mzl/flower/service/impl/district/DistrictTengxunServiceImpl.java | 221 ++++++++++++++++++++++++++++++++++++ src/main/java/com/mzl/flower/dto/response/system/AreaAppDTO.java | 22 +++ src/main/java/com/mzl/flower/web/system/ProvinceController.java | 12 ++ src/main/java/com/mzl/flower/service/district/DistrictTengxunService.java | 4 src/main/resources/mapper/statisticsAnalysis/FlowerMaterialMapper.xml | 19 +- src/main/java/com/mzl/flower/service/impl/map/MapTengxunServiceImpl.java | 13 + 7 files changed, 307 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/mzl/flower/dto/response/system/AreaAppDTO.java b/src/main/java/com/mzl/flower/dto/response/system/AreaAppDTO.java new file mode 100644 index 0000000..8a02e50 --- /dev/null +++ b/src/main/java/com/mzl/flower/dto/response/system/AreaAppDTO.java @@ -0,0 +1,22 @@ +package com.mzl.flower.dto.response.system; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.ArrayList; +import java.util.List; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class AreaAppDTO { + private String value; + private String text; + private List<AreaAppDTO> children=new ArrayList<>(); + + public AreaAppDTO(String value, String text) { + this.value = value; + this.text = text; + } +} diff --git a/src/main/java/com/mzl/flower/dto/response/system/AreaAppTmpDTO.java b/src/main/java/com/mzl/flower/dto/response/system/AreaAppTmpDTO.java new file mode 100644 index 0000000..0de4473 --- /dev/null +++ b/src/main/java/com/mzl/flower/dto/response/system/AreaAppTmpDTO.java @@ -0,0 +1,27 @@ +package com.mzl.flower.dto.response.system; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.ArrayList; +import java.util.List; + +@Data +@NoArgsConstructor + +public class AreaAppTmpDTO { + private String areaId; + private String areaType; + private String name; + private String parentAreaId; + private List<AreaAppTmpDTO> children=new ArrayList<>(); + + public AreaAppTmpDTO(String areaId, String areaType, String name, String parentAreaId) { + this.areaId = areaId; + this.areaType = areaType; + this.name = name; + this.parentAreaId = parentAreaId; + } + +} diff --git a/src/main/java/com/mzl/flower/service/district/DistrictTengxunService.java b/src/main/java/com/mzl/flower/service/district/DistrictTengxunService.java index 5b7f829..bd14f73 100644 --- a/src/main/java/com/mzl/flower/service/district/DistrictTengxunService.java +++ b/src/main/java/com/mzl/flower/service/district/DistrictTengxunService.java @@ -68,4 +68,8 @@ String getChineseArea(); void clearChineseDataCache(); + + String getChineseAreaForApp(); + + String getChineseAreaForWeb(); } 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 ""; + } } } diff --git a/src/main/java/com/mzl/flower/service/impl/map/MapTengxunServiceImpl.java b/src/main/java/com/mzl/flower/service/impl/map/MapTengxunServiceImpl.java index fc0b4f5..4c4bd15 100644 --- a/src/main/java/com/mzl/flower/service/impl/map/MapTengxunServiceImpl.java +++ b/src/main/java/com/mzl/flower/service/impl/map/MapTengxunServiceImpl.java @@ -51,13 +51,22 @@ List<TengxunDistrict> allDistricts = response.getResult(); // 将腾讯地图返回的数据格式转换成标准的三层结构 - List<TengxunDistrict> formatterDistrict =normalizeTreeToThreeLevels(allDistricts); +// List<TengxunDistrict> formatterDistrict =normalizeTreeToThreeLevels(allDistricts); +// +// List<TengxunDistrict> allTengxunDistricts = new ArrayList<>(); +// // 递归遍历所有的节点,然后把所有节点加入到 allDistricts 列表中 +// for (TengxunDistrict tengxunDistrict : formatterDistrict){ +// addDistrictToList(tengxunDistrict, null, allTengxunDistricts); // 从根节点开始,父节点 ID 为 null +// } + +// List<TengxunDistrict> formatterDistrict =normalizeTreeToThreeLevels(allDistricts); List<TengxunDistrict> allTengxunDistricts = new ArrayList<>(); // 递归遍历所有的节点,然后把所有节点加入到 allDistricts 列表中 - for (TengxunDistrict tengxunDistrict : formatterDistrict){ + for (TengxunDistrict tengxunDistrict : allDistricts){ addDistrictToList(tengxunDistrict, null, allTengxunDistricts); // 从根节点开始,父节点 ID 为 null } + List<DistrictTengxunDO> districtTengxunDOList= ConverterUtil.transList(allTengxunDistricts, DistrictTengxunDO.class); districtTengxunService.saveRemoteDistricts(districtTengxunDOList); diff --git a/src/main/java/com/mzl/flower/web/system/ProvinceController.java b/src/main/java/com/mzl/flower/web/system/ProvinceController.java index ec2fa4e..0682cae 100644 --- a/src/main/java/com/mzl/flower/web/system/ProvinceController.java +++ b/src/main/java/com/mzl/flower/web/system/ProvinceController.java @@ -88,4 +88,16 @@ districtTengxunService.clearChineseDataCache(); return returnData(R.SUCCESS.getCode(), null); } + + @ApiOperation(value = "获取中国省市区数据") + @GetMapping("/web/area/json") + public ResponseEntity<String> getChineseAreaForWeb() { + + // 高德地图 +// return returnData(R.SUCCESS.getCode(), districtService.getChineseArea()); + // 腾讯地图 + return returnData(R.SUCCESS.getCode(), districtTengxunService.getChineseAreaForWeb()); +// return returnData(R.SUCCESS.getCode(), provinceService.getChineseArea()); + } + } diff --git a/src/main/resources/mapper/statisticsAnalysis/FlowerMaterialMapper.xml b/src/main/resources/mapper/statisticsAnalysis/FlowerMaterialMapper.xml index 6da77be..67746bd 100644 --- a/src/main/resources/mapper/statisticsAnalysis/FlowerMaterialMapper.xml +++ b/src/main/resources/mapper/statisticsAnalysis/FlowerMaterialMapper.xml @@ -125,15 +125,18 @@ )) </if> - <if test="dto.flowerStatus != null and dto.flowerStatus != ''"> - <if test="dto.flowerStatus == 'OFF'"> - AND f.status IN ('OFF', 'FORCE_OFF') - </if> - <if test="dto.flowerStatus != 'OFF'"> - AND f.status = #{dto.flowerStatus} - </if> - </if> +<!-- <if test="dto.flowerStatus != null and dto.flowerStatus != ''">--> +<!-- <if test="dto.flowerStatus == 'OFF'">--> +<!-- AND f.status IN ('OFF', 'FORCE_OFF')--> +<!-- </if>--> +<!-- <if test="dto.flowerStatus != 'OFF'">--> +<!-- AND f.status = #{dto.flowerStatus}--> +<!-- </if>--> +<!-- </if>--> + <if test="dto.flowerStatus != null and dto.flowerStatus != ''"> + AND f.status = #{dto.flowerStatus} + </if> <if test="dto.flowerLevel !=null and dto.flowerLevel !=''"> AND f.`level` = #{dto.flowerLevel} </if> -- Gitblit v1.9.3