| | |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | @Override |
| | | public List<TreeSelectOption> getAreaOptions() { |
| | | List<TreeSelectOption> res = new ArrayList<>(); |
| | | List<Province> list = provinceMapper.selectList(null); |
| | | for (Province province : list) { |
| | | // 查询省下面的市 |
| | | List<City> cityList = cityMapper.selectList(new LambdaQueryWrapper<City>().eq(City::getProvinceId, province.getId())); |
| | | |
| | | // 批量查询所有省份 |
| | | List<Province> provinces = provinceMapper.selectList(null); |
| | | if (provinces.isEmpty()) { |
| | | return res; |
| | | } |
| | | |
| | | // 获取所有省份ID用于批量查询城市 |
| | | List<Long> provinceIds = provinces.stream() |
| | | .map(Province::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 批量查询所有城市 |
| | | List<City> allCities = cityMapper.selectList( |
| | | new LambdaQueryWrapper<City>().in(City::getProvinceId, provinceIds)); |
| | | |
| | | // 将城市按省份ID分组 |
| | | Map<Long, List<City>> cityMap = allCities.stream() |
| | | .collect(Collectors.groupingBy(City::getProvinceId)); |
| | | |
| | | // 获取所有城市ID用于批量查询区域 |
| | | List<Long> cityIds = allCities.stream() |
| | | .map(City::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 批量查询所有区域 |
| | | List<District> allDistricts = districtMapper.selectList( |
| | | new LambdaQueryWrapper<District>().in(District::getCityId, cityIds)); |
| | | |
| | | // 将区域按城市ID分组 |
| | | Map<Long, List<District>> districtMap = allDistricts.stream() |
| | | .collect(Collectors.groupingBy(District::getCityId)); |
| | | |
| | | // 构建树形结构 |
| | | for (Province province : provinces) { |
| | | List<TreeSelectOption> cities = new ArrayList<>(); |
| | | |
| | | List<City> cityList = cityMap.get(province.getId()); |
| | | if (cityList != null && !cityList.isEmpty()) { |
| | | for (City city : cityList) { |
| | | // 查询市下面的区 |
| | | List<District> districtList = districtMapper.selectList(new LambdaQueryWrapper<District>().eq(District::getCityId, city.getId())); |
| | | List<TreeSelectOption> districts = new ArrayList<>(); |
| | | |
| | | List<District> districtList = districtMap.get(city.getId()); |
| | | if (districtList != null && !districtList.isEmpty()) { |
| | | for (District district : districtList) { |
| | | districts.add(new TreeSelectOption(district.getId().toString(), district.getName(), district.getId(), null)); |
| | | districts.add(new TreeSelectOption(district.getId().toString(), |
| | | district.getName(), district.getId(), null)); |
| | | } |
| | | } |
| | | |
| | | TreeSelectOption cityTreeSelectOption = new TreeSelectOption(); |
| | |
| | | cityTreeSelectOption.setChildren(districts); |
| | | |
| | | cities.add(cityTreeSelectOption); |
| | | } |
| | | } |
| | | |
| | | TreeSelectOption treeSelectOption = new TreeSelectOption(); |
| | |
| | | return res; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public String getAreaDisplay(Long provinceId, Long cityId, Long districtId) { |
| | | Province province = provinceMapper.selectById(provinceId); |