hsy
9 天以前 e11706ad56843610601c14aa15868c4c3b908548
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();
    }
}