From 1cdb6459f1e05b23f15920fe90aad5e6e7f1d061 Mon Sep 17 00:00:00 2001
From: xiangpei <xiangpei@timesnew.cn>
Date: 星期三, 19 二月 2025 14:06:16 +0800
Subject: [PATCH] 部门新增修改接口调整

---
 system/src/main/java/com/ycl/system/service/impl/SysDeptServiceImpl.java |  137 +++++++++++++++++++++++++++++++++++++--------
 1 files changed, 111 insertions(+), 26 deletions(-)

diff --git a/system/src/main/java/com/ycl/system/service/impl/SysDeptServiceImpl.java b/system/src/main/java/com/ycl/system/service/impl/SysDeptServiceImpl.java
index a54ad3f..7154ec4 100644
--- a/system/src/main/java/com/ycl/system/service/impl/SysDeptServiceImpl.java
+++ b/system/src/main/java/com/ycl/system/service/impl/SysDeptServiceImpl.java
@@ -1,9 +1,12 @@
 package com.ycl.system.service.impl;
 
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
+import java.util.*;
 import java.util.stream.Collectors;
+
+import com.ycl.common.base.Result;
+import com.ycl.common.core.domain.StringTreeSelect;
+import com.ycl.system.domain.base.BaseSelect;
+import lombok.RequiredArgsConstructor;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import com.ycl.common.annotation.DataScope;
@@ -27,13 +30,12 @@
  * @author ycl
  */
 @Service
