liding
2 天以前 befc0e5606ab7c913dda0346152a4150d0ee5f79
ruoyi-system/src/main/java/com/ruoyi/basic/service/impl/ProvinceServiceImpl.java
@@ -1,22 +1,98 @@
package com.ruoyi.basic.service.impl;
import com.ruoyi.basic.entity.Province;
import com.ruoyi.basic.mapper.ProvinceMapper;
    import com.ruoyi.basic.service.ProvinceService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
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;
/**
* <p>
    * 省表 服务实现类
    * </p>
*
* @author ruoyi
* @since 2025-05-31
*/
 * <p>
 * 省表 服务实现类
 * </p>
 *
 * @author ruoyi
 * @since 2025-05-31
 */
@Service
@RequiredArgsConstructor
    public class ProvinceServiceImpl extends ServiceImpl<ProvinceMapper, Province> implements ProvinceService {
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> list = provinceMapper.selectList(null);
        for (Province province : list) {
            // 查询省下面的市
            List<City> cityList = cityMapper.selectList(new LambdaQueryWrapper<City>().eq(City::getProvinceId, province.getId()));
            List<TreeSelectOption> cities = new ArrayList<>();
            for (City city : cityList) {
                // 查询市下面的区
                List<District> districtList = districtMapper.selectList(new LambdaQueryWrapper<District>().eq(District::getCityId, city.getId()));
                List<TreeSelectOption> districts = new ArrayList<>();
                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();
    }
}