ProvinceCityArea.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package org.jeecg.common.constant;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.core.io.Resource;
  5. import org.springframework.stereotype.Component;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Scanner;
  11. @Component("pca")
  12. public class ProvinceCityArea {
  13. @Value("classpath:static/pca.json")
  14. private Resource jsonData;
  15. List<Area> areaList;
  16. public String getText(String code){
  17. this.initAreaList();
  18. if(this.areaList!=null || this.areaList.size()>0){
  19. List<String> ls = new ArrayList<String>();
  20. getAreaByCode(code,ls);
  21. return String.join("/",ls);
  22. }
  23. return "";
  24. }
  25. public String getCode(String text){
  26. this.initAreaList();
  27. if(areaList!=null || areaList.size()>0){
  28. for(int i=areaList.size()-1;i>=0;i--){
  29. if(text.indexOf(areaList.get(i).getText())>=0){
  30. return areaList.get(i).getId();
  31. }
  32. }
  33. }
  34. return null;
  35. }
  36. public void getAreaByCode(String code,List<String> ls){
  37. for(Area area: areaList){
  38. if(area.getId().equals(code)){
  39. String pid = area.getPid();
  40. ls.add(0,area.getText());
  41. getAreaByCode(pid,ls);
  42. }
  43. }
  44. }
  45. private void initAreaList(){
  46. if(this.areaList==null || this.areaList.size()==0){
  47. this.areaList = new ArrayList<Area>();
  48. try {
  49. File file = jsonData.getFile();
  50. String jsonData = this.jsonRead(file);
  51. JSONObject baseJson = JSONObject.parseObject(jsonData);
  52. //第一层 省
  53. JSONObject provinceJson = baseJson.getJSONObject("86");
  54. for(String provinceKey: provinceJson.keySet()){
  55. Area province = new Area(provinceKey,provinceJson.getString(provinceKey),"86");
  56. this.areaList.add(province);
  57. //第二层 市
  58. JSONObject cityJson = baseJson.getJSONObject(provinceKey);
  59. for(String cityKey:cityJson.keySet()){
  60. Area city = new Area(cityKey,cityJson.getString(cityKey),provinceKey);
  61. this.areaList.add(city);
  62. //第三层 区
  63. JSONObject areaJson = baseJson.getJSONObject(cityKey);
  64. if(areaJson!=null){
  65. for(String areaKey:areaJson.keySet()){
  66. Area area = new Area(areaKey,areaJson.getString(areaKey),cityKey);
  67. this.areaList.add(area);
  68. }
  69. }
  70. }
  71. }
  72. } catch (IOException e) {
  73. e.printStackTrace();
  74. }
  75. }
  76. }
  77. private String jsonRead(File file){
  78. Scanner scanner = null;
  79. StringBuilder buffer = new StringBuilder();
  80. try {
  81. scanner = new Scanner(file, "utf-8");
  82. while (scanner.hasNextLine()) {
  83. buffer.append(scanner.nextLine());
  84. }
  85. } catch (Exception e) {
  86. } finally {
  87. if (scanner != null) {
  88. scanner.close();
  89. }
  90. }
  91. return buffer.toString();
  92. }
  93. class Area{
  94. String id;
  95. String text;
  96. String pid;
  97. public Area(String id,String text,String pid){
  98. this.id = id;
  99. this.text = text;
  100. this.pid = pid;
  101. }
  102. public String getId() {
  103. return id;
  104. }
  105. public String getText() {
  106. return text;
  107. }
  108. public String getPid() {
  109. return pid;
  110. }
  111. }
  112. }