🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
``` /** * @author 张跃帅 * @Description: 系统行政区域-工具 * @date 2020/08/12 */ public class SystemAreaUtil { private static final SystemAreaService systemAreaService = SpringUtil.getBean(SystemAreaService.class); /** * 获取-系统行政区划tree */ public static List<Tree<String>> getTree(String name) { // 变量创建ArrayList List<Tree<String>> treeNodeList = CollectionUtil.newArrayList(); // 创建查询包装器 LambdaQueryWrapper<SystemAreaEntity> queryWrapper = new LambdaQueryWrapper<>(); // 设置条件 queryWrapper.eq(SystemAreaEntity::getDataFlag, CommonDataFlagEnum.NORMAL.getCode()) .eq(SystemAreaEntity::getDelFlag, CommonDelFlagEnum.NOT_DELETE.getCode()); // 设置排序 queryWrapper.orderByAsc(SystemAreaEntity::getAreaCode); // 查询 List<SystemAreaEntity> systemAreaList = systemAreaService.list(queryWrapper); // 判断 if (systemAreaList != null && systemAreaList.size() > 0) { // 获取参数 String parentId = CommonTreeParentIdEnum.PARENTID_0.getCode(); // tree转换器 treeNodeList = TreeUtil.build(systemAreaList, parentId, (systemArea, tree) -> { // 赋值 tree.setId(Convert.toStr(systemArea.getId())); tree.setParentId(Convert.toStr(systemArea.getPid())); tree.setName(systemArea.getAreaName()); // 追加自定义属性 tree.putExtra("areaCode", systemArea.getAreaCode()); tree.putExtra("areaFullName", systemArea.getAreaFullName()); }); } // 判断-条件搜索 if (treeNodeList != null && treeNodeList.size() > 0 && StrUtil.isNotBlank(name)) { // 变量创建ArrayList List<Tree<String>> searchTreeNodeList = CollectionUtil.newArrayList(); // 遍历 for (Tree<String> treeNode : treeNodeList) { // 搜索查询 searchTreeNodeByName(treeNode, name, searchTreeNodeList); } // 赋值 treeNodeList = searchTreeNodeList; } // 返回 return treeNodeList; } /** * 递归遍历树节点-搜索匹配节点 */ private static List<Tree<String>> searchTreeNodeByName(Tree<String> treeNode, String name, List<Tree<String>> searchTreeNodeList) { // 获取参数 String nodeName = treeNode.getName().toString(); List<Tree<String>> childNodeList = treeNode.getChildren(); // 判断 if (nodeName.contains(name)) { // 添加 searchTreeNodeList.add(treeNode); } // 判断 if (childNodeList != null && childNodeList.size() > 0) { // 遍历 for (Tree<String> childNode : treeNode.getChildren()) { // 匹配子节点 searchTreeNodeByName(childNode, name, searchTreeNodeList); } } // 返回 return searchTreeNodeList; } }