无关风月
2024-12-25 042fbcd557ef21ab256a542f1fef039269684340
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.jilongda.optometrist.authority.service.impl;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jilongda.optometrist.authority.mapper.SecResourcesMapper;
import com.jilongda.optometrist.authority.mapper.SecRoleMapper;
import com.jilongda.optometrist.authority.model.SecResources;
import com.jilongda.common.basic.PageInfo;
import com.jilongda.common.utils.SpringUtils;
import com.jilongda.optometrist.authority.dto.SecUserQueryDTO;
import com.jilongda.optometrist.authority.model.SecRole;
import com.jilongda.optometrist.authority.service.SecRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jilongda.optometrist.authority.vo.SecResourceVO;
import com.jilongda.optometrist.authority.vo.SecUserVO;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import static java.util.stream.Collectors.toList;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaochen
 * @since 2022-06-08
 */
@Service
public class SecRoleServiceImpl extends ServiceImpl<SecRoleMapper, SecRole> implements SecRoleService {
 
    @Autowired
    private SecRoleMapper secRoleMapper;
    @Autowired
    private SecResourcesMapper secResourcesMapper;
 
    @Override
    public SecRoleMapper getSecRoleMapper() {
        return secRoleMapper;
    }
 
    @Override
    public PageInfo<SecUserVO> getUserByRole(SecUserQueryDTO dto) {
        PageInfo<SecUserVO> pageInfo = new PageInfo<SecUserVO>(dto.getPageNum(), dto.getPageSize());
        List<SecUserVO> secUserVOS = secRoleMapper.getUserByRole(dto, pageInfo);
        pageInfo.setRecords(secUserVOS);
        // 获取列表
        return pageInfo;
    }
 
    @Override
    public List<SecResourceVO> selectRoleResourcesByRid(Long rid) {
        List<SecResources> allResources = secResourcesMapper.selectList(Wrappers.emptyWrapper());
        List<Long> resourceIds = secRoleMapper.selectResByRid(rid);
        SecResourceVO item;
        //获取根节点的集合
        List<SecResourceVO> root = new ArrayList<>();
        Map<Long, SecResourceVO> tempMap = new HashMap<>(3);
        //子节点集合
        SecResourceVO parent;
        for (SecResources resources : allResources) {
            item = SpringUtils.beanCopy(resources, SecResourceVO.class);
            if (resourceIds.contains(resources.getId())) {
                item.setFlag(true);
            }
            //每次循环都将集合重新循环添加到父集合中
            tempMap.put(resources.getId(), item);
            parent = tempMap.get(resources.getParentId());
            if (null != parent) {
                //父集合不为空时,获取下级集合
                //子节点集合
                parent.getChildren().add(item);
            } else {
                //根集合
                root.add(item);
            }
        }
        return root;
    }
 
    @Override
    public List<SecResourceVO> getRecursion(Long rid) {
        List<SecResourceVO> secResourceVOS = secResourcesMapper.getList(rid);
        // 判空
        if (CollectionUtils.isEmpty(secResourceVOS))
            return Lists.newArrayList();
        // 找出所有父级
        List<SecResourceVO> parent = secResourceVOS.stream().filter(resourceVO -> resourceVO.getParentId().equals("0")).collect(toList());
        // 通过父级递归子集
        getChildren(secResourceVOS,parent);
        return parent;
    }
 
    private void getChildren(List<SecResourceVO> secResourceVOS, List<SecResourceVO> parentList) {
        parentList.forEach(parent -> {
            List<SecResourceVO> childrenList = secResourceVOS.stream().filter(resourceVO -> resourceVO.getParentId().equals(parent.getId())).collect(toList());
            parent.setChildren(childrenList);
            if (!CollectionUtils.isEmpty(childrenList))
                getChildren(secResourceVOS,childrenList);
        });
    }
 
}