+@RequiredArgsConstructor
 public class SysDeptServiceImpl implements ISysDeptService
 {
-    @Autowired
-    private SysDeptMapper deptMapper;
 
-    @Autowired
-    private SysRoleMapper roleMapper;
+    private final SysDeptMapper deptMapper;
+    private final SysRoleMapper roleMapper;
 
     /**
      * 鏌ヨ閮ㄩ棬绠$悊鏁版嵁
@@ -59,6 +61,48 @@
     {
         List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept);
         return buildDeptTreeSelect(depts);
+    }
+
+    @Override
+    public List<TreeSelect> deptTreeNoDataAuth(SysDept dept) {
+        List<SysDept> depts = deptMapper.selectDeptList(dept);;
+        return buildDeptTreeSelect(depts);
+    }
+
+    @Override
+    public List<StringTreeSelect> flowDeptTree(SysDept dept) {
+        List<SysDept> depts = deptMapper.selectDeptList(dept);
+        List<StringTreeSelect> list = depts.stream().map(item -> {
+            StringTreeSelect d = new StringTreeSelect();
+            d.setId("dept:" + item.getDeptId());
+            d.setLabel(item.getDeptName());
+            d.setParentId("dept:" + item.getParentId());
+            return d;
+        }).collect(Collectors.toList());
+
+
+        Map<String, StringTreeSelect> nodeMap = new HashMap<>();
+        // 灏嗘墍鏈夎妭鐐规斁鍏ap涓紝鏂逛究鍚庣画鏌ユ壘
+        for (StringTreeSelect node : list) {
+            nodeMap.put(node.getId(), node);
+        }
+        List<StringTreeSelect> treeList = new ArrayList<>();
+        // 鏋勫缓鏍戠粨鏋�
+        for (StringTreeSelect node : list) {
+            StringTreeSelect root = null;
+            if ("dept:0".equals(node.getParentId()) || node.getParentId().isEmpty()) {
+                // 鏍硅妭鐐�
+                root = node;
+                treeList.add(root);
+            } else {
+                // 鎵惧埌鐖惰妭鐐癸紝骞跺皢褰撳墠鑺傜偣娣诲姞鍒扮埗鑺傜偣鐨刢hildren鍒楄〃涓�
+                StringTreeSelect parentNode = nodeMap.get(node.getParentId());
+                if (parentNode != null) {
+                    parentNode.getChildren().add(node);
+                }
+            }
+        }
+        return treeList;
     }
 
     /**
@@ -87,6 +131,8 @@
         }
         return returnList;
     }
+
+
 
     /**
      * 鏋勫缓鍓嶇鎵�闇�瑕佷笅鎷夋爲缁撴瀯
@@ -124,6 +170,11 @@
     public SysDept selectDeptById(Long deptId)
     {
         return deptMapper.selectDeptById(deptId);
+    }
+
+    @Override
+    public List<SysDept> selectDeptByIds(List<Long> deptIds) {
+        return deptMapper.selectDeptByIds(deptIds);
     }
 
     /**
@@ -211,13 +262,18 @@
     @Override
     public int insertDept(SysDept dept)
     {
-        SysDept info = deptMapper.selectDeptById(dept.getParentId());
-        // 濡傛灉鐖惰妭鐐逛笉涓烘甯哥姸鎬�,鍒欎笉鍏佽鏂板瀛愯妭鐐�
-        if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
-        {
-            throw new ServiceException("閮ㄩ棬鍋滅敤锛屼笉鍏佽鏂板");
+        if (Objects.nonNull(dept.getParentId())) {
+            SysDept info = deptMapper.selectDeptById(dept.getParentId());
+            // 濡傛灉鐖惰妭鐐逛笉涓烘甯哥姸鎬�,鍒欎笉鍏佽鏂板瀛愯妭鐐�
+            if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
+            {
+                throw new ServiceException("涓婄骇閮ㄩ棬宸插仠鐢紝涓嶅厑璁告柊澧炰笅绾ч儴闂�");
+            }
+            dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
+        } else {
+            dept.setParentId(0L);
+            dept.setAncestors("");
         }
-        dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
         return deptMapper.insertDept(dept);
     }
 
@@ -230,22 +286,23 @@
     @Override
     public int updateDept(SysDept dept)
     {
-        SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId());
         SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId());
-        if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept))
-        {
-            String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
-            String oldAncestors = oldDept.getAncestors();
-            dept.setAncestors(newAncestors);
-            updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
+        if (Objects.isNull(oldDept)) {
+            throw new RuntimeException("淇敼閮ㄩ棬琚垹闄ゆ垨涓嶅瓨鍦�");
+        }
+        if (Objects.nonNull(dept.getParentId())) {
+            SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId());
+            if (Objects.nonNull(newParentDept)) {
+                String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
+                String oldAncestors = oldDept.getAncestors();
+                dept.setAncestors(newAncestors);
+                updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
+            }
+        } else {
+            dept.setDeptId(0L);
+            dept.setAncestors("");
         }
         int result = deptMapper.updateDept(dept);
-        if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors())
-                && !StringUtils.equals("0", dept.getAncestors()))
-        {
-            // 濡傛灉璇ラ儴闂ㄦ槸鍚敤鐘舵�侊紝鍒欏惎鐢ㄨ閮ㄩ棬鐨勬墍鏈変笂绾ч儴闂�
-            updateParentDeptStatusNormal(dept);
-        }
         return result;
     }
 
@@ -335,4 +392,32 @@
     {
         return getChildList(list, t).size() > 0;
     }
+
+    /**
+     * 閮ㄩ棬涓嬫媺鍒楄〃
+     * @return
+     */
+    @Override
+    public Result all(SysDept dept) {
+        List<BaseSelect> vos = deptMapper.selectDeptList(dept).stream().map(sysDept -> {
+                    BaseSelect baseSelect = new BaseSelect();
+                    baseSelect.setId(Integer.parseInt(sysDept.getDeptId() + ""));
+                    baseSelect.setValue(sysDept.getDeptName());
+                    return baseSelect;
+                }
+        ).collect(Collectors.toList());
+        return Result.ok().data(vos);
+    }
+
+
+    @Override
+    public List<StringTreeSelect> flowableAll() {
+        List<StringTreeSelect> list = deptMapper.selectDeptList(new SysDept()).stream().map(sysDept -> {
+            StringTreeSelect stringTreeSelect = new StringTreeSelect();
+            stringTreeSelect.setId("dept:" + sysDept.getDeptId());
+            stringTreeSelect.setLabel(sysDept.getDeptName());
+            return stringTreeSelect;
+        }).collect(Collectors.toList());
+        return list;
+    }
 }

--
Gitblit v1.8.0