From d6d5d6ea4905c59423145320baaaa8b90aa5bd7a Mon Sep 17 00:00:00 2001
From: fengxiang <110431245@qq.com>
Date: Fri, 21 Sep 2018 15:24:58 +0800
Subject: [PATCH] 更新 地图地区code 条件,增加 乡镇社区入口
---
src/main/java/com/moral/service/AreaService.java | 3
src/main/java/com/moral/service/impl/AreaServiceImpl.java | 158 ++++++++++++++++++++++++++++++++++++++++++++--------
src/main/java/com/moral/service/impl/OrganizationServiceImpl.java | 3
src/main/java/com/moral/controller/ScreenController.java | 2
src/main/java/com/moral/controller/AreaController.java | 2
5 files changed, 138 insertions(+), 30 deletions(-)
diff --git a/src/main/java/com/moral/controller/AreaController.java b/src/main/java/com/moral/controller/AreaController.java
index e43be29..f67ef19 100644
--- a/src/main/java/com/moral/controller/AreaController.java
+++ b/src/main/java/com/moral/controller/AreaController.java
@@ -22,7 +22,7 @@
@Resource
AreaService areaService;
@GetMapping("getfullnames")
- public ResultBean<String> getFullNames(Integer code){
+ public ResultBean<String> getFullNames(Long code){
return new ResultBean<>(areaService.queryFullNameByCode(code));
}
@GetMapping("get-provinces")
diff --git a/src/main/java/com/moral/controller/ScreenController.java b/src/main/java/com/moral/controller/ScreenController.java
index 87ca7e3..2478a83 100644
--- a/src/main/java/com/moral/controller/ScreenController.java
+++ b/src/main/java/com/moral/controller/ScreenController.java
@@ -422,7 +422,7 @@
* @return
*/
@RequestMapping(value = "/map-page", method = RequestMethod.GET)
- public ModelAndView map(ModelAndView model, @RequestParam("areaCode")int code, @RequestParam("accountId")int accountId){
+ public ModelAndView map(ModelAndView model, @RequestParam("areaCode")long code, @RequestParam("accountId")int accountId){
Account account = accountService.getAccountById(accountId);
String regionName = areaService.queryFullNameByCode(code);
if(account!=null&®ionName!=null){
diff --git a/src/main/java/com/moral/service/AreaService.java b/src/main/java/com/moral/service/AreaService.java
index 8c381b4..ff83b08 100644
--- a/src/main/java/com/moral/service/AreaService.java
+++ b/src/main/java/com/moral/service/AreaService.java
@@ -13,7 +13,8 @@
List<City> getCities(int provinceCode);
List<Area> getAreas(int cityCode);
- String queryFullNameByCode(Integer code);
+
+ String queryFullNameByCode(Long code);
List<Town> getTowns(Integer areaCode);
List<Village> getVillages(Long townCode);
diff --git a/src/main/java/com/moral/service/impl/AreaServiceImpl.java b/src/main/java/com/moral/service/impl/AreaServiceImpl.java
index 3f5b3db..3099420 100644
--- a/src/main/java/com/moral/service/impl/AreaServiceImpl.java
+++ b/src/main/java/com/moral/service/impl/AreaServiceImpl.java
@@ -1,5 +1,6 @@
package com.moral.service.impl;
+import com.moral.common.exception.BusinessException;
import com.moral.entity.Area;
import com.moral.entity.City;
import com.moral.entity.Province;
@@ -15,6 +16,8 @@
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
+import javax.validation.constraints.NotNull;
+import java.util.ArrayList;
import java.util.List;
@@ -58,34 +61,137 @@
* @return
*/
@Override
- public String queryFullNameByCode(Integer code){
- String codeStr = code.toString();
- String fullName = "";
- // ���������������code
- if(!codeStr.endsWith("00")){
- String provinceCode = codeStr.substring(0,2)+"0000";
- Province province = provinceMapper.selectByPrimaryKey(Integer.valueOf(provinceCode));
- String cityCode = codeStr.substring(0,4)+"00";
- City city = cityMapper.selectByPrimaryKey(Integer.valueOf(cityCode));
- Area area = areaMapper.selectByPrimaryKey(code);
- fullName = province.getProvinceName().replaceAll(" ", "")
- +" "+city.getCityName().replaceAll(" ", "")
- +" "+area.getAreaName().replaceAll(" ", "");
- } else if(!codeStr.endsWith("0000")){
- // ��������� ���������code
- String provinceCode = codeStr.substring(0,2)+"0000";
- Province province = provinceMapper.selectByPrimaryKey(Integer.valueOf(provinceCode));
- City city = cityMapper.selectByPrimaryKey(code);
- fullName = province.getProvinceName().replaceAll(" ", "")
- +" "+city.getCityName().replaceAll(" ", "");
- } else {
- // ��������� ���code
- Province province = provinceMapper.selectByPrimaryKey(code);
- fullName = province.getProvinceName().replaceAll(" ", "");
+ public String queryFullNameByCode(Long code){
+ StringBuilder fullName = new StringBuilder();
+ List<Number> codeList = regionCodeToList(code);
+ for (int num = 0;num<codeList.size();num++){
+ int codeLevel = num+1;
+ Number currentCode = codeList.get(num);
+ switch (codeLevel) {
+ case 1:
+ Province province = provinceMapper.selectByPrimaryKey(currentCode);
+ if (province !=null) {
+ String provinceName = province.getProvinceName();
+ provinceName = provinceName!=null ? provinceName.replace(" ","") : "";
+ fullName.append(provinceName);
+ }
+ break;
+ case 2:
+ City city = cityMapper.selectByPrimaryKey(currentCode);
+ if(city!=null) {
+ String cityName = city.getCityName();
+ cityName = cityName!=null ? cityName.replace(" ",""):"";
+ fullName.append(" ");
+ fullName.append(cityName);
+ }
+ break;
+ case 3:
+ Area area = areaMapper.selectByPrimaryKey(currentCode);
+ if(area!=null){
+ String areaName = area.getAreaName();
+ areaName = areaName!=null ? areaName.replace(" ",""):"";
+ fullName.append(" ");
+ fullName.append(areaName);
+ }
+ break;
+ case 4:
+ Town town = townMapper.selectByPrimaryKey(currentCode);
+ if(town!=null){
+ String townName = town.getTownName();
+ townName = townName!=null ? townName.replace(" ",""):"";
+ fullName.append(" ");
+ fullName.append(townName);
+ }
+ break;
+ default:
+ Village village = villageMapper.selectByPrimaryKey(currentCode);
+ if(village!=null) {
+ String villageName = village.getVillageName();
+ villageName = villageName!=null ? villageName.replace(" ",""):"";
+ fullName.append(" ");
+ fullName.append(villageName);
+ }
+ break;
+ }
}
- return fullName;
+ return fullName.toString();
}
-
+ private List<Number> regionCodeToList(Long code) {
+ List<Number> codeList = new ArrayList();
+ if(code != null){
+ String codeStr = code.toString();
+ int codeLevel = 0;
+ codeLevel =getCodeLevel(codeStr);
+ int codeLevelTemp = 1;
+ do {
+ String currentCodeStr = getCodeByLevel(codeStr,codeLevelTemp);
+ Number currentCode = 0;
+ if(codeLevelTemp < 4) {
+ currentCode = Integer.parseInt(currentCodeStr);
+ }else{
+ currentCode = Long.parseLong(currentCodeStr);
+ }
+ codeList.add(currentCode);
+ codeLevelTemp++;
+ }while (codeLevelTemp <= codeLevel);
+ }
+ return codeList;
+ }
+ private int getCodeLevel(String code){
+ return isProvinceCode(code) ? 1 :
+ isCityCode(code) ? 2 :
+ isAreaCode(code) ? 3 :
+ isTownCode(code) ? 4 : 5;
+ }
+ static private String[] SpecialTwonCodes = new String[]{"653130103001"};
+ static private boolean isSpecialTwonCode(String code){
+ for (String codeStr:SpecialTwonCodes){
+ if(codeStr.equals(code)){
+ return true;
+ }
+ }
+ return false;
+ }
+ private String getCodeByLevel(@NotNull String sourceCode,int codeLevel){
+ String zeroStr = "0000";
+ int totalCount = 0;
+ int effectiveCount = 0;
+ if(codeLevel <4){
+ totalCount = 6;
+ effectiveCount = codeLevel == 1 ? 2:
+ codeLevel == 2 ? 4:6;
+ } else{
+ totalCount = 12;
+ effectiveCount = codeLevel == 4 ? 9:12;
+ }
+ //������
+ if(codeLevel==4&&isSpecialTwonCode(sourceCode)){
+ return sourceCode;
+ }
+ if(totalCount == effectiveCount && sourceCode.length() == totalCount){
+ return sourceCode;
+ }else {
+ if (sourceCode.length()<totalCount){
+ throw new BusinessException("totalCount is more than the length of sourceCode");
+ }
+ return sourceCode.substring(0,effectiveCount)+zeroStr.substring(0,totalCount-effectiveCount);
+ }
+ }
+ private boolean isProvinceCode(String code){
+ return code.length() == 6 && code.endsWith("0000");
+ }
+ private boolean isCityCode(String code){
+ return code.length() == 6 && code.endsWith("00");
+ }
+ private boolean isAreaCode(String code){
+ return code.length() == 6 && !code.endsWith("00");
+ }
+ private boolean isTownCode(String code){
+ return code.length() == 12 && (code.endsWith("000")||isSpecialTwonCode(code));
+ }
+ private boolean isVillageCode(String code){
+ return code.length() == 12 && !code.endsWith("000");
+ }
@Override
public List<Town> getTowns(Integer areaCode) {
Example example = new Example(Town.class);
diff --git a/src/main/java/com/moral/service/impl/OrganizationServiceImpl.java b/src/main/java/com/moral/service/impl/OrganizationServiceImpl.java
index 29a321f..7b8d709 100644
--- a/src/main/java/com/moral/service/impl/OrganizationServiceImpl.java
+++ b/src/main/java/com/moral/service/impl/OrganizationServiceImpl.java
@@ -74,6 +74,7 @@
List<Organization> organizationList = organizationMapper.selectWithAreaNameByExample(example);
return new PageBean(organizationList);
}
+ @Override
public void addOrModify(Organization organization){
try{
if(organization.getId()==null){
@@ -81,7 +82,7 @@
organization.setIsDelete(Constants.IS_DELETE_FALSE);
organizationMapper.insertSelective(organization);
}else{
- organizationMapper.updateByPrimaryKeySelective(organization);
+ organizationMapper.updateByPrimaryKey(organization);
}
}
catch (Exception ex){
--
Gitblit v1.8.0