递归查询企业树和条件筛选
//注意 本递归 性能不行,当层级多达5级左右 , 需要后期优化,适合参考和修改
public List<EtpInfoSVo> makeTrees(Integer etpId) {
List<EtpInfoSVo> tree = new ArrayList<EtpInfoSVo>();
List<EtpInfoSVo> root = new ArrayList<EtpInfoSVo>();
List<EtpInfoSVo> etpTree = baseMapper.getEtpTree();//查询总企业
if (etpId == CommonConstants.ETP_ID_1) {
//如果是最顶级 直接获取所有 多级
root = getEtpName(root,etpTree);
} else {
//如果不是则查询当前传过来的
root = getEtpNameByEtpId(etpId,root,etpTree);
}
//进行递归放进树
//时间复杂度为 平方阶O(n2)
//性能不行 需要优化
for (EtpInfoSVo category : root) {
category = getChildrenTree(category, etpId,etpTree);
tree.add(category);
}
return tree;
}
/**
* 获取顶级分类
*
* @return
*/
private List<EtpInfoSVo> getEtpName(List<EtpInfoSVo> root, List<EtpInfoSVo> etpTree) {
for (EtpInfoSVo category : etpTree) {
if (category.getParentId().intValue() == 0) {
root.add(category);
}
}
return root;
}
/**
* 获取顶级分类
*
* @return
*/
private List<EtpInfoSVo> getEtpNameByEtpId(Integer etpId,List<EtpInfoSVo> root, List<EtpInfoSVo> etpTree) {
for (EtpInfoSVo category : etpTree) {
if (category.getParentId().intValue() != 0 && category.getId().intValue() == etpId.intValue()) {
root.add(category);
}
}
return root;
}
/**
* 以树形的形式获取子分类
*
* @param parent
* @return
*/
private EtpInfoSVo getChildrenTree(EtpInfoSVo parent, Integer parentId,List<EtpInfoSVo> etpTree) {
List<EtpInfoSVo> children = new ArrayList<EtpInfoSVo>();
for (EtpInfoSVo category : etpTree) {
//这里是逻辑重点
if (category.getParentId().intValue() == parent.getId().intValue()) {
children.add(getChildrenTree(category, parentId,etpTree));
}
}
parent.setChildren(children);
return parent;
}
//sql
``<select id="getEtpTree" resultMap="etpInfoMaps">
select
parent_id,
id,
etp_name,
etp_no as etpNo
from etp_info
<where>
and del_flag = 0
</where>
ORDER BY parent_id desc
</select>