From 427f9796bf60fe1262131a8fd6ae6c25e02574be Mon Sep 17 00:00:00 2001
From: heheng <475597332@qq.com>
Date: 星期二, 08 七月 2025 10:27:37 +0800
Subject: [PATCH] 排序改造

---
 multi-system/src/main/java/com/gkhy/exam/system/service/impl/SysDeptServiceImpl.java |  122 +++++++++++++++++++++++++++++++---------
 1 files changed, 93 insertions(+), 29 deletions(-)

diff --git a/multi-system/src/main/java/com/gkhy/exam/system/service/impl/SysDeptServiceImpl.java b/multi-system/src/main/java/com/gkhy/exam/system/service/impl/SysDeptServiceImpl.java
index 425c716..51fef94 100644
--- a/multi-system/src/main/java/com/gkhy/exam/system/service/impl/SysDeptServiceImpl.java
+++ b/multi-system/src/main/java/com/gkhy/exam/system/service/impl/SysDeptServiceImpl.java
@@ -13,6 +13,7 @@
 import com.gkhy.exam.common.exception.ApiException;
 import com.gkhy.exam.common.utils.SecurityUtils;
 import com.gkhy.exam.common.utils.SpringUtils;
+import com.gkhy.exam.system.domain.SysDeptManage;
 import com.gkhy.exam.system.domain.SysDeptResponsibility;
 import com.gkhy.exam.system.domain.SysFunctionalDistribution;
 import com.gkhy.exam.system.domain.vo.*;
@@ -28,6 +29,7 @@
 
 import java.time.LocalDateTime;
 import java.util.*;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 
 /**
@@ -50,7 +52,9 @@
     @Autowired
     private SysFunctionalDistributionMapper sysFunctionalDistributionMapper;
     public static final String[] DEPT_ROOT = {"4.1",  "4.2", "4.3", "4.4", "5.1", "5.2", "5.3","6.1", "6.2", "6.3",
-            "7.1.1",  "7.1.2", "7.1.3", "7.1.4", "7.1.5", "7.1.6"};
+            "7.1.1",  "7.1.2", "7.1.3", "7.1.4", "7.1.5", "7.1.6","7.2", "7.3", "7.4", "7.5", "7.6","8.1","8.2","8.2.1","8.2.2", "8.2.3", "8.2.4", "8.3", "8.3.1", "8.3.2", "8.3.3", "8.3.4", "8.3.5", "8.3.6", "8.3.7", "8.3.8", "8.4",
+    "8.4.1", "8.4.2", "8.4.3", "8.5", "8.5.1", "8.5.2", "8.5.3", "8.5.4", "8.5.5", "8.5.6", "8.5.7", "8.6","8.7","9.1","9.1.1","9.1.2","9.1.3","9.2", "9.3","10.1"
+            ,"10.2", "10.3"};
     /**
      * 查询部门管理数据
      * 
@@ -79,11 +83,56 @@
      * @return 部门树信息集合
      */
     @Override
-    public List<TreeSelect> selectDeptTreeList(SysDept dept)
+    public List<SysDept> selectDeptTreeList(SysDept dept)
     {
         List<SysDept> depts = SpringUtils.getAopProxy(this).getOutDeptList(dept);
-        return buildDeptTreeSelect(depts);
+
+        List<SysDeptManage> allManage = deptMapper.getAllManage(dept.getCompanyId());
+        // 构建父部门到子部门的映射
+        Map<Long, List<Long>> parentToChildrenMap = new HashMap<>();
+        for (SysDeptManage responsibility : allManage) {
+            parentToChildrenMap.computeIfAbsent(responsibility.getDeptId(), k -> new ArrayList<>())
+                    .add(responsibility.getSubDeptId());
+        }
+
+        // 构建部门ID到部门对象的映射
+        Map<Long, SysDept> deptMap = depts.stream()
+                .collect(Collectors.toMap(SysDept::getDeptId, Function.identity()));
+
+
+        return buildDeptTree(depts,parentToChildrenMap, deptMap);
     }
