fire_point/src/main/java/com/xkrs/controller/StreetController.java

81 lines
3.2 KiB
Java

package com.xkrs.controller;
import cn.hutool.core.io.IoUtil;
import com.xkrs.dao.CityDao;
import com.xkrs.dao.CountyDao;
import com.xkrs.dao.StreetDao;
import com.xkrs.model.entity.CityEntity;
import com.xkrs.model.entity.CountyEntity;
import com.xkrs.model.entity.StreetEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
@RestController
public class StreetController {
@Resource
private CityDao cityDao;
@Resource
private CountyDao countyDao;
@Resource
private StreetDao streetDao;
@GetMapping("/formatStreetData")
public String formatStreetData() {
File file = new File("/Users/liuchengqian/Downloads/Administrative-divisions-of-China-master/dist/streets2.csv");
FileInputStream fileInputStream = IoUtil.toStream(file);
BufferedReader reader = IoUtil.getReader(fileInputStream, "UTF-8");
Stream<String> lines = reader.lines();
AtomicInteger count = new AtomicInteger();
lines.forEach(content -> {
try {
content = content.replace("\"", "");
String[] split = content.split(",");
StreetEntity streetEntity = new StreetEntity();
streetEntity.setProCode(split[3] + "0000");
List<CityEntity> byProCode1 = cityDao.findByProCode(streetEntity.getProCode());
if (byProCode1 == null || byProCode1.isEmpty()) {
streetEntity.setProName("");
} else {
streetEntity.setProName(byProCode1.get(0).getProName());
}
streetEntity.setCityCode(split[4] + "00");
List<CountyEntity> byCityCode1 = countyDao.findByCityCode(streetEntity.getCityCode());
if (byCityCode1 == null || byCityCode1.isEmpty()) {
streetEntity.setCityName("");
} else {
streetEntity.setCityName(byCityCode1.get(0).getCityName());
}
streetEntity.setCountyCode(split[2]);
List<CountyEntity> byCountyCode = countyDao.findByCountyCode(streetEntity.getCountyCode());
if (byCountyCode == null || byCountyCode.isEmpty()) {
streetEntity.setCountyName("");
} else {
streetEntity.setCountyName(byCountyCode.get(0).getCountyName());
}
streetEntity.setStreetCode(split[0]);
streetEntity.setStreetName(split[1]);
streetDao.save(streetEntity);
count.getAndIncrement();
System.out.println(content + " >>> " + count);
System.out.println(streetEntity.toString());
} catch (Exception e) {
e.printStackTrace();
System.out.println("格式化街道数据时发生异常:" + e.getMessage());
}
});
return "666";
}
}