CivilCodeFileConf.java 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package com.genersoft.iot.vmp.conf;
  2. import com.genersoft.iot.vmp.common.CivilCodePo;
  3. import com.genersoft.iot.vmp.utils.CivilCodeUtil;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.CommandLineRunner;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.context.annotation.Lazy;
  10. import org.springframework.core.annotation.Order;
  11. import org.springframework.core.io.ClassPathResource;
  12. import org.springframework.util.ObjectUtils;
  13. import java.io.BufferedReader;
  14. import java.io.File;
  15. import java.io.InputStream;
  16. import java.io.InputStreamReader;
  17. import java.nio.file.Files;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. /**
  21. * 启动时读取行政区划表
  22. */
  23. @Configuration
  24. @Order(value=14)
  25. public class CivilCodeFileConf implements CommandLineRunner {
  26. private final static Logger logger = LoggerFactory.getLogger(CivilCodeFileConf.class);
  27. @Autowired
  28. @Lazy
  29. private UserSetting userSetting;
  30. @Override
  31. public void run(String... args) throws Exception {
  32. if (ObjectUtils.isEmpty(userSetting.getCivilCodeFile())) {
  33. logger.warn("[行政区划] 文件未设置,可能造成目录刷新结果不完整");
  34. return;
  35. }
  36. InputStream inputStream;
  37. if (userSetting.getCivilCodeFile().startsWith("classpath:")){
  38. String filePath = userSetting.getCivilCodeFile().substring("classpath:".length());
  39. ClassPathResource civilCodeFile = new ClassPathResource(filePath);
  40. if (!civilCodeFile.exists()) {
  41. logger.warn("[行政区划] 文件<{}>不存在,可能造成目录刷新结果不完整", userSetting.getCivilCodeFile());
  42. return;
  43. }
  44. inputStream = civilCodeFile.getInputStream();
  45. }else {
  46. File civilCodeFile = new File(userSetting.getCivilCodeFile());
  47. if (!civilCodeFile.exists()) {
  48. logger.warn("[行政区划] 文件<{}>不存在,可能造成目录刷新结果不完整", userSetting.getCivilCodeFile());
  49. return;
  50. }
  51. inputStream = Files.newInputStream(civilCodeFile.toPath());
  52. }
  53. BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(inputStream));
  54. int index = -1;
  55. String line;
  56. List<CivilCodePo> civilCodePoList = new ArrayList<>();
  57. while ((line = inputStreamReader.readLine()) != null) {
  58. index ++;
  59. if (index == 0) {
  60. continue;
  61. }
  62. String[] infoArray = line.split(",");
  63. CivilCodePo civilCodePo = CivilCodePo.getInstance(infoArray);
  64. civilCodePoList.add(civilCodePo);
  65. }
  66. CivilCodeUtil.INSTANCE.add(civilCodePoList);
  67. inputStreamReader.close();
  68. inputStream.close();
  69. if (civilCodePoList.isEmpty()) {
  70. logger.warn("[行政区划] 文件内容为空,可能造成目录刷新结果不完整");
  71. }else {
  72. logger.info("[行政区划] 加载成功,共加载数据{}条", civilCodePoList.size());
  73. }
  74. }
  75. }