xiangpei
2025-03-03 ddb766143ae7d04eb193d6f93719582e9f72296c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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<FlowableTypeMapper, FlowableType> implements FlowableTypeService {
 
    @Override
    public List<FlowableType> selectTypeList(FlowableType flowableType) {
        return baseMapper.selectTypeList(flowableType);
    }
 
    @Override
    public List<FlowableType> buildTreeSelect(List<FlowableType> list) {
        List<FlowableType> returnList = new ArrayList<FlowableType>();
        List<Integer> tempList = list.stream().map(FlowableType::getId).collect(Collectors.toList());
        for (Iterator<FlowableType> 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<FlowableType> list, FlowableType t)
    {
        // 得到子节点列表
        List<FlowableType> childList = getChildList(list, t);
        t.setChildren(childList);
        for (FlowableType tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
 
    /**
     * 得到子节点列表
     */
    private List<FlowableType> getChildList(List<FlowableType> list, FlowableType t)
    {
        List<FlowableType> tlist = new ArrayList<FlowableType>();
        Iterator<FlowableType> 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<FlowableType> list, FlowableType t)
    {
        return getChildList(list, t).size() > 0;
    }
 
}