JAVA 单位部门人员树
简介
单位部门人员树的需求功能,由于是两个表的数据进行树结构的整合展示需求,单位部门数据库设计为一张表可以使用treeNode工具类来实现单位部门tree结构的展示,结合到人员的另一张表,就无法使用treeNode工具,经过搜索和查询,借鉴了许多度友们的思路和代码,使用递归的方式完成,代码如下:
controller层
/*
* @Description:查询部门组织架构树(查询部门和部门人员)
* @param: [paramsMap]
* @return: org.springblade.core.tool.api.R<java.util.List<org.springblade.modules.system.vo.DeptTree>>
* @author: Lxq
* @Date: 2020/5/20 0020
*/
@GetMapping("/queryDeptUserTreeList")
@ApiOperationSupport(order = 7)
@ApiOperation(value = "查询部门组织架构人员树", notes = "传入deptId")
@CacheEvict(cacheNames = {SYS_CACHE}, allEntries = true)
public R<List<DeptTree>> queryDeptUserTreeList(@RequestParam Map<String, Object> paramsMap,BladeUser bladeUser) {
List<DeptTree> dList=deptService.queryDeptUserTreeList(bladeUser.getTenantId(),paramsMap);
return R.data(dList);
}
service层
/**
* 查询部门组织架构树(查询部门和部门人员)
* @param tenantId
* @param paramsMap
* @return List<DeptTree>
* @Author lxq
*/
List<DeptTree> queryDeptUserTreeList(String tenantId,Map<String, Object> paramsMap);
业务逻辑实现层
/*
* @Description:查询部门组织架构树(查询部门和部门人员)
* @param: [paramsMap]
* @return: java.util.List<org.springblade.modules.system.vo.DeptTree>
* @author: Lxq
* @Date: 2020/5/20 0020
*/
@Override
public List<DeptTree> queryDeptUserTreeList(String tenantId, Map<String, Object> paramsMap){
if (AuthUtil.isAdministrator()) {
tenantId = StringPool.EMPTY;
}
String paramTenantId = Func.toStr(paramsMap.get("tenantId"));
if (Func.isNotEmpty(paramTenantId) && AuthUtil.isAdministrator()) {
tenantId = paramTenantId;
}
List<DeptTree> dList=baseMapper.queryDeptTreeList(tenantId,paramsMap);
for(DeptTree dt:dList){
List<DeptTree> t=baseMapper.selectDeptChildrenById(dt.getDeptId());
dt.setChildrenList(t);
}
//为每个部门插入相关人员
for (DeptTree dt:dList) {
getTreeNodeData(dt);
}
return dList;
}
/**
* @Description:为每个部门插入相关人员
* @param: [dt]
* @return: void
* @author: Lxq
* @Date: 2020/5/20 0020
*/
public void getTreeNodeData(DeptTree dt){
List<DeptTree> userList=baseMapper.selectUserByDeptId(dt.getId());
if(userList!=null && userList.size()>0){
for(DeptTree u:userList){
u.setType("1");//标记为人员
}
}
List<DeptTree> t=dt.getChildrenList();
if(t!=null && t.size()>0){
assert userList != null;
t.addAll(userList);
for(DeptTree t1:t){
getTreeNodeData(t1);
}
}else{
dt.setChildrenList(userList);
}
}
dao层
/**
* 根据前端页面传入的dept_id,查出所有部门
* @param tenantId
* @param paramsMap
* @return
* @author: Lxq
* @Date: 2020/6/15 0015
*/
List<DeptTree> queryDeptTreeList(@Param("tenantId") String tenantId,@Param("params") Map<String, Object> paramsMap);
/**
* 为每个部门插入相关人员
* @param id
* @return
*/
List<DeptTree> selectUserByDeptId(Long id);
/**
* 再递归查询出一级部门下的所有子部门
* @param deptId
* @return
*/
List<DeptTree> selectDeptChildrenById(@Param("deptId") Long deptId);
mapper.xml
<!-- 仅查询所有部门(不含人员) -->
<!-- 初始化部门树 -->
<resultMap type="org.springblade.modules.system.vo.DeptTree" id="deptTree">
<result column="DEPT_ID" property="id" javaType="java.lang.Long" />
<result column="DEPT_NAME" property="name" javaType="java.lang.String" />
<result column="deptId" property="deptId" javaType="java.lang.Long" />
<result column="id" property="id" javaType="java.lang.Long" />
<result column="parentId" property="parentId" javaType="java.lang.Long" />
<result column="deptName" property="deptName" javaType="java.lang.String" />
<!-- <collection column="DEPT_ID" property="childrenList" javaType="java.util.ArrayList" select="selectDeptChildrenById"/>-->
</resultMap>
<!-- 查询当前部门下的所有人员 -->
<select id="selectUserByDeptId" resultMap="deptTreeV" parameterType="java.lang.Long">
SELECT
b.user_id as "userId",c.name as "userName",a.id as "deptId",a.dept_name as "deptName"
FROM blade_dept a
left join blade_user_dept b on a.id =b.dept_id
left join blade_user c on b.user_id =c.id
where a.is_deleted='0' AND a.id=#{deptId}
</select>
<!-- 根据前端页面传入的dept_id,查出所有父级部门 -->
<select id="queryDeptTreeList" resultMap="deptTree" parameterType="java.util.Map">
select id,parent_id AS "parentId",dept_name,id as "deptId",dept_name as "deptName" from blade_dept
where is_deleted='0'
<if test="tenantId != null and tenantId != ''">
and tenant_id = #{param1}
</if>
<if test="params.deptId != null and params.deptId != ''">
AND id=#{params.deptId}
</if>
AND parent_id='0'
</select>
<!-- 再递归查询出一级部门下的所有子部门 -->
<select id="selectDeptChildrenById" resultMap="deptTree" parameterType="java.lang.Long">
select id,dept_name,parent_id AS "parentId",id as "deptId",dept_name as "deptName" from blade_dept where is_deleted='0' and parent_id= #{deptId}
</select>
entity实体类
@Data
@ApiModel(value = "DeptTree对象", description = "DeptTree对象")
public class DeptTree {
private static final long serialVersionUID = 1L;
@JsonSerialize(using = ToStringSerializer.class)
private Long id;
/**
* 父节点ID
*/
@JsonSerialize(using = ToStringSerializer.class)
private Long parentId;
/**
* 名称
*/
private String name;
/**
* 当前部门下的子部门集合
*/
private List<DeptTree> childrenList;
/**
* type=0 表示是部门;type=1 表示是人员
*/
private String type="0";
/**
* 部门id
*/
@JsonSerialize(using = ToStringSerializer.class)
private Long deptId;
/**
* 部门名称
*/
private String deptName;
}