diff --git a/src/main/java/com/dc/dc_project/controller/StandardCategoryController.java b/src/main/java/com/dc/dc_project/controller/StandardCategoryController.java index 4789920..3e9cfd6 100644 --- a/src/main/java/com/dc/dc_project/controller/StandardCategoryController.java +++ b/src/main/java/com/dc/dc_project/controller/StandardCategoryController.java @@ -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); + } } diff --git a/src/main/java/com/dc/dc_project/model/dto/StandardCategoryDto.java b/src/main/java/com/dc/dc_project/model/dto/StandardCategoryDto.java index 5fa8680..2a8d0a2 100644 --- a/src/main/java/com/dc/dc_project/model/dto/StandardCategoryDto.java +++ b/src/main/java/com/dc/dc_project/model/dto/StandardCategoryDto.java @@ -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; /** diff --git a/src/main/java/com/dc/dc_project/model/dto/StandardCategoryQueDto.java b/src/main/java/com/dc/dc_project/model/dto/StandardCategoryQueDto.java new file mode 100644 index 0000000..8c9cb64 --- /dev/null +++ b/src/main/java/com/dc/dc_project/model/dto/StandardCategoryQueDto.java @@ -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; +} diff --git a/src/main/java/com/dc/dc_project/model/dto/StandardCategoryUpdateDto.java b/src/main/java/com/dc/dc_project/model/dto/StandardCategoryUpdateDto.java new file mode 100644 index 0000000..aa587de --- /dev/null +++ b/src/main/java/com/dc/dc_project/model/dto/StandardCategoryUpdateDto.java @@ -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; +} diff --git a/src/main/java/com/dc/dc_project/service/StandardCategoryService.java b/src/main/java/com/dc/dc_project/service/StandardCategoryService.java index c95f765..6a69e86 100644 --- a/src/main/java/com/dc/dc_project/service/StandardCategoryService.java +++ b/src/main/java/com/dc/dc_project/service/StandardCategoryService.java @@ -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 { * @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); } diff --git a/src/main/java/com/dc/dc_project/service/impl/StandardCategoryServiceImpl.java b/src/main/java/com/dc/dc_project/service/impl/StandardCategoryServiceImpl.java index f5e3f8f..ff2cf41 100644 --- a/src/main/java/com/dc/dc_project/service/impl/StandardCategoryServiceImpl.java +++ b/src/main/java/com/dc/dc_project/service/impl/StandardCategoryServiceImpl.java @@ -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 queryWrapper = new LambdaQueryWrapper() + .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 list = this.list(queryWrapper); + return ResponseResult.success(list); + } + + @Override + public ResponseResult delete(StandardCategoryQueDto standardCategoryDto) { + if(standardCategoryDto.getId() == null) + return ResponseResult.error("请选择要删除的分类"); + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper() + .eq(StandardCategory::getId, standardCategoryDto.getId()) + .eq(StandardCategory::getIsDeleted, 0); + StandardCategory standardCategory = this.getOne(queryWrapper); + if(standardCategory == null){ + return ResponseResult.error("该分类不存在"); + } + LambdaQueryWrapper queryWrapper1 = new LambdaQueryWrapper() + .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 queryWrapper = new LambdaQueryWrapper() + .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("修改成功"); + } } diff --git a/src/main/java/com/dc/dc_project/utils/TreeUtil.java b/src/main/java/com/dc/dc_project/utils/TreeUtil.java index 3e36fc4..9fc5298 100644 --- a/src/main/java/com/dc/dc_project/utils/TreeUtil.java +++ b/src/main/java/com/dc/dc_project/utils/TreeUtil.java @@ -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 > List buildTree(List list) { return buildTree(list, null); } + + /** + * 构建树结构(带排序) + * + * @param list 节点列表 + * @param comparator 排序比较器(可为null) + * @param 实现TreeNode接口的类型 + * @return 根节点列表(已排序) + */ + public static > List buildTreeWithSort( + List list, + Comparator comparator) { + + if (list == null || list.isEmpty()) { + return new ArrayList<>(); + } + + // 1. 构建ID映射 + Map nodeMap = list.stream() + .collect(Collectors.toMap(TreeNode::getId, node -> node, (a, b) -> a)); + + // 2. 构建父子关系 + List 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 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 > void sortTreeNodes(List nodes, Comparator comparator) { + if (nodes == null || nodes.isEmpty()) { + return; + } + + // 排序当前层级 + nodes.sort(comparator); + + // 递归排序子节点 + nodes.forEach(node -> { + List children = node.getChildren(); + if (children != null && !children.isEmpty()) { + sortTreeNodes(children, comparator); + } + }); + } + } -/** - * 树节点接口(所有需要构建树的实体类需要实现此接口) - * - * @param 具体的实体类型 - */ -