机构树形结构 不采用用递归查询数据库
This commit is contained in:
+8
@@ -93,6 +93,14 @@ public class TsInstitutionController extends BaseController<TsInstitution> {
|
||||
}
|
||||
|
||||
|
||||
@ApiOperation("通过部门机构名称查询部门树形结构 废弃使用")
|
||||
@GetMapping("/findInstitution")
|
||||
public List<TsInstitution> findInstitution(String institutionName){
|
||||
//TODO 调用业务接口
|
||||
tsInstitutionService.findInstitutionTreeByName(institutionName);
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// System.out.println(json);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.adc.da.slrs.sarInstitution.dao;
|
||||
import com.adc.da.slrs.sarInstitution.entity.InstitutionAndUser;
|
||||
import com.adc.da.slrs.sarInstitution.entity.TsInstitution;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
@@ -29,7 +30,7 @@ public interface TsInstitutionDao extends BaseMapper<TsInstitution> {
|
||||
List<TsInstitution> selectNextUser(String institutionId);
|
||||
|
||||
|
||||
|
||||
List<TsInstitution> selectTreeByIds(@Param("rootIds") List<String> rootIds);
|
||||
|
||||
/**
|
||||
* 查询最高级机构及其人员
|
||||
|
||||
+8
@@ -43,5 +43,13 @@ public interface ITsInstitutionService extends IService<TsInstitution> {
|
||||
*/
|
||||
public List<TsInstitution> getFirst();
|
||||
|
||||
|
||||
/**
|
||||
* 根据机构部门名称查询机构树
|
||||
* @param institutionName
|
||||
* @return
|
||||
*/
|
||||
public List<TsInstitution> findInstitutionTreeByName(String institutionName);
|
||||
|
||||
int clearData();
|
||||
}
|
||||
|
||||
+80
-17
@@ -15,8 +15,8 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -38,14 +38,48 @@ public class TsInstitutionServiceImpl extends ServiceImpl<TsInstitutionDao, TsIn
|
||||
*/
|
||||
@Override
|
||||
public List<TsInstitution> getInstitution() {
|
||||
|
||||
|
||||
//查询第一层机构
|
||||
List<TsInstitution> institutionRoot=tsInstitutionDao.selectRoot();
|
||||
for(TsInstitution tsInstitution:institutionRoot){
|
||||
tsInstitution.setChildren(recursionGetInstitution(tsInstitution));
|
||||
}
|
||||
return institutionRoot;
|
||||
|
||||
//转换为hashSet
|
||||
List<String> root = institutionRoot.stream()
|
||||
.map(item -> item.getId())
|
||||
.collect(Collectors.toList());
|
||||
HashSet<String> rootSet = new HashSet<>(root);
|
||||
|
||||
List<TsInstitution> institutionList = this.list();//查询所有的的数据
|
||||
//获取父节点,0表示父节点
|
||||
List<TsInstitution> collect = institutionList.stream()
|
||||
.filter(e -> rootSet.contains(e.getId()))
|
||||
.map(e -> {
|
||||
|
||||
List<TsInstitution> childNode = getChildNode(e, institutionList);
|
||||
|
||||
e.setChildren(new ArrayList<>(childNode));
|
||||
return e;
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
return collect;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归查询子节点
|
||||
* @param root
|
||||
* @param contentKnowledgeList
|
||||
* @return
|
||||
*/
|
||||
private List<TsInstitution> getChildNode(TsInstitution root, List<TsInstitution> contentKnowledgeList) {
|
||||
List<TsInstitution> childrenList = contentKnowledgeList.stream().filter(e -> Objects.equals(e.getParentId(), root.getId())).map(
|
||||
e -> {
|
||||
List<TsInstitution> childNode = getChildNode(e, contentKnowledgeList);
|
||||
e.setChildren(new ArrayList<>(childNode));
|
||||
return e;
|
||||
}
|
||||
).collect(Collectors.toList());
|
||||
return childrenList;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -109,21 +143,29 @@ public class TsInstitutionServiceImpl extends ServiceImpl<TsInstitutionDao, TsIn
|
||||
*/
|
||||
@Override
|
||||
public List<TsInstitution> getInstitutionAndUser(String userName) {
|
||||
//查询第一层机构
|
||||
List<TsInstitution> institutionRoot=tsInstitutionDao.selectRootAndUser(userName);
|
||||
List<TsInstitution> tsInstitutions = new ArrayList<>();
|
||||
|
||||
//把查到的数据结构修改,把用户放
|
||||
for(TsInstitution tsInstitution:institutionRoot){
|
||||
tsInstitution.setDisabled(true);
|
||||
tsInstitution.setChildren( recursionGetInstitutionAndUser(tsInstitution) );
|
||||
for(TsUserVO user:tsInstitution.getUsers()){
|
||||
if(tsInstitution.getChildren()==null){
|
||||
tsInstitution.setChildren(new ArrayList<>());
|
||||
//当条件为null时避免查询全部,直接返回第一级
|
||||
if (userName==null||userName==""){
|
||||
tsInstitutions=this.getFirst();
|
||||
}else {
|
||||
//查询第一层机构
|
||||
tsInstitutions=tsInstitutionDao.selectRootAndUser(userName);
|
||||
|
||||
//把查到的数据结构修改,把用户放
|
||||
for(TsInstitution tsInstitution:tsInstitutions){
|
||||
tsInstitution.setDisabled(true);
|
||||
// tsInstitution.setChildren( recursionGetInstitutionAndUser(tsInstitution) );
|
||||
for(TsUserVO user:tsInstitution.getUsers()){
|
||||
if(tsInstitution.getChildren()==null){
|
||||
tsInstitution.setChildren(new ArrayList<>());
|
||||
}
|
||||
// tsInstitution.getChildren().add(user);
|
||||
}
|
||||
tsInstitution.getChildren().add(user);
|
||||
}
|
||||
}
|
||||
return institutionRoot;
|
||||
|
||||
return tsInstitutions;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -213,6 +255,27 @@ public class TsInstitutionServiceImpl extends ServiceImpl<TsInstitutionDao, TsIn
|
||||
return tsInstitutions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TsInstitution> findInstitutionTreeByName(String institutionName) {
|
||||
|
||||
QueryWrapper<TsInstitution> tsInstitutionQuery = new QueryWrapper<>();
|
||||
tsInstitutionQuery.select("id")
|
||||
.like("name",institutionName);
|
||||
|
||||
/**
|
||||
* 查询出id的数据集合并转换成String类型
|
||||
*/
|
||||
List<String> rootIds = this.listObjs(tsInstitutionQuery)
|
||||
.stream()
|
||||
.map(item -> item.toString())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<TsInstitution> tsInstitutions = tsInstitutionDao.selectTreeByIds(rootIds);
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int clearData() {
|
||||
return tsInstitutionDao.clearData();
|
||||
|
||||
@@ -40,6 +40,24 @@
|
||||
select * from ts_user
|
||||
WHERE institution_id=#{institutionId}
|
||||
</select>
|
||||
|
||||
<select id="selectTreeByIds" resultMap="TsInstitution">
|
||||
<foreach collection="rootIds" item="id">
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
ts_institution
|
||||
WHERE
|
||||
FIND_IN_SET(
|
||||
id,
|
||||
GET_PARENT_NODE (#{id})
|
||||
);
|
||||
</foreach>
|
||||
|
||||
|
||||
</select>
|
||||
|
||||
|
||||
<select id="selectRootAndUser" resultMap="InstitutionAndUser">
|
||||
select i.*,u.usid,u.account,u.uname,u.institution_id from ts_institution i
|
||||
left join ts_user u
|
||||
|
||||
Reference in New Issue
Block a user