From 84e29c9adb718b42a693e954d2b771651ef36796 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期三, 12 八月 2026 17:20:02 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/fix/hsy/20260811' into dev_business

---
 yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java |   83 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 83 insertions(+), 0 deletions(-)

diff --git a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java
index c48e711..79da07a 100644
--- a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java
+++ b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java
@@ -2,11 +2,15 @@
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
 import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
+import cn.iocoder.yudao.framework.common.exception.ServiceException;
 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
 import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
 import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO;
 import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptSaveReqVO;
+import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptImportExcelVO;
+import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptImportRespVO;
 import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
 import cn.iocoder.yudao.module.system.dal.mysql.dept.DeptMapper;
 import cn.iocoder.yudao.module.system.dal.redis.RedisKeyConstants;
@@ -235,4 +239,83 @@
         });
     }
 
+    @Override
+    public DeptImportRespVO importDeptList(List<DeptImportExcelVO> importDepts, boolean isUpdateSupport) {
+        if (CollUtil.isEmpty(importDepts)) {
+            throw exception(DEPT_IMPORT_LIST_IS_EMPTY);
+        }
+        DeptImportRespVO respVO = DeptImportRespVO.builder().createDeptNames(new ArrayList<>())
+                .updateDeptNames(new ArrayList<>()).failureDeptNames(new LinkedHashMap<>()).build();
+
+        // 1. 鍒濆鍖栫幇鏈夌殑鍏ㄨ矾寰勪笌閮ㄩ棬ID鏄犲皠
+        List<DeptDO> allDepts = getDeptList(new DeptListReqVO());
+        Map<Long, DeptDO> idDeptMap = cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap(allDepts, DeptDO::getId);
+        Map<String, Long> fullPathDeptIdMap = new HashMap<>();
+        for (DeptDO dept : allDepts) {
+            fullPathDeptIdMap.put(getFullPath(dept, idDeptMap), dept.getId());
+        }
+
+        // 2. 灏嗗鍏ョ殑閮ㄩ棬鎸夌収鐖堕儴闂ㄨ矾寰勬繁搴︽帓搴忥紝纭繚鍏堝鐞嗙埗閮ㄩ棬
+        importDepts.sort(Comparator.comparingInt(dept -> StrUtil.isEmpty(dept.getParentPath()) ? 0 : dept.getParentPath().split("/").length));
+
+        // 3. 閬嶅巻瀵煎叆
+        for (DeptImportExcelVO importDept : importDepts) {
+            // 3.1 鑾峰彇鐖堕儴闂� ID
+            Long parentId = DeptDO.PARENT_ID_ROOT;
+            if (StrUtil.isNotEmpty(importDept.getParentPath())) {
+                parentId = fullPathDeptIdMap.get(importDept.getParentPath());
+                if (parentId == null) {
+                    respVO.getFailureDeptNames().put(importDept.getName(), "涓婄骇閮ㄩ棬涓嶅瓨鍦�: " + importDept.getParentPath());
+                    continue;
+                }
+            }
+
+            // 3.2 鏍¢獙閮ㄩ棬鍚嶇О鏄惁鍞竴
+            try {
+                validateDeptNameUnique(null, parentId, importDept.getName());
+            } catch (ServiceException ex) {
+                if (!isUpdateSupport) {
+                    respVO.getFailureDeptNames().put(importDept.getName(), ex.getMessage());
+                    continue;
+                }
+                // 鏀寔鏇存柊锛屾墽琛屾洿鏂�
+                DeptDO existDept = deptMapper.selectByParentIdAndName(parentId, importDept.getName());
+                DeptDO updateDept = BeanUtils.toBean(importDept, DeptDO.class);
+                updateDept.setId(existDept.getId());
+                updateDept.setParentId(parentId);
+                deptMapper.updateById(updateDept);
+                respVO.getUpdateDeptNames().add(importDept.getName());
+                
+                String myFullPath = StrUtil.isEmpty(importDept.getParentPath()) ? importDept.getName() : importDept.getParentPath() + "/" + importDept.getName();
+                fullPathDeptIdMap.put(myFullPath, existDept.getId());
+                continue;
+            }
+
+            // 3.3 鏂板閮ㄩ棬
+            DeptDO newDept = BeanUtils.toBean(importDept, DeptDO.class);
+            newDept.setParentId(parentId);
+            deptMapper.insert(newDept);
+            respVO.getCreateDeptNames().add(importDept.getName());
+            
+            // 鏇存柊缂撳瓨锛屼緵鍚庣画瀛愰儴闂ㄤ娇鐢�
+            String myFullPath = StrUtil.isEmpty(importDept.getParentPath()) ? importDept.getName() : importDept.getParentPath() + "/" + importDept.getName();
+            fullPathDeptIdMap.put(myFullPath, newDept.getId());
+        }
+
+        return respVO;
+    }
+
+    private String getFullPath(DeptDO dept, Map<Long, DeptDO> idDeptMap) {
+        StringBuilder path = new StringBuilder(dept.getName());
+        Long parentId = dept.getParentId();
+        while (parentId != null && !DeptDO.PARENT_ID_ROOT.equals(parentId)) {
+            DeptDO parent = idDeptMap.get(parentId);
+            if (parent == null) {
+                break;
+            }
+            path.insert(0, parent.getName() + "/");
+            parentId = parent.getParentId();
+        }
+        return path.toString();
+    }
 }

--
Gitblit v1.9.3