package com.ycl.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ycl.domain.entity.FlowableType; import com.ycl.mapper.FlowableTypeMapper; import com.ycl.service.FlowableTypeService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.stream.Collectors; @Service @RequiredArgsConstructor public class FlowableTypeServiceImpl extends ServiceImpl implements FlowableTypeService { @Override public List selectTypeList(FlowableType flowableType) { return baseMapper.selectTypeList(flowableType); } @Override public List buildTreeSelect(List list) { List returnList = new ArrayList(); List tempList = list.stream().map(FlowableType::getId).collect(Collectors.toList()); for (Iterator iterator = list.iterator(); iterator.hasNext();) { FlowableType type = (FlowableType) iterator.next(); // 如果是顶级节点, 遍历该父节点的所有子节点 if (!tempList.contains(type.getParentId())) { recursionFn(list, type); returnList.add(type); } } if (returnList.isEmpty()) { returnList = list; } return returnList; } private void recursionFn(List list, FlowableType t) { // 得到子节点列表 List childList = getChildList(list, t); t.setChildren(childList); for (FlowableType tChild : childList) { if (hasChild(list, tChild)) { recursionFn(list, tChild); } } } /** * 得到子节点列表 */ private List getChildList(List list, FlowableType t) { List tlist = new ArrayList(); Iterator it = list.iterator(); while (it.hasNext()) { FlowableType n = (FlowableType) it.next(); if (n.getParentId().longValue() == t.getId().longValue()) { tlist.add(n); } } return tlist; } /** * 判断是否有子节点 */ private boolean hasChild(List list, FlowableType t) { return getChildList(list, t).size() > 0; } }