2023-06-19 18:42:36 +08:00
|
|
|
package com.genersoft.iot.vmp.conf;
|
|
|
|
|
|
|
|
import com.genersoft.iot.vmp.common.CivilCodePo;
|
2024-03-14 14:54:20 +08:00
|
|
|
import com.genersoft.iot.vmp.utils.CivilCodeUtil;
|
2023-06-19 18:42:36 +08:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.boot.CommandLineRunner;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
|
import org.springframework.core.annotation.Order;
|
|
|
|
import org.springframework.core.io.ClassPathResource;
|
|
|
|
import org.springframework.util.ObjectUtils;
|
|
|
|
|
2023-08-10 20:00:43 +08:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
2023-06-19 18:42:36 +08:00
|
|
|
import java.nio.file.Files;
|
2024-03-14 14:54:20 +08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2023-06-19 18:42:36 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 启动时读取行政区划表
|
|
|
|
*/
|
|
|
|
@Configuration
|
|
|
|
@Order(value=14)
|
|
|
|
public class CivilCodeFileConf implements CommandLineRunner {
|
|
|
|
|
|
|
|
private final static Logger logger = LoggerFactory.getLogger(CivilCodeFileConf.class);
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Lazy
|
|
|
|
private UserSetting userSetting;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run(String... args) throws Exception {
|
|
|
|
if (ObjectUtils.isEmpty(userSetting.getCivilCodeFile())) {
|
|
|
|
logger.warn("[行政区划] 文件未设置,可能造成目录刷新结果不完整");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
InputStream inputStream;
|
|
|
|
if (userSetting.getCivilCodeFile().startsWith("classpath:")){
|
|
|
|
String filePath = userSetting.getCivilCodeFile().substring("classpath:".length());
|
|
|
|
ClassPathResource civilCodeFile = new ClassPathResource(filePath);
|
|
|
|
if (!civilCodeFile.exists()) {
|
|
|
|
logger.warn("[行政区划] 文件<{}>不存在,可能造成目录刷新结果不完整", userSetting.getCivilCodeFile());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
inputStream = civilCodeFile.getInputStream();
|
|
|
|
|
|
|
|
}else {
|
|
|
|
File civilCodeFile = new File(userSetting.getCivilCodeFile());
|
|
|
|
if (!civilCodeFile.exists()) {
|
|
|
|
logger.warn("[行政区划] 文件<{}>不存在,可能造成目录刷新结果不完整", userSetting.getCivilCodeFile());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
inputStream = Files.newInputStream(civilCodeFile.toPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
|
int index = -1;
|
|
|
|
String line;
|
2024-03-14 14:54:20 +08:00
|
|
|
List<CivilCodePo> civilCodePoList = new ArrayList<>();
|
2023-06-19 18:42:36 +08:00
|
|
|
while ((line = inputStreamReader.readLine()) != null) {
|
|
|
|
index ++;
|
|
|
|
if (index == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
String[] infoArray = line.split(",");
|
|
|
|
CivilCodePo civilCodePo = CivilCodePo.getInstance(infoArray);
|
2024-03-14 14:54:20 +08:00
|
|
|
civilCodePoList.add(civilCodePo);
|
2023-06-19 18:42:36 +08:00
|
|
|
}
|
2024-03-14 14:54:20 +08:00
|
|
|
CivilCodeUtil.INSTANCE.add(civilCodePoList);
|
2023-06-19 18:42:36 +08:00
|
|
|
inputStreamReader.close();
|
|
|
|
inputStream.close();
|
2024-03-14 14:54:20 +08:00
|
|
|
if (civilCodePoList.isEmpty()) {
|
2023-06-19 18:42:36 +08:00
|
|
|
logger.warn("[行政区划] 文件内容为空,可能造成目录刷新结果不完整");
|
|
|
|
}else {
|
2024-03-14 14:54:20 +08:00
|
|
|
logger.info("[行政区划] 加载成功,共加载数据{}条", civilCodePoList.size());
|
2023-06-19 18:42:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|