liyong
2026-01-29 e6eb127b2ef4f297b093dcd9a5b47be457f67043
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.ruoyi.basic.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.entity.City;
import com.ruoyi.basic.entity.District;
import com.ruoyi.basic.entity.Province;
import com.ruoyi.basic.entity.dto.TreeSelectOption;
import com.ruoyi.basic.mapper.CityMapper;
import com.ruoyi.basic.mapper.DistrictMapper;
import com.ruoyi.basic.mapper.ProvinceMapper;
import com.ruoyi.basic.service.ProvinceService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 省表 服务实现类
 * </p>
 *
 * @author ruoyi
 * @since 2025-05-31
 */
@Service
@RequiredArgsConstructor
public class ProvinceServiceImpl extends ServiceImpl<ProvinceMapper, Province> implements ProvinceService {
 
    @Autowired
    private ProvinceMapper provinceMapper;
 
    @Autowired
    private CityMapper cityMapper;
 
    @Autowired
    private DistrictMapper districtMapper;
 
    @Override
    public List<TreeSelectOption> getAreaOptions() {
        List<TreeSelectOption> res = new ArrayList<>();
 
        // 批量查询所有省份
        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<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));
                        }
                    }
 
                    TreeSelectOption cityTreeSelectOption = new TreeSelectOption();
                    cityTreeSelectOption.setValue(city.getId().toString());
                    cityTreeSelectOption.setLabel(city.getName());
                    cityTreeSelectOption.setId(city.getId());
                    cityTreeSelectOption.setChildren(districts);
 
                    cities.add(cityTreeSelectOption);
                }
            }
 
            TreeSelectOption treeSelectOption = new TreeSelectOption();
            treeSelectOption.setValue(province.getId().toString());
            treeSelectOption.setLabel(province.getName());
            treeSelectOption.setId(province.getId());
            treeSelectOption.setChildren(cities);
            res.add(treeSelectOption);
        }
 
        return res;
    }
 
 
    @Override
    public String getAreaDisplay(Long provinceId, Long cityId, Long districtId) {
        Province province = provinceMapper.selectById(provinceId);
        if (province == null) {
            throw new RuntimeException("省不存在");
        }
        City city = cityMapper.selectById(cityId);
        if (city == null) {
            throw new RuntimeException("市不存在");
        }
        District district = districtMapper.selectById(districtId);
        if (district == null) {
            throw new RuntimeException("区不存在");
        }
 
        // 如果省和市同名
        if (province.getName().equals(city.getName())) {
            return province.getName() + " " + district.getName();
        }
        return province.getName() + city.getName() + district.getName();
    }
}