From 77d85c90181265553373d133878bf1d8a5e98f0e Mon Sep 17 00:00:00 2001 From: WendyYang Date: Thu, 2 Mar 2023 16:48:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8C=BA=E5=9F=9F=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ningdatech/pmapi/common/helper/RegionCacheHelper.java | 9 +++++++++ .../pmapi/common/helper/impl/RegionsCacheHelperImpl.java | 10 ++++++++-- .../com/ningdatech/pmapi/common/model/RegionMapKey.java | 10 ++-------- .../com/ningdatech/pmapi/sys/service/IMenuServiceTest.java | 4 +--- .../ningdatech/pmapi/sys/service/IRegionServiceTest.java | 14 ++++++++++---- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/common/helper/RegionCacheHelper.java b/pmapi/src/main/java/com/ningdatech/pmapi/common/helper/RegionCacheHelper.java index 334b327..adfa0d0 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/common/helper/RegionCacheHelper.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/common/helper/RegionCacheHelper.java @@ -35,6 +35,15 @@ public interface RegionCacheHelper { RegionDTO getByCodeAndLevel(String code, int level); /** + * 获取区域名称 + * + * @param code 区域编码 + * @param level 区域层级 + * @return 名称 + */ + String getRegionName(String code, int level); + + /** * 根据传入的地区code与level获取所有上层地区集合 * 按照level升序排列 * diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/common/helper/impl/RegionsCacheHelperImpl.java b/pmapi/src/main/java/com/ningdatech/pmapi/common/helper/impl/RegionsCacheHelperImpl.java index 427ffb0..90ad7c2 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/common/helper/impl/RegionsCacheHelperImpl.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/common/helper/impl/RegionsCacheHelperImpl.java @@ -34,6 +34,11 @@ public class RegionsCacheHelperImpl extends AbstractRegionCacheHelper implements } @Override + public String getRegionName(String code, int level) { + return getByCodeAndLevel(code, level).getRegionName(); + } + + @Override public List all() { return super.all(); } @@ -101,12 +106,13 @@ public class RegionsCacheHelperImpl extends AbstractRegionCacheHelper implements } StringBuilder builder = new StringBuilder(); List regionCodes = StrUtils.split(region.getRegionCodePath()); - for (int i = regionCodes.size() - 1; i > 0; i--) { + for (int i = regionCodes.size() - 2; i > 0; i--) { if (level <= sLevel) { break; } RegionDTO tmp = getByCodeAndLevel(regionCodes.get(i), --level); - builder.append(tmp.getRegionName()).append(StrPool.SLASH); + builder.insert(0, StrPool.SLASH); + builder.insert(0, tmp.getRegionName()); } builder.append(region.getRegionName()); return builder.toString(); diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/common/model/RegionMapKey.java b/pmapi/src/main/java/com/ningdatech/pmapi/common/model/RegionMapKey.java index 31c5bf8..213b0f6 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/common/model/RegionMapKey.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/common/model/RegionMapKey.java @@ -16,7 +16,6 @@ import java.util.Objects; * @since 13:54 2023/3/1 */ @Data -@Builder @AllArgsConstructor public class RegionMapKey { @@ -24,11 +23,6 @@ public class RegionMapKey { return new RegionMapKey(regionCode, regionLevel); } - - @Tolerate - public RegionMapKey() { - } - /** * 区域码 */ @@ -48,8 +42,8 @@ public class RegionMapKey { return false; } RegionMapKey regionMapKey = (RegionMapKey) o; - return regionCode.equals(regionMapKey.getRegionCode()) && - regionLevel.equals(regionMapKey.getRegionLevel()); + return regionCode.equals(regionMapKey.getRegionCode()) + && regionLevel.equals(regionMapKey.getRegionLevel()); } @Override diff --git a/pmapi/src/test/java/com/ningdatech/pmapi/sys/service/IMenuServiceTest.java b/pmapi/src/test/java/com/ningdatech/pmapi/sys/service/IMenuServiceTest.java index 7b5d953..7d9a566 100644 --- a/pmapi/src/test/java/com/ningdatech/pmapi/sys/service/IMenuServiceTest.java +++ b/pmapi/src/test/java/com/ningdatech/pmapi/sys/service/IMenuServiceTest.java @@ -1,11 +1,9 @@ package com.ningdatech.pmapi.sys.service; import cn.hutool.core.collection.CollUtil; -import cn.hutool.core.io.FileUtil; import cn.hutool.db.Db; import cn.hutool.db.Entity; import cn.hutool.json.JSONObject; -import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.ningdatech.pmapi.AppTests; import com.ningdatech.pmapi.sys.model.entity.Menu; @@ -74,7 +72,7 @@ class IMenuServiceTest extends AppTests { public void initMenu() { // menuService.remove(null); roleMenuService.remove(Wrappers.lambdaQuery(RoleMenu.class) - .eq(RoleMenu::getRoleId,1)); + .eq(RoleMenu::getRoleId, 1)); // String str = FileUtil.readString("/Users/wendy/Desktop/long_text_2023-02-13-15-28-42.txt", "UTF-8"); // List obj = JSONUtil.toList(str, JSONObject.class); // save(obj, 0); diff --git a/pmapi/src/test/java/com/ningdatech/pmapi/sys/service/IRegionServiceTest.java b/pmapi/src/test/java/com/ningdatech/pmapi/sys/service/IRegionServiceTest.java index 17321c9..a228101 100644 --- a/pmapi/src/test/java/com/ningdatech/pmapi/sys/service/IRegionServiceTest.java +++ b/pmapi/src/test/java/com/ningdatech/pmapi/sys/service/IRegionServiceTest.java @@ -5,9 +5,8 @@ import com.ningdatech.basic.util.CollUtils; import com.ningdatech.pmapi.AppTests; import com.ningdatech.pmapi.common.helper.RegionCacheHelper; import com.ningdatech.pmapi.sys.model.entity.Region; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; @@ -22,13 +21,15 @@ import java.util.concurrent.atomic.AtomicLong; * @author WendyYang * @since 16:57 2023/3/1 */ -public class IRegionServiceTest extends AppTests { +class IRegionServiceTest extends AppTests { @Autowired + private RegionCacheHelper regionCacheHelper; + @Autowired private IRegionService regionService; @Test - public void init() { + public void test1() { AtomicLong idIncr = new AtomicLong(1); Map> map = CollUtils.group(regionService.list(), Region::getRegionLevel); regionService.remove(null); @@ -54,5 +55,10 @@ public class IRegionServiceTest extends AppTests { }); } + @Test + public void test2() { + System.out.println(regionCacheHelper.getDisplayName("330100", 2)); + System.out.println(regionCacheHelper.getFullDisplayName("330102", 3)); + } }