1.处理树状结构的赋值,递归算法
下面是根据某个机构赋值它下面所有的层级关系
Service层处理如下
public Map<String, Object> getAllData(String officeId, String businessId) {
Map<String, Object> map = new HashMap<>();
List<Office> all = officeDao.findByParentId(officeId, businessId);
ArrayList<String> strings = new ArrayList<>();
List<Map<String, Object>> maps = new ArrayList<>();
setTreeData(officeId, all, strings, maps);
map.put("ids", strings);
map.put("all", maps);
return map;
}
private List<Map<String, Object>> setTreeData(String officeId, List<Office> officeList, List<String> ids, List<Map<String, Object>> all) {
List<Map<String, Object>> list = new ArrayList<>();
if (!CollectionUtils.isEmpty(officeList)) {
for (Office office : officeList) {
Map<String, Object> map = new HashMap<>();
map.put("id", office.getId());
map.put("title", office.getName());
List<Office> parents = officeDao.findByParentId(officeId, office.getId());
if (CollectionUtils.isNotEmpty(parents)) {//判断当前节点是否有子节点
List list1 = setTreeData(officeId, parents, ids, all);
map.put("children", list1);
} else {
ids.add(office.getId());
map.put("children", new ArrayList<>());
}
list.add(map);
}
}
return list;
}
dao层如下
/**
*
* @param officeId
* @param parentId
* @return
*/
List<CamBusiness> findByParentId(@Param("officeId") String officeId,@Param("parentId") String parentId);
xml如下
SELECT
a.id AS "id",
a.parent_id AS "parent.id",
a.parent_ids AS "parentIds",
a.name AS "name",
a.code AS "code"
FROM sys_company a
LEFT JOIN sys_company p ON p.id = a.parent_id
LEFT JOIN sys_office q ON q.id = a.canteen_id
<where>
a.del_flag = '0'
<if test="officeId != null and officeId != ''">
AND q.office_id = #{officeId}
</if>
<if test="parentId != null and parentId != ''">
AND a.parent_id = #{parentId}
</if>
</where>
结果: