diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/controller/DiyCityController.java b/ruoyi-system/src/main/java/com/ruoyi/system/controller/DiyCityController.java index 2174bf0..a3f5726 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/controller/DiyCityController.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/controller/DiyCityController.java @@ -1,6 +1,9 @@ package com.ruoyi.system.controller; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import javax.servlet.http.HttpServletResponse; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.beans.factory.annotation.Autowired; @@ -20,12 +23,13 @@ import com.ruoyi.system.domain.DiyCity; import com.ruoyi.system.service.IDiyCityService; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.core.page.TableDataInfo; +import java.util.Objects; /** * 自定义地区Controller - * + * * @author ruoyi - * @date 2025-05-13 + * @date 2025-05-30 */ @RestController @RequestMapping("/system/DiyCity") @@ -45,7 +49,44 @@ public class DiyCityController extends BaseController List list = diyCityService.selectDiyCityList(diyCity); return getDataTable(list); } + /** + * 获取自定义地区树形结构数据 + */ + @PreAuthorize("@ss.hasPermi('system:DiyCity:query')") + @GetMapping("/getTreeData") + public AjaxResult getTreeData() { + List list = diyCityService.selectDiyCityList(new DiyCity()); + List> rootList = new ArrayList<>(); + for (DiyCity city : list) { + Long parentId = city.getParentId(); + Long id = Long.valueOf(city.getId()); + // 一级节点 + if (parentId == null || parentId == 0L) { + Map node = toMap(city); + // 组装children + List> children = new ArrayList<>(); + for (DiyCity child : list) { + Long childParentId = child.getParentId(); + if (id != null && Objects.equals(id, childParentId)) { + children.add(toMap(child)); + } + } + node.put("children", children); + rootList.add(node); + } + } + return success(rootList); + } + + private Map toMap(DiyCity city) { + Map map = new HashMap<>(); + map.put("id", city.getId()); + map.put("label", city.getTitle()); + map.put("parentId", city.getParentId()); + // 其他字段按需添加 + return map; + } /** * 导出自定义地区列表 */ @@ -96,9 +137,122 @@ public class DiyCityController extends BaseController */ @PreAuthorize("@ss.hasPermi('system:DiyCity:remove')") @Log(title = "自定义地区", businessType = BusinessType.DELETE) - @DeleteMapping("/{ids}") + @DeleteMapping("/{ids}") public AjaxResult remove(@PathVariable Integer[] ids) { return toAjax(diyCityService.deleteDiyCityByIds(ids)); } } + + + +//package com.ruoyi.system.controller; +// +//import java.util.ArrayList; +//import java.util.HashMap; +//import java.util.List; +//import java.util.Map; +//import javax.servlet.http.HttpServletResponse; +//import org.springframework.security.access.prepost.PreAuthorize; +//import org.springframework.beans.factory.annotation.Autowired; +//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.system.domain.DiyCity; +//import com.ruoyi.system.service.IDiyCityService; +//import com.ruoyi.common.utils.poi.ExcelUtil; +//import com.ruoyi.common.core.page.TableDataInfo; +// +///** +// * 自定义地区Controller +// * +// * @author ruoyi +// * @date 2025-05-13 +// */ +//@RestController +//@RequestMapping("/system/DiyCity") +//public class DiyCityController extends BaseController +//{ +// @Autowired +// private IDiyCityService diyCityService; +// +// /** +// * 查询自定义地区列表 +// */ +// @PreAuthorize("@ss.hasPermi('system:DiyCity:list')") +// @GetMapping("/list") +// public TableDataInfo list(DiyCity diyCity) +// { +// startPage(); +// List list = diyCityService.selectDiyCityList(diyCity); +// return getDataTable(list); +// } +// +// + +// +// /** +// * 导出自定义地区列表 +// */ +// @PreAuthorize("@ss.hasPermi('system:DiyCity:export')") +// @Log(title = "自定义地区", businessType = BusinessType.EXPORT) +// @PostMapping("/export") +// public void export(HttpServletResponse response, DiyCity diyCity) +// { +// List list = diyCityService.selectDiyCityList(diyCity); +// ExcelUtil util = new ExcelUtil(DiyCity.class); +// util.exportExcel(response, list, "自定义地区数据"); +// } +// +// /** +// * 获取自定义地区详细信息 +// */ +// @PreAuthorize("@ss.hasPermi('system:DiyCity:query')") +// @GetMapping(value = "/{id}") +// public AjaxResult getInfo(@PathVariable("id") Integer id) +// { +// return success(diyCityService.selectDiyCityById(id)); +// } +// +// /** +// * 新增自定义地区 +// */ +// @PreAuthorize("@ss.hasPermi('system:DiyCity:add')") +// @Log(title = "自定义地区", businessType = BusinessType.INSERT) +// @PostMapping +// public AjaxResult add(@RequestBody DiyCity diyCity) +// { +// return toAjax(diyCityService.insertDiyCity(diyCity)); +// } +// +// /** +// * 修改自定义地区 +// */ +// @PreAuthorize("@ss.hasPermi('system:DiyCity:edit')") +// @Log(title = "自定义地区", businessType = BusinessType.UPDATE) +// @PutMapping +// public AjaxResult edit(@RequestBody DiyCity diyCity) +// { +// return toAjax(diyCityService.updateDiyCity(diyCity)); +// } +// +// /** +// * 删除自定义地区 +// */ +// @PreAuthorize("@ss.hasPermi('system:DiyCity:remove')") +// @Log(title = "自定义地区", businessType = BusinessType.DELETE) +// @DeleteMapping("/{ids}") +// public AjaxResult remove(@PathVariable Integer[] ids) +// { +// return toAjax(diyCityService.deleteDiyCityByIds(ids)); +// } +//} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/DiyCity.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/DiyCity.java index f08a635..c6c30fc 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/domain/DiyCity.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/DiyCity.java @@ -1,7 +1,6 @@ package com.ruoyi.system.domain; import java.util.Date; -import java.util.List; import com.fasterxml.jackson.annotation.JsonFormat; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; @@ -10,9 +9,9 @@ import com.ruoyi.common.core.domain.BaseEntity; /** * 自定义地区对象 diy_city - * + * * @author ruoyi - * @date 2025-05-13 + * @date 2025-05-30 */ public class DiyCity extends BaseEntity { @@ -61,140 +60,130 @@ public class DiyCity extends BaseEntity @Excel(name = "${comment}", readConverterExp = "$column.readConverterExp()") private Date updatedAt; - private List children; - - public void setId(Integer id) + public void setId(Integer id) { this.id = id; } - public Integer getId() + public Integer getId() { return id; } - public void setTitle(String title) + public void setTitle(String title) { this.title = title; } - public String getTitle() + public String getTitle() { return title; } - public void setParentId(Long parentId) + public void setParentId(Long parentId) { this.parentId = parentId; } - public Long getParentId() + public Long getParentId() { return parentId; } - public void setOrder(Long order) + public void setOrder(Long order) { this.order = order; } - public Long getOrder() + public Long getOrder() { return order; } - public void setLat(String lat) + public void setLat(String lat) { this.lat = lat; } - public String getLat() + public String getLat() { return lat; } - public void setLng(String lng) + public void setLng(String lng) { this.lng = lng; } - public String getLng() + public String getLng() { return lng; } - public void setProvinceId(Long provinceId) + public void setProvinceId(Long provinceId) { this.provinceId = provinceId; } - public Long getProvinceId() + public Long getProvinceId() { return provinceId; } - public void setCityId(Long cityId) + public void setCityId(Long cityId) { this.cityId = cityId; } - public Long getCityId() + public Long getCityId() { return cityId; } - public void setDistrictId(Long districtId) + public void setDistrictId(Long districtId) { this.districtId = districtId; } - public Long getDistrictId() + public Long getDistrictId() { return districtId; } - public void setCreatedAt(Date createdAt) + public void setCreatedAt(Date createdAt) { this.createdAt = createdAt; } - public Date getCreatedAt() + public Date getCreatedAt() { return createdAt; } - public void setUpdatedAt(Date updatedAt) + public void setUpdatedAt(Date updatedAt) { this.updatedAt = updatedAt; } - public Date getUpdatedAt() + public Date getUpdatedAt() { return updatedAt; } - public List getChildren() { - return children; - } - - public void setChildren(List children) { - this.children = children; - } - @Override public String toString() { return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) - .append("id", getId()) - .append("title", getTitle()) - .append("parentId", getParentId()) - .append("order", getOrder()) - .append("lat", getLat()) - .append("lng", getLng()) - .append("provinceId", getProvinceId()) - .append("cityId", getCityId()) - .append("districtId", getDistrictId()) - .append("createdAt", getCreatedAt()) - .append("updatedAt", getUpdatedAt()) - .toString(); + .append("id", getId()) + .append("title", getTitle()) + .append("parentId", getParentId()) + .append("order", getOrder()) + .append("lat", getLat()) + .append("lng", getLng()) + .append("provinceId", getProvinceId()) + .append("cityId", getCityId()) + .append("districtId", getDistrictId()) + .append("createdAt", getCreatedAt()) + .append("updatedAt", getUpdatedAt()) + .toString(); } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/DiyCityMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/DiyCityMapper.java index a85a9f8..7893026 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/DiyCityMapper.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/DiyCityMapper.java @@ -5,15 +5,15 @@ import com.ruoyi.system.domain.DiyCity; /** * 自定义地区Mapper接口 - * + * * @author ruoyi - * @date 2025-05-13 + * @date 2025-05-30 */ -public interface DiyCityMapper +public interface DiyCityMapper { /** * 查询自定义地区 - * + * * @param id 自定义地区主键 * @return 自定义地区 */ @@ -21,7 +21,7 @@ public interface DiyCityMapper /** * 查询自定义地区列表 - * + * * @param diyCity 自定义地区 * @return 自定义地区集合 */ @@ -29,7 +29,7 @@ public interface DiyCityMapper /** * 新增自定义地区 - * + * * @param diyCity 自定义地区 * @return 结果 */ @@ -37,7 +37,7 @@ public interface DiyCityMapper /** * 修改自定义地区 - * + * * @param diyCity 自定义地区 * @return 结果 */ @@ -45,7 +45,7 @@ public interface DiyCityMapper /** * 删除自定义地区 - * + * * @param id 自定义地区主键 * @return 结果 */ @@ -53,7 +53,7 @@ public interface DiyCityMapper /** * 批量删除自定义地区 - * + * * @param ids 需要删除的数据主键集合 * @return 结果 */ diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/IDiyCityService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/IDiyCityService.java index 34f1575..bb463b6 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/IDiyCityService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/IDiyCityService.java @@ -5,15 +5,15 @@ import com.ruoyi.system.domain.DiyCity; /** * 自定义地区Service接口 - * + * * @author ruoyi - * @date 2025-05-13 + * @date 2025-05-30 */ -public interface IDiyCityService +public interface IDiyCityService { /** * 查询自定义地区 - * + * * @param id 自定义地区主键 * @return 自定义地区 */ @@ -21,7 +21,7 @@ public interface IDiyCityService /** * 查询自定义地区列表 - * + * * @param diyCity 自定义地区 * @return 自定义地区集合 */ @@ -29,7 +29,7 @@ public interface IDiyCityService /** * 新增自定义地区 - * + * * @param diyCity 自定义地区 * @return 结果 */ @@ -37,7 +37,7 @@ public interface IDiyCityService /** * 修改自定义地区 - * + * * @param diyCity 自定义地区 * @return 结果 */ @@ -45,7 +45,7 @@ public interface IDiyCityService /** * 批量删除自定义地区 - * + * * @param ids 需要删除的自定义地区主键集合 * @return 结果 */ @@ -53,7 +53,7 @@ public interface IDiyCityService /** * 删除自定义地区信息 - * + * * @param id 自定义地区主键 * @return 结果 */ diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/DiyCityServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/DiyCityServiceImpl.java index 38b01a2..6802242 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/DiyCityServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/DiyCityServiceImpl.java @@ -1,9 +1,6 @@ package com.ruoyi.system.service.impl; import java.util.List; -import java.util.Map; -import java.util.HashMap; -import java.util.ArrayList; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ruoyi.system.mapper.DiyCityMapper; @@ -12,19 +9,19 @@ import com.ruoyi.system.service.IDiyCityService; /** * 自定义地区Service业务层处理 - * + * * @author ruoyi - * @date 2025-05-13 + * @date 2025-05-30 */ @Service -public class DiyCityServiceImpl implements IDiyCityService +public class DiyCityServiceImpl implements IDiyCityService { @Autowired private DiyCityMapper diyCityMapper; /** * 查询自定义地区 - * + * * @param id 自定义地区主键 * @return 自定义地区 */ @@ -36,43 +33,19 @@ public class DiyCityServiceImpl implements IDiyCityService /** * 查询自定义地区列表 - * + * * @param diyCity 自定义地区 * @return 自定义地区 */ @Override public List selectDiyCityList(DiyCity diyCity) { - List cities = diyCityMapper.selectDiyCityList(diyCity); - return buildTree(cities); - } - - private List buildTree(List cities) { - Map map = new HashMap<>(); - List roots = new ArrayList<>(); - - for (DiyCity city : cities) { - map.put(city.getId(), city); - city.setChildren(new ArrayList<>()); - } - - for (DiyCity city : cities) { - if (city.getParentId() == null) { - roots.add(city); - } else { - DiyCity parent = map.get(city.getParentId()); - if (parent != null) { - parent.getChildren().add(city); - } - } - } - - return roots; + return diyCityMapper.selectDiyCityList(diyCity); } /** * 新增自定义地区 - * + * * @param diyCity 自定义地区 * @return 结果 */ @@ -84,7 +57,7 @@ public class DiyCityServiceImpl implements IDiyCityService /** * 修改自定义地区 - * + * * @param diyCity 自定义地区 * @return 结果 */ @@ -96,7 +69,7 @@ public class DiyCityServiceImpl implements IDiyCityService /** * 批量删除自定义地区 - * + * * @param ids 需要删除的自定义地区主键 * @return 结果 */ @@ -108,7 +81,7 @@ public class DiyCityServiceImpl implements IDiyCityService /** * 删除自定义地区信息 - * + * * @param id 自定义地区主键 * @return 结果 */ diff --git a/ruoyi-system/src/main/resources/mapper/system/DiyCityMapper.xml b/ruoyi-system/src/main/resources/mapper/system/DiyCityMapper.xml index 97710b0..f3565c8 100644 --- a/ruoyi-system/src/main/resources/mapper/system/DiyCityMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/DiyCityMapper.xml @@ -36,7 +36,6 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" and created_at = #{createdAt} and updated_at = #{updatedAt} - order by id desc