From 19871e8ac9606710b1009f5f86366fb445a7ace8 Mon Sep 17 00:00:00 2001
From: heheng <475597332@qq.com>
Date: 星期三, 09 七月 2025 16:06:20 +0800
Subject: [PATCH] 优化改造sql

---
 multi-system/src/main/java/com/gkhy/exam/system/service/impl/SysDeptServiceImpl.java |  100 +++++++++++++++++++++++++++-----------------------
 1 files changed, 54 insertions(+), 46 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 8f934d1..f9ccb38 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
@@ -119,56 +119,30 @@
      * @return 部门树信息集合
      */
     @Override
-    public List<SysDept> selectDeptTreeList(SysDept dept)
+    public List<TreeSelect> selectDeptTreeList(SysDept dept)
     {
         List<SysDept> depts = SpringUtils.getAopProxy(this).getOutDeptList(dept);
 
-        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()));
+//        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);
+        return buildDeptTreeSelect(depts);
     }
 
-    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;
+    @Override
+    public List<SysDept> getParentInfo(Long deptId) {
+        return deptMapper.getParentInfo(deptId);
     }
-
-    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);
-    }
-
 
     /**
      * 构建前端所需要树结构
@@ -346,7 +320,7 @@
     }
 
     @Override
-    @Transactional
+    @Transactional(rollbackFor = RuntimeException.class)
     public int saveDept(SysDeptSaveDTOReq dept) {
 //        Long companyId = SecurityUtils.getCompanyId();
 //        if (!companyId.equals(dept.getCompanyId())){
@@ -358,11 +332,29 @@
         if (!b){
             throw new ApiException("部门名称已存在!");
         }
+        SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId());
+
         int i = 0;
         if (sysDept.getDeptId() == null){
+            if (ObjectUtil.isNotNull(newParentDept)){
+                String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
+                dept.setAncestors(newAncestors);
+            }
             sysDept.setCreateBy(SecurityUtils.getUsername());
              i = deptMapper.insertDept(sysDept);
         }else {
+            if (sysDept.getParentId().equals(sysDept.getDeptId())){
+                throw new ApiException("上级部门不能是自己!");
+            }
+
+            SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId());
+            if (ObjectUtil.isNotNull(newParentDept) && ObjectUtil.isNotNull(oldDept))
+            {
+                String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
+                String oldAncestors = oldDept.getAncestors();
+                dept.setAncestors(newAncestors);
+                updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
+            }
             sysDept.setUpdateBy(SecurityUtils.getUsername());
             sysDept.setUpdateTime(LocalDateTime.now());
             i = deptMapper.updateDept(sysDept);
@@ -376,8 +368,6 @@
             //处理条款
             batchSaveCaluse(sysDept.getDeptId(), dept.getCompanyId(), dept.getCaluseVO1List());
 
-            List<Long> childDeptIds = dept.getChildDeptIds();
-            bacthInsertManager(sysDept.getDeptId(), childDeptIds, dept.getCompanyId());
         }
 
         return sysDept.getDeptId().byteValue();
@@ -451,7 +441,18 @@
         List<DeptVo> deptVos = deptMapper.selectDeptList(sysDept);
         if (deptVos != null && !deptVos.isEmpty()){
             List<SysFunctionalDistribution> sysFunctionalDistributions = new ArrayList<>();
+            Map<String, CaluseVO1> clauseNumMap = new HashMap<>();
             for (DeptVo deptVo : deptVos) {
+                List<CaluseVO1> caluseVO1List = deptVo.getCaluseVO1List();
+                if (ObjectUtil.isNotEmpty(caluseVO1List)){
+                // 转成 Map<String, CaluseVO1>,key 是 clauseNum
+                    clauseNumMap = caluseVO1List.stream()
+                            .collect(Collectors.toMap(
+                                    CaluseVO1::getClauseNum, // key
+                                    Function.identity(),     // value
+                                    (existing, replacement) -> existing // 如果有重复 key,保留现有的值
+                            ));
+                }
                 for (String s : DEPT_ROOT) {
                     SysFunctionalDistribution sysFunctionalDistribution = new SysFunctionalDistribution();
                     sysFunctionalDistribution.setDeptId(deptVo.getDeptId());
@@ -459,7 +460,14 @@
                     sysFunctionalDistribution.setCreateBy(SecurityUtils.getUsername());
                     sysFunctionalDistribution.setCreateTime(LocalDateTime.now());
                     sysFunctionalDistribution.setClauseNum(s);
-                    sysFunctionalDistribution.setChooseLab(0);
+                    if (clauseNumMap.isEmpty()){
+                        sysFunctionalDistribution.setChooseLab(0);
+                    }else if (clauseNumMap.containsKey(s)){
+                        sysFunctionalDistribution.setChooseLab(1);
+                    }else {
+                        sysFunctionalDistribution.setChooseLab(0);
+                    }
+
                     sysFunctionalDistributions.add(sysFunctionalDistribution);
                 }
 

--
Gitblit v1.9.2