添加 返回省市区县街道信息接口

This commit is contained in:
liuchengqian 2022-05-07 10:00:51 +08:00
parent bed348df8d
commit 7ae8bb4cfe
2 changed files with 73 additions and 0 deletions

View File

@ -1,13 +1,16 @@
package com.xkrs.controller; package com.xkrs.controller;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import com.xkrs.common.encapsulation.PromptMessageEnum;
import com.xkrs.dao.CityDao; import com.xkrs.dao.CityDao;
import com.xkrs.dao.CountyDao; import com.xkrs.dao.CountyDao;
import com.xkrs.dao.StreetDao; import com.xkrs.dao.StreetDao;
import com.xkrs.model.entity.CityEntity; import com.xkrs.model.entity.CityEntity;
import com.xkrs.model.entity.CountyEntity; import com.xkrs.model.entity.CountyEntity;
import com.xkrs.model.entity.StreetEntity; import com.xkrs.model.entity.StreetEntity;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -15,9 +18,13 @@ import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream; import java.util.stream.Stream;
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
@RestController @RestController
public class StreetController { public class StreetController {
@ -30,6 +37,59 @@ public class StreetController {
@Resource @Resource
private StreetDao streetDao; private StreetDao streetDao;
/**
* 获取省列表
*/
@GetMapping("/getProvinceList")
public String getProvinceList() {
// 获取区域信息
Locale locale = LocaleContextHolder.getLocale();
List<Map<String, String>> provinceList = streetDao.selectProvinceList();
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, provinceList, locale);
}
/**
* 根据省编号获取市列表
*
* @param provinceCode 省编号
* @return 市列表
*/
@GetMapping("/getCityList")
public String getCityList(@RequestParam("provinceCode") String provinceCode) {
// 获取区域信息
Locale locale = LocaleContextHolder.getLocale();
List<Map<String, String>> cityList = streetDao.selectCityList(provinceCode);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, cityList, locale);
}
/**
* 根据市编号获取区县列表
*
* @param cityCode 市编号
* @return 区县列表
*/
@GetMapping("/getCountyList")
public String getCountyList(@RequestParam("cityCode") String cityCode) {
// 获取区域信息
Locale locale = LocaleContextHolder.getLocale();
List<Map<String, String>> countyList = streetDao.selectCountyList(cityCode);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, countyList, locale);
}
/**
* 根据区县编号获取街道列表
*
* @param countyCode 区县编号
* @return 街道列表
*/
@GetMapping("/getStreetList")
public String getStreetList(@RequestParam("countyCode") String countyCode) {
// 获取区域信息
Locale locale = LocaleContextHolder.getLocale();
List<Map<String, String>> streetList = streetDao.selectStreetList(countyCode);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, streetList, locale);
}
@GetMapping("/formatStreetData") @GetMapping("/formatStreetData")
public String formatStreetData() { public String formatStreetData() {
File file = new File("/Users/liuchengqian/Downloads/Administrative-divisions-of-China-master/dist/streets2.csv"); File file = new File("/Users/liuchengqian/Downloads/Administrative-divisions-of-China-master/dist/streets2.csv");

View File

@ -3,9 +3,11 @@ package com.xkrs.dao;
import com.xkrs.model.entity.StreetEntity; import com.xkrs.model.entity.StreetEntity;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
@Component @Component
@ -27,4 +29,15 @@ public interface StreetDao extends JpaRepository<StreetEntity, Integer>, JpaSpec
Optional<StreetEntity> findByStreetCode(String streetCode); Optional<StreetEntity> findByStreetCode(String streetCode);
@Query(value = "SELECT DISTINCT t.pro_code AS code, t.pro_name AS name FROM street AS t", nativeQuery = true)
List<Map<String, String>> selectProvinceList();
@Query(value = "SELECT DISTINCT t.city_code AS code, t.city_name AS name FROM street AS t WHERE t.pro_code = ?1", nativeQuery = true)
List<Map<String, String>> selectCityList(String provinceCode);
@Query(value = "SELECT DISTINCT t.county_code AS code, t.county_name AS name FROM street AS t WHERE t.city_code = ?1", nativeQuery = true)
List<Map<String, String>> selectCountyList(String cityCode);
@Query(value = "SELECT DISTINCT t.street_code AS code, t.street_name AS name FROM street AS t WHERE t.county_code = ?1", nativeQuery = true)
List<Map<String, String>> selectStreetList(String countyCode);
} }