da556a2a3ab42c8dd5719a9531c0df57fcf93ee0.svn-base 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package org.jeecg.common.constant;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.jeecg.common.util.oConvertUtils;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.core.io.Resource;
  6. import org.springframework.stereotype.Component;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.util.ArrayList;
  10. import java.util.Scanner;
  11. import java.util.Set;
  12. import java.util.List;
  13. @Component("pca")
  14. public class ProvinceCityArea {
  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. //System.out.println("=====================");
  47. if(this.areaList==null || this.areaList.size()==0){
  48. this.areaList = new ArrayList<Area>();
  49. try {
  50. String jsonData = oConvertUtils.readStatic("classpath:static/pca.json");
  51. JSONObject baseJson = JSONObject.parseObject(jsonData);
  52. //第一层 省
  53. JSONObject provinceJson = baseJson.getJSONObject("86");
  54. for(String provinceKey: provinceJson.keySet()){
  55. //System.out.println("===="+provinceKey);
  56. Area province = new Area(provinceKey,provinceJson.getString(provinceKey),"86");
  57. this.areaList.add(province);
  58. //第二层 市
  59. JSONObject cityJson = baseJson.getJSONObject(provinceKey);
  60. for(String cityKey:cityJson.keySet()){
  61. //System.out.println("-----"+cityKey);
  62. Area city = new Area(cityKey,cityJson.getString(cityKey),provinceKey);
  63. this.areaList.add(city);
  64. //第三层 区
  65. JSONObject areaJson = baseJson.getJSONObject(cityKey);
  66. if(areaJson!=null){
  67. for(String areaKey:areaJson.keySet()){
  68. //System.out.println("········"+areaKey);
  69. Area area = new Area(areaKey,areaJson.getString(areaKey),cityKey);
  70. this.areaList.add(area);
  71. }
  72. }
  73. }
  74. }
  75. } catch (Exception e) {
  76. e.printStackTrace();
  77. }
  78. }
  79. }
  80. private String jsonRead(File file){
  81. Scanner scanner = null;
  82. StringBuilder buffer = new StringBuilder();
  83. try {
  84. scanner = new Scanner(file, "utf-8");
  85. while (scanner.hasNextLine()) {
  86. buffer.append(scanner.nextLine());
  87. }
  88. } catch (Exception e) {
  89. } finally {
  90. if (scanner != null) {
  91. scanner.close();
  92. }
  93. }
  94. return buffer.toString();
  95. }
  96. class Area{
  97. String id;
  98. String text;
  99. String pid;
  100. public Area(String id,String text,String pid){
  101. this.id = id;
  102. this.text = text;
  103. this.pid = pid;
  104. }
  105. public String getId() {
  106. return id;
  107. }
  108. public String getText() {
  109. return text;
  110. }
  111. public String getPid() {
  112. return pid;
  113. }
  114. }
  115. }