标准分类模块开发
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package com.dc.dc_project.controller;
|
||||
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.dc.dc_project.common.ResponseResult;
|
||||
import com.dc.dc_project.model.dto.StandardCategoryDto;
|
||||
import com.dc.dc_project.model.dto.StandardCategoryQueDto;
|
||||
import com.dc.dc_project.model.dto.StandardCategoryUpdateDto;
|
||||
import com.dc.dc_project.service.StandardCategoryService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -20,12 +23,23 @@ public class StandardCategoryController {
|
||||
private final StandardCategoryService standardCategoryService;
|
||||
|
||||
@PostMapping("/list")
|
||||
public String list() {
|
||||
return standardCategoryService.list().toString();
|
||||
public ResponseResult list(@RequestBody StandardCategoryQueDto standardCategoryDto) {
|
||||
Long userId = StpUtil.getLoginIdAsLong();
|
||||
return standardCategoryService.getList(standardCategoryDto);
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public ResponseResult add(@RequestBody @Validated StandardCategoryDto standardCategoryDto) {
|
||||
return standardCategoryService.add(standardCategoryDto);
|
||||
}
|
||||
|
||||
@PostMapping("/delete")
|
||||
public ResponseResult delete(@RequestBody StandardCategoryQueDto standardCategoryDto) {
|
||||
return standardCategoryService.delete(standardCategoryDto);
|
||||
}
|
||||
|
||||
@PostMapping("/update")
|
||||
public ResponseResult update(@RequestBody StandardCategoryUpdateDto standardCategoryDto) {
|
||||
return standardCategoryService.updateStandardCategory(standardCategoryDto);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.dc.dc_project.model.dto;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
@@ -19,6 +20,7 @@ public class StandardCategoryDto {
|
||||
/**
|
||||
* 分类名称(如水利、电力、公路等)
|
||||
*/
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.dc.dc_project.model.dto;
|
||||
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class StandardCategoryQueDto {
|
||||
|
||||
/**
|
||||
* 上级分类ID
|
||||
*/
|
||||
private Long parentId = 0L;
|
||||
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 分类名称(如水利、电力、公路等)
|
||||
*/
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 分类编码(可用于同步外部标准库)
|
||||
*/
|
||||
private String code;
|
||||
|
||||
/**
|
||||
* 排序号
|
||||
*/
|
||||
private Integer sortOrder;
|
||||
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
private String remark;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.dc.dc_project.model.dto;
|
||||
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class StandardCategoryUpdateDto {
|
||||
|
||||
/**
|
||||
* 上级分类ID
|
||||
*/
|
||||
private Long parentId = 0L;
|
||||
|
||||
@NotNull
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 分类名称(如水利、电力、公路等)
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 分类编码(可用于同步外部标准库)
|
||||
*/
|
||||
private String code;
|
||||
|
||||
/**
|
||||
* 排序号
|
||||
*/
|
||||
private Integer sortOrder;
|
||||
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
private String remark;
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package com.dc.dc_project.service;
|
||||
|
||||
import com.dc.dc_project.common.ResponseResult;
|
||||
import com.dc.dc_project.model.dto.StandardCategoryDto;
|
||||
import com.dc.dc_project.model.dto.StandardCategoryQueDto;
|
||||
import com.dc.dc_project.model.dto.StandardCategoryUpdateDto;
|
||||
import com.dc.dc_project.model.pojo.StandardCategory;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
@@ -19,4 +21,28 @@ public interface StandardCategoryService extends IService<StandardCategory> {
|
||||
* @return
|
||||
**/
|
||||
ResponseResult add(StandardCategoryDto standardCategoryDto);
|
||||
|
||||
/**
|
||||
* 获取标准分类列表
|
||||
* @param standardCategoryDto
|
||||
* @
|
||||
* @return
|
||||
**/
|
||||
ResponseResult getList(StandardCategoryQueDto standardCategoryDto);
|
||||
|
||||
/**
|
||||
* 删除标准分类
|
||||
* @param standardCategoryDto
|
||||
* @
|
||||
* @return
|
||||
**/
|
||||
ResponseResult delete(StandardCategoryQueDto standardCategoryDto);
|
||||
|
||||
/**
|
||||
* 修改标准分类
|
||||
* @param standardCategoryDto
|
||||
* @
|
||||
* @return
|
||||
**/
|
||||
ResponseResult updateStandardCategory(StandardCategoryUpdateDto standardCategoryDto);
|
||||
}
|
||||
|
||||
@@ -4,12 +4,16 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.dc.dc_project.common.ResponseResult;
|
||||
import com.dc.dc_project.model.dto.StandardCategoryDto;
|
||||
import com.dc.dc_project.model.dto.StandardCategoryQueDto;
|
||||
import com.dc.dc_project.model.dto.StandardCategoryUpdateDto;
|
||||
import com.dc.dc_project.model.pojo.StandardCategory;
|
||||
import com.dc.dc_project.service.StandardCategoryService;
|
||||
import com.dc.dc_project.mapper.StandardCategoryMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author ADMIN
|
||||
* @description 针对表【sys_standard_category(标准分类表(行业/专业领域))】的数据库操作Service实现
|
||||
@@ -45,6 +49,63 @@ public class StandardCategoryServiceImpl extends ServiceImpl<StandardCategoryMap
|
||||
log.info("添加成功");
|
||||
return ResponseResult.success("添加成功");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseResult getList(StandardCategoryQueDto standardCategoryDto) {
|
||||
LambdaQueryWrapper<StandardCategory> queryWrapper = new LambdaQueryWrapper<StandardCategory>()
|
||||
.eq(StandardCategory::getIsDeleted, 0)
|
||||
.eq(standardCategoryDto.getParentId() != null, StandardCategory::getParentId, standardCategoryDto.getParentId())
|
||||
.eq(standardCategoryDto.getId() != null, StandardCategory::getId, standardCategoryDto.getId())
|
||||
.like(standardCategoryDto.getName() != null, StandardCategory::getName, standardCategoryDto.getName())
|
||||
.like(standardCategoryDto.getCode() != null, StandardCategory::getCode, standardCategoryDto.getCode())
|
||||
.like(standardCategoryDto.getRemark() != null, StandardCategory::getRemark, standardCategoryDto.getRemark());
|
||||
List<StandardCategory> list = this.list(queryWrapper);
|
||||
return ResponseResult.success(list);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseResult delete(StandardCategoryQueDto standardCategoryDto) {
|
||||
if(standardCategoryDto.getId() == null)
|
||||
return ResponseResult.error("请选择要删除的分类");
|
||||
LambdaQueryWrapper<StandardCategory> queryWrapper = new LambdaQueryWrapper<StandardCategory>()
|
||||
.eq(StandardCategory::getId, standardCategoryDto.getId())
|
||||
.eq(StandardCategory::getIsDeleted, 0);
|
||||
StandardCategory standardCategory = this.getOne(queryWrapper);
|
||||
if(standardCategory == null){
|
||||
return ResponseResult.error("该分类不存在");
|
||||
}
|
||||
LambdaQueryWrapper<StandardCategory> queryWrapper1 = new LambdaQueryWrapper<StandardCategory>()
|
||||
.eq(StandardCategory::getParentId, standardCategory.getId())
|
||||
.eq(StandardCategory::getIsDeleted, 0);
|
||||
if(this.count(queryWrapper1) > 0){
|
||||
return ResponseResult.error("该分类下有子分类,请先删除或修改子分类");
|
||||
}
|
||||
standardCategory.setIsDeleted(1);
|
||||
if(!this.updateById(standardCategory)){
|
||||
return ResponseResult.error("删除失败");
|
||||
}
|
||||
return ResponseResult.success("删除成功");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseResult updateStandardCategory(StandardCategoryUpdateDto standardCategoryDto) {
|
||||
LambdaQueryWrapper<StandardCategory> queryWrapper = new LambdaQueryWrapper<StandardCategory>()
|
||||
.eq(StandardCategory::getId, standardCategoryDto.getId())
|
||||
.eq(StandardCategory::getIsDeleted, 0);
|
||||
StandardCategory standardCategory = this.getOne(queryWrapper);
|
||||
if(standardCategory == null){
|
||||
return ResponseResult.error("该分类不存在");
|
||||
}
|
||||
standardCategory.setName(standardCategoryDto.getName() != null ? standardCategoryDto.getName() : standardCategory.getName());
|
||||
standardCategory.setCode(standardCategoryDto.getCode() != null ? standardCategoryDto.getCode() : standardCategory.getCode());
|
||||
standardCategory.setSortOrder(standardCategoryDto.getSortOrder() != null ? standardCategoryDto.getSortOrder() : standardCategory.getSortOrder());
|
||||
standardCategory.setRemark(standardCategoryDto.getRemark() != null ? standardCategoryDto.getRemark() : standardCategory.getRemark());
|
||||
standardCategory.setParentId(standardCategoryDto.getParentId() != null ? standardCategoryDto.getParentId() : standardCategory.getParentId());
|
||||
if(!this.updateById(standardCategory)){
|
||||
return ResponseResult.error("修改失败");
|
||||
}
|
||||
return ResponseResult.success("修改成功");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2,9 +2,7 @@ package com.dc.dc_project.utils;
|
||||
|
||||
import com.dc.dc_project.model.TreeNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -57,11 +55,75 @@ public class TreeUtil {
|
||||
public static <T extends TreeNode<T>> List<T> buildTree(List<T> list) {
|
||||
return buildTree(list, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建树结构(带排序)
|
||||
*
|
||||
* @param list 节点列表
|
||||
* @param comparator 排序比较器(可为null)
|
||||
* @param <T> 实现TreeNode接口的类型
|
||||
* @return 根节点列表(已排序)
|
||||
*/
|
||||
public static <T extends TreeNode<T>> List<T> buildTreeWithSort(
|
||||
List<T> list,
|
||||
Comparator<T> comparator) {
|
||||
|
||||
if (list == null || list.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// 1. 构建ID映射
|
||||
Map<Object, T> nodeMap = list.stream()
|
||||
.collect(Collectors.toMap(TreeNode::getId, node -> node, (a, b) -> a));
|
||||
|
||||
// 2. 构建父子关系
|
||||
List<T> rootNodes = new ArrayList<>();
|
||||
|
||||
for (T node : list) {
|
||||
Object parentId = node.getParentId();
|
||||
|
||||
if (parentId == null || !nodeMap.containsKey(parentId)) {
|
||||
rootNodes.add(node);
|
||||
} else {
|
||||
T parent = nodeMap.get(parentId);
|
||||
if (parent != null) {
|
||||
List<T> children = parent.getChildren();
|
||||
if (children == null) {
|
||||
children = new ArrayList<>();
|
||||
parent.setChildren(children);
|
||||
}
|
||||
children.add(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 递归排序所有节点
|
||||
if (comparator != null) {
|
||||
sortTreeNodes(rootNodes, comparator);
|
||||
}
|
||||
|
||||
return rootNodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归排序树节点
|
||||
*/
|
||||
private static <T extends TreeNode<T>> void sortTreeNodes(List<T> nodes, Comparator<T> comparator) {
|
||||
if (nodes == null || nodes.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 排序当前层级
|
||||
nodes.sort(comparator);
|
||||
|
||||
// 递归排序子节点
|
||||
nodes.forEach(node -> {
|
||||
List<T> children = node.getChildren();
|
||||
if (children != null && !children.isEmpty()) {
|
||||
sortTreeNodes(children, comparator);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 树节点接口(所有需要构建树的实体类需要实现此接口)
|
||||
*
|
||||
* @param <T> 具体的实体类型
|
||||
*/
|
||||
|
||||
|
||||
Reference in New Issue
Block a user