【fix sonar】ProvinceCityArea文件

This commit is contained in:
梁琦涛
2023-03-03 13:47:59 +08:00
parent f54a553b41
commit 3ebfb0feb5
@@ -1,17 +1,15 @@
package com.jero.common.constant; package com.jero.common.constant;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.jero.common.util.oConvertUtils; import com.jero.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Value; import org.apache.commons.collections4.CollectionUtils;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Scanner;
import java.util.Set;
import java.util.List; import java.util.List;
import java.util.Scanner;
@Component("pca") @Component("pca")
public class ProvinceCityArea { public class ProvinceCityArea {
@@ -19,8 +17,8 @@ public class ProvinceCityArea {
public String getText(String code){ public String getText(String code){
this.initAreaList(); this.initAreaList();
if(this.areaList!=null || this.areaList.size()>0){ if(!CollectionUtils.isEmpty(this.areaList)){
List<String> ls = new ArrayList<String>(); List<String> ls = new ArrayList<>();
getAreaByCode(code,ls); getAreaByCode(code,ls);
return String.join("/",ls); return String.join("/",ls);
} }
@@ -29,7 +27,7 @@ public class ProvinceCityArea {
public String getCode(String text){ public String getCode(String text){
this.initAreaList(); this.initAreaList();
if(areaList!=null || areaList.size()>0){ if(!CollectionUtils.isEmpty(areaList)){
for(int i=areaList.size()-1;i>=0;i--){ for(int i=areaList.size()-1;i>=0;i--){
if(text.indexOf(areaList.get(i).getText())>=0){ if(text.indexOf(areaList.get(i).getText())>=0){
return areaList.get(i).getId(); return areaList.get(i).getId();
@@ -50,29 +48,25 @@ public class ProvinceCityArea {
} }
private void initAreaList(){ private void initAreaList(){
//System.out.println("====================="); if(CollectionUtils.isEmpty(this.areaList)){
if(this.areaList==null || this.areaList.size()==0){ this.areaList = new ArrayList<>();
this.areaList = new ArrayList<Area>();
try { try {
String jsonData = oConvertUtils.readStatic("classpath:static/pca.json"); String jsonData = oConvertUtils.readStatic("classpath:static/pca.json");
JSONObject baseJson = JSONObject.parseObject(jsonData); JSONObject baseJson = JSON.parseObject(jsonData);
//第一层 省 //第一层 省
JSONObject provinceJson = baseJson.getJSONObject("86"); JSONObject provinceJson = baseJson.getJSONObject("86");
for(String provinceKey: provinceJson.keySet()){ for(String provinceKey: provinceJson.keySet()){
//System.out.println("===="+provinceKey);
Area province = new Area(provinceKey,provinceJson.getString(provinceKey),"86"); Area province = new Area(provinceKey,provinceJson.getString(provinceKey),"86");
this.areaList.add(province); this.areaList.add(province);
//第二层 市 //第二层 市
JSONObject cityJson = baseJson.getJSONObject(provinceKey); JSONObject cityJson = baseJson.getJSONObject(provinceKey);
for(String cityKey:cityJson.keySet()){ for(String cityKey:cityJson.keySet()){
//System.out.println("-----"+cityKey);
Area city = new Area(cityKey,cityJson.getString(cityKey),provinceKey); Area city = new Area(cityKey,cityJson.getString(cityKey),provinceKey);
this.areaList.add(city); this.areaList.add(city);
//第三层 区 //第三层 区
JSONObject areaJson = baseJson.getJSONObject(cityKey); JSONObject areaJson = baseJson.getJSONObject(cityKey);
if(areaJson!=null){ if(areaJson!=null){
for(String areaKey:areaJson.keySet()){ for(String areaKey:areaJson.keySet()){
//System.out.println("········"+areaKey);
Area area = new Area(areaKey,areaJson.getString(areaKey),cityKey); Area area = new Area(areaKey,areaJson.getString(areaKey),cityKey);
this.areaList.add(area); this.areaList.add(area);
} }
@@ -88,19 +82,13 @@ public class ProvinceCityArea {
private String jsonRead(File file){ private String jsonRead(File file){
Scanner scanner = null;
StringBuilder buffer = new StringBuilder(); StringBuilder buffer = new StringBuilder();
try { try(Scanner scanner = new Scanner(file, "utf-8");) {
scanner = new Scanner(file, "utf-8");
while (scanner.hasNextLine()) { while (scanner.hasNextLine()) {
buffer.append(scanner.nextLine()); buffer.append(scanner.nextLine());
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
} }
return buffer.toString(); return buffer.toString();
} }