1
This commit is contained in:
parent
a6261ae239
commit
50065a8496
@ -1,32 +1,22 @@
|
||||
package com.ruoyi.base.controller;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.ruoyi.base.domain.BaseCity;
|
||||
import com.ruoyi.base.service.IBaseCityService;
|
||||
import com.ruoyi.base.vo.BaseCityVO;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import com.ruoyi.common.annotation.Log;
|
||||
import com.ruoyi.common.core.controller.BaseController;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.enums.BusinessType;
|
||||
import com.ruoyi.base.domain.BaseCity;
|
||||
import com.ruoyi.base.service.IBaseCityService;
|
||||
import com.ruoyi.common.utils.poi.ExcelUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 城市Controller
|
||||
@ -52,10 +42,11 @@ public class BaseCityController extends BaseController {
|
||||
|
||||
/**
|
||||
* 查询城市数列表
|
||||
*
|
||||
* @return com.ruoyi.common.core.domain.AjaxResult
|
||||
* @author PQZ
|
||||
* @date 15:11 2025/4/2
|
||||
* @return com.ruoyi.common.core.domain.AjaxResult
|
||||
**/
|
||||
**/
|
||||
@GetMapping("/treeCity")
|
||||
public AjaxResult treeCity() {
|
||||
List<BaseCityVO> list = baseCityService.treeCity();
|
||||
@ -75,14 +66,6 @@ public class BaseCityController extends BaseController {
|
||||
util.exportExcel(response, list, "城市数据");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取城市详细信息
|
||||
*/
|
||||
@PreAuthorize("@ss.hasPermi('base:city:query')")
|
||||
@GetMapping(value = "/{id}")
|
||||
public AjaxResult getInfo(@PathVariable("id") Long id) {
|
||||
return success(baseCityService.getById(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增城市
|
||||
@ -104,6 +87,15 @@ public class BaseCityController extends BaseController {
|
||||
return toAjax(baseCityService.updateById(baseCity));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取城市详细信息
|
||||
*/
|
||||
@GetMapping(value = "/{id}")
|
||||
public AjaxResult getInfo(@PathVariable("id") Long id) {
|
||||
return success(baseCityService.getById(id));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除城市
|
||||
*/
|
||||
|
@ -1,8 +1,11 @@
|
||||
package com.ruoyi.base.service.impl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.ruoyi.base.vo.BaseCityVO;
|
||||
import com.ruoyi.common.utils.DateUtils;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
@ -16,13 +19,12 @@ import com.ruoyi.base.service.IBaseCityService;
|
||||
|
||||
/**
|
||||
* 城市Service业务层处理
|
||||
*
|
||||
*
|
||||
* @author vinjor-m
|
||||
* @date 2025-03-18
|
||||
*/
|
||||
@Service
|
||||
public class BaseCityServiceImpl extends ServiceImpl<BaseCityMapper,BaseCity> implements IBaseCityService
|
||||
{
|
||||
public class BaseCityServiceImpl extends ServiceImpl<BaseCityMapper, BaseCity> implements IBaseCityService {
|
||||
@Autowired
|
||||
private BaseCityMapper baseCityMapper;
|
||||
|
||||
@ -40,12 +42,41 @@ public class BaseCityServiceImpl extends ServiceImpl<BaseCityMapper,BaseCity> i
|
||||
**/
|
||||
@Override
|
||||
public List<BaseCityVO> treeCity() {
|
||||
|
||||
return null;
|
||||
LambdaQueryWrapper<BaseCity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||
lambdaQueryWrapper.eq(BaseCity::getIsShow, 1);
|
||||
List<BaseCity> list = list(lambdaQueryWrapper);
|
||||
return buildCityTree(list);
|
||||
}
|
||||
|
||||
private List<BaseCityVO> buildCityTree(List<BaseCityVO> list) {
|
||||
/**
|
||||
* 生成树结构
|
||||
*
|
||||
* @param list List<BaseCity>
|
||||
* @return java.util.List<com.ruoyi.base.vo.BaseCityVO>
|
||||
* @author PQZ
|
||||
* @date 9:44 2025/4/3
|
||||
**/
|
||||
private List<BaseCityVO> buildCityTree(List<BaseCity> list) {
|
||||
List<BaseCityVO> tree = new ArrayList<>();
|
||||
//创建一个Map来存储每个城市及子城市
|
||||
Map<Long, BaseCityVO> cityMap = new HashMap<>();
|
||||
for (BaseCity city : list) {
|
||||
cityMap.put(city.getCityId(), new BaseCityVO(city.getCityId(), city.getName()));
|
||||
}
|
||||
//组装城市VO列表
|
||||
for (BaseCity city : list) {
|
||||
BaseCityVO cityVO = cityMap.get(city.getCityId());
|
||||
if (city.getParentId() == 0) {
|
||||
//跟城市添加到列表
|
||||
tree.add(cityVO);
|
||||
} else {
|
||||
//非根城市天际到其父城市的孩子中
|
||||
BaseCityVO parentCityVO = cityMap.get(city.getParentId());
|
||||
if (parentCityVO != null) {
|
||||
parentCityVO.getChildren().add(cityVO);
|
||||
}
|
||||
}
|
||||
}
|
||||
return tree;
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,21 @@
|
||||
package com.ruoyi.base.vo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class BaseCityVO {
|
||||
String text;
|
||||
String value;
|
||||
Long value;
|
||||
List<BaseCityVO> children;
|
||||
|
||||
public BaseCityVO(Long value,String text) {
|
||||
this.text = text;
|
||||
this.value = value;
|
||||
this.children = new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user