Quellcode durchsuchen

根据deptId查找子类

shiqian vor 3 Jahren
Ursprung
Commit
0cd54145e8

+ 5 - 0
boman-modules/boman-system/src/main/java/com/boman/system/controller/SysDeptController.java

@@ -89,6 +89,11 @@ public class SysDeptController extends BaseController
         return deptService.listChildrenDepts(deptId);
     }
 
+    @GetMapping("/list/towns")
+    public List<SysDept> listTowns(Long deptId) {
+        return deptService.listTowns(deptId);
+    }
+
     /**
      * 根据部门编号获取详细信息
      */

+ 2 - 0
boman-modules/boman-system/src/main/java/com/boman/system/service/ISysDeptService.java

@@ -149,4 +149,6 @@ public interface ISysDeptService
      * @return com.boman.domain.SysDept
      */
     SysDept getParentById(Long parentId);
+
+    List<SysDept> listTowns(Long deptId);
 }

+ 41 - 0
boman-modules/boman-system/src/main/java/com/boman/system/service/impl/SysDeptServiceImpl.java

@@ -8,6 +8,7 @@ import com.boman.common.core.utils.number.NumberUtils;
 import com.alibaba.fastjson.JSONObject;
 import com.boman.common.core.utils.obj.ObjectUtils;
 import com.boman.common.redis.service.RedisService;
+import com.boman.common.security.service.TokenService;
 import com.boman.domain.SysUser;
 import com.boman.domain.constant.CacheConstants;
 import com.boman.system.api.model.LoginUser;
@@ -29,6 +30,8 @@ import com.boman.system.service.ISysDeptService;
 
 import javax.annotation.Resource;
 
+import static com.boman.common.core.utils.StringUtils.isNotEmpty;
+import static com.boman.common.core.utils.obj.ObjectUtils.isEmpty;
 import static com.boman.common.datascope.aspect.DataScopeAspect.DATA_SCOPE_ALL;
 
 /**
@@ -535,4 +538,42 @@ public class SysDeptServiceImpl implements ISysDeptService {
     public SysDept getParentById(Long parentId) {
         return deptMapper.getParentById(parentId);
     }
+
+    @Override
+    public List<SysDept> listTowns(Long deptId) {
+        String token = SecurityUtils.getToken();
+        LoginUser loginUser = redisService.getCacheObject(CacheConstants.LOGIN_TOKEN_KEY + token);
+
+        List<SysRole> roles = loginUser.getSysUser().getRoles();
+        List<String> roleKeyList = roles.stream().map(SysRole::getRoleKey).collect(Collectors.toList());
+
+        List<SysDept> townsDepts = new ArrayList<>(16);
+        List<SysDept> allDepts = selectDeptsList();
+        if (roleKeyList.contains("admin") || roleKeyList.contains("city")) {
+            Long deptId1;
+            if (null == deptId) deptId1 = 1L; // 查所有乡镇
+            else deptId1 = deptId; // 查乡镇下的村
+
+            for (SysDept allDept : allDepts) {
+                if (isEmpty(allDept.getParentId())) continue;
+                if (allDept.getParentId().equals(deptId1)) {
+                    townsDepts.add(allDept);
+                }
+            }
+        } else if (roleKeyList.contains("sys:town")) {
+            if (null == deptId) {
+                // 乡镇的登录进来,只能选择自己的乡镇,不可以选择其他的乡镇,因此只返回自己的乡镇
+                townsDepts.add(loginUser.getSysUser().getDept());
+            } else {
+                for (SysDept allDept : allDepts) {
+                    if (isEmpty(allDept.getParentId())) continue;
+                    if (allDept.getParentId().equals(deptId)) {
+                        townsDepts.add(allDept);
+                    }
+                }
+            }
+        }
+
+        return townsDepts;
+    }
 }