Commit 5043368e authored by yanzg's avatar yanzg

处理结果

parent 53f0815b
...@@ -3,6 +3,9 @@ package com.yanzuoguang.util.helper; ...@@ -3,6 +3,9 @@ package com.yanzuoguang.util.helper;
import com.yanzuoguang.util.exception.CodeException; import com.yanzuoguang.util.exception.CodeException;
public class AreaHelper { public class AreaHelper {
public static final int COUNTRY_LENGTH = 3;
public static final int PROVINCE_LENGTH = 5;
public static final int CITY_LENGTH = 7;
public static final int AREA_LENGTH = 9; public static final int AREA_LENGTH = 9;
/** /**
...@@ -12,7 +15,7 @@ public class AreaHelper { ...@@ -12,7 +15,7 @@ public class AreaHelper {
* @return * @return
*/ */
public static String getCountryId(String fromAreaId) { public static String getCountryId(String fromAreaId) {
return getLengthId(fromAreaId, 3); return getLengthId(fromAreaId, COUNTRY_LENGTH);
} }
/** /**
...@@ -22,7 +25,7 @@ public class AreaHelper { ...@@ -22,7 +25,7 @@ public class AreaHelper {
* @return * @return
*/ */
public static String getProvinceId(String fromAreaId) { public static String getProvinceId(String fromAreaId) {
return getLengthId(fromAreaId, 5); return getLengthId(fromAreaId, PROVINCE_LENGTH);
} }
/** /**
...@@ -32,7 +35,7 @@ public class AreaHelper { ...@@ -32,7 +35,7 @@ public class AreaHelper {
* @return * @return
*/ */
public static String getCityId(String fromAreaId) { public static String getCityId(String fromAreaId) {
return getLengthId(fromAreaId, 7); return getLengthId(fromAreaId, CITY_LENGTH);
} }
/** /**
...@@ -75,8 +78,8 @@ public class AreaHelper { ...@@ -75,8 +78,8 @@ public class AreaHelper {
return fromAreaId; return fromAreaId;
} }
String to = StringHelper.trimEnd(fromAreaId, "00"); String to = StringHelper.trimEnd(fromAreaId, "00");
while (to.length() < 3) { if (to.length() < COUNTRY_LENGTH) {
to += "0"; to = getFull(to).substring(0, COUNTRY_LENGTH);
} }
return to; return to;
} }
...@@ -101,4 +104,22 @@ public class AreaHelper { ...@@ -101,4 +104,22 @@ public class AreaHelper {
return sb.toString(); return sb.toString();
} }
} }
/**
* 获取上级目录Id
*
* @param fromAreaId
* @return
*/
public static String getParentId(String fromAreaId) {
if (StringHelper.isEmpty(fromAreaId)) {
return fromAreaId;
}
String simpleId = getSimple(fromAreaId);
String parentSimpleId = StringHelper.EMPTY;
if (simpleId.length() > COUNTRY_LENGTH) {
parentSimpleId = simpleId.substring(0, simpleId.length() - 2);
}
return getFull(parentSimpleId);
}
} }
...@@ -18,6 +18,11 @@ public class TestAreaHelper { ...@@ -18,6 +18,11 @@ public class TestAreaHelper {
System.out.println(AreaHelper.getProvinceId(from)); System.out.println(AreaHelper.getProvinceId(from));
System.out.println(AreaHelper.getCountryId(from)); System.out.println(AreaHelper.getCountryId(from));
System.out.println(AreaHelper.getFull(from)); System.out.println(AreaHelper.getFull(from));
System.out.println(AreaHelper.getParentId(AreaHelper.getCountryId(from)));
System.out.println(AreaHelper.getParentId(AreaHelper.getProvinceId(from)));
System.out.println(AreaHelper.getParentId(AreaHelper.getCityId(from)));
System.out.println(AreaHelper.getParentId(from));
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment