From 321d507a5670cb36c1550445c4795a76c9d7d226 Mon Sep 17 00:00:00 2001
From: mitao <2763622819@qq.com>
Date: 星期三, 03 四月 2024 18:06:49 +0800
Subject: [PATCH] 部门端历史数据模块、字段查看模块返回数据层级关系处理

---
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TbFieldCategoryServiceImpl.java |   59 +++++++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 43 insertions(+), 16 deletions(-)

diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TbFieldCategoryServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TbFieldCategoryServiceImpl.java
index 022e3b5..2f6d1f9 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TbFieldCategoryServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TbFieldCategoryServiceImpl.java
@@ -6,6 +6,7 @@
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.common.basic.PageDTO;
 import com.ruoyi.common.enums.ShowStatusEnum;
+import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.BeanUtils;
 import com.ruoyi.common.utils.CollUtils;
 import com.ruoyi.common.utils.StringUtils;
@@ -16,6 +17,7 @@
 import com.ruoyi.system.mapper.TbFieldCategoryMapper;
 import com.ruoyi.system.query.FieldCategoryQuery;
 import com.ruoyi.system.service.TbFieldCategoryService;
+import com.ruoyi.system.vo.FieldCategoryDetailVO;
 import com.ruoyi.system.vo.FieldCategoryVO;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -84,32 +86,29 @@
         updateCategoryAndChildren(dto.getId(), dto.getStatus());
     }
 
-    private void updateCategoryAndChildren(Integer id, ShowStatusEnum status) {
-        TbFieldCategory category = this.getById(id);
-        if (Objects.isNull(category)) {
-            throw new RuntimeException("非法id");
-        }
+    private void updateCategoryAndChildren(Long id, ShowStatusEnum status) {
+        TbFieldCategory category = this.validateParam(id);
         this.lambdaUpdate()
                 .eq(TbFieldCategory::getId, id)
                 .set(TbFieldCategory::getStatus, status)
                 .update();
         List<TbFieldCategory> children = this.lambdaQuery().eq(TbFieldCategory::getParentId, category.getId()).list();
         if (CollUtils.isNotEmpty(children)) {
-            List<Integer> childIds = children.stream().map(TbFieldCategory::getId).collect(Collectors.toList());
+            List<Long> childIds = children.stream().map(TbFieldCategory::getId).collect(Collectors.toList());
             childIds.forEach(childId -> updateCategoryAndChildren(childId, status));
         }
     }
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public void deleteChildren(Integer id) {
+    public void deleteChildren(Long id) {
         //认定为二级分类
         TbFieldCategory category = this.getById(id);
         if (Objects.nonNull(category)) {
             //查询是否有三级分类
             List<TbFieldCategory> threeCategoryList = this.lambdaQuery().eq(TbFieldCategory::getParentId, id).list();
             if (CollectionUtils.isNotEmpty(threeCategoryList)) {
-                List<Integer> ids = threeCategoryList.stream().map(TbFieldCategory::getId).collect(Collectors.toList());
+                List<Long> ids = threeCategoryList.stream().map(TbFieldCategory::getId).collect(Collectors.toList());
                 //删除该二级分类下面的三级分类
                 this.removeByIds(ids);
             }
@@ -120,20 +119,17 @@
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public void delete(Integer id) {
+    public void delete(Long id) {
         //一级分类
-        TbFieldCategory category = this.getById(id);
-        if (Objects.isNull(category)) {
-            throw new RuntimeException("非法参数");
-        }
+        validateParam(id);
         //查询是否有二级分类
         List<TbFieldCategory> threeCategoryList = this.lambdaQuery().eq(TbFieldCategory::getParentId, id).list();
         if (CollectionUtils.isNotEmpty(threeCategoryList)) {
-            List<Integer> ids = threeCategoryList.stream().map(TbFieldCategory::getId).collect(Collectors.toList());
+            List<Long> ids = threeCategoryList.stream().map(TbFieldCategory::getId).collect(Collectors.toList());
             //查询三级分类
             List<TbFieldCategory> list = this.lambdaQuery().in(TbFieldCategory::getParentId, ids).list();
             if (CollectionUtils.isNotEmpty(list)) {
-                List<Integer> ids1 = list.stream().map(TbFieldCategory::getId).collect(Collectors.toList());
+                List<Long> ids1 = list.stream().map(TbFieldCategory::getId).collect(Collectors.toList());
                 //删除三级分类
                 this.removeByIds(ids1);
             }
@@ -144,8 +140,17 @@
         this.removeById(id);
     }
 
+    private TbFieldCategory validateParam(Long id) {
+        TbFieldCategory category = this.getById(id);
+        if (Objects.isNull(category)) {
+            throw new ServiceException("非法参数");
+        }
+        return category;
+    }
+
     @Override
     public void edit(FieldCategoryUpdateDTO dto) {
+        validateParam(dto.getId());
         //更新一级分类
         updateCategory(dto);
         List<FieldCategoryUpdateDTO> children = dto.getChildren();
@@ -172,7 +177,7 @@
     }
 
     @Override
-    public List<FieldCategoryVO> queryFieldCategories(Integer id) {
+    public List<FieldCategoryVO> queryFieldCategories(Long id) {
         List<TbFieldCategory> list = this.lambdaQuery()
                 .select(TbFieldCategory::getId,TbFieldCategory::getFieldCategoryName)
                 .eq(TbFieldCategory::getParentId, id)
@@ -180,4 +185,26 @@
                 .list();
         return BeanUtils.copyList(list, FieldCategoryVO.class);
     }
+
+    @Override
+    public FieldCategoryDetailVO getDetailsById(Long id) {
+        TbFieldCategory oneCategory = this.getById(id);
+        if (Objects.isNull(oneCategory)) {
+            return new FieldCategoryDetailVO();
+        }
+        FieldCategoryDetailVO vo = BeanUtils.copyBean(oneCategory, FieldCategoryDetailVO.class);
+        //根据一级分类id,查询二级分类
+        List<TbFieldCategory> twoCategoryList = this.lambdaQuery().eq(TbFieldCategory::getParentId, oneCategory.getId()).list();
+        twoCategoryList.forEach(item->{
+            FieldCategoryDetailVO twoCategoryVO = BeanUtils.copyBean(item, FieldCategoryDetailVO.class);
+            vo.getChildren().add(twoCategoryVO);
+            //根据二级分类id,查询三级分类
+            List<TbFieldCategory> threeCategoryList = this.lambdaQuery().eq(TbFieldCategory::getParentId, item.getId()).list();
+            threeCategoryList.forEach(threeCategory->{
+                FieldCategoryDetailVO threeCategoryVO = BeanUtils.copyBean(threeCategory, FieldCategoryDetailVO.class);
+                twoCategoryVO.getChildren().add(threeCategoryVO);
+            });
+        });
+        return vo;
+    }
 }

--
Gitblit v1.7.1