Sfoglia il codice sorgente

refactor(dept): 优化部门服务实现逻辑

- 移除不必要的 BaseEntity 和 ComDept 导入
- 简化集合类导入语句
- 移除部门查询中的客户ID过滤条件
- 添加公司ID大于0的查询条件
- 扩展部门查询字段包括父ID、排序号和状态
- 注释掉部门权限检查逻辑
- 在插入部门后设置部门ID到业务对象
- 新增批量查询部门名称的方法并添加批量大小限制
hurx 3 mesi fa
parent
commit
d342611e31

+ 43 - 12
ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysDeptServiceImpl.java

@@ -15,13 +15,11 @@ import org.dromara.common.core.context.PlatformContext;
 import org.dromara.common.core.enums.SysPlatformCode;
 import org.dromara.common.core.exception.ServiceException;
 import org.dromara.common.core.utils.*;
-import org.dromara.common.mybatis.core.domain.BaseEntity;
 import org.dromara.common.mybatis.core.page.PageQuery;
 import org.dromara.common.mybatis.core.page.TableDataInfo;
 import org.dromara.common.mybatis.helper.DataBaseHelper;
 import org.dromara.common.redis.utils.CacheUtils;
 import org.dromara.common.satoken.utils.LoginHelper;
-import org.dromara.system.domain.ComDept;
 import org.dromara.system.domain.SysDept;
 import org.dromara.system.domain.SysRole;
 import org.dromara.system.domain.SysUser;
@@ -37,10 +35,7 @@ import org.springframework.cache.annotation.Caching;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * 部门管理 服务实现
@@ -99,12 +94,14 @@ public class SysDeptServiceImpl implements ISysDeptService {
         lqw.eq(SysDept::getDelFlag, SystemConstants.NORMAL);
         lqw.eq(ObjectUtil.isNotNull(bo.getDeptId()), SysDept::getDeptId, bo.getDeptId());
         lqw.eq(ObjectUtil.isNotNull(bo.getParentId()), SysDept::getParentId, bo.getParentId());
-        lqw.eq(ObjectUtil.isNotNull(bo.getCustomerId()), SysDept::getCustomerId, bo.getCustomerId());
         lqw.like(StringUtils.isNotBlank(bo.getDeptName()), SysDept::getDeptName, bo.getDeptName());
         lqw.like(StringUtils.isNotBlank(bo.getDeptCategory()), SysDept::getDeptCategory, bo.getDeptCategory());
         lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysDept::getStatus, bo.getStatus());
         lqw.between(params.get("beginTime") != null && params.get("endTime") != null,
             SysDept::getCreateTime, params.get("beginTime"), params.get("endTime"));
+        if (ObjectUtil.isNotNull(bo.getCompanyId()) && !bo.getCompanyId().equals(0L)) {
+            lqw.gt(SysDept::getCompanyId, 0);
+        }
         lqw.orderByAsc(SysDept::getAncestors);
         lqw.orderByAsc(SysDept::getParentId);
         lqw.orderByAsc(SysDept::getOrderNum);
@@ -186,7 +183,12 @@ public class SysDeptServiceImpl implements ISysDeptService {
     @Override
     public List<SysDeptVo> selectDeptByIds(List<Long> deptIds) {
         return baseMapper.selectDeptList(new LambdaQueryWrapper<SysDept>()
-            .select(SysDept::getDeptId, SysDept::getDeptName, SysDept::getLeader)
+            .select(SysDept::getDeptId,
+                SysDept::getDeptName,
+                SysDept::getLeader,
+                SysDept::getParentId,
+                SysDept::getOrderNum,
+                SysDept::getStatus)
             .eq(SysDept::getStatus, SystemConstants.NORMAL)
             .in(CollUtil.isNotEmpty(deptIds), SysDept::getDeptId, deptIds));
     }
@@ -275,9 +277,9 @@ public class SysDeptServiceImpl implements ISysDeptService {
         if (LoginHelper.isSuperAdmin()) {
             return;
         }
-        if (baseMapper.countDeptById(deptId) == 0) {
-            throw new ServiceException("没有权限访问部门数据!");
-        }
+//        if (baseMapper.countDeptById(deptId) == 0) {
+//            throw new ServiceException("没有权限访问部门数据!");
+//        }
     }
 
     @Override
@@ -318,7 +320,11 @@ public class SysDeptServiceImpl implements ISysDeptService {
         }
         //在 "main" 平台上下文中执行插入,并获取返回值
         Integer rows = PlatformContextUtil.executeWithPlatform(targetPlatform, () -> {
-            return baseMapper.insert(dept);
+            Integer i = baseMapper.insert(dept);
+            if (i > 0) {
+                bo.setDeptId(dept.getDeptId());
+            }
+            return i;
         });
         return rows;
     }
@@ -426,4 +432,29 @@ public class SysDeptServiceImpl implements ISysDeptService {
             .eq(SysDept::getStatus, SystemConstants.NORMAL));
     }
 
+    public Map<Long, String> selectDeptNameByIds(Set<Long> ids) {
+        if (ids == null || ids.isEmpty()) {
+            return Collections.emptyMap();
+        }
+
+        // 限制批量大小
+        if (ids.size() > 1000) {
+            throw new IllegalArgumentException("Batch size exceeds limit: " + ids.size());
+        }
+
+        List<SysDept> deptList = baseMapper.selectByIds(ids);
+        Map<Long, String> resultMap = new HashMap<>(ids.size());
+
+        // 初始化所有请求的 ID 为 null
+        ids.forEach(id -> resultMap.put(id, null));
+
+        if (deptList != null) {
+            deptList.stream()
+                .filter(dept -> dept.getDeptId() != null && dept.getDeptName() != null)
+                .forEach(dept -> resultMap.put(dept.getDeptId(), dept.getDeptName()));
+        }
+
+        return resultMap;
+    }
+
 }