+
+    public List<SysDept> buildDeptTree(List<SysDept> allDepts, Map<Long, List<Long>> parentToChildrenMap, Map<Long, SysDept> deptMap) {
+        List<SysDept> rootNodes = new ArrayList<>();
+
+        for (SysDept dept : allDepts) {
+            if (!deptMap.values().stream()
+                    .anyMatch(d -> parentToChildrenMap.containsValue(dept.getDeptId()))) {
+                // 如果没有其他部门将它作为子部门,则为根节点
+                rootNodes.add(dept);
+            }
+        }
+
+        for (SysDept root : rootNodes) {
+            buildChildren(root, parentToChildrenMap, deptMap);
+        }
+
+        return rootNodes;
+    }
+
+    private void buildChildren(SysDept parent, Map<Long, List<Long>> parentToChildrenMap, Map<Long, SysDept> deptMap) {
+        List<Long> childIds = parentToChildrenMap.getOrDefault(parent.getDeptId(), Collections.emptyList());
+
+        List<SysDept> children = childIds.stream()
+                .map(deptMap::get)
+                .filter(Objects::nonNull)
+                .peek(child -> buildChildren(child, parentToChildrenMap, deptMap))
+                .collect(Collectors.toList());
+
+        parent.setChildren(children);
+    }
+
 
     /**
      * 构建前端所需要树结构
@@ -226,7 +275,7 @@
     @Override
     public void checkDeptDataScope(Long deptId)
     {
-        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId()) && ObjectUtil.isNotNull(deptId))
+        if (!SecurityUtils.adminUser() && ObjectUtil.isNotNull(deptId))
         {
             SysDept dept = new SysDept();
             dept.setDeptId(deptId);
@@ -260,13 +309,12 @@
     @Override
     @Transactional
     public int saveDept(SysDeptSaveDTOReq dept) {
-        Long companyId = SecurityUtils.getCompanyId();
-        if (!companyId.equals(dept.getCompanyId())){
-            throw new ApiException("无权操作!");
-        }
+//        Long companyId = SecurityUtils.getCompanyId();
+//        if (!companyId.equals(dept.getCompanyId())){
+//            throw new ApiException("无权操作!");
+//        }
         SysDept sysDept = new SysDept();
         BeanUtils.copyProperties(dept, sysDept);
-        sysDept.setCompanyId(companyId);
         boolean b = checkDeptNameUnique(sysDept);
         if (!b){
             throw new ApiException("部门名称已存在!");
@@ -287,25 +335,44 @@
                 delCaluse(sysDept.getDeptId(), delCaluseIds);
             }
             //处理条款
-            batchSaveCaluse(sysDept.getDeptId(), companyId, dept.getCaluseVO1List());
+            batchSaveCaluse(sysDept.getDeptId(), dept.getCompanyId(), dept.getCaluseVO1List());
 
+            List<Long> childDeptIds = dept.getChildDeptIds();
+            bacthInsertManager(sysDept.getDeptId(), childDeptIds, dept.getCompanyId());
         }
 
         return sysDept.getDeptId().byteValue();
     }
 
+    private void bacthInsertManager(Long deptId, List<Long> managerIds,Long companyId) {
+        deptMapper.deleteMangeBatch(deptId);
+        if (managerIds != null && !managerIds.isEmpty())
+        {
+            List<SysDeptManage> sysDeptManagers = new ArrayList<>();
+            for (Long managerId : managerIds)
+            {
+                SysDeptManage sysDeptManage = new SysDeptManage();
+                sysDeptManage.setDeptId(deptId);
+                sysDeptManage.setSubDeptId(managerId);
+                sysDeptManage.setCompanyId(companyId);
+                sysDeptManagers.add(sysDeptManage);
+            }
+            deptMapper.insetMangeBatch(sysDeptManagers);
+        }
+    }
+
     @Override
     @Transactional
     public int saveDeptResponsibility(SysDeptResponsibilityReqVo reqVo) {
-        Long companyId = SecurityUtils.getCompanyId();
-        if (!companyId.equals(reqVo.getCompanyId())){
-            throw new ApiException("无权操作!");
-        }
+//        Long companyId = SecurityUtils.getCompanyId();
+//        if (!companyId.equals(reqVo.getCompanyId())){
+//            throw new ApiException("无权操作!");
+//        }
         SysDept sysDept = deptMapper.selectDeptById(reqVo.getDeptId());
         if (null == sysDept){
             throw new ApiException("部门不存在!");
         }
-        if (!companyId.equals(sysDept.getCompanyId())){
+        if (!reqVo.getCompanyId().equals(sysDept.getCompanyId())){
             throw new ApiException("无权操作!");
         }
         SysDept dept = new SysDept();
@@ -326,9 +393,7 @@
 
     @Override
     public List<SysFunctionalDistribution> getFunctionalDistributionList(Long companyId) {
-        if (companyId != null){
-            companyId = SecurityUtils.getCompanyId();
-        }
+
        return sysFunctionalDistributionMapper.selectListVo(companyId);
     }
 
@@ -336,13 +401,14 @@
     @Transactional
     public int initFunctionalDistribution(Long companyId) {
 
-        if (!companyId.equals(SecurityUtils.getCompanyId())){
-            throw new ApiException("无权操作!");
-        }
+//        if (!companyId.equals(SecurityUtils.getCompanyId())){
+//            throw new ApiException("无权操作!");
+//        }
 
         sysFunctionalDistributionMapper.delByCompanyId(companyId);
         SysDept sysDept = new SysDept();
         sysDept.setCompanyId(companyId);
+        sysDept.setResponsType("1");
         List<DeptVo> deptVos = deptMapper.selectDeptList(sysDept);
         if (deptVos != null && !deptVos.isEmpty()){
             List<SysFunctionalDistribution> sysFunctionalDistributions = new ArrayList<>();
@@ -370,23 +436,21 @@
 
     @Override
     public int saveFunctionalDistribution(FunctionalDistributionVo reqVo) {
-        Long companyId = SecurityUtils.getCompanyId();
-        if (!companyId.equals(reqVo.getCompanyId())){
-            throw new ApiException("无权操作!");
-        }
+//        Long companyId = SecurityUtils.getCompanyId();
+//        if (!companyId.equals(reqVo.getCompanyId())){
+//            throw new ApiException("无权操作!");
+//        }
         List<SysFunctionalDistribution> list = reqVo.getList();
         if (list != null && !list.isEmpty())
         {
             for (SysFunctionalDistribution sysFunctionalDistribution : list) {
-                sysFunctionalDistribution.setCompanyId(companyId);
+                sysFunctionalDistribution.setCompanyId(reqVo.getCompanyId());
                 sysFunctionalDistribution.setUpdateBy(SecurityUtils.getUsername());
                 sysFunctionalDistribution.setUpdateTime(LocalDateTime.now());
             }
         }
         int i = sysFunctionalDistributionMapper.batchUpdate(list);
-        if (i < 1){
-            throw new ApiException("保存失败!");
-        }
+
         return 1;
     }
 

--
Gitblit v1.9.2