From a770364a2323290fdf9e3247b47475a12452f401 Mon Sep 17 00:00:00 2001 From: chenhj <1263187585@qq.com> Date: 星期三, 04 六月 2025 17:57:49 +0800 Subject: [PATCH] 接口省市区对接 --- ruoyi-common/src/main/java/com/ruoyi/basic/entity/dto/SelectOption.java | 21 +++++ ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java | 2 ruoyi-system/src/main/java/com/ruoyi/basic/service/ProvinceService.java | 12 +++ basic-server/src/main/java/com/ruoyi/basic/service/impl/SupplyServiceImpl.java | 17 ++++ ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/AreaController.java | 28 +++++++ ruoyi-common/src/main/java/com/ruoyi/basic/entity/dto/TreeSelectOption.java | 25 ++++++ ruoyi-system/src/main/java/com/ruoyi/basic/service/impl/ProvinceServiceImpl.java | 100 ++++++++++++++++++++++--- 7 files changed, 192 insertions(+), 13 deletions(-) diff --git a/basic-server/src/main/java/com/ruoyi/basic/service/impl/SupplyServiceImpl.java b/basic-server/src/main/java/com/ruoyi/basic/service/impl/SupplyServiceImpl.java index 3f406b0..9946698 100644 --- a/basic-server/src/main/java/com/ruoyi/basic/service/impl/SupplyServiceImpl.java +++ b/basic-server/src/main/java/com/ruoyi/basic/service/impl/SupplyServiceImpl.java @@ -65,6 +65,23 @@ public int addOrEditSupply(SupplyDto supplyDto) { Supply supply = new Supply(); BeanUtils.copyProperties(supplyDto, supply); + if (supplyDto.getBids().size() != 3) { + throw new RuntimeException("璇烽�夋嫨缁忚惀鍦板潃鐪佸競鍖�"); + } + + if (supplyDto.getCids().size() != 3) { + throw new RuntimeException("璇烽�夋嫨鑱旂郴鍦板潃鐪佸競鍖�"); + } + + supply.setBProvinceId(supplyDto.getBids().get(0)); + supply.setBCityId(supplyDto.getBids().get(1)); + supply.setBDistrictId(supplyDto.getBids().get(2)); + + supply.setCProvinceId(supplyDto.getCids().get(0)); + supply.setCCityId(supplyDto.getCids().get(1)); + supply.setCDistrictId(supplyDto.getCids().get(2)); + + if (Objects.isNull(supplyDto.getId())) { return supplyMapper.insert(supply); } else { diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/AreaController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/AreaController.java new file mode 100644 index 0000000..c408e4e --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/AreaController.java @@ -0,0 +1,28 @@ +package com.ruoyi.web.controller.system; + +import com.ruoyi.basic.service.ProvinceService; +import com.ruoyi.common.core.domain.R; +import lombok.AllArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * <p> + * 鍦板尯鎺ュ彛 + * </p> + */ + +@RestController +@AllArgsConstructor +@RequestMapping("/area") +public class AreaController { + @Autowired + private ProvinceService provinceService; + + @GetMapping("/options") + public R getOptions() { + return R.ok(provinceService.getAreaOptions()); + } +} diff --git a/ruoyi-common/src/main/java/com/ruoyi/basic/entity/dto/SelectOption.java b/ruoyi-common/src/main/java/com/ruoyi/basic/entity/dto/SelectOption.java new file mode 100644 index 0000000..7c9c157 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/basic/entity/dto/SelectOption.java @@ -0,0 +1,21 @@ +package com.ruoyi.basic.entity.dto; + +import lombok.Data; + +/** + * 涓嬫媺妗嗛�夐」閫氱敤杩斿洖缁撴瀯浣� + */ +@Data +public class SelectOption { + private String value; + private String label; + private Long id; + + public SelectOption(String value, String label, Long id) { + this.value = value; + this.label = label; + this.id = id; + } + + public SelectOption() {} +} diff --git a/ruoyi-common/src/main/java/com/ruoyi/basic/entity/dto/TreeSelectOption.java b/ruoyi-common/src/main/java/com/ruoyi/basic/entity/dto/TreeSelectOption.java new file mode 100644 index 0000000..8995318 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/basic/entity/dto/TreeSelectOption.java @@ -0,0 +1,25 @@ +package com.ruoyi.basic.entity.dto; + +import lombok.Data; + +import java.util.List; + +/** + * 鏍戦�夐」閫氱敤杩斿洖缁撴瀯浣� + */ +@Data +public class TreeSelectOption { + private String value; + private String label; + private Long id; + private List<TreeSelectOption> children; + + public TreeSelectOption(String value, String label, Long id, List<TreeSelectOption> children) { + this.value = value; + this.label = label; + this.id = id; + this.children = children; + } + + public TreeSelectOption() {} +} diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java index 2279f3c..6b82043 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java @@ -64,7 +64,7 @@ public String login(String username, String password, String code, String uuid) { // 楠岃瘉鐮佹牎楠� - validateCaptcha(username, code, uuid); +// validateCaptcha(username, code, uuid); // 鐧诲綍鍓嶇疆鏍¢獙 loginPreCheck(username, password); // 鐢ㄦ埛楠岃瘉 diff --git a/ruoyi-system/src/main/java/com/ruoyi/basic/service/ProvinceService.java b/ruoyi-system/src/main/java/com/ruoyi/basic/service/ProvinceService.java index 3f1a1d3..b2e3724 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/basic/service/ProvinceService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/basic/service/ProvinceService.java @@ -2,6 +2,9 @@ import com.ruoyi.basic.entity.Province; import com.baomidou.mybatisplus.extension.service.IService; +import com.ruoyi.basic.entity.dto.TreeSelectOption; + +import java.util.List; /** * <p> @@ -13,4 +16,13 @@ */ public interface ProvinceService extends IService<Province> { + + /** + * 鑾峰彇鍖哄煙鏍戦�夐」 + * + * @return List<TreeSelectOption> + */ + List<TreeSelectOption> getAreaOptions(); + + String getAreaDisplay(Long provinceId, Long cityId, Long districtId); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/basic/service/impl/ProvinceServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/basic/service/impl/ProvinceServiceImpl.java index 9c9aa6b..0fb305b 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/basic/service/impl/ProvinceServiceImpl.java +++ b/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(); + } +} -- Gitblit v1.9.3