diff --git a/src/main/java/com/xkrs/controller/StreetController.java b/src/main/java/com/xkrs/controller/StreetController.java index 07fa55d..a641b62 100644 --- a/src/main/java/com/xkrs/controller/StreetController.java +++ b/src/main/java/com/xkrs/controller/StreetController.java @@ -1,13 +1,16 @@ package com.xkrs.controller; import cn.hutool.core.io.IoUtil; +import com.xkrs.common.encapsulation.PromptMessageEnum; 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.context.i18n.LocaleContextHolder; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @@ -15,9 +18,13 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.util.List; +import java.util.Locale; +import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Stream; +import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject; + @RestController public class StreetController { @@ -30,6 +37,59 @@ public class StreetController { @Resource private StreetDao streetDao; + /** + * 获取省列表 + */ + @GetMapping("/getProvinceList") + public String getProvinceList() { + // 获取区域信息 + Locale locale = LocaleContextHolder.getLocale(); + List> 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> 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> 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> streetList = streetDao.selectStreetList(countyCode); + return outputEncapsulationObject(PromptMessageEnum.SUCCESS, streetList, locale); + } + @GetMapping("/formatStreetData") public String formatStreetData() { File file = new File("/Users/liuchengqian/Downloads/Administrative-divisions-of-China-master/dist/streets2.csv"); diff --git a/src/main/java/com/xkrs/dao/StreetDao.java b/src/main/java/com/xkrs/dao/StreetDao.java index 2f41595..c4a77be 100644 --- a/src/main/java/com/xkrs/dao/StreetDao.java +++ b/src/main/java/com/xkrs/dao/StreetDao.java @@ -3,9 +3,11 @@ package com.xkrs.dao; import com.xkrs.model.entity.StreetEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Component; import java.util.List; +import java.util.Map; import java.util.Optional; @Component @@ -27,4 +29,15 @@ public interface StreetDao extends JpaRepository, JpaSpec Optional findByStreetCode(String streetCode); + @Query(value = "SELECT DISTINCT t.pro_code AS code, t.pro_name AS name FROM street AS t", nativeQuery = true) + List> 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> 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> 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> selectStreetList(String countyCode